1 /*
2 * Copyright (C) 2019 Tomohiro Kusumi <tkusumi@netbsd.org>
3 */
4 #include "../unittest.h"
5
6 #ifndef CONFIG_STRSEP
7 #include "../../oslib/strsep.h"
8 #else
9 #include <string.h>
10 #endif
11
12 /*
13 * strsep(3) - "If *stringp is NULL, the strsep() function returns NULL and does
14 * nothing else."
15 */
test_strsep_1(void)16 static void test_strsep_1(void)
17 {
18 char *string = NULL;
19 const char *p;
20
21 p = strsep(&string, "");
22 CU_ASSERT_EQUAL(p, NULL);
23 CU_ASSERT_EQUAL(string, NULL);
24
25 p = strsep(&string, "ABC");
26 CU_ASSERT_EQUAL(p, NULL);
27 CU_ASSERT_EQUAL(string, NULL);
28 }
29
30 /*
31 * strsep(3) - "In case no delimiter was found, the token is taken to be the
32 * entire string *stringp, and *stringp is made NULL."
33 */
test_strsep_2(void)34 static void test_strsep_2(void)
35 {
36 char src[] = "ABCDEFG";
37 char *string = src;
38 const char *p;
39
40 p = strsep(&string, "");
41 CU_ASSERT_EQUAL(p, src);
42 CU_ASSERT_EQUAL(*p, 'A');
43 CU_ASSERT_EQUAL(string, NULL);
44
45 string = src;
46 p = strsep(&string, "@");
47 CU_ASSERT_EQUAL(p, src);
48 CU_ASSERT_EQUAL(*p, 'A');
49 CU_ASSERT_EQUAL(string, NULL);
50 }
51
52 /*
53 * strsep(3) - "This token is terminated with a '\0' character (by overwriting
54 * the delimiter) and *stringp is updated to point past the token."
55 */
test_strsep_3(void)56 static void test_strsep_3(void)
57 {
58 char src[] = "ABCDEFG";
59 char *string = src;
60 const char *p;
61
62 p = strsep(&string, "ABC");
63 CU_ASSERT_EQUAL(p, &src[0]);
64 CU_ASSERT_EQUAL(*p, '\0');
65 CU_ASSERT_EQUAL(strcmp(string, "BCDEFG"), 0);
66 CU_ASSERT_EQUAL(*string, 'B');
67
68 p = strsep(&string, "ABC");
69 CU_ASSERT_EQUAL(p, &src[1]);
70 CU_ASSERT_EQUAL(*p, '\0');
71 CU_ASSERT_EQUAL(strcmp(string, "CDEFG"), 0);
72 CU_ASSERT_EQUAL(*string, 'C');
73
74 p = strsep(&string, "ABC");
75 CU_ASSERT_EQUAL(p, &src[2]);
76 CU_ASSERT_EQUAL(*p, '\0');
77 CU_ASSERT_EQUAL(strcmp(string, "DEFG"), 0);
78 CU_ASSERT_EQUAL(*string, 'D');
79
80 p = strsep(&string, "ABC");
81 CU_ASSERT_EQUAL(p, &src[3]);
82 CU_ASSERT_EQUAL(*p, 'D');
83 CU_ASSERT_EQUAL(string, NULL);
84 }
85
86 static struct fio_unittest_entry tests[] = {
87 {
88 .name = "strsep/1",
89 .fn = test_strsep_1,
90 },
91 {
92 .name = "strsep/2",
93 .fn = test_strsep_2,
94 },
95 {
96 .name = "strsep/3",
97 .fn = test_strsep_3,
98 },
99 {
100 .name = NULL,
101 },
102 };
103
fio_unittest_oslib_strsep(void)104 CU_ErrorCode fio_unittest_oslib_strsep(void)
105 {
106 return fio_unittest_add_suite("oslib/strsep.c", NULL, NULL, tests);
107 }
108