test_config.c (1690B)
1 #include <stdlib.h> 2 #include <check.h> 3 4 #include "lq/err.h" 5 #include "lq/config.h" 6 7 8 START_TEST(check_core) { 9 int r; 10 11 r = lq_config_init(); 12 lq_config_free(); 13 ck_assert_int_eq(r, ERR_OK); 14 } 15 END_TEST 16 17 START_TEST(check_register) { 18 int r; 19 20 lq_config_init(); 21 r = lq_config_register(LQ_TYP_STR, NULL); 22 lq_config_free(); 23 ck_assert_int_ge(r, 0); 24 } 25 END_TEST 26 27 START_TEST(check_set_get) { 28 int r; 29 long v; 30 char *p; 31 int c; 32 33 c = LQ_CFG_LAST; 34 lq_config_init(); 35 r = lq_config_register(LQ_TYP_NUM, NULL); 36 ck_assert_int_ge(r, 0); 37 r = lq_config_register(LQ_TYP_STR, NULL); 38 ck_assert_int_ge(r, 0); 39 40 // set with the index returned from the last register action 41 r = lq_config_set(r, "foobarbaz"); 42 ck_assert_int_eq(r, ERR_OK); 43 44 v = 42; 45 r = lq_config_set(c, &v); 46 ck_assert_int_eq(r, ERR_OK); 47 48 r = lq_config_get(c, (void**)&p); 49 ck_assert_int_eq(r, ERR_OK); 50 v = *((long*)p); 51 ck_assert_int_eq(v, 42); 52 53 r = lq_config_get(c + 1, (void**)&p); 54 ck_assert_int_eq(r, ERR_OK); 55 ck_assert_str_eq(p, "foobarbaz"); 56 57 r = lq_config_set(c + 2, &v); 58 ck_assert_int_eq(r, ERR_OVERFLOW); 59 60 r = lq_config_get(c + 2, (void**)&p); 61 ck_assert_int_eq(r, ERR_OVERFLOW); 62 63 lq_config_free(); 64 } 65 END_TEST 66 67 Suite * common_suite(void) { 68 Suite *s; 69 TCase *tc; 70 71 s = suite_create("config"); 72 tc = tcase_create("core"); 73 tcase_add_test(tc, check_core); 74 tcase_add_test(tc, check_register); 75 tcase_add_test(tc, check_set_get); 76 suite_add_tcase(s, tc); 77 78 return s; 79 } 80 81 int main(void) { 82 int n_fail; 83 84 Suite *s; 85 SRunner *sr; 86 87 s = common_suite(); 88 sr = srunner_create(s); 89 90 srunner_run_all(sr, CK_VERBOSE); 91 n_fail = srunner_ntests_failed(sr); 92 srunner_free(sr); 93 94 return (n_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE; 95 }