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/>.
7 * Written by Russ Allbery <eagle@eyrie.org>
8 * Copyright 2018 Russ Allbery <eagle@eyrie.org>
10 * The Board of Trustees of the Leland Stanford Junior University
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
17 * SPDX-License-Identifier: FSFAP
21 #include <portable/system.h>
25 #include <tests/tap/basic.h>
27 char *test_strndup(const char *, size_t);
38 result = test_strndup("foo", 8);
39 is_string("foo", result, "strndup longer than string");
41 result = test_strndup("foo", 2);
42 is_string("fo", result, "strndup shorter than string");
44 result = test_strndup("foo", 3);
45 is_string("foo", result, "strndup same size as string");
47 result = test_strndup("foo", 0);
48 is_string("", result, "strndup of size 0");
50 memcpy(buffer, "foo", 3);
51 result = test_strndup(buffer, 3);
52 is_string("foo", result, "strndup of non-nul-terminated string");
55 result = test_strndup(NULL, 0);
56 is_string(NULL, result, "strndup of NULL");
57 is_int(errno, EINVAL, "...and returns EINVAL");