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.

213 lines
9.5 KiB

  1. #include <stdlib.h>
  2. #include <time.h>
  3. #include <string.h>
  4. #include <unity.h>
  5. #include "../src/helperFunctions.c"
  6. void test_calculateStringLength()
  7. {
  8. char *testStrings[] = {"linux","table","book","men","woman","boy","girl","computer","old","new","water","fire","bright","dark","black","white"}; int expectedResults[] = {5,5,4,3,5,3,4,8,3,3,5,4,6,4,5,5};
  9. int numberOfValues= sizeof(expectedResults) / sizeof(int);
  10. for(int i=0;i<numberOfValues;++i){
  11. TEST_ASSERT_EQUAL_INT(expectedResults[i], calculateStringLength(testStrings[i]));
  12. }
  13. }
  14. void test_isLetterOfAlphabet()
  15. {
  16. /*test block 1*/
  17. char *testStringsToTrue[] = {"adhj","kasdlwq","vbeuqor","kalkwyynmakj","kakswxl","akljlxjkcyxyklj","asdjhuwpwe","xbcvddd","klajksdjkl","ghghgtie","kajsd"};
  18. unsigned int numberOfElements = sizeof(testStringsToTrue) / sizeof(char *);
  19. for(int i=0;i<numberOfElements;++i){
  20. TEST_ASSERT_TRUE(isLetterOfAlphabet(testStringsToTrue[i]));
  21. }
  22. /*test block 2*/
  23. char *testStringsToTrue_2[] = {"bcjyxhkjyxchjqwo","tree","garden","thinker","philosophy","linux","computer","lesson","teacher","table","drink","water","every", "Frank","city","economic","programming","task","smart","smarter","smartest","dumb","wood","animals","forest","display","hot","cold","ice","bear","keyboard","pair","pencil"};
  24. numberOfElements = sizeof(testStringsToTrue_2) / sizeof(char *);
  25. for(int i=0;i<numberOfElements;++i){
  26. TEST_ASSERT_TRUE(isLetterOfAlphabet(testStringsToTrue_2[i]));
  27. }
  28. /*test block 3*/
  29. char *testStringsToFalse[] = {"ashjdkj32123","4213jashj","laskdj2","1sbabjsdh","askjasdkjd0","123932131a","abcd2hutz","81287asjk231jkhs","aslkjasdlkjsd123","cbc451873"};
  30. numberOfElements = sizeof(testStringsToFalse) / sizeof(char *);
  31. for(int i=0;i<numberOfElements;++i){
  32. TEST_ASSERT_FALSE(isLetterOfAlphabet(testStringsToFalse[i]));
  33. }
  34. /*test block 4*/
  35. char *testStringsToFalse_2[] = {"1234","56789","00000010101010","3748927398273498","757575757","1726371238726","19237182937192837","875378612873621","128973192837","99494949499929292929292938382828","1827391237981273","7481263871236782136"};
  36. numberOfElements = sizeof(testStringsToFalse_2) / sizeof(char *);
  37. for(int i=0;i<numberOfElements;++i){
  38. TEST_ASSERT_FALSE(isLetterOfAlphabet(testStringsToFalse_2[i]));
  39. }
  40. }
  41. void test_toUnsignedInteger()
  42. {
  43. /*test block 1*/
  44. char *strings[] = {"111","123","542","994","9000","8384","6473","12345","57837","78387","93276","1000","8444","48484"};
  45. int expected[] = {111,123,542,994,9000,8384,6473,12345,57837,78387,93276,1000,8444,48484};
  46. int length = sizeof(expected)/sizeof(int);
  47. for(int i=0;i<length;++i){
  48. TEST_ASSERT_EQUAL_INT(expected[i], toUnsignedInteger(strings[i]));
  49. }
  50. /*test block 2*/
  51. char *strings_2[] = {"9999","99999","9","99","999","0","19","10","90","8765"};
  52. int expected_2[] = {9999,99999,9,99,999,0,19,10,90,8765};
  53. length = sizeof(expected_2)/sizeof(int);
  54. for(int i=0;i<length;++i){
  55. TEST_ASSERT_EQUAL_INT(expected_2[i], toUnsignedInteger(strings_2[i]));
  56. }
  57. /*test block 3*/
  58. char *strings_3[] = {"0","1","1","2","3","5","8","13","21","34","55","89","144","233"};
  59. int expected_3[] = {0,1,1,2,3,5,8,13,21,34,55,89,144,233};
  60. length = sizeof(expected_3)/sizeof(int);
  61. for(int i=0;i<length;++i){
  62. TEST_ASSERT_EQUAL_INT(expected_3[i], toUnsignedInteger(strings_3[i]));
  63. }
  64. }
  65. void test_everyCharacterIsDigit()
  66. {
  67. /*test block 1*/
  68. char *expectTrue[] = {"0","11","222","3333","4444","134132","12352378","12847273","1237873","9992475","987232","34723873278","578347823783","758378723","44293884742",
  69. "3184123872873","8912892383","18282828","55757575757528282","123823883282383282575757283832","99999999999999999999999999999999999","128321378","81293982139823","21412323"
  70. "575757575754646464648383838383","1298557648298219821398129381928391283918238912831283928391283129839281391283918238912391238912839182391239857517828"};
  71. int length = sizeof(expectTrue)/sizeof(char *);
  72. for(int i=0;i<length;++i){
  73. TEST_ASSERT_TRUE(everyCharacterIsDigit(expectTrue[i]));
  74. }
  75. /*test block 2*/
  76. char *expectFalse[] = {"a","bcd","dhdd","3asad87","askj","nxbdj","489sjk2kj","kjasjkd38234","aksjlas","bcbc838ch","akjsjkdjkq919191","askjsdakj492","kasjcncn","9919a19","cbajsh","askjajkd","ajshdasjh","jyxhyxjchyx","kasjdakj","vbvb88888888888888828282828282828askjh"};
  77. length = sizeof(expectFalse)/sizeof(char *);
  78. for(int i=0;i<length;++i){
  79. TEST_ASSERT_FALSE(everyCharacterIsDigit(expectFalse[i]));
  80. }
  81. }
  82. void test_power()
  83. {
  84. /*test block 1*/
  85. int testValues[] = {1,2,3,4,5,6,7,8,9,10};
  86. int expectedValues[] = {1,4,9,16,25,36,49,64,81,100};
  87. int length = sizeof(testValues)/sizeof(int);
  88. const int exponent = 2;
  89. for(int i=0;i<length;++i){
  90. TEST_ASSERT_EQUAL_INT(expectedValues[i], power(testValues[i],exponent));
  91. }
  92. /*test block 2*/
  93. int testValues_2[] = {11,12,13,14,15,16,17,18,19,20};
  94. int expectedValues_2[] = {121,144,169,196,225,256,289,324,361,400};
  95. length = sizeof(testValues_2)/sizeof(int);
  96. for(int i=0;i<length;++i){
  97. TEST_ASSERT_EQUAL_INT(expectedValues_2[i],power(testValues_2[i],exponent));
  98. }
  99. /*test block 3*/
  100. int testValues_3[] = {1,2,3,4,5,6,7,8,9,10};
  101. int expectedValues_3[] = {1,8,27,64,125,216,343,512,729,1000};
  102. const int exponent_2 = 3;
  103. length = sizeof(testValues_3)/sizeof(int);
  104. for(int i=0;i<length;++i){
  105. TEST_ASSERT_EQUAL_INT(expectedValues_3[i],power(testValues_3[i],exponent_2));
  106. }
  107. /*test block 4*/
  108. int testValues_4[] = {11,12,13,14,15,16,17,18,19,20};
  109. int expectedValues_4[] = {1331,1728,2197,2744,3375,4096,4913,5832,6859,8000};
  110. length = sizeof(testValues_4)/sizeof(int);
  111. for(int i=0;i<length;++i){
  112. TEST_ASSERT_EQUAL_INT(expectedValues_4[i],power(testValues_4[i],exponent_2));
  113. }
  114. /*test block 5*/
  115. int testValues_5[] = {0,0,19,2,4,5,11,54,32,12,77};
  116. int exponents[] = {0,1,2,7,4,2,0,1,2,4,2};
  117. int expectedValues_5[] = {0, 0, 361,128,256,25,1,54,1024,20736,5929};
  118. length = sizeof(testValues_5)/sizeof(int);
  119. for(int i=0;i<length;++i){
  120. TEST_ASSERT_EQUAL_INT(expectedValues_5[i], power(testValues_5[i],exponents[i]));
  121. }
  122. }
  123. void test_to_string()
  124. {
  125. /*initializing test values*/
  126. char *result_1[] = {"0","1","2","3","4","5","6","7","8","9","10"};
  127. char *result_2[] = {"500","502","504","506","508","510","512","514","516","518"};
  128. char *result_3[] = {"1000","2000","3000","4000","5000","6000","7000","8000","9000","10000"};
  129. char *result_4[] = {"9999","8999","7999","6999","5999","4999","3999","2999","1999","999"};
  130. char *result_5[] = {"1000000","2000000","3000000","4000000","5000000","6000000","7000000",
  131. "8000000","9000000","10000000"};
  132. /*assertions*/
  133. for(int i=0;i<=10;++i){
  134. TEST_ASSERT_EQUAL_STRING(result_1[i],to_string(i));
  135. }
  136. for(int i=0, j=500;i<10;++i,j+=2){
  137. TEST_ASSERT_EQUAL_STRING(result_2[i],to_string(j));
  138. }
  139. for(int i=0, j=1000;i<10;++i,j+=1000){
  140. TEST_ASSERT_EQUAL_STRING(result_3[i],to_string(j));
  141. }
  142. for(int i=0, j=9999;i<10;++i,j-=1000){
  143. TEST_ASSERT_EQUAL_STRING(result_4[i], to_string(j));
  144. }
  145. for(int i=0, j=1000000;i<10;++i,j+=1000000){
  146. TEST_ASSERT_EQUAL_STRING(result_5[i],to_string(j));
  147. }
  148. }
  149. void test_generateCheckString()
  150. {
  151. /*test block 1*/
  152. 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};
  153. 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"};
  154. 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"};
  155. for(int i=0;i<26;++i){
  156. TEST_ASSERT_EQUAL_STRING(result_1[i],generateCheckString(numbers_1[i],*(strings_1+i)));
  157. }
  158. /*test block 2*/
  159. 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};
  160. char *strings_2[] = {"z","zy","zyx","zyxw","zyxwv","zyxwvu","zyxwvut","zyxwvuts","zyxwvutsr","zyxwvutsrq","zyxwvutsrqp",
  161. "zyxwvutsrqpo","zyxwvutsrqpon","zyxwvutsrqponm","zyxwvutsrqponml","zyxwvutsrqponmlk",
  162. "zyxwvutsrqponmlkj","zyxwvutsrqponmlkji","zyxwvutsrqponmlkjih","zyxwvutsrqponmlkjihg","zyxwvutsrqponmlkjihgf",
  163. "zyxwvutsrqponmlkjihgfe","zyxwvutsrqponmlkjihgfed","zyxwvutsrqponmlkjihgfedc","zyxwvutsrqponmlkjihgfedcb",
  164. "zyxwvutsrqponmlkjihgfedcba"};
  165. char *result_2[] = {"0=z","1=zy","1=zyx","2=zyxw","3=zyxwv","5=zyxwvu","8=zyxwvut","13=zyxwvuts","21=zyxwvutsr","34=zyxwvutsrq",
  166. "55=zyxwvutsrqp","89=zyxwvutsrqpo","144=zyxwvutsrqpon","233=zyxwvutsrqponm","377=zyxwvutsrqponml",
  167. "610=zyxwvutsrqponmlk","987=zyxwvutsrqponmlkj","1597=zyxwvutsrqponmlkji","2584=zyxwvutsrqponmlkjih",
  168. "4181=zyxwvutsrqponmlkjihg","6765=zyxwvutsrqponmlkjihgf","10946=zyxwvutsrqponmlkjihgfe",
  169. "17711=zyxwvutsrqponmlkjihgfed","28657=zyxwvutsrqponmlkjihgfedc","46368=zyxwvutsrqponmlkjihgfedcb",
  170. "75025=zyxwvutsrqponmlkjihgfedcba"};
  171. for(int i=0;i<26;++i){
  172. TEST_ASSERT_EQUAL_STRING(result_2[i],generateCheckString(numbers_2[i],*(strings_2+i)));
  173. }
  174. /*test block 3*/
  175. srand(time(0));
  176. int random_number=0;
  177. char *random_numbers_strings[20];
  178. int random_numbers[20];
  179. for(int i=0;i<20;++i){
  180. random_number = (rand() % 100) + 1;
  181. random_numbers_strings[i] = to_string(random_number);
  182. random_numbers[i] = random_number;
  183. }
  184. char *strings_3[] = {"tree","plant","tea","programming","assembler","unix","BSD","snow","mountain","table","wood","forest", "calculator","book","light","keyboard","old","paper","pencil","voltage"};
  185. char *result_3[20];
  186. for(int i=0;i<20;++i){
  187. random_numbers_strings[i] = strcat(random_numbers_strings[i],"=");
  188. result_3[i] = strcat(random_numbers_strings[i],strings_3[i]);
  189. printf("%s\n",result_3[i]);
  190. }
  191. for(int i=0;i<20;++i){
  192. TEST_ASSERT_EQUAL_STRING(result_3[i],generateCheckString(random_numbers[i],strings_3[i]));
  193. }
  194. }