]> git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/test-strbuf.c
libkmod: remove __secure_getenv handling
[thirdparty/kmod.git] / testsuite / test-strbuf.c
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3 * Copyright (C) 2014 Intel Corporation. All rights reserved.
4 */
5
6 #include <errno.h>
7 #include <stddef.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12
13 #include <shared/strbuf.h>
14 #include <shared/util.h>
15
16 #include "testsuite.h"
17
18 static const char *TEXT =
19 "this is a very long test that is longer than the size we initially se in the strbuf";
20
21 static int test_strbuf_pushchar(const struct test *t)
22 {
23 struct strbuf buf;
24 char *result1, *result2;
25 const char *c;
26
27 strbuf_init(&buf);
28
29 for (c = TEXT; *c != '\0'; c++)
30 strbuf_pushchar(&buf, *c);
31
32 result1 = (char *) strbuf_str(&buf);
33 assert_return(result1 == buf.bytes, EXIT_FAILURE);
34 assert_return(streq(result1, TEXT), EXIT_FAILURE);
35 result1 = strdup(result1);
36
37 result2 = strbuf_steal(&buf);
38 assert_return(streq(result1, result2), EXIT_FAILURE);
39
40 free(result1);
41 free(result2);
42
43 return 0;
44 }
45 DEFINE_TEST(test_strbuf_pushchar,
46 .description = "test strbuf_{pushchar, str, steal}");
47
48 static int test_strbuf_pushchars(const struct test *t)
49 {
50 struct strbuf buf;
51 char *result1, *saveptr = NULL, *str, *result2;
52 const char *c;
53 size_t lastwordlen = 0;
54
55 strbuf_init(&buf);
56 str = strdup(TEXT);
57 for (c = strtok_r(str, " ", &saveptr); c != NULL;
58 c = strtok_r(NULL, " ", &saveptr)) {
59 strbuf_pushchars(&buf, c);
60 strbuf_pushchar(&buf, ' ');
61 lastwordlen = strlen(c);
62 }
63
64 strbuf_popchar(&buf);
65 result1 = (char *) strbuf_str(&buf);
66 assert_return(result1 == buf.bytes, EXIT_FAILURE);
67 assert_return(streq(result1, TEXT), EXIT_FAILURE);
68
69 strbuf_popchars(&buf, lastwordlen);
70 result2 = strbuf_steal(&buf);
71 assert_return(!streq(TEXT, result2), EXIT_FAILURE);
72 assert_return(strncmp(TEXT, result2, strlen(TEXT) - lastwordlen) == 0,
73 EXIT_FAILURE);
74 assert_return(result2[strlen(TEXT) - lastwordlen] == '\0',
75 EXIT_FAILURE);
76
77 free(str);
78 free(result2);
79
80 return 0;
81 }
82 DEFINE_TEST(test_strbuf_pushchars,
83 .description = "test strbuf_{pushchars, popchar, popchars}");
84
85
86 TESTSUITE_MAIN();