]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-strxcpyx.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "string-util.h"
10 static void test_strpcpy(void) {
15 space_left
= sizeof(target
);
16 space_left
= strpcpy(&s
, space_left
, "12345");
17 space_left
= strpcpy(&s
, space_left
, "hey hey hey");
18 space_left
= strpcpy(&s
, space_left
, "waldo");
19 space_left
= strpcpy(&s
, space_left
, "ba");
20 space_left
= strpcpy(&s
, space_left
, "r");
21 space_left
= strpcpy(&s
, space_left
, "foo");
23 assert_se(streq(target
, "12345hey hey heywaldobar"));
24 assert_se(space_left
== 0);
27 static void test_strpcpyf(void) {
32 space_left
= sizeof(target
);
33 space_left
= strpcpyf(&s
, space_left
, "space left: %zu. ", space_left
);
34 space_left
= strpcpyf(&s
, space_left
, "foo%s", "bar");
36 assert_se(streq(target
, "space left: 25. foobar"));
37 assert_se(space_left
== 3);
41 space_left
= strpcpyf(&s
, 12, "00 left: %i. ", 999);
42 assert_se(streq(target
, "00 left: 99"));
43 assert_se(space_left
== 0);
44 assert_se(target
[12] == '2');
47 static void test_strpcpyl(void) {
52 space_left
= sizeof(target
);
53 space_left
= strpcpyl(&s
, space_left
, "waldo", " test", " waldo. ", NULL
);
54 space_left
= strpcpyl(&s
, space_left
, "Banana", NULL
);
56 assert_se(streq(target
, "waldo test waldo. Banana"));
57 assert_se(space_left
== 1);
60 static void test_strscpy(void) {
64 space_left
= sizeof(target
);
65 space_left
= strscpy(target
, space_left
, "12345");
67 assert_se(streq(target
, "12345"));
68 assert_se(space_left
== 20);
71 static void test_strscpyl(void) {
75 space_left
= sizeof(target
);
76 space_left
= strscpyl(target
, space_left
, "12345", "waldo", "waldo", NULL
);
78 assert_se(streq(target
, "12345waldowaldo"));
79 assert_se(space_left
== 10);
82 static void test_sd_event_code_migration(void) {
83 char b
[100 * DECIMAL_STR_MAX(unsigned) + 1];
84 char c
[100 * DECIMAL_STR_MAX(unsigned) + 1], *p
;
89 for (i
= o
= 0; i
< 100; i
++)
90 o
+= snprintf(&b
[o
], sizeof(b
) - o
, "%u ", i
);
94 for (i
= 0; i
< 100; i
++)
95 l
= strpcpyf(&p
, l
, "%u ", i
);
97 assert_se(streq(b
, c
));
100 int main(int argc
, char *argv
[]) {
107 test_sd_event_code_migration();