]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-strxcpyx.c
util: split out escaping code into escape.[ch]
[thirdparty/systemd.git] / src / test / test-strxcpyx.c
CommitLineData
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
24#include "util.h"
c62c294f
TA
25#include "strxcpyx.h"
26
27static void test_strpcpy(void) {
28 char target[25];
29 char *s = target;
30 size_t space_left;
31
32 space_left = sizeof(target);
33 space_left = strpcpy(&s, space_left, "12345");
34 space_left = strpcpy(&s, space_left, "hey hey hey");
35 space_left = strpcpy(&s, space_left, "waldo");
36 space_left = strpcpy(&s, space_left, "ba");
37 space_left = strpcpy(&s, space_left, "r");
38 space_left = strpcpy(&s, space_left, "foo");
39
bdf7026e
TA
40 assert_se(streq(target, "12345hey hey heywaldobar"));
41 assert_se(space_left == 0);
c62c294f
TA
42}
43
44static void test_strpcpyf(void) {
45 char target[25];
46 char *s = target;
47 size_t space_left;
48
49 space_left = sizeof(target);
1fa2f38f 50 space_left = strpcpyf(&s, space_left, "space left: %zu. ", space_left);
c62c294f
TA
51 space_left = strpcpyf(&s, space_left, "foo%s", "bar");
52
bdf7026e
TA
53 assert_se(streq(target, "space left: 25. foobar"));
54 assert_se(space_left == 3);
c62c294f
TA
55}
56
57static void test_strpcpyl(void) {
58 char target[25];
59 char *s = target;
60 size_t space_left;
61
62 space_left = sizeof(target);
63 space_left = strpcpyl(&s, space_left, "waldo", " test", " waldo. ", NULL);
64 space_left = strpcpyl(&s, space_left, "Banana", NULL);
65
bdf7026e
TA
66 assert_se(streq(target, "waldo test waldo. Banana"));
67 assert_se(space_left == 1);
c62c294f
TA
68}
69
70static void test_strscpy(void) {
71 char target[25];
72 size_t space_left;
73
74 space_left = sizeof(target);
75 space_left = strscpy(target, space_left, "12345");
76
bdf7026e
TA
77 assert_se(streq(target, "12345"));
78 assert_se(space_left == 20);
c62c294f
TA
79}
80
81static void test_strscpyl(void) {
82 char target[25];
83 size_t space_left;
84
85 space_left = sizeof(target);
86 space_left = strscpyl(target, space_left, "12345", "waldo", "waldo", NULL);
87
bdf7026e
TA
88 assert_se(streq(target, "12345waldowaldo"));
89 assert_se(space_left == 10);
c62c294f
TA
90}
91
92int main(int argc, char *argv[]) {
93 test_strpcpy();
94 test_strpcpyf();
95 test_strpcpyl();
96 test_strscpy();
97 test_strscpyl();
98
99 return 0;
100}