You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
5.7 KiB

  1. #include <stdlib.h>
  2. #include <time.h>
  3. #include <string.h>
  4. #include <unity.h>
  5. #include "../src/stringManipulation.c"
  6. void test_everyCharacterIsDigit()
  7. {
  8. /*test values*/
  9. char str_1[] = "1234", str_2[] = "hey123", str_3[] = "good evening!", str_4[] = "äöü";
  10. /*assertions*/
  11. TEST_ASSERT_TRUE(everyCharacterIsDigit(str_1));
  12. TEST_ASSERT_FALSE(everyCharacterIsDigit(str_2));
  13. TEST_ASSERT_FALSE(everyCharacterIsDigit(str_3));
  14. TEST_ASSERT_FALSE(everyCharacterIsDigit(str_4));
  15. }
  16. void test_power()
  17. {
  18. /*test block 1*/
  19. int testValues[] = {1,2,3,4,5,6,7,8,9,10};
  20. int expectedValues[] = {1,4,9,16,25,36,49,64,81,100};
  21. int length = sizeof(testValues)/sizeof(int);
  22. const int exponent = 2;
  23. for(int i=0;i<length;++i){
  24. TEST_ASSERT_EQUAL_INT(expectedValues[i], power(testValues[i],exponent));
  25. }
  26. /*test block 2*/
  27. int testValues_2[] = {11,12,13,14,15,16,17,18,19,20};
  28. int expectedValues_2[] = {121,144,169,196,225,256,289,324,361,400};
  29. length = sizeof(testValues_2)/sizeof(int);
  30. for(int i=0;i<length;++i){
  31. TEST_ASSERT_EQUAL_INT(expectedValues_2[i],power(testValues_2[i],exponent));
  32. }
  33. /*test block 3*/
  34. int testValues_3[] = {1,2,3,4,5,6,7,8,9,10};
  35. int expectedValues_3[] = {1,8,27,64,125,216,343,512,729,1000};
  36. const int exponent_2 = 3;
  37. length = sizeof(testValues_3)/sizeof(int);
  38. for(int i=0;i<length;++i){
  39. TEST_ASSERT_EQUAL_INT(expectedValues_3[i],power(testValues_3[i],exponent_2));
  40. }
  41. /*test block 4*/
  42. int testValues_4[] = {11,12,13,14,15,16,17,18,19,20};
  43. int expectedValues_4[] = {1331,1728,2197,2744,3375,4096,4913,5832,6859,8000};
  44. length = sizeof(testValues_4)/sizeof(int);
  45. for(int i=0;i<length;++i){
  46. TEST_ASSERT_EQUAL_INT(expectedValues_4[i],power(testValues_4[i],exponent_2));
  47. }
  48. /*test block 5*/
  49. int testValues_5[] = {0,0,19,2,4,5,11,54,32,12,77};
  50. int exponents[] = {0,1,2,7,4,2,0,1,2,4,2};
  51. int expectedValues_5[] = {0, 0, 361,128,256,25,1,54,1024,20736,5929};
  52. length = sizeof(testValues_5)/sizeof(int);
  53. for(int i=0;i<length;++i){
  54. TEST_ASSERT_EQUAL_INT(expectedValues_5[i], power(testValues_5[i],exponents[i]));
  55. }
  56. }
  57. void test_to_string()
  58. {
  59. /*initializing test values*/
  60. char *result_1[] = {"0","1","2","3","4","5","6","7","8","9","10"};
  61. char *result_2[] = {"500","502","504","506","508","510","512","514","516","518"};
  62. char *result_3[] = {"1000","2000","3000","4000","5000","6000","7000","8000","9000","10000"};
  63. char *result_4[] = {"9999","8999","7999","6999","5999","4999","3999","2999","1999","999"};
  64. char *result_5[] = {"1000000","2000000","3000000","4000000","5000000","6000000","7000000",
  65. "8000000","9000000","10000000"};
  66. /*assertions*/
  67. for(int i=0;i<=10;++i){
  68. TEST_ASSERT_EQUAL_STRING(result_1[i],to_string(i));
  69. }
  70. for(int i=0, j=500;i<10;++i,j+=2){
  71. TEST_ASSERT_EQUAL_STRING(result_2[i],to_string(j));
  72. }
  73. for(int i=0, j=1000;i<10;++i,j+=1000){
  74. TEST_ASSERT_EQUAL_STRING(result_3[i],to_string(j));
  75. }
  76. for(int i=0, j=9999;i<10;++i,j-=1000){
  77. TEST_ASSERT_EQUAL_STRING(result_4[i], to_string(j));
  78. }
  79. for(int i=0, j=1000000;i<10;++i,j+=1000000){
  80. TEST_ASSERT_EQUAL_STRING(result_5[i],to_string(j));
  81. }
  82. }
  83. void test_generateCheckString()
  84. {
  85. /*test block 1*/
  86. int numbers_1[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
  87. char *strings_1[] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
  88. char *result_1[] = {"0=a","1=b","2=c","3=d","4=e","5=f","6=g","7=h","8=i","9=j","10=k","11=l","12=m","13=n","14=o","15=p","16=q","17=r", "18=s","19=t","20=u","21=v","22=w","23=x","24=y","25=z"};
  89. for(int i=0;i<26;++i){
  90. TEST_ASSERT_EQUAL_STRING(result_1[i],generateCheckString(numbers_1[i],*(strings_1+i)));
  91. }
  92. /*test block 2*/
  93. int numbers_2[] = {0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025};
  94. char *strings_2[] = {"z","zy","zyx","zyxw","zyxwv","zyxwvu","zyxwvut","zyxwvuts","zyxwvutsr","zyxwvutsrq","zyxwvutsrqp",
  95. "zyxwvutsrqpo","zyxwvutsrqpon","zyxwvutsrqponm","zyxwvutsrqponml","zyxwvutsrqponmlk",
  96. "zyxwvutsrqponmlkj","zyxwvutsrqponmlkji","zyxwvutsrqponmlkjih","zyxwvutsrqponmlkjihg","zyxwvutsrqponmlkjihgf",
  97. "zyxwvutsrqponmlkjihgfe","zyxwvutsrqponmlkjihgfed","zyxwvutsrqponmlkjihgfedc","zyxwvutsrqponmlkjihgfedcb",
  98. "zyxwvutsrqponmlkjihgfedcba"};
  99. char *result_2[] = {"0=z","1=zy","1=zyx","2=zyxw","3=zyxwv","5=zyxwvu","8=zyxwvut","13=zyxwvuts","21=zyxwvutsr","34=zyxwvutsrq",
  100. "55=zyxwvutsrqp","89=zyxwvutsrqpo","144=zyxwvutsrqpon","233=zyxwvutsrqponm","377=zyxwvutsrqponml",
  101. "610=zyxwvutsrqponmlk","987=zyxwvutsrqponmlkj","1597=zyxwvutsrqponmlkji","2584=zyxwvutsrqponmlkjih",
  102. "4181=zyxwvutsrqponmlkjihg","6765=zyxwvutsrqponmlkjihgf","10946=zyxwvutsrqponmlkjihgfe",
  103. "17711=zyxwvutsrqponmlkjihgfed","28657=zyxwvutsrqponmlkjihgfedc","46368=zyxwvutsrqponmlkjihgfedcb",
  104. "75025=zyxwvutsrqponmlkjihgfedcba"};
  105. for(int i=0;i<26;++i){
  106. TEST_ASSERT_EQUAL_STRING(result_2[i],generateCheckString(numbers_2[i],*(strings_2+i)));
  107. }
  108. /*test block 3*/
  109. srand(time(0));
  110. int random_number=0;
  111. char *random_numbers_strings[20];
  112. int random_numbers[20];
  113. for(int i=0;i<20;++i){
  114. random_number = (rand() % 100) + 1;
  115. random_numbers_strings[i] = to_string(random_number);
  116. random_numbers[i] = random_number;
  117. }
  118. char *strings_3[] = {"tree","plant","tea","programming","assembler","unix","BSD","snow","mountain","table","wood","forest", "calculator","book","light","keyboard","old","paper","pencil","voltage"};
  119. char *result_3[20];
  120. for(int i=0;i<20;++i){
  121. random_numbers_strings[i] = strcat(random_numbers_strings[i],"=");
  122. result_3[i] = strcat(random_numbers_strings[i],strings_3[i]);
  123. printf("%s\n",result_3[i]);
  124. }
  125. for(int i=0;i<20;++i){
  126. TEST_ASSERT_EQUAL_STRING(result_3[i],generateCheckString(random_numbers[i],strings_3[i]));
  127. }
  128. }