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