]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-strxcpyx.c
test-time-util: Properly restore TZ variable
[thirdparty/systemd.git] / src / test / test-strxcpyx.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
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
9static 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
26static 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
46static 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
59static 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
70static 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
81static 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;
b798490f 86 int o, r;
442ac269 87
b798490f
LB
88 for (i = o = 0; i < 100; i++) {
89 r = snprintf(&b[o], sizeof(b) - o, "%u ", i);
90 assert_se(r >= 0 && r < (int) sizeof(b) - o);
91 o += r;
92 }
442ac269
YW
93
94 p = c;
95 l = sizeof(c);
96 for (i = 0; i < 100; i++)
97 l = strpcpyf(&p, l, "%u ", i);
98
99 assert_se(streq(b, c));
100}
101
c62c294f
TA
102int main(int argc, char *argv[]) {
103 test_strpcpy();
104 test_strpcpyf();
105 test_strpcpyl();
106 test_strscpy();
107 test_strscpyl();
108
442ac269
YW
109 test_sd_event_code_migration();
110
c62c294f
TA
111 return 0;
112}