]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
c62c294f | 2 | |
442ac269 | 3 | #include <stdio.h> |
c62c294f | 4 | |
c62c294f | 5 | #include "strxcpyx.h" |
4f7452a8 | 6 | #include "tests.h" |
c62c294f | 7 | |
4f7452a8 | 8 | TEST(strpcpy) { |
c62c294f TA |
9 | char target[25]; |
10 | char *s = target; | |
11 | size_t space_left; | |
648a799f | 12 | bool truncated; |
c62c294f TA |
13 | |
14 | space_left = sizeof(target); | |
648a799f YW |
15 | space_left = strpcpy_full(&s, space_left, "12345", &truncated); |
16 | assert_se(!truncated); | |
17 | space_left = strpcpy_full(&s, space_left, "hey hey hey", &truncated); | |
18 | assert_se(!truncated); | |
19 | space_left = strpcpy_full(&s, space_left, "waldo", &truncated); | |
20 | assert_se(!truncated); | |
21 | space_left = strpcpy_full(&s, space_left, "ba", &truncated); | |
22 | assert_se(!truncated); | |
23 | space_left = strpcpy_full(&s, space_left, "r", &truncated); | |
24 | assert_se(!truncated); | |
25 | assert_se(space_left == 1); | |
c79e88b3 | 26 | ASSERT_STREQ(target, "12345hey hey heywaldobar"); |
c62c294f | 27 | |
648a799f YW |
28 | space_left = strpcpy_full(&s, space_left, "", &truncated); |
29 | assert_se(!truncated); | |
30 | assert_se(space_left == 1); | |
c79e88b3 | 31 | ASSERT_STREQ(target, "12345hey hey heywaldobar"); |
648a799f YW |
32 | |
33 | space_left = strpcpy_full(&s, space_left, "f", &truncated); | |
34 | assert_se(truncated); | |
bdf7026e | 35 | assert_se(space_left == 0); |
c79e88b3 | 36 | ASSERT_STREQ(target, "12345hey hey heywaldobar"); |
648a799f YW |
37 | |
38 | space_left = strpcpy_full(&s, space_left, "", &truncated); | |
39 | assert_se(!truncated); | |
40 | assert_se(space_left == 0); | |
c79e88b3 | 41 | ASSERT_STREQ(target, "12345hey hey heywaldobar"); |
648a799f YW |
42 | |
43 | space_left = strpcpy_full(&s, space_left, "foo", &truncated); | |
44 | assert_se(truncated); | |
45 | assert_se(space_left == 0); | |
c79e88b3 | 46 | ASSERT_STREQ(target, "12345hey hey heywaldobar"); |
c62c294f TA |
47 | } |
48 | ||
4f7452a8 | 49 | TEST(strpcpyf) { |
c62c294f TA |
50 | char target[25]; |
51 | char *s = target; | |
52 | size_t space_left; | |
648a799f | 53 | bool truncated; |
c62c294f TA |
54 | |
55 | space_left = sizeof(target); | |
648a799f YW |
56 | space_left = strpcpyf_full(&s, space_left, &truncated, "space left: %zu. ", space_left); |
57 | assert_se(!truncated); | |
58 | space_left = strpcpyf_full(&s, space_left, &truncated, "foo%s", "bar"); | |
59 | assert_se(!truncated); | |
bdf7026e | 60 | assert_se(space_left == 3); |
c79e88b3 | 61 | ASSERT_STREQ(target, "space left: 25. foobar"); |
648a799f YW |
62 | |
63 | space_left = strpcpyf_full(&s, space_left, &truncated, "%i", 42); | |
64 | assert_se(!truncated); | |
65 | assert_se(space_left == 1); | |
c79e88b3 | 66 | ASSERT_STREQ(target, "space left: 25. foobar42"); |
648a799f YW |
67 | |
68 | space_left = strpcpyf_full(&s, space_left, &truncated, "%s", ""); | |
69 | assert_se(!truncated); | |
70 | assert_se(space_left == 1); | |
c79e88b3 | 71 | ASSERT_STREQ(target, "space left: 25. foobar42"); |
648a799f YW |
72 | |
73 | space_left = strpcpyf_full(&s, space_left, &truncated, "%c", 'x'); | |
74 | assert_se(truncated); | |
75 | assert_se(space_left == 0); | |
c79e88b3 | 76 | ASSERT_STREQ(target, "space left: 25. foobar42"); |
648a799f YW |
77 | |
78 | space_left = strpcpyf_full(&s, space_left, &truncated, "%s", ""); | |
79 | assert_se(!truncated); | |
80 | assert_se(space_left == 0); | |
c79e88b3 | 81 | ASSERT_STREQ(target, "space left: 25. foobar42"); |
648a799f YW |
82 | |
83 | space_left = strpcpyf_full(&s, space_left, &truncated, "abc%s", "hoge"); | |
84 | assert_se(truncated); | |
85 | assert_se(space_left == 0); | |
c79e88b3 | 86 | ASSERT_STREQ(target, "space left: 25. foobar42"); |
54d46a78 ZJS |
87 | |
88 | /* test overflow */ | |
89 | s = target; | |
648a799f YW |
90 | space_left = strpcpyf_full(&s, 12, &truncated, "00 left: %i. ", 999); |
91 | assert_se(truncated); | |
c79e88b3 | 92 | ASSERT_STREQ(target, "00 left: 99"); |
54d46a78 ZJS |
93 | assert_se(space_left == 0); |
94 | assert_se(target[12] == '2'); | |
c62c294f TA |
95 | } |
96 | ||
4f7452a8 | 97 | TEST(strpcpyl) { |
c62c294f TA |
98 | char target[25]; |
99 | char *s = target; | |
100 | size_t space_left; | |
648a799f | 101 | bool truncated; |
c62c294f TA |
102 | |
103 | space_left = sizeof(target); | |
648a799f YW |
104 | space_left = strpcpyl_full(&s, space_left, &truncated, "waldo", " test", " waldo. ", NULL); |
105 | assert_se(!truncated); | |
106 | space_left = strpcpyl_full(&s, space_left, &truncated, "Banana", NULL); | |
107 | assert_se(!truncated); | |
108 | assert_se(space_left == 1); | |
c79e88b3 | 109 | ASSERT_STREQ(target, "waldo test waldo. Banana"); |
648a799f YW |
110 | |
111 | space_left = strpcpyl_full(&s, space_left, &truncated, "", "", "", NULL); | |
112 | assert_se(!truncated); | |
bdf7026e | 113 | assert_se(space_left == 1); |
c79e88b3 | 114 | ASSERT_STREQ(target, "waldo test waldo. Banana"); |
648a799f YW |
115 | |
116 | space_left = strpcpyl_full(&s, space_left, &truncated, "", "x", "", NULL); | |
117 | assert_se(truncated); | |
118 | assert_se(space_left == 0); | |
c79e88b3 | 119 | ASSERT_STREQ(target, "waldo test waldo. Banana"); |
648a799f YW |
120 | |
121 | space_left = strpcpyl_full(&s, space_left, &truncated, "hoge", NULL); | |
122 | assert_se(truncated); | |
123 | assert_se(space_left == 0); | |
c79e88b3 | 124 | ASSERT_STREQ(target, "waldo test waldo. Banana"); |
c62c294f TA |
125 | } |
126 | ||
4f7452a8 | 127 | TEST(strscpy) { |
c62c294f TA |
128 | char target[25]; |
129 | size_t space_left; | |
648a799f | 130 | bool truncated; |
c62c294f TA |
131 | |
132 | space_left = sizeof(target); | |
648a799f YW |
133 | space_left = strscpy_full(target, space_left, "12345", &truncated); |
134 | assert_se(!truncated); | |
c62c294f | 135 | |
c79e88b3 | 136 | ASSERT_STREQ(target, "12345"); |
bdf7026e | 137 | assert_se(space_left == 20); |
c62c294f TA |
138 | } |
139 | ||
4f7452a8 | 140 | TEST(strscpyl) { |
c62c294f TA |
141 | char target[25]; |
142 | size_t space_left; | |
648a799f | 143 | bool truncated; |
c62c294f TA |
144 | |
145 | space_left = sizeof(target); | |
648a799f YW |
146 | space_left = strscpyl_full(target, space_left, &truncated, "12345", "waldo", "waldo", NULL); |
147 | assert_se(!truncated); | |
c62c294f | 148 | |
c79e88b3 | 149 | ASSERT_STREQ(target, "12345waldowaldo"); |
bdf7026e | 150 | assert_se(space_left == 10); |
c62c294f TA |
151 | } |
152 | ||
4f7452a8 | 153 | TEST(sd_event_code_migration) { |
442ac269 YW |
154 | char b[100 * DECIMAL_STR_MAX(unsigned) + 1]; |
155 | char c[100 * DECIMAL_STR_MAX(unsigned) + 1], *p; | |
156 | unsigned i; | |
157 | size_t l; | |
b798490f | 158 | int o, r; |
442ac269 | 159 | |
b798490f LB |
160 | for (i = o = 0; i < 100; i++) { |
161 | r = snprintf(&b[o], sizeof(b) - o, "%u ", i); | |
162 | assert_se(r >= 0 && r < (int) sizeof(b) - o); | |
163 | o += r; | |
164 | } | |
442ac269 YW |
165 | |
166 | p = c; | |
167 | l = sizeof(c); | |
168 | for (i = 0; i < 100; i++) | |
169 | l = strpcpyf(&p, l, "%u ", i); | |
170 | ||
c79e88b3 | 171 | ASSERT_STREQ(b, c); |
442ac269 YW |
172 | } |
173 | ||
4f7452a8 | 174 | DEFINE_TEST_MAIN(LOG_INFO); |