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