2 #include "../../src/utils.h"
4 #define FAKE_PATH "nonexistentreallyfakepath"
11 static void test_unix_absolute_path_on_absolute_returns_it(void **state) {
14 const char *paths[] = {"/", "/foo", "/foo/./../bar"};
16 for(size_t i = 0; i < sizeof(paths) / sizeof(*paths); ++i) {
17 char *got = absolute_path(paths[i]);
18 assert_ptr_not_equal(paths[i], got);
19 assert_string_equal(paths[i], got);
24 static void test_unix_absolute_path_on_empty_returns_null(void **state) {
26 assert_null(absolute_path(NULL));
27 assert_null(absolute_path("\0"));
30 static void test_unix_absolute_path_relative(void **state) {
33 testcase_t cases[] = {
36 {"./"FAKE_PATH, "/./"FAKE_PATH},
37 {"../foo/./../"FAKE_PATH, "/../foo/./../"FAKE_PATH},
40 for(size_t i = 0; i < sizeof(cases) / sizeof(*cases); ++i) {
41 char *got = absolute_path(cases[i].arg);
42 assert_string_equal(cases[i].want, got);
47 static void test_int_to_str(const char *ref, int num) {
48 char *result = int_to_str(num);
49 assert_string_equal(ref, result);
53 static void test_int_to_str_return_expected(void **state) {
56 test_int_to_str("0", 0);
57 test_int_to_str("-1337", -1337);
58 test_int_to_str("65535", 65535);
61 static void test_is_decimal_fail_empty(void **state) {
64 assert_false(is_decimal(NULL));
65 assert_false(is_decimal(""));
68 static void test_is_decimal_fail_hex(void **state) {
71 assert_false(is_decimal("DEADBEEF"));
72 assert_false(is_decimal("0xCAFE"));
75 static void test_is_decimal_fail_junk_suffix(void **state) {
78 assert_false(is_decimal("123foobar"));
79 assert_false(is_decimal("777 "));
82 static void test_is_decimal_pass_simple(void **state) {
85 assert_true(is_decimal("0"));
86 assert_true(is_decimal("123"));
89 static void test_is_decimal_pass_signs(void **state) {
92 assert_true(is_decimal("-123"));
93 assert_true(is_decimal("+123"));
96 static void test_is_decimal_pass_whitespace_prefix(void **state) {
99 assert_true(is_decimal(" \r\n\t 777"));
102 static int setup_path_unix(void **state) {
104 assert_int_equal(0, chdir("/"));
108 static void test_string_eq(void **state) {
111 assert_true(string_eq(NULL, NULL));
112 assert_true(string_eq("", ""));
113 assert_true(string_eq("\tfoo 123", "\tfoo 123"));
115 assert_false(string_eq(NULL, ""));
116 assert_false(string_eq("", NULL));
117 assert_false(string_eq("foo", "FOO"));
118 assert_false(string_eq("foo", " foo"));
122 const struct CMUnitTest tests[] = {
123 cmocka_unit_test_setup(test_unix_absolute_path_on_absolute_returns_it, setup_path_unix),
124 cmocka_unit_test_setup(test_unix_absolute_path_on_empty_returns_null, setup_path_unix),
125 cmocka_unit_test_setup(test_unix_absolute_path_relative, setup_path_unix),
126 cmocka_unit_test(test_int_to_str_return_expected),
127 cmocka_unit_test(test_is_decimal_fail_empty),
128 cmocka_unit_test(test_is_decimal_fail_hex),
129 cmocka_unit_test(test_is_decimal_fail_junk_suffix),
130 cmocka_unit_test(test_is_decimal_pass_simple),
131 cmocka_unit_test(test_is_decimal_pass_signs),
132 cmocka_unit_test(test_is_decimal_pass_whitespace_prefix),
133 cmocka_unit_test(test_string_eq),
137 cmocka_set_skip_filter("test_unix_*");
140 return cmocka_run_group_tests(tests, NULL, NULL);