test_stack_sd.c (2667B)
1 #include "test_stack_sd.h" 2 3 void test_stack_sd_init() 4 { 5 stack_sd s; 6 stack_sd_init(&s); 7 8 CU_ASSERT(s.nmemb == 0); 9 CU_ASSERT(s.alloc == BASE_STACK_SD_SIZE); 10 CU_ASSERT(s.data != NULL); 11 12 stack_sd_clear(&s); 13 14 stack_sd_init_size(&s, 4); 15 16 CU_ASSERT(s.nmemb == 0); 17 CU_ASSERT(s.alloc == 4); 18 CU_ASSERT(s.data != NULL); 19 20 stack_sd_clear(&s); 21 } 22 23 void test_stack_sd_push() 24 { 25 stack_sd s; 26 stack_sd_init_size(&s, 4); 27 28 sd topush[5]; 29 size_t i; 30 for(i = 0; i < 5; i++) { 31 sd_init_u64(&topush[i], i); 32 stack_sd_push(&s, topush[i]); 33 34 CU_ASSERT(s.nmemb == i+1); 35 } 36 CU_ASSERT(s.nmemb == 5); 37 CU_ASSERT(s.alloc == 8); 38 39 CU_ASSERT(memcmp(s.data, topush, sizeof(*topush) * 5) == 0); 40 41 for(i = 0; i < 5; i++) sd_clear(&topush[i]); 42 stack_sd_clear(&s); 43 } 44 45 void test_stack_sd_get() 46 { 47 stack_sd s; 48 stack_sd_init(&s); 49 50 sd topush[20]; 51 size_t i; 52 for(i = 0; i < 20; i++) { 53 sd_init_u64(&topush[i], i); 54 stack_sd_push(&s, topush[i]); 55 } 56 57 sd tmp; 58 for(i = 0; i < 20; i++) { 59 tmp = stack_sd_get(&s, i); 60 CU_ASSERT(tmp.size == topush[i].size); 61 CU_ASSERT(tmp.data == topush[i].data); 62 } 63 64 for(i = 0; i < 5; i++) sd_clear(&topush[i]); 65 stack_sd_clear(&s); 66 } 67 68 void test_stack_sd_getlast() 69 { 70 stack_sd s; 71 stack_sd_init_size(&s, 8); 72 73 sd topush[20]; 74 size_t i; 75 for(i = 0; i < 20; i++) { 76 sd_init_u64(&topush[i], i); 77 stack_sd_push(&s, topush[i]); 78 } 79 80 sd tmp = stack_sd_getlast(&s); 81 CU_ASSERT(tmp.size == topush[19].size); 82 CU_ASSERT(tmp.data == topush[19].data); 83 84 for(i = 0; i < 5; i++) sd_clear(&topush[i]); 85 stack_sd_clear(&s); 86 } 87 88 void test_stack_sd_pop() 89 { 90 stack_sd s; 91 stack_sd_init_size(&s, 8); 92 93 sd topush[20]; 94 size_t i; 95 for(i = 0; i < 20; i++) { 96 sd_init_u64(&topush[i], i); 97 stack_sd_push(&s, topush[i]); 98 } 99 100 sd tmp; 101 for(i = 0; i < 20; i++) { 102 tmp = stack_sd_pop(&s); 103 CU_ASSERT(tmp.size == topush[19-i].size); 104 CU_ASSERT(tmp.data == topush[19-i].data); 105 } 106 107 tmp = stack_sd_pop(&s); 108 CU_ASSERT(tmp.size == STACK_SD_EMPTY_POP.size); 109 CU_ASSERT(tmp.data == STACK_SD_EMPTY_POP.data); 110 111 tmp = stack_sd_pop(&s); 112 CU_ASSERT(tmp.size == STACK_SD_EMPTY_POP.size); 113 CU_ASSERT(tmp.data == STACK_SD_EMPTY_POP.data); 114 115 for(i = 0; i < 5; i++) sd_clear(&topush[i]); 116 stack_sd_clear(&s); 117 } 118 119 void test_stack_sd_lookup() 120 { 121 stack_sd s; 122 stack_sd_init_size(&s, 8); 123 124 sd topush[20]; 125 size_t i; 126 for(i = 0; i < 20; i++) { 127 sd_init_u64(&topush[i], i); 128 stack_sd_push(&s, topush[i]); 129 } 130 131 sd tmp; 132 sd_init_u64(&tmp, 13); 133 CU_ASSERT(stack_sd_lookup(&s, tmp) == 13); 134 sd_clear(&tmp); 135 sd_init_u64(&tmp, 22); 136 CU_ASSERT(stack_sd_lookup(&s, tmp) == STACK_SD_LOOKUP_NOT_FOUND); 137 sd_clear(&tmp); 138 139 for(i = 0; i < 5; i++) sd_clear(&topush[i]); 140 stack_sd_clear(&s); 141 }