stack_str

Stack for strings
git clone git://www.tkruger.se/stack_str.git
Log | Files | Refs | README

test_stack_str.c (1882B)


      1 #include <stdlib.h>
      2 #include <string.h>
      3 #include <stdio.h>
      4 #include <assert.h>
      5 #include "stack_str.h"
      6 
      7 static void test_stack_str_init() {
      8   stack_str t;
      9   stack_str_init(&t);
     10 
     11   assert(t.nmemb == 0);
     12   assert(t.alloc > 0);
     13   assert(t.data != NULL);
     14 
     15   stack_str_clear(&t);
     16 }
     17 
     18 static void test_stack_str_push() {
     19   stack_str t;
     20   stack_str_init(&t);
     21 
     22   uint64_t i;
     23   char buf[2 * BASE_STACK_STR_SIZE][256];
     24   for (i = 0; i < 2 * BASE_STACK_STR_SIZE; i++) {
     25     snprintf(buf[i], 256, "%llu", i);
     26     stack_str_push(&t, buf[i]);
     27   }
     28 
     29   assert(t.alloc == 2 * BASE_STACK_STR_SIZE);
     30   assert(t.nmemb == 2 * BASE_STACK_STR_SIZE);
     31   for (i = 0; i < 2 * BASE_STACK_STR_SIZE; i++) {
     32     assert(strncmp(buf[i], t.data[i], 256) == 0);
     33   }
     34 
     35   stack_str_clear(&t);
     36 }
     37 
     38 static void test_stack_str_get_getlast() {
     39   stack_str t;
     40   stack_str_init(&t);
     41 
     42   uint64_t i;
     43   char buf[2 * BASE_STACK_STR_SIZE][256];
     44   for (i = 0; i < 2 * BASE_STACK_STR_SIZE; i++) {
     45     snprintf(buf[i], 256, "%llu", i);
     46     stack_str_push(&t, buf[i]);
     47   }
     48 
     49   for (i = 0; i < t.nmemb; i++) {
     50     assert(stack_str_get(&t, i) == buf[i]);
     51   }
     52   assert(stack_str_getlast(&t) == buf[2 * BASE_STACK_STR_SIZE - 1]);
     53 
     54   stack_str_clear(&t);
     55 }
     56 
     57 static void test_stack_str_pop() {
     58   stack_str t;
     59   stack_str_init(&t);
     60 
     61   char *strs[3] = {"hello", "world", "this is a msg"};
     62 
     63   stack_str_push(&t, strs[0]);
     64   stack_str_push(&t, strs[1]);
     65   stack_str_push(&t, strs[2]);
     66 
     67   assert(t.nmemb == 3);
     68   assert(stack_str_pop(&t) == strs[2]);
     69   assert(t.nmemb == 2);
     70   assert(stack_str_pop(&t) == strs[1]);
     71   assert(t.nmemb == 1);
     72   assert(stack_str_pop(&t) == strs[0]);
     73   assert(t.nmemb == 0);
     74   assert(stack_str_pop(&t) == STACK_STR_EMPTY_POP);
     75   assert(t.nmemb == 0);
     76 
     77   stack_str_clear(&t);
     78 }
     79 
     80 int main() {
     81   test_stack_str_init();
     82   test_stack_str_push();
     83   test_stack_str_pop();
     84   test_stack_str_get_getlast();
     85 
     86   printf("test ok\n");
     87 }