]>
git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/test-strbuf.c
1 // SPDX-License-Identifier: LGPL-2.1-or-later
3 * Copyright (C) 2014 Intel Corporation. All rights reserved.
13 #include <shared/strbuf.h>
14 #include <shared/util.h>
16 #include "testsuite.h"
18 static const char *TEXT
=
19 "this is a very long test that is longer than the size we initially se in the strbuf";
21 static int test_strbuf_pushchar(const struct test
*t
)
24 char *result1
, *result2
;
29 for (c
= TEXT
; *c
!= '\0'; c
++)
30 strbuf_pushchar(&buf
, *c
);
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
);
37 result2
= strbuf_steal(&buf
);
38 assert_return(streq(result1
, result2
), EXIT_FAILURE
);
45 DEFINE_TEST(test_strbuf_pushchar
,
46 .description
= "test strbuf_{pushchar, str, steal}");
48 static int test_strbuf_pushchars(const struct test
*t
)
51 char *result1
, *saveptr
= NULL
, *str
, *result2
;
53 size_t lastwordlen
= 0;
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
);
65 result1
= (char *) strbuf_str(&buf
);
66 assert_return(result1
== buf
.bytes
, EXIT_FAILURE
);
67 assert_return(streq(result1
, TEXT
), EXIT_FAILURE
);
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,
74 assert_return(result2
[strlen(TEXT
) - lastwordlen
] == '\0',
82 DEFINE_TEST(test_strbuf_pushchars
,
83 .description
= "test strbuf_{pushchars, popchar, popchars}");