]>
git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/test-strbuf.c
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>
26 #include <shared/util.h>
28 #include "testsuite.h"
30 static const char *TEXT
=
31 "this is a very long test that is longer than the size we initially se in the strbuf";
33 static int test_strbuf_pushchar(const struct test
*t
)
36 char *result1
, *result2
;
41 for (c
= TEXT
; *c
!= '\0'; c
++)
42 strbuf_pushchar(&buf
, *c
);
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
);
49 result2
= strbuf_steal(&buf
);
50 assert_return(streq(result1
, result2
), EXIT_FAILURE
);
57 DEFINE_TEST(test_strbuf_pushchar
,
58 .description
= "test strbuf_{pushchar, str, steal}");
60 static int test_strbuf_pushchars(const struct test
*t
)
63 char *result1
, *saveptr
= NULL
, *str
, *result2
;
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
);
77 result1
= (char *) strbuf_str(&buf
);
78 assert_return(result1
== buf
.bytes
, EXIT_FAILURE
);
79 assert_return(streq(result1
, TEXT
), EXIT_FAILURE
);
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,
86 assert_return(result2
[strlen(TEXT
) - lastwordlen
] == '\0',
94 DEFINE_TEST(test_strbuf_pushchars
,
95 .description
= "test strbuf_{pushchars, popchar, popchars}");