unittest_stralloc.c (2614B)
1 #include <check.h> 2 3 #include "alloc.h" 4 #include "stralloc.h" 5 6 START_TEST(test_stralloc_thingy) 7 { 8 stralloc thingy = {0}; 9 const char *input = "thingy"; 10 11 stralloc_copys(&thingy,input); 12 stralloc_0(&thingy); 13 14 ck_assert_str_eq(input, thingy.s); 15 16 alloc_free(thingy.s); 17 } 18 END_TEST 19 20 START_TEST(test_stralloc_sizes) 21 { 22 stralloc thingy = {0}; 23 const char *input = "thingy"; 24 unsigned int olen; 25 26 // make room for exactly 42 bytes 27 int r = stralloc_ready(&thingy, 42); 28 ck_assert_int_eq(r, 1); 29 ck_assert_uint_eq(thingy.len, 0); 30 ck_assert_uint_eq(thingy.a, 42); 31 // make sure that there is room for 42 more bytes 32 // given that there are 42 bytes in it and nothing is used this does nothing 33 r = stralloc_readyplus(&thingy, 42); 34 ck_assert_int_eq(r, 1); 35 ck_assert_uint_eq(thingy.len, 0); 36 ck_assert_uint_eq(thingy.a, 42); 37 // make sure there is room for 16 more bytes 38 // there is already more room available, so this does nothing 39 r = stralloc_readyplus(&thingy, 16); 40 ck_assert_int_eq(r, 1); 41 ck_assert_uint_eq(thingy.len, 0); 42 ck_assert_uint_eq(thingy.a, 42); 43 44 r = stralloc_copys(&thingy,input); 45 ck_assert_int_eq(r, 1); 46 ck_assert_uint_eq(thingy.len, strlen(input)); 47 ck_assert_uint_eq(thingy.a, 42); 48 49 r = stralloc_0(&thingy); 50 ck_assert_int_eq(r, 1); 51 ck_assert_uint_eq(thingy.len, strlen(input) + 1); 52 ck_assert_uint_eq(thingy.a, 42); 53 54 // make sure that there is room for 42 more bytes 55 r = stralloc_readyplus(&thingy, 42); 56 ck_assert_int_eq(r, 1); 57 ck_assert_uint_eq(thingy.len, strlen(input) + 1); 58 ck_assert_uint_ge(thingy.a, thingy.len + 42); 59 olen = thingy.a; 60 61 // another stralloc_ready should not touch anything 62 r = stralloc_ready(&thingy, 42); 63 ck_assert_int_eq(r, 1); 64 ck_assert_uint_eq(thingy.len, strlen(input) + 1); 65 ck_assert_uint_ge(thingy.a, olen); 66 67 unsigned int ofl = -30; 68 69 r = stralloc_readyplus(&thingy, ofl); 70 ck_assert_int_eq(r, 0); 71 ck_assert_uint_eq(thingy.len, strlen(input) + 1); 72 ck_assert_uint_ge(thingy.a, olen); 73 74 ck_assert_str_eq(input, thingy.s); 75 76 alloc_free(thingy.s); 77 } 78 END_TEST 79 80 TCase 81 *stralloc_something(void) 82 { 83 TCase *tc = tcase_create("basic operations"); 84 85 tcase_add_test(tc, test_stralloc_thingy); 86 tcase_add_test(tc, test_stralloc_sizes); 87 88 return tc; 89 } 90 91 Suite 92 *stralloc_suite(void) 93 { 94 Suite *s = suite_create("notqmail stralloc"); 95 96 suite_add_tcase(s, stralloc_something()); 97 98 return s; 99 } 100 101 int 102 main(void) 103 { 104 int number_failed; 105 106 SRunner *sr = srunner_create(stralloc_suite()); 107 srunner_run_all(sr, CK_NORMAL); 108 number_failed = srunner_ntests_failed(sr); 109 srunner_free(sr); 110 111 return number_failed; 112 }