]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-strxcpyx.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / test / test-strxcpyx.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2013 Thomas H.P. Andersen
4 ***/
5
6 #include <string.h>
7
8 #include "string-util.h"
9 #include "strxcpyx.h"
10 #include "util.h"
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
25 assert_se(streq(target, "12345hey hey heywaldobar"));
26 assert_se(space_left == 0);
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);
35 space_left = strpcpyf(&s, space_left, "space left: %zu. ", space_left);
36 space_left = strpcpyf(&s, space_left, "foo%s", "bar");
37
38 assert_se(streq(target, "space left: 25. foobar"));
39 assert_se(space_left == 3);
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');
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
58 assert_se(streq(target, "waldo test waldo. Banana"));
59 assert_se(space_left == 1);
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
69 assert_se(streq(target, "12345"));
70 assert_se(space_left == 20);
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
80 assert_se(streq(target, "12345waldowaldo"));
81 assert_se(space_left == 10);
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 }