Anophoo

tests

Oct 23rd, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1.  
  2. int testAppend1(){
  3. List l;
  4. ListNew(&l);
  5. int x = 10;
  6. ListAppend(&l, &x, sizeof(int));
  7. int y;
  8. ListGet(&l, 0, &y);
  9. return (y == 10 && y == x);
  10. }
  11.  
  12. int testAppend2(){
  13. List l;
  14. ListNew(&l);
  15. int x = 10;
  16. ListAppend(&l, &x, sizeof(int));
  17. x++;
  18. ListAppend(&l, &x, sizeof(int));
  19. x++;
  20. ListAppend(&l, &x, sizeof(int));
  21. int ans[10];
  22. ListGet(&l, 0, ans);
  23. ListGet(&l, 1, ans+1);
  24. ListGet(&l, 2, ans+2);
  25. // printf("results - %d, %d, %d\n", ans[0], ans[1], ans[2]);
  26. return (ans[0] == 10 && ans[1] == 11 && ans[2] == 12);
  27. }
  28.  
  29. int testAppend3(){
  30. List l;
  31. ListNew(&l);
  32. int ans = 0;
  33. for (int i=0; i<1000; i++) {
  34. ListAppend(&l, &i, sizeof(int));
  35. ans = ans + i;
  36. }
  37. int tmp;
  38. int sum = 0;
  39. for (int i=0; i<1000; i++) {
  40. ListGet(&l, i, &tmp);
  41. sum = sum + tmp;
  42. }
  43.  
  44. return (sum == ans);
  45. }
  46.  
  47. int testAppend4(){
  48. List l;
  49. ListNew(&l);
  50. int k = 100;
  51. char c = 11;
  52. ListAppend(&l, &k, sizeof(int));
  53. ListAppend(&l, &c, sizeof(char));
  54. int arr1[10];
  55. ListGet(&l, 0, arr1);
  56. char arr2[10];
  57. ListGet(&l, 1, arr2);
  58. return (arr1[0] == 100 && arr2[0] == 11);
  59. }
  60.  
  61. int testAppend5(){
  62. List l;
  63. ListNew(&l);
  64. int k = 100;
  65. char c = 11;
  66. ListAppend(&l, &k, sizeof(int));
  67. ListAppend(&l, &c, sizeof(char));
  68. k++;
  69. c++;
  70. ListAppend(&l, &k, sizeof(int));
  71. ListAppend(&l, &c, sizeof(char));
  72. int arr1[10];
  73. ListGet(&l, 0, arr1);
  74. ListGet(&l, 2, arr1+1);
  75. char arr2[10];
  76. ListGet(&l, 1, arr2);
  77. ListGet(&l, 3, arr2+1);
  78. return (arr1[0] == 100 && arr1[1] == 101 && arr2[0] == 11 && arr2[1] == 12);
  79. }
  80.  
  81. int testAppend6(){
  82. List l;
  83. ListNew(&l);
  84. int sum1 = 0, sum2 = 0;
  85. char c = 0;
  86. for (int i=0; i<1000; i++) {
  87. ListAppend(&l, &i, sizeof(int));
  88. ListAppend(&l, &c, sizeof(char));
  89. sum1 = sum1 + i;
  90. sum2 = sum2 + c;
  91. c++;
  92. }
  93. int tmp1;
  94. char tmp2;
  95. for (int i=0; i<2000; i+=2) {
  96. ListGet(&l, i, &tmp1);
  97. ListGet(&l, i+1, &tmp2);
  98. sum1 = sum1 - tmp1;
  99. sum2 = sum2 - tmp2;
  100. }
  101.  
  102. return (sum1 == 0 && sum2 == 0);
  103. }
  104.  
  105. int testAppend7(){
  106. List l;
  107. ListNew(&l);
  108. char * str = "abc";
  109. ListAppend(&l, str, strlen(str)+1);
  110. int tmp = 7;
  111. ListAppend(&l, &tmp, sizeof(int));
  112. tmp = 0;
  113. char res[4];
  114. ListGet(&l, 1, &tmp);
  115. ListGet(&l, 0, res);
  116. return (tmp == 7 && strcmp(str, res) == 0);
  117. }
  118.  
  119. int testAppend8(){
  120. List l;
  121. ListNew(&l);
  122. char * str = "abc";
  123. ListAppend(&l, str, strlen(str)+1);
  124. int tmp = 7;
  125. ListAppend(&l, &tmp, sizeof(int));
  126. tmp = 0;
  127. char * ptr = strdup("123qweqt8eq7wgdgdasjdgasd");
  128. ListAppend(&l, &ptr, sizeof(char *));
  129. char res[4];
  130. ListGet(&l, 1, &tmp);
  131. ListGet(&l, 0, res);
  132. char * resPtr;
  133. ListGet(&l, 2, &resPtr);
  134.  
  135. return (tmp == 7 && strcmp(str, res) == 0 && strcmp(ptr, resPtr) == 0);
  136. }
  137.  
  138. int testRemove9(){
  139. List l;
  140. ListNew(&l);
  141. int x = 10;
  142. ListAppend(&l, &x, sizeof(int));
  143. x++;
  144. ListAppend(&l, &x, sizeof(int));
  145. ListRemove(&l, 0);
  146. int y;
  147. ListGet(&l, 0, &y);
  148. return (y == 11 && y == x);
  149. }
  150.  
  151. int testRemove10(){
  152. List l;
  153. ListNew(&l);
  154. int x = 10;
  155. ListAppend(&l, &x, sizeof(int));
  156. x++;
  157. ListAppend(&l, &x, sizeof(int));
  158. x++;
  159. ListAppend(&l, &x, sizeof(int));
  160.  
  161. ListRemove(&l, 2);
  162. ListRemove(&l, 0);
  163. int ans[10];
  164. ListGet(&l, 0, ans);
  165.  
  166. return (ans[0] == 11);
  167. }
  168.  
  169. int testRemove11(){
  170. List l;
  171. ListNew(&l);
  172. int ans = 0;
  173. for (int i=0; i<1000; i++) {
  174. ListAppend(&l, &i, sizeof(int));
  175. if (i >= 500) {
  176. ans = ans + i;
  177. }
  178. }
  179.  
  180. for (int i=0; i<500; i++) {
  181. ListRemove(&l, 0);
  182. }
  183.  
  184. int tmp;
  185. int sum = 0;
  186. for (int i=0; i<500; i++) {
  187. ListGet(&l, i, &tmp);
  188. sum = sum + tmp;
  189. }
  190.  
  191. return (sum == ans);
  192. }
  193.  
  194. int testRemove12(){
  195. List l;
  196. ListNew(&l);
  197. int k = 100;
  198. char c = 11;
  199. ListAppend(&l, &k, sizeof(int));
  200. ListAppend(&l, &c, sizeof(char));
  201. ListRemove(&l, 1);
  202. ListRemove(&l, 0);
  203. ListAppend(&l, &k, sizeof(int));
  204. ListAppend(&l, &c, sizeof(char));
  205. ListAppend(&l, &k, sizeof(int));
  206. ListAppend(&l, &c, sizeof(char));
  207. ListRemove(&l, 1);
  208. ListRemove(&l, 0);
  209. int arr1[10];
  210. ListGet(&l, 0, arr1);
  211. char arr2[10];
  212. ListGet(&l, 1, arr2);
  213. return (arr1[0] == 100 && arr2[0] == 11);
  214. }
  215.  
  216. int testRemove13(){
  217. List l;
  218. ListNew(&l);
  219. int k = 100;
  220. char c = 11;
  221. ListAppend(&l, &k, sizeof(int));
  222. ListAppend(&l, &c, sizeof(char));
  223. k++;
  224. c++;
  225. ListAppend(&l, &k, sizeof(int));
  226. ListAppend(&l, &c, sizeof(char));
  227. ListRemove(&l, 2);
  228. ListRemove(&l, 2);
  229. ListAppend(&l, &k, sizeof(int));
  230. ListAppend(&l, &c, sizeof(char));
  231.  
  232. int arr1[10];
  233. ListGet(&l, 0, arr1);
  234. ListGet(&l, 2, arr1+1);
  235. char arr2[10];
  236. ListGet(&l, 1, arr2);
  237. ListGet(&l, 3, arr2+1);
  238. return (arr1[0] == 100 && arr1[1] == 101 && arr2[0] == 11 && arr2[1] == 12);
  239. }
  240.  
  241.  
  242. int sum19 = 0;
  243. void testPrint14F(void * d) {
  244. sum19 += *(int *)d;
  245. }
  246.  
  247. int testPrint14(){
  248. List l;
  249. ListNew(&l);
  250. int sum1 = 0;
  251. char c = 0;
  252. for (int i=0; i<1000; i++) {
  253. ListAppend(&l, &i, sizeof(int));
  254. sum1 = sum1 + i;
  255. }
  256.  
  257. ListPrint(&l, testPrint14F);
  258. return (sum19 == sum1);
  259. }
  260.  
  261. int sum20 = 0;
  262. int counter20 = 0;
  263. void testPrint15F(void * d) {
  264. if (counter20 % 2 == 0)
  265. sum20 += *(int *)d;
  266. else
  267. sum20 += *(char *)d;
  268. counter20++;
  269. }
  270.  
  271. int testPrint15(){
  272. List l;
  273. ListNew(&l);
  274. int sum1 = 0;
  275. char c = 0;
  276. for (int i=0; i<1000; i++) {
  277. ListAppend(&l, &i, sizeof(int));
  278. ListAppend(&l, &c, sizeof(char));
  279. sum1 = sum1 + i + c;
  280. }
  281.  
  282. ListPrint(&l, testPrint15F);
  283. return (sum20 == sum1);
  284. }
  285.  
  286.  
  287.  
  288. int main() {
  289. int numTests = 15;
  290. int score = 0;
  291. if (testAppend1()) score++;
  292. if (testAppend2()) score++;
  293. if (testAppend3()) score++;
  294. if (testAppend4()) score++;
  295. if (testAppend5()) score++;
  296. if (testAppend6()) score++;
  297. if (testAppend7()) score++;
  298. if (testAppend8()) score++;
  299. if (testRemove9()) score++;
  300. if (testRemove10()) score++;
  301. if (testRemove11()) score++;
  302. if (testRemove12()) score++;
  303. if (testRemove13()) score++;
  304. if (testPrint14()) score++;
  305. if (testPrint15()) score++;
  306.  
  307.  
  308. printf("%s%d%s%d\n", "score: ", score, "/", numTests);
  309. printf("%s\n", "hey");
  310. return 0;
  311. }
Advertisement
Add Comment
Please, Sign In to add comment