Advertisement
mvaganov

clist_test.c

May 11th, 2021 (edited)
1,027
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 19.54 KB | None | 0 0
  1. //#define game // include this symbol if platform_conio.h is available
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #include "clist.h"
  6.  
  7. #define SUCCESS 0
  8. #define FAIL 1
  9.  
  10. typedef struct {
  11.     const char* file;
  12.     int line;
  13.     char message[240];
  14. } TestErrorResult;
  15.  
  16. typedef TestErrorResult* (*TestFunction)();
  17.  
  18. typedef struct {
  19.     const char * name;
  20.     TestFunction func;
  21. } TestFunctionEntry;
  22.  
  23. #ifdef game
  24. #include "platform_conio.h"
  25. int WaitJustOneMomentTillKeyPress(int milliseconds) {
  26.     int soon = platform_upTimeMS() + milliseconds;
  27.     while(platform_upTimeMS() < soon && !platform_kbhit()) {
  28.         platform_sleep(1);
  29.     }
  30.     if (platform_kbhit()) {
  31.         platform_getchar();
  32.         return 1;
  33.     }
  34.     return 0;
  35. }
  36. void Success(const char* message, int pause, int flashes) {
  37.     int delay = pause / (flashes * 2);
  38.     for(int i = 0; i < flashes; ++i) {
  39.         platform_setColor(PLATFORM_COLOR_GREEN|PLATFORM_COLOR_INTENSITY, -1);
  40.         printf("\r%s",message);
  41.         if(WaitJustOneMomentTillKeyPress(delay)) break;
  42.         platform_setColor(PLATFORM_COLOR_GREEN, -1);
  43.         printf("\r%s",message);
  44.         if(WaitJustOneMomentTillKeyPress(delay)) break;
  45.     }
  46.     platform_setColor(PLATFORM_COLOR_INTENSITY, -1);
  47.     printf("\r%s",message);
  48.     platform_setColor(-1, -1);
  49. }
  50. #else
  51. void Success(const char* message, int pause, int flashes) {}
  52. #endif
  53.  
  54. static TestErrorResult _err;
  55.  
  56. #define ERR(format, ...)    (\
  57.     _err.file = __FILE__,\
  58.     _err.line = __LINE__ ,\
  59.     sprintf(_err.message, format, ##__VA_ARGS__),\
  60.     &_err\
  61. )
  62.  
  63. #define IMPLEMENT_CHECK(result) if (result == Listfloat_NotImplemented) return ERR("function not implemented!");
  64.  
  65. int AppendArrayToCharBuffer(char* buffer, int bufferSize, float* list, int listCount, int maxDigits) {
  66.     if (list == NULL || listCount == 0) { return SUCCESS; }
  67.     char smallBuffer[16];
  68.     int index = 0;
  69.  
  70.     for(int i = 0; i < bufferSize; ++i) {
  71.         if (buffer[i] == '\0') {
  72.             index = i;
  73.             break;
  74.         }
  75.     }
  76.     for(int i = 0; i < listCount; ++i) {
  77.         sprintf(smallBuffer, "%f", list[i]);
  78.         int len = strlen(smallBuffer);
  79.         if (len > maxDigits) {
  80.             len = maxDigits;
  81.         }
  82.         if (index+len >= bufferSize) { break; }
  83.         for (int j = 0; j < len; ++j) {
  84.             buffer[index+j] = smallBuffer[j];
  85.         }
  86.         index += len;
  87.         if (index+1 >= bufferSize) { break; }
  88.         buffer[index++] = ' ';
  89.         if (index+1 >= bufferSize) { break; }
  90.     }
  91.     buffer[index] = '\0';
  92.     return SUCCESS;
  93. }
  94.  
  95. #define LISTERR(list,format, ...)   (_err.file = __FILE__, _err.line = __LINE__ , sprintf(_err.message, format, ##__VA_ARGS__),\
  96.     AppendArrayToCharBuffer(_err.message, sizeof(_err.message), (list)->get, (list)->count, 5), &_err)
  97.  
  98. TestErrorResult* test_Listfloat_Init() {
  99.     Listfloat list;
  100.     int result = Listfloat_Init(&list);
  101.     IMPLEMENT_CHECK(result)
  102.     if (result == FAIL) {
  103.         return ERR("Listfloat_Init failed. it should not fail");
  104.     }
  105.     if (list.get != NULL || list.capacity != 0 || list.count != 0) {
  106.         return ERR("Listfloat_Init should set all values to zero. list: 0x%zx, capacity:%d, count:%d",
  107.             list.get, list.capacity, list.count);
  108.     }
  109.     Listfloat_Release(&list);
  110.     return NULL;
  111. }
  112.  
  113. TestErrorResult* test_Listfloat_SetCapacity(){
  114.     Listfloat list;
  115.     Listfloat_Init(&list);
  116.     int expectedSize = 10;
  117.     int result = Listfloat_SetCapacity(&list, expectedSize);
  118.     IMPLEMENT_CHECK(result)
  119.     if (result != Listfloat_Success) { return ERR("SetCapacity failed (%d)", result); }
  120.     if (list.get == NULL) { return ERR("SetCapacity clearly did not allocate memory", result); }
  121.     if (list.capacity != expectedSize)
  122.         return ERR("capacity did not grow as expected. expected %d, value is %d",
  123.             expectedSize, list.capacity);
  124.     if (list.count != 0) {
  125.         return ERR("count changed, unexpectedly: %d", list.count);
  126.     }
  127.     if (list.get == 0) {
  128.         return ERR("data is still null", 0);
  129.     }
  130.     Listfloat_Release(&list);
  131.     return NULL;
  132. }
  133.  
  134. TestErrorResult* test_Listfloat_Release() {
  135.     Listfloat list;
  136.     Listfloat_Init(&list);
  137.     int result = Listfloat_SetCapacity(&list, 2);
  138.     result = Listfloat_Release(&list);
  139.     IMPLEMENT_CHECK(result)
  140.     if (list.capacity != 0) {
  141.         return ERR("capacity isn't zero, it's %d!", list.capacity);
  142.     }
  143.     if (list.count != 0) {
  144.         return ERR("count isn't zero, it's %d!", list.count);
  145.     }
  146.     if (list.get != NULL) {
  147.         return ERR("data isn't null, it's 0x%x!", list.get);
  148.     }
  149.     return NULL;
  150. }
  151.  
  152. TestErrorResult* test_Listfloat_Add() {
  153.     Listfloat list;
  154.     Listfloat_Init(&list);
  155.     int result = Listfloat_Add(&list, 1);
  156.     IMPLEMENT_CHECK(result)
  157.     if (result != 0) {
  158.         return ERR("Add failed (%d)", result);
  159.     }
  160.     if (list.capacity == 0) {
  161.         return ERR("capacity did not grow", 0);
  162.     }
  163.     if (list.count != 1) {
  164.         return ERR("count is not 1, as expected: %d", list.count);
  165.     }
  166.     if (list.get == 0) {
  167.         return ERR("data is still null", 0);
  168.     }
  169.     Listfloat_Release(&list);
  170.     return NULL;
  171. }
  172.  
  173. float RandomFloat() { return (rand() & 0xffff) / (float)(0x10000); }
  174.  
  175. TestErrorResult* AddRandomFloats(Listfloat * self, int count) {
  176.     int initialCount = self->count;
  177.     float * randomNums = malloc(sizeof(float)*count);
  178.     for(int i = 0; i < count; ++i){
  179.         randomNums[i] = RandomFloat();
  180.         int result = Listfloat_Add(self, randomNums[i]);
  181.         if (result != 0) {
  182.             return LISTERR(self,"Add failed: %d\n", result);
  183.         }
  184.         if (self->count != initialCount+i+1) {
  185.             return LISTERR(self,"count unexpected value: %d\n", self->count);
  186.         }
  187.         for(int j = 0; j < i; ++j) {
  188.             if (self->get[initialCount+j] != randomNums[j]) {
  189.                 return LISTERR(self,"unexpected value at index %d: %f, instead of %f\n",
  190.                     j, self->get[initialCount+j], randomNums[i]);
  191.             }
  192.         }
  193.     }
  194.     free(randomNums);
  195.     return NULL;
  196. }
  197.  
  198. #define GIVEN_A_RANDOM_LIST(listname, size) \
  199.     Listfloat listname; \
  200.     Listfloat_Init(&listname); \
  201.     { TestErrorResult* err = AddRandomFloats(&listname, size); \
  202.     if (err != NULL) { \
  203.         return err; \
  204.     } }
  205.  
  206. TestErrorResult* test_Listfloat_Adds() {
  207.     GIVEN_A_RANDOM_LIST(list,10)
  208.     Listfloat_Release(&list);
  209.     return NULL;
  210. }
  211.  
  212. TestErrorResult* test_Listfloat_Remove() {
  213.     GIVEN_A_RANDOM_LIST(list,10)
  214.     int result = Listfloat_Remove(&list, -1);
  215.     IMPLEMENT_CHECK(result)
  216.     if (result == 0) {
  217.         return LISTERR(&list,"Remove returned no error code with negative index\n", 0);
  218.     }
  219.     result = Listfloat_Remove(&list, list.capacity);
  220.     if (result == 0) {
  221.         return LISTERR(&list,"Remove returned no error code with OOB index\n", 0);
  222.     }
  223.     int initialCapacity = list.capacity;
  224.     int originalCount = list.count;
  225.     for(int i = 0; i < originalCount; ++i) {
  226.         int indexToRemove = rand() % list.count;
  227.         float valueToRemove = list.get[indexToRemove];
  228.         float nextValue = 0;
  229.         if (indexToRemove < list.count-1) {
  230.             nextValue = list.get[indexToRemove+1];
  231.         }
  232.         result = Listfloat_Remove(&list, indexToRemove);
  233.         if (result != 0) {
  234.             return LISTERR(&list,"Remove returned an error code %d when none was expected\n", result);
  235.         }
  236.         if (list.count != originalCount-(1+i)) {
  237.             return LISTERR(&list,"Remove didn't reduce the count as expected. got %d instead of %d\n",
  238.                 list.count, originalCount-(1+i));
  239.         }
  240.         if (list.capacity != initialCapacity) {
  241.             return LISTERR(&list,"Remove changed the capacity? expected capacity to stay %d, it changed to %d\n",
  242.                 initialCapacity, list.capacity);
  243.         }
  244.         if (list.get[indexToRemove] == valueToRemove) {
  245.             return LISTERR(&list,"Remove(%d) didn't remove.\n", indexToRemove);
  246.         }
  247.     }
  248.     Listfloat_Release(&list);
  249.     return NULL;
  250. }
  251.  
  252. TestErrorResult* test_Listfloat_RemoveRange() {
  253.     GIVEN_A_RANDOM_LIST(list,20)
  254.     int initialSize = list.count;
  255.     int start = 1 + rand() % (initialSize-3), length = 2;
  256.     int result = Listfloat_RemoveRange(&list, -1, 2);
  257.     IMPLEMENT_CHECK(result)
  258.     if (result == 0) {
  259.         return ERR("RemoveRange returned no error code with negative index");
  260.     }
  261.     result = Listfloat_RemoveRange(&list, list.capacity, 1);
  262.     if (result == 0) {
  263.         return ERR("RemoveRange returned no error code with OOB index");
  264.     }
  265.     result = Listfloat_RemoveRange(&list, 1, -1);
  266.     if (result == 0) {
  267.         return ERR("RemoveRange returned no error code with negative count");
  268.     }
  269.     result = Listfloat_RemoveRange(&list, start, length);
  270.     if (result != 0) {
  271.         return ERR("RemoveRange returned error (%d) for values %d, %d (should be valid for size %d)",
  272.             result, start, length, initialSize);
  273.     }
  274.     if (list.count != initialSize-length) {
  275.         return ERR("count did not reduce as expected. expected %d, instead of %d",
  276.             (initialSize-length), list.count);
  277.     }
  278.     Listfloat_Release(&list);
  279.     return NULL;
  280. }
  281.  
  282. TestErrorResult* test_Listfloat_Insert() {
  283.     Listfloat list;
  284.     Listfloat_Init(&list);
  285.     int index = 0;
  286.     float value = RandomFloat();
  287.     // insert index 0 from empty list
  288.     int result = Listfloat_Insert(&list, index, value);
  289.     IMPLEMENT_CHECK(result)
  290.     if (result != 0) { return ERR("insert should return 0 if successful"); }
  291.     result = Listfloat_Insert(&list, -1, value);
  292.     if (result == 0) { return ERR("insert should return error code for invalid indexes"); }
  293.     result = Listfloat_Insert(&list, list.capacity+1, value);
  294.     if (result == 0) { return ERR("insert should return error code for invalid indexes"); }
  295.     if (list.count != 1) {
  296.         return ERR("inserting %f into empty list should add", value);
  297.     }
  298.     if (list.count != 1 || list.get[index] != value) {
  299.         return LISTERR(&list,"inserting %f into empty list should add\n", value);
  300.     }
  301.     int expectedCount = list.count;
  302.     for(index = 0; index < 3; ++index) {
  303.         value = RandomFloat();
  304.         result = Listfloat_Insert(&list, index, value);
  305.         expectedCount++;
  306.         if (list.count != expectedCount || list.get[index] != value) {
  307.             return LISTERR(&list,"inserting %f into %d didn't work\n", value, index);
  308.         }
  309.     }
  310.     Listfloat_Release(&list);
  311.     return NULL;
  312. }
  313.  
  314. TestErrorResult* test_Listfloat_Clear() {
  315.     GIVEN_A_RANDOM_LIST(list,10)
  316.     if(list.count == 0) { return ERR("list started empty. that shouldn't happen."); }
  317.     int result = Listfloat_Clear(&list);
  318.     IMPLEMENT_CHECK(result)
  319.     if(list.count != 0) { return LISTERR(&list,"clear does not set count to zero!\n"); }
  320.     Listfloat_Release(&list);
  321.     return NULL;
  322. }
  323.  
  324. TestErrorResult* test_Listfloat_IndexOf() {
  325.     GIVEN_A_RANDOM_LIST(list,20)
  326.     int found = Listfloat_IndexOf(&list, 2);
  327.     IMPLEMENT_CHECK(found)
  328.     if (found >= 0) { return LISTERR(&list,"found element 2, which should be impossible\n"); }
  329.     for(int i = 0; i < 10; i++) {
  330.         int index = rand() % (list.count-2);
  331.         list.get[index] = 2;
  332.         list.get[index+2] = 2;
  333.         found = Listfloat_IndexOf(&list, 2);
  334.         if (found != index) {
  335.             return LISTERR(&list,"didn't find element 2 (at %d), IndexOf found it at %d\n", index, found);
  336.         }
  337.         found = Listfloat_IndexOfStartingAt(&list, 2, index+1, list.count);
  338.         if (found != index+2) {
  339.             return LISTERR(&list,"didn't find element 2 after %d (at %d), IndexOf found it at %d\n",
  340.                 index+1, index+2, found);
  341.         }
  342.         list.get[index] = 0;
  343.         list.get[index+2] = 0;
  344.     }
  345.     found = Listfloat_IndexOf(&list, 2);
  346.     if (found >= 0) { return LISTERR(&list,"found element 2 again, which should be impossible\n"); }
  347.     Listfloat_Release(&list);
  348.     return NULL;
  349. }
  350.  
  351. TestErrorResult* test_Listfloat_IndexOfLast() {
  352.     GIVEN_A_RANDOM_LIST(list,20)
  353.     int found = Listfloat_IndexOfLast(&list, 2);
  354.     IMPLEMENT_CHECK(found)
  355.     if (found >= 0) { return LISTERR(&list,"found element 2, which should be impossible\n"); }
  356.     for(int i = 0; i < 10; i++) {
  357.         int index = (rand() % (list.count-2)) + 2;
  358.         list.get[index] = 2;
  359.         list.get[index-2] = 2;
  360.         found = Listfloat_IndexOfLast(&list, 2);
  361.         if (found != index) {
  362.             return LISTERR(&list,"didn't find element 2 (at %d), IndexOf found it at %d\n", index, found);
  363.         }
  364.         found = Listfloat_IndexOfLastEndingAt(&list, 2, index-1, 0);
  365.         if (found != index-2) {
  366.             return LISTERR(&list,"didn't find element 2 after %d (at %d), IndexOf found it at %d\n",
  367.                 index+1, index-2, found);
  368.         }
  369.         list.get[index] = 0;
  370.         list.get[index-2] = 0;
  371.     }
  372.     found = Listfloat_IndexOf(&list, 2);
  373.     if (found >= 0) {
  374.         return LISTERR(&list,"found element 2 again, which should be impossible\n");
  375.     }
  376.     Listfloat_Release(&list);
  377.     return NULL;
  378. }
  379.  
  380. void Swap(float* a, float* b) {
  381.     float temp = *a;
  382.     *a = *b;
  383.     *b = temp;
  384. }
  385.  
  386. int IsListInOrder(Listfloat* list, SortPredicate sortPredicate) {
  387.     for(int i = 1; i < list->count; ++i)
  388.         if (sortPredicate(&(list->get[i]), &(list->get[i-1])))
  389.             return FAIL;
  390.     return SUCCESS;
  391. }
  392.  
  393. int SortPredicateLittleEndian(void* a, void* b) {
  394.     float fa = *((float*)a);
  395.     float fb = *((float*)b);
  396.     return fa < fb;
  397. }
  398.  
  399. void MakeSureListIsOutOfOrder (Listfloat* list) {
  400.     if (IsListInOrder(list, SortPredicateLittleEndian) == FAIL) {
  401.         return;
  402.     }
  403.     if (list->get[0] != list->get[1]) {
  404.         Swap(&list->get[0], &list->get[1]);
  405.         return;
  406.     }
  407.     list->get[0] = list->get[1] + 1;
  408. }
  409.  
  410. TestErrorResult* test_Listfloat_Sort() {
  411.     GIVEN_A_RANDOM_LIST(list,20)
  412.     MakeSureListIsOutOfOrder(&list);
  413.     int result = Listfloat_Sort(&list);
  414.     IMPLEMENT_CHECK(result)
  415.     if (IsListInOrder(&list, SortPredicateLittleEndian) == FAIL) {
  416.         return LISTERR(&list, "sort did not sort a random list\n");
  417.     }
  418.     Listfloat_Release(&list);
  419.     return NULL;
  420. }
  421.  
  422. int SortPredicateBigEndian(void* a, void* b) {
  423.     float fa = *((float*)a);
  424.     float fb = *((float*)b);
  425.     return fb < fa;
  426. }
  427.  
  428. TestErrorResult* test_Listfloat_SortPredicate() {
  429.     GIVEN_A_RANDOM_LIST(list,20)
  430.     MakeSureListIsOutOfOrder(&list);
  431.     int result = Listfloat_SortPredicate(&list, SortPredicateLittleEndian);
  432.     IMPLEMENT_CHECK(result)
  433.     if (IsListInOrder(&list, SortPredicateLittleEndian) == FAIL) {
  434.         return LISTERR(&list, "sort predicate did not sort a random list in order\n");
  435.     }
  436.     Listfloat_Clear(&list);
  437.     AddRandomFloats(&list, 30);
  438.     Listfloat_SortPredicate(&list, SortPredicateBigEndian);
  439.     if (IsListInOrder(&list, SortPredicateBigEndian) == FAIL) {
  440.         return LISTERR(&list, "sort predicate did not sort a random list in reverse order\n");
  441.     }
  442.     Listfloat_Release(&list);
  443.     return NULL;
  444. }
  445.  
  446. int __SortPredicateLittleEndianCounted_count;
  447. int SortPredicateLittleEndianCounted(void* X, void* Y) {
  448.     ++__SortPredicateLittleEndianCounted_count;
  449.     float r = *((float*)X) - *((float*)Y);
  450.     return r < 0 ? -1 : r > 0 ? 1 : 0;
  451. }
  452.  
  453. int GetLog2(int value) {
  454.     int count = 0;
  455.     while(value > 0) {
  456.         value >>= 1;
  457.         ++count;
  458.     }
  459.     return count;
  460. }
  461.  
  462. TestErrorResult* test_Listfloat_BinarySearchComparer() {
  463.     GIVEN_A_RANDOM_LIST(list,40)
  464.     Listfloat_SortPredicate(&list, SortPredicateLittleEndian);
  465.     int maxSearches = GetLog2(list.count);
  466.     int indexToSearch = 0;//(list.count-1) /2 /2;
  467.     float lookingFor = list.get[indexToSearch];
  468.     __SortPredicateLittleEndianCounted_count = 0;
  469.     int found = Listfloat_BinarySearchComparer(&list, lookingFor, SortPredicateLittleEndianCounted);
  470.     IMPLEMENT_CHECK(found)
  471.     if (found < 0 || list.get[found] != lookingFor) {
  472.         return LISTERR(&list, "binarysearch looked for %f (at index %d), and %d was returned after %d searches\n",
  473.             lookingFor, indexToSearch, found, __SortPredicateLittleEndianCounted_count);
  474.     }
  475.     if (__SortPredicateLittleEndianCounted_count > maxSearches) {
  476.         return LISTERR(&list, "binarysearch found %f at %d, after %d searches (too many searches)\n",
  477.             lookingFor, indexToSearch, __SortPredicateLittleEndianCounted_count);
  478.     }
  479.     indexToSearch = list.count-1-indexToSearch;
  480.     lookingFor = list.get[indexToSearch];
  481.     __SortPredicateLittleEndianCounted_count = 0;
  482.     found = Listfloat_BinarySearchComparer(&list, lookingFor, SortPredicateLittleEndianCounted);
  483.     if (found < 0 || list.get[found] != lookingFor) {
  484.         return LISTERR(&list, "binarysearch looked for %f (at index %d), and %d was returned after %d searches\n",
  485.             lookingFor, indexToSearch, found, __SortPredicateLittleEndianCounted_count);
  486.     }
  487.     if (__SortPredicateLittleEndianCounted_count > maxSearches) {
  488.         return LISTERR(&list, "binarysearch found %f at %d, after %d searches (too many searches)\n",
  489.             lookingFor, indexToSearch, __SortPredicateLittleEndianCounted_count);
  490.     }
  491.     Listfloat_Release(&list);
  492.     return NULL;
  493. }
  494.  
  495. void AddTwoListsToErrorMessage(TestErrorResult* e, float* list1, int list1count, float* list2, int list2count, int digits) {
  496.     AppendArrayToCharBuffer(e->message, sizeof(e->message), list1, list1count, digits);
  497.     int len = strlen(e->message);
  498.     if(len+2 < sizeof(e->message)) {
  499.         e->message[len] = '\n';
  500.         e->message[len+1] = '\0';
  501.         AppendArrayToCharBuffer(e->message, sizeof(e->message), list2, list2count, digits);
  502.     }
  503. }
  504.  
  505. TestErrorResult* test_Listfloat_Reverse() {
  506.     GIVEN_A_RANDOM_LIST(list,10)
  507.     float* copy = malloc(sizeof(float)*list.count);
  508.     for(int i = 0; i < 10; ++i)
  509.         list.get[i] = i;
  510.     Listfloat_Copy(copy, list.get, list.count);
  511.     int result = Listfloat_Reverse(&list);
  512.     IMPLEMENT_CHECK(result)
  513.     for(int i = 0; i < list.count; ++i) {
  514.         if (list.get[i] != copy[list.count-1-i]) {
  515.             TestErrorResult* e = ERR("reverse doesn't work.\n");
  516.             AddTwoListsToErrorMessage(e, copy, list.count, list.get, list.count, 4);
  517.             return e;
  518.         }
  519.     }
  520.     free(copy);
  521.     Listfloat_Release(&list);
  522.     return NULL;
  523. }
  524.  
  525. TestErrorResult* test_Listfloat_GetSublist() {
  526.     GIVEN_A_RANDOM_LIST(list,10)
  527.     int count = (rand() % 5) + 3;
  528.     int start = rand() % 3;
  529.     float* destination = malloc(sizeof(float)*count);
  530.     int result = Listfloat_GetSublist(&list, start, count, destination);
  531.     IMPLEMENT_CHECK(result)
  532.     for(int i = 0; i < count; ++i) {
  533.         if (destination[i] != list.get[start+i]) {
  534.             TestErrorResult* e = ERR("failed to get complete sublist index:%d count:%d.\n", start, count);
  535.             AddTwoListsToErrorMessage(e, destination, count, list.get, list.count, 4);
  536.             return e;
  537.         }
  538.     }
  539.     free(destination);
  540.     Listfloat_Release(&list);
  541.     return NULL;
  542. }
  543.  
  544. TestErrorResult* test_Listfloat_InsertRange() {
  545.     int originalListSize = (rand() % 5) + 5;
  546.     int count = (rand() % 5) + 3;
  547.     int start = rand() % 3;
  548.     GIVEN_A_RANDOM_LIST(list,originalListSize)
  549.     GIVEN_A_RANDOM_LIST(sublist,count)
  550.     int result = Listfloat_InsertRange(&list, start, sublist.get, sublist.count);
  551.     IMPLEMENT_CHECK(result)
  552.     if (list.count != originalListSize + sublist.count) {
  553.         TestErrorResult* e = ERR("InsertRange(%d, %d) didn't increase size correctly.\n", start, count);
  554.         AddTwoListsToErrorMessage(e, list.get, list.count, sublist.get, sublist.count, 4);
  555.         return e;
  556.     }
  557.     for(int i = 0; i < sublist.count; ++i) {
  558.         if (list.get[start+i] != sublist.get[i]) {
  559.             TestErrorResult* e = ERR("InsertRange(%d, %d) didn't copy data correctly.\n", start, count);
  560.             AddTwoListsToErrorMessage(e, list.get, list.count, sublist.get, sublist.count, 4);
  561.             return e;
  562.         }
  563.     }
  564.     Listfloat_Release(&sublist);
  565.     Listfloat_Release(&list);
  566.     return NULL;
  567. }
  568.  
  569. // TODO make test cases for
  570. // for each
  571. // find index (predicate)
  572. // find last (predicate)
  573. // find all (predicate)
  574.  
  575. #define TEST(a) {#a, a}
  576. TestFunctionEntry testFunctionEntries[] = {
  577.     TEST(test_Listfloat_Init),
  578.     TEST(test_Listfloat_SetCapacity),
  579.     TEST(test_Listfloat_Release),
  580.     TEST(test_Listfloat_Add),
  581.     TEST(test_Listfloat_Adds),
  582.     TEST(test_Listfloat_Remove),
  583.     TEST(test_Listfloat_RemoveRange),
  584.     TEST(test_Listfloat_Clear),
  585.     TEST(test_Listfloat_Insert),
  586.     TEST(test_Listfloat_IndexOf),
  587.     TEST(test_Listfloat_IndexOfLast),  
  588.     TEST(test_Listfloat_Sort),
  589.     TEST(test_Listfloat_SortPredicate),
  590.     TEST(test_Listfloat_BinarySearchComparer),
  591.     TEST(test_Listfloat_Reverse),
  592.     TEST(test_Listfloat_GetSublist),
  593.     TEST(test_Listfloat_InsertRange),
  594.     TEST(NULL),
  595. };
  596.  
  597.  
  598. #include <time.h>
  599.  
  600. int RunTest(TestFunctionEntry* test) {
  601.     TestErrorResult* error = test->func();
  602.     if (error != NULL) {
  603.         printf("%s failed @ %s:%d\n%s\n", test->name, error->file, error->line, error->message);
  604.         return FAIL;
  605.     }
  606.     return SUCCESS;
  607. }
  608.  
  609. void RunTests() {
  610.     srand(time(0));
  611.     int t = 0;
  612.     while(testFunctionEntries[t].func) {
  613.         printf("%s\r", testFunctionEntries[t].name);
  614.         if(RunTest(&testFunctionEntries[t]) == FAIL)
  615.             return;
  616.         Success(testFunctionEntries[t].name, 500, 2);
  617.         printf("\n");
  618.         ++t;
  619.     }
  620.     Success("ALL TESTS PASSED!", 500*10, 2*10);
  621. }
  622.  
  623. int main(int argc, char** argv) {
  624.     RunTests();
  625.     return 0;
  626. }
  627.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement