0
|
1 /* |
|
2 * test-shrink.c -- test buf_shrink |
|
3 * |
|
4 * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr> |
|
5 * |
|
6 * Permission to use, copy, modify, and/or distribute this software for any |
|
7 * purpose with or without fee is hereby granted, provided that the above |
|
8 * copyright notice and this permission notice appear in all copies. |
|
9 * |
|
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
|
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
|
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
17 */ |
|
18 |
|
19 #define GREATEST_USE_ABBREVS 0 |
|
20 #include <greatest.h> |
|
21 |
|
22 #include "buf.h" |
|
23 |
|
24 GREATEST_TEST |
|
25 shrink_set(void) |
|
26 { |
|
27 struct buf b = {0}; |
|
28 |
|
29 /* Reserve 10 but write 5 character, shrink should set a capacity of 5. */ |
|
30 buf_reserve(&b, 10U); |
|
31 buf_puts(&b, "abcde"); |
|
32 buf_shrink(&b); |
|
33 GREATEST_ASSERT_STR_EQ(b.data, "abcde"); |
|
34 GREATEST_ASSERT_EQ(b.length, 5U); |
|
35 GREATEST_ASSERT_EQ(b.capacity, 5U); |
|
36 buf_finish(&b); |
|
37 |
|
38 GREATEST_PASS(); |
|
39 } |
|
40 |
|
41 GREATEST_TEST |
|
42 shrink_empty(void) |
|
43 { |
|
44 struct buf b = {0}; |
|
45 |
|
46 /* |
|
47 * Reserve 10, the data should be allocated but since we have a null |
|
48 * length it should be completely free'd. |
|
49 */ |
|
50 buf_reserve(&b, 10U); |
|
51 buf_shrink(&b); |
|
52 GREATEST_ASSERT(!b.data); |
|
53 GREATEST_ASSERT_EQ(b.length, 0U); |
|
54 GREATEST_ASSERT_EQ(b.capacity, 0U); |
|
55 buf_finish(&b); |
|
56 |
|
57 GREATEST_PASS(); |
|
58 } |
|
59 |
|
60 GREATEST_TEST |
|
61 shrink_null(void) |
|
62 { |
|
63 struct buf b = {0}; |
|
64 |
|
65 buf_shrink(&b); |
|
66 GREATEST_ASSERT(!b.data); |
|
67 GREATEST_ASSERT_EQ(b.length, 0U); |
|
68 GREATEST_ASSERT_EQ(b.capacity, 0U); |
|
69 buf_finish(&b); |
|
70 |
|
71 GREATEST_PASS(); |
|
72 } |
|
73 |
|
74 GREATEST_MAIN_DEFS(); |
|
75 |
|
76 int |
|
77 main(int argc, char **argv) |
|
78 { |
|
79 GREATEST_MAIN_BEGIN(); |
|
80 GREATEST_RUN_TEST(shrink_set); |
|
81 GREATEST_RUN_TEST(shrink_empty); |
|
82 GREATEST_RUN_TEST(shrink_null); |
|
83 GREATEST_MAIN_END(); |
|
84 } |