1 /* check-sources:disable-copyright-check */
2 #include <stdio.h>
3 #include <check.h>
4 #include <droplet.h>
5 #include "utest_main.h"
6 
7 
START_TEST(sbuf_test)8 START_TEST(sbuf_test)
9 {
10   dpl_sbuf_t* b;
11   dpl_sbuf_t* b2;
12   FILE* fp;
13   char pbuf[1024];
14 
15   int sizes[] = {1, 2, 4, 8, 16, 32, 65};
16   unsigned int i;
17   for (i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++) {
18     b = dpl_sbuf_new(sizes[i]);
19     dpl_assert_ptr_not_null(b);
20     b2 = dpl_sbuf_dup(b);
21     dpl_assert_ptr_not_null(b2);
22     dpl_sbuf_free(b);
23     dpl_sbuf_free(b2);
24   }
25 
26   memset(pbuf, 0xff, sizeof(pbuf));
27   fp = fmemopen(pbuf, sizeof(pbuf), "w");
28   dpl_assert_ptr_not_null(fp);
29 
30   const char* strs[] = {"truc", "bidule", "machin", "chose"};
31   for (i = 0; i < sizeof(strs) / sizeof(strs[0]); i++) {
32     b = dpl_sbuf_new_from_str(strs[i]);
33     dpl_assert_ptr_not_null(b);
34     dpl_assert_str_eq(strs[i], dpl_sbuf_get_str(b));
35     b2 = dpl_sbuf_dup(b);
36     dpl_assert_ptr_not_null(b2);
37     dpl_assert_str_eq(strs[i], dpl_sbuf_get_str(b2));
38     dpl_sbuf_print(fp, b);
39     dpl_sbuf_free(b);
40     dpl_sbuf_free(b2);
41   }
42 
43   const char* full_str = "trucbidulemachinchose";
44   fflush(fp);
45   fail_unless(0 == memcmp(full_str, pbuf, strlen(full_str)), NULL);
46 
47   b = dpl_sbuf_new_from_str("");
48   for (i = 0; i < sizeof(strs) / sizeof(strs[0]); i++) {
49     dpl_status_t ret;
50     ret = dpl_sbuf_add_str(b, strs[i]);
51     fail_unless(DPL_SUCCESS == ret, NULL);
52   }
53   dpl_assert_str_eq(full_str, dpl_sbuf_get_str(b));
54   dpl_sbuf_free(b);
55   fclose(fp);
56 }
57 END_TEST
58 
59 
sbuf_suite()60 Suite* sbuf_suite()
61 {
62   Suite* s = suite_create("sbuf");
63   TCase* t = tcase_create("base");
64   tcase_add_test(t, sbuf_test);
65   suite_add_tcase(s, t);
66   return s;
67 }
68