2 * Copyright (C) 2016 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/>.
22 #include <shared/scratchbuf.h>
24 #include "testsuite.h"
26 static int test_scratchbuf_onlystack(const struct test
*t
)
28 struct scratchbuf sbuf
;
29 const char *smallstr
= "xyz";
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
);
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
);
43 memcpy(scratchbuf_str(&sbuf
), smallstr
, strlen(smallstr
) + 1);
44 assert_return(strcmp(scratchbuf_str(&sbuf
), smallstr
) == 0, EXIT_FAILURE
);
48 DEFINE_TEST(test_scratchbuf_onlystack
,
49 .description
= "test scratchbuf for buffer on stack only");
52 static int test_scratchbuf_heap(const struct test
*t
)
54 struct scratchbuf sbuf
;
55 const char *smallstr
= "xyz";
56 const char *largestr
= "xyzxyzxyz";
57 const char *largestr2
= "xyzxyzxyzxyzxyz";
60 scratchbuf_init(&sbuf
, buf
, sizeof(buf
));
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);
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
);
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
);
82 scratchbuf_release(&sbuf
);
86 DEFINE_TEST(test_scratchbuf_heap
,
87 .description
= "test scratchbuf for buffer on that grows to heap");