]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-strxcpyx.c
tests: add tests of strxcpyx
[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"
25#include "strv.h"
26#include "strxcpyx.h"
27
28static 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
41 assert(streq(target, "12345hey hey heywaldobar"));
42 assert(space_left == 0);
43}
44
45static void test_strpcpyf(void) {
46 char target[25];
47 char *s = target;
48 size_t space_left;
49
50 space_left = sizeof(target);
51 space_left = strpcpyf(&s, space_left, "space left: %ld. ", space_left);
52 space_left = strpcpyf(&s, space_left, "foo%s", "bar");
53
54 assert(streq(target, "space left: 25. foobar"));
55 assert(space_left == 3);
56}
57
58static 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
67 assert(streq(target, "waldo test waldo. Banana"));
68 assert(space_left == 1);
69}
70
71static 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
78 assert(streq(target, "12345"));
79 assert(space_left == 20);
80}
81
82static 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
89 assert(streq(target, "12345waldowaldo"));
90 assert(space_left == 10);
91}
92
93int 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}