test_stretchy_array.c (2248B)
1 /* copyright (c) 2013-2015 vincent forest (vaplv@free.fr) 2 * 3 * the rsys library is free software: you can redistribute it and/or modify 4 * it under the terms of the gnu lesser general public license as published 5 * by the free software foundation, either version 3 of the license, or 6 * (at your option) any later version. 7 * 8 * the rsys library is distributed in the hope that it will be useful, 9 * but without any warranty; without even the implied warranty of 10 * merchantability or fitness for a particular purpose. see the 11 * gnu lesser general public license for more details. 12 * 13 * you should have received a copy of the gnu lesser general public license 14 * along with the rsys library. if not, see <http://www.gnu.org/licenses/>. */ 15 16 #include "stretchy_array.h" 17 #include "math.h" 18 19 static void 20 test_double(void) 21 { 22 double* array = NULL; 23 24 CHK(sa_size(array) == 0); 25 sa_clear(array); 26 27 CHK(sa_add(array, 4) == array); 28 CHK(sa_size(array) == 4); 29 30 array[0] = PI; 31 array[1] = PI; 32 array[2] = PI; 33 array[3] = PI; 34 CHK(sa_last(array) == PI); 35 36 sa_push(array, 1.2345); 37 CHK(sa_last(array) == 1.2345); 38 sa_push(array, 6.7890); 39 CHK(sa_last(array) == 6.7890); 40 41 CHK(array[0] == PI); 42 CHK(array[1] == PI); 43 CHK(array[2] == PI); 44 CHK(array[3] == PI); 45 CHK(array[4] == 1.2345); 46 CHK(array[5] == 6.7890); 47 CHK(sa_size(array) == 6); 48 49 sa_clear(array); 50 CHK(sa_size(array) == 0); 51 52 sa_release(array); 53 } 54 55 static void 56 test_int(void) 57 { 58 int* array = NULL; 59 int* p; 60 size_t i; 61 62 CHK(sa_size(array) == 0); 63 64 FOR_EACH(i, 0, 1024) 65 sa_push(array, (int)i); 66 67 CHK(sa_size(array) == 1024); 68 69 FOR_EACH(i, 0, 1024) 70 CHK(array[i] == (int)i); 71 72 CHK(sa_last(array) == 1023); 73 74 p = sa_add(array, 32); 75 CHK(sa_size(array) == 1056); 76 77 FOR_EACH(i, 0, 32) 78 p[i] = (int)i; 79 80 CHK(sa_last(array) == 31); 81 FOR_EACH(i, 0, 32) 82 array[1024 + i] = (int)i; 83 84 sa_clear(array); 85 CHK(sa_size(array) == 0); 86 p = sa_add(array, 16); 87 CHK(p == array); 88 CHK(sa_size(array) == 16); 89 FOR_EACH(i, 0, 16) 90 p[i] = -(int)i; 91 92 FOR_EACH(i, 0, 16) 93 CHK(array[i] == -(int)i); 94 95 sa_release(array); 96 } 97 98 int 99 main(int argc, char** argv) 100 { 101 (void)argc, (void)argv; 102 test_int(); 103 test_double(); 104 CHK(mem_allocated_size() == 0); 105 return 0; 106 }