1 /*
2 * strndup test suite.
3 *
4 * The canonical version of this file is maintained in the rra-c-util package,
5 * which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
6 *
7 * Written by Russ Allbery <eagle@eyrie.org>
8 * Copyright 2018 Russ Allbery <eagle@eyrie.org>
9 * Copyright 2011-2012
10 * The Board of Trustees of the Leland Stanford Junior University
11 *
12 * Copying and distribution of this file, with or without modification, are
13 * permitted in any medium without royalty provided the copyright notice and
14 * this notice are preserved. This file is offered as-is, without any
15 * warranty.
16 *
17 * SPDX-License-Identifier: FSFAP
18 */
19
20 #include <config.h>
21 #include <portable/system.h>
22
23 #include <errno.h>
24
25 #include <tests/tap/basic.h>
26
27 char *test_strndup(const char *, size_t);
28
29
30 int
main(void)31 main(void)
32 {
33 char buffer[3];
34 char *result;
35
36 plan(7);
37
38 result = test_strndup("foo", 8);
39 is_string("foo", result, "strndup longer than string");
40 free(result);
41 result = test_strndup("foo", 2);
42 is_string("fo", result, "strndup shorter than string");
43 free(result);
44 result = test_strndup("foo", 3);
45 is_string("foo", result, "strndup same size as string");
46 free(result);
47 result = test_strndup("foo", 0);
48 is_string("", result, "strndup of size 0");
49 free(result);
50 memcpy(buffer, "foo", 3);
51 result = test_strndup(buffer, 3);
52 is_string("foo", result, "strndup of non-nul-terminated string");
53 free(result);
54 errno = 0;
55 result = test_strndup(NULL, 0);
56 is_string(NULL, result, "strndup of NULL");
57 is_int(errno, EINVAL, "...and returns EINVAL");
58
59 return 0;
60 }
61