]>
Commit | Line | Data |
---|---|---|
48e4d181 LDM |
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 | |
dea2dfee | 15 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
48e4d181 LDM |
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 | ||
27 | #include "testsuite.h" | |
28 | ||
29 | static const char *TEXT = | |
30 | "this is a very long test that is longer than the size we initially se in the strbuf"; | |
31 | ||
32 | static int test_strbuf_pushchar(const struct test *t) | |
33 | { | |
34 | struct strbuf buf; | |
35 | char *result1, *result2; | |
36 | const char *c; | |
37 | ||
38 | strbuf_init(&buf); | |
39 | ||
40 | for (c = TEXT; *c != '\0'; c++) | |
41 | strbuf_pushchar(&buf, *c); | |
42 | ||
43 | result1 = (char *) strbuf_str(&buf); | |
44 | assert_return(result1 == buf.bytes, EXIT_FAILURE); | |
45 | assert_return(strcmp(result1, TEXT) == 0, EXIT_FAILURE); | |
46 | result1 = strdup(result1); | |
47 | ||
48 | result2 = strbuf_steal(&buf); | |
49 | assert_return(strcmp(result1, result2) == 0, EXIT_FAILURE); | |
50 | ||
51 | free(result1); | |
52 | free(result2); | |
53 | ||
54 | return 0; | |
55 | } | |
56 | DEFINE_TEST(test_strbuf_pushchar, | |
57 | .description = "test strbuf_{pushchar, str, steal}"); | |
58 | ||
59 | static int test_strbuf_pushchars(const struct test *t) | |
60 | { | |
61 | struct strbuf buf; | |
62 | char *result1, *saveptr = NULL, *str, *result2; | |
63 | const char *c; | |
64 | int lastwordlen = 0; | |
65 | ||
66 | strbuf_init(&buf); | |
67 | str = strdup(TEXT); | |
68 | for (c = strtok_r(str, " ", &saveptr); c != NULL; | |
69 | c = strtok_r(NULL, " ", &saveptr)) { | |
70 | strbuf_pushchars(&buf, c); | |
71 | strbuf_pushchar(&buf, ' '); | |
72 | lastwordlen = strlen(c); | |
73 | } | |
74 | ||
75 | strbuf_popchar(&buf); | |
76 | result1 = (char *) strbuf_str(&buf); | |
77 | assert_return(result1 == buf.bytes, EXIT_FAILURE); | |
78 | assert_return(strcmp(result1, TEXT) == 0, EXIT_FAILURE); | |
79 | ||
80 | strbuf_popchars(&buf, lastwordlen); | |
81 | result2 = strbuf_steal(&buf); | |
82 | assert_return(strcmp(TEXT, result2) != 0, EXIT_FAILURE); | |
83 | assert_return(strncmp(TEXT, result2, strlen(TEXT) - lastwordlen) == 0, | |
84 | EXIT_FAILURE); | |
85 | assert_return(result2[strlen(TEXT) - lastwordlen] == '\0', | |
86 | EXIT_FAILURE); | |
87 | ||
88 | free(str); | |
89 | free(result2); | |
90 | ||
91 | return 0; | |
92 | } | |
93 | DEFINE_TEST(test_strbuf_pushchars, | |
94 | .description = "test strbuf_{pushchars, popchar, popchars}"); | |
95 | ||
96 | ||
97 | TESTSUITE_MAIN(); |