]> git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/test-strbuf.c
testsuite: depmod: check netsted loops reporting
[thirdparty/kmod.git] / testsuite / test-strbuf.c
1 /*
2 * Copyright (C) 2014 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <errno.h>
19 #include <stddef.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include <shared/strbuf.h>
26 #include <shared/util.h>
27
28 #include "testsuite.h"
29
30 static const char *TEXT =
31 "this is a very long test that is longer than the size we initially se in the strbuf";
32
33 static int test_strbuf_pushchar(const struct test *t)
34 {
35 struct strbuf buf;
36 char *result1, *result2;
37 const char *c;
38
39 strbuf_init(&buf);
40
41 for (c = TEXT; *c != '\0'; c++)
42 strbuf_pushchar(&buf, *c);
43
44 result1 = (char *) strbuf_str(&buf);
45 assert_return(result1 == buf.bytes, EXIT_FAILURE);
46 assert_return(streq(result1, TEXT), EXIT_FAILURE);
47 result1 = strdup(result1);
48
49 result2 = strbuf_steal(&buf);
50 assert_return(streq(result1, result2), EXIT_FAILURE);
51
52 free(result1);
53 free(result2);
54
55 return 0;
56 }
57 DEFINE_TEST(test_strbuf_pushchar,
58 .description = "test strbuf_{pushchar, str, steal}");
59
60 static int test_strbuf_pushchars(const struct test *t)
61 {
62 struct strbuf buf;
63 char *result1, *saveptr = NULL, *str, *result2;
64 const char *c;
65 int lastwordlen = 0;
66
67 strbuf_init(&buf);
68 str = strdup(TEXT);
69 for (c = strtok_r(str, " ", &saveptr); c != NULL;
70 c = strtok_r(NULL, " ", &saveptr)) {
71 strbuf_pushchars(&buf, c);
72 strbuf_pushchar(&buf, ' ');
73 lastwordlen = strlen(c);
74 }
75
76 strbuf_popchar(&buf);
77 result1 = (char *) strbuf_str(&buf);
78 assert_return(result1 == buf.bytes, EXIT_FAILURE);
79 assert_return(streq(result1, TEXT), EXIT_FAILURE);
80
81 strbuf_popchars(&buf, lastwordlen);
82 result2 = strbuf_steal(&buf);
83 assert_return(!streq(TEXT, result2), EXIT_FAILURE);
84 assert_return(strncmp(TEXT, result2, strlen(TEXT) - lastwordlen) == 0,
85 EXIT_FAILURE);
86 assert_return(result2[strlen(TEXT) - lastwordlen] == '\0',
87 EXIT_FAILURE);
88
89 free(str);
90 free(result2);
91
92 return 0;
93 }
94 DEFINE_TEST(test_strbuf_pushchars,
95 .description = "test strbuf_{pushchars, popchar, popchars}");
96
97
98 TESTSUITE_MAIN();