]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
c62c294f | 2 | |
442ac269 | 3 | #include <stdio.h> |
c62c294f | 4 | |
07630cea | 5 | #include "string-util.h" |
c62c294f | 6 | #include "strxcpyx.h" |
07630cea | 7 | #include "util.h" |
c62c294f TA |
8 | |
9 | static void test_strpcpy(void) { | |
10 | char target[25]; | |
11 | char *s = target; | |
12 | size_t space_left; | |
13 | ||
14 | space_left = sizeof(target); | |
15 | space_left = strpcpy(&s, space_left, "12345"); | |
16 | space_left = strpcpy(&s, space_left, "hey hey hey"); | |
17 | space_left = strpcpy(&s, space_left, "waldo"); | |
18 | space_left = strpcpy(&s, space_left, "ba"); | |
19 | space_left = strpcpy(&s, space_left, "r"); | |
20 | space_left = strpcpy(&s, space_left, "foo"); | |
21 | ||
bdf7026e TA |
22 | assert_se(streq(target, "12345hey hey heywaldobar")); |
23 | assert_se(space_left == 0); | |
c62c294f TA |
24 | } |
25 | ||
26 | static void test_strpcpyf(void) { | |
27 | char target[25]; | |
28 | char *s = target; | |
29 | size_t space_left; | |
30 | ||
31 | space_left = sizeof(target); | |
1fa2f38f | 32 | space_left = strpcpyf(&s, space_left, "space left: %zu. ", space_left); |
c62c294f TA |
33 | space_left = strpcpyf(&s, space_left, "foo%s", "bar"); |
34 | ||
bdf7026e TA |
35 | assert_se(streq(target, "space left: 25. foobar")); |
36 | assert_se(space_left == 3); | |
54d46a78 ZJS |
37 | |
38 | /* test overflow */ | |
39 | s = target; | |
40 | space_left = strpcpyf(&s, 12, "00 left: %i. ", 999); | |
41 | assert_se(streq(target, "00 left: 99")); | |
42 | assert_se(space_left == 0); | |
43 | assert_se(target[12] == '2'); | |
c62c294f TA |
44 | } |
45 | ||
46 | static void test_strpcpyl(void) { | |
47 | char target[25]; | |
48 | char *s = target; | |
49 | size_t space_left; | |
50 | ||
51 | space_left = sizeof(target); | |
52 | space_left = strpcpyl(&s, space_left, "waldo", " test", " waldo. ", NULL); | |
53 | space_left = strpcpyl(&s, space_left, "Banana", NULL); | |
54 | ||
bdf7026e TA |
55 | assert_se(streq(target, "waldo test waldo. Banana")); |
56 | assert_se(space_left == 1); | |
c62c294f TA |
57 | } |
58 | ||
59 | static void test_strscpy(void) { | |
60 | char target[25]; | |
61 | size_t space_left; | |
62 | ||
63 | space_left = sizeof(target); | |
64 | space_left = strscpy(target, space_left, "12345"); | |
65 | ||
bdf7026e TA |
66 | assert_se(streq(target, "12345")); |
67 | assert_se(space_left == 20); | |
c62c294f TA |
68 | } |
69 | ||
70 | static void test_strscpyl(void) { | |
71 | char target[25]; | |
72 | size_t space_left; | |
73 | ||
74 | space_left = sizeof(target); | |
75 | space_left = strscpyl(target, space_left, "12345", "waldo", "waldo", NULL); | |
76 | ||
bdf7026e TA |
77 | assert_se(streq(target, "12345waldowaldo")); |
78 | assert_se(space_left == 10); | |
c62c294f TA |
79 | } |
80 | ||
442ac269 YW |
81 | static void test_sd_event_code_migration(void) { |
82 | char b[100 * DECIMAL_STR_MAX(unsigned) + 1]; | |
83 | char c[100 * DECIMAL_STR_MAX(unsigned) + 1], *p; | |
84 | unsigned i; | |
85 | size_t l; | |
86 | int o; | |
87 | ||
88 | for (i = o = 0; i < 100; i++) | |
89 | o += snprintf(&b[o], sizeof(b) - o, "%u ", i); | |
90 | ||
91 | p = c; | |
92 | l = sizeof(c); | |
93 | for (i = 0; i < 100; i++) | |
94 | l = strpcpyf(&p, l, "%u ", i); | |
95 | ||
96 | assert_se(streq(b, c)); | |
97 | } | |
98 | ||
c62c294f TA |
99 | int main(int argc, char *argv[]) { |
100 | test_strpcpy(); | |
101 | test_strpcpyf(); | |
102 | test_strpcpyl(); | |
103 | test_strscpy(); | |
104 | test_strscpyl(); | |
105 | ||
442ac269 YW |
106 | test_sd_event_code_migration(); |
107 | ||
c62c294f TA |
108 | return 0; |
109 | } |