]>
git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/test-strbuf.c
972e6a4c4a375545d25739f6a51adc8d45dc6bf3
2 * Copyright (C) 2014 Intel Corporation. All rights reserved.
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.
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.
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/>.
25 #include <shared/strbuf.h>
27 #include "testsuite.h"
29 static const char *TEXT
=
30 "this is a very long test that is longer than the size we initially se in the strbuf";
32 static int test_strbuf_pushchar(const struct test
*t
)
35 char *result1
, *result2
;
40 for (c
= TEXT
; *c
!= '\0'; c
++)
41 strbuf_pushchar(&buf
, *c
);
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
);
48 result2
= strbuf_steal(&buf
);
49 assert_return(strcmp(result1
, result2
) == 0, EXIT_FAILURE
);
56 DEFINE_TEST(test_strbuf_pushchar
,
57 .description
= "test strbuf_{pushchar, str, steal}");
59 static int test_strbuf_pushchars(const struct test
*t
)
62 char *result1
, *saveptr
= NULL
, *str
, *result2
;
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
);
76 result1
= (char *) strbuf_str(&buf
);
77 assert_return(result1
== buf
.bytes
, EXIT_FAILURE
);
78 assert_return(strcmp(result1
, TEXT
) == 0, EXIT_FAILURE
);
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,
85 assert_return(result2
[strlen(TEXT
) - lastwordlen
] == '\0',
93 DEFINE_TEST(test_strbuf_pushchars
,
94 .description
= "test strbuf_{pushchars, popchar, popchars}");