]>
Commit | Line | Data |
---|---|---|
c62c294f TA |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2013 Thomas H.P. Andersen | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <string.h> | |
23 | ||
07630cea | 24 | #include "string-util.h" |
c62c294f | 25 | #include "strxcpyx.h" |
07630cea | 26 | #include "util.h" |
c62c294f TA |
27 | |
28 | static void test_strpcpy(void) { | |
29 | char target[25]; | |
30 | char *s = target; | |
31 | size_t space_left; | |
32 | ||
33 | space_left = sizeof(target); | |
34 | space_left = strpcpy(&s, space_left, "12345"); | |
35 | space_left = strpcpy(&s, space_left, "hey hey hey"); | |
36 | space_left = strpcpy(&s, space_left, "waldo"); | |
37 | space_left = strpcpy(&s, space_left, "ba"); | |
38 | space_left = strpcpy(&s, space_left, "r"); | |
39 | space_left = strpcpy(&s, space_left, "foo"); | |
40 | ||
bdf7026e TA |
41 | assert_se(streq(target, "12345hey hey heywaldobar")); |
42 | assert_se(space_left == 0); | |
c62c294f TA |
43 | } |
44 | ||
45 | static void test_strpcpyf(void) { | |
46 | char target[25]; | |
47 | char *s = target; | |
48 | size_t space_left; | |
49 | ||
50 | space_left = sizeof(target); | |
1fa2f38f | 51 | space_left = strpcpyf(&s, space_left, "space left: %zu. ", space_left); |
c62c294f TA |
52 | space_left = strpcpyf(&s, space_left, "foo%s", "bar"); |
53 | ||
bdf7026e TA |
54 | assert_se(streq(target, "space left: 25. foobar")); |
55 | assert_se(space_left == 3); | |
c62c294f TA |
56 | } |
57 | ||
58 | static void test_strpcpyl(void) { | |
59 | char target[25]; | |
60 | char *s = target; | |
61 | size_t space_left; | |
62 | ||
63 | space_left = sizeof(target); | |
64 | space_left = strpcpyl(&s, space_left, "waldo", " test", " waldo. ", NULL); | |
65 | space_left = strpcpyl(&s, space_left, "Banana", NULL); | |
66 | ||
bdf7026e TA |
67 | assert_se(streq(target, "waldo test waldo. Banana")); |
68 | assert_se(space_left == 1); | |
c62c294f TA |
69 | } |
70 | ||
71 | static void test_strscpy(void) { | |
72 | char target[25]; | |
73 | size_t space_left; | |
74 | ||
75 | space_left = sizeof(target); | |
76 | space_left = strscpy(target, space_left, "12345"); | |
77 | ||
bdf7026e TA |
78 | assert_se(streq(target, "12345")); |
79 | assert_se(space_left == 20); | |
c62c294f TA |
80 | } |
81 | ||
82 | static void test_strscpyl(void) { | |
83 | char target[25]; | |
84 | size_t space_left; | |
85 | ||
86 | space_left = sizeof(target); | |
87 | space_left = strscpyl(target, space_left, "12345", "waldo", "waldo", NULL); | |
88 | ||
bdf7026e TA |
89 | assert_se(streq(target, "12345waldowaldo")); |
90 | assert_se(space_left == 10); | |
c62c294f TA |
91 | } |
92 | ||
93 | int main(int argc, char *argv[]) { | |
94 | test_strpcpy(); | |
95 | test_strpcpyf(); | |
96 | test_strpcpyl(); | |
97 | test_strscpy(); | |
98 | test_strscpyl(); | |
99 | ||
100 | return 0; | |
101 | } |