1 #include "test_def.h"
2
3 #include "lwip/def.h"
4
5 #define MAGIC_UNTOUCHED_BYTE 0x7a
6 #define TEST_BUFSIZE 32
7 #define GUARD_SIZE 4
8
9 /* Setups/teardown functions */
10
11 static void
def_setup(void)12 def_setup(void)
13 {
14 }
15
16 static void
def_teardown(void)17 def_teardown(void)
18 {
19 }
20
21 static void
def_check_range_untouched(const char * buf,size_t len)22 def_check_range_untouched(const char *buf, size_t len)
23 {
24 size_t i;
25
26 for (i = 0; i < len; i++) {
27 fail_unless(buf[i] == (char)MAGIC_UNTOUCHED_BYTE);
28 }
29 }
30
test_def_itoa(int number,const char * expected)31 static void test_def_itoa(int number, const char *expected)
32 {
33 char buf[TEST_BUFSIZE];
34 char *test_buf = &buf[GUARD_SIZE];
35
36 size_t exp_len = strlen(expected);
37 fail_unless(exp_len + 4 < (TEST_BUFSIZE - (2 * GUARD_SIZE)));
38
39 memset(buf, MAGIC_UNTOUCHED_BYTE, sizeof(buf));
40 lwip_itoa(test_buf, exp_len + 1, number);
41 def_check_range_untouched(buf, GUARD_SIZE);
42 fail_unless(test_buf[exp_len] == 0);
43 fail_unless(!memcmp(test_buf, expected, exp_len));
44 def_check_range_untouched(&test_buf[exp_len + 1], TEST_BUFSIZE - GUARD_SIZE - exp_len - 1);
45
46 /* check with too small buffer */
47 memset(buf, MAGIC_UNTOUCHED_BYTE, sizeof(buf));
48 lwip_itoa(test_buf, exp_len, number);
49 def_check_range_untouched(buf, GUARD_SIZE);
50 def_check_range_untouched(&test_buf[exp_len + 1], TEST_BUFSIZE - GUARD_SIZE - exp_len - 1);
51
52 /* check with too large buffer */
53 memset(buf, MAGIC_UNTOUCHED_BYTE, sizeof(buf));
54 lwip_itoa(test_buf, exp_len + 4, number);
55 def_check_range_untouched(buf, GUARD_SIZE);
56 fail_unless(test_buf[exp_len] == 0);
57 fail_unless(!memcmp(test_buf, expected, exp_len));
58 def_check_range_untouched(&test_buf[exp_len + 4], TEST_BUFSIZE - GUARD_SIZE - exp_len - 4);
59 }
60
START_TEST(test_def_lwip_itoa)61 START_TEST(test_def_lwip_itoa)
62 {
63 LWIP_UNUSED_ARG(_i);
64
65 test_def_itoa(0, "0");
66 test_def_itoa(1, "1");
67 test_def_itoa(-1, "-1");
68 test_def_itoa(15, "15");
69 test_def_itoa(-15, "-15");
70 test_def_itoa(156, "156");
71 test_def_itoa(1192, "1192");
72 test_def_itoa(-156, "-156");
73 }
74 END_TEST
75
76 /** Create the suite including all tests for this module */
77 Suite *
def_suite(void)78 def_suite(void)
79 {
80 testfunc tests[] = {
81 TESTFUNC(test_def_lwip_itoa)
82 };
83 return create_suite("DEF", tests, sizeof(tests)/sizeof(testfunc), def_setup, def_teardown);
84 }
85