]>
Commit | Line | Data |
---|---|---|
780a4e97 LDM |
1 | /* |
2 | * Copyright (C) 2016 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 | |
15 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
18 | #include <errno.h> | |
19 | #include <string.h> | |
20 | #include <unistd.h> | |
21 | ||
22 | #include <shared/scratchbuf.h> | |
23 | ||
24 | #include "testsuite.h" | |
25 | ||
26 | static int test_scratchbuf_onlystack(const struct test *t) | |
27 | { | |
28 | struct scratchbuf sbuf; | |
29 | const char *smallstr = "xyz"; | |
30 | char buf[3 + 2]; | |
31 | char buf2[3 + 1]; | |
32 | ||
33 | scratchbuf_init(&sbuf, buf, sizeof(buf)); | |
34 | assert_return(scratchbuf_alloc(&sbuf, strlen(smallstr) + 1) == 0, EXIT_FAILURE); | |
35 | assert_return(sbuf.need_free == false, EXIT_FAILURE); | |
36 | scratchbuf_release(&sbuf); | |
37 | ||
38 | scratchbuf_init(&sbuf, buf2, sizeof(buf2)); | |
39 | assert_return(scratchbuf_alloc(&sbuf, strlen(smallstr) + 1) == 0, EXIT_FAILURE); | |
40 | assert_return(sbuf.need_free == false, EXIT_FAILURE); | |
41 | scratchbuf_release(&sbuf); | |
42 | ||
43 | memcpy(scratchbuf_str(&sbuf), smallstr, strlen(smallstr) + 1); | |
44 | assert_return(strcmp(scratchbuf_str(&sbuf), smallstr) == 0, EXIT_FAILURE); | |
45 | ||
46 | return 0; | |
47 | } | |
48 | DEFINE_TEST(test_scratchbuf_onlystack, | |
49 | .description = "test scratchbuf for buffer on stack only"); | |
50 | ||
51 | ||
52 | static int test_scratchbuf_heap(const struct test *t) | |
53 | { | |
54 | struct scratchbuf sbuf; | |
55 | const char *smallstr = "xyz"; | |
56 | const char *largestr = "xyzxyzxyz"; | |
57 | const char *largestr2 = "xyzxyzxyzxyzxyz"; | |
58 | char buf[3 + 1]; | |
59 | ||
60 | scratchbuf_init(&sbuf, buf, sizeof(buf)); | |
61 | ||
62 | /* Initially only on stack */ | |
63 | assert_return(scratchbuf_alloc(&sbuf, strlen(smallstr) + 1) == 0, EXIT_FAILURE); | |
64 | assert_return(sbuf.need_free == false, EXIT_FAILURE); | |
65 | memcpy(scratchbuf_str(&sbuf), smallstr, strlen(smallstr) + 1); | |
66 | ||
67 | /* Grow once to heap */ | |
68 | assert_return(scratchbuf_alloc(&sbuf, strlen(largestr) + 1) == 0, EXIT_FAILURE); | |
69 | assert_return(sbuf.need_free == true, EXIT_FAILURE); | |
70 | assert_return(sbuf.size == strlen(largestr) + 1, EXIT_FAILURE); | |
71 | assert_return(strcmp(scratchbuf_str(&sbuf), smallstr) == 0, EXIT_FAILURE); | |
72 | memcpy(scratchbuf_str(&sbuf), largestr, strlen(largestr) + 1); | |
73 | assert_return(strcmp(scratchbuf_str(&sbuf), largestr) == 0, EXIT_FAILURE); | |
74 | ||
75 | /* Grow again - realloc should take place */ | |
76 | assert_return(scratchbuf_alloc(&sbuf, strlen(largestr2) + 1) == 0, EXIT_FAILURE); | |
77 | assert_return(sbuf.need_free == true, EXIT_FAILURE); | |
78 | assert_return(sbuf.size == strlen(largestr2) + 1, EXIT_FAILURE); | |
79 | memcpy(scratchbuf_str(&sbuf), largestr2, strlen(largestr2) + 1); | |
80 | assert_return(strcmp(scratchbuf_str(&sbuf), largestr2) == 0, EXIT_FAILURE); | |
81 | ||
82 | scratchbuf_release(&sbuf); | |
83 | ||
84 | return 0; | |
85 | } | |
86 | DEFINE_TEST(test_scratchbuf_heap, | |
87 | .description = "test scratchbuf for buffer on that grows to heap"); | |
88 | ||
89 | TESTSUITE_MAIN(); |