summaryrefslogtreecommitdiffstats
path: root/tests/tests/unit/test_strtcpy.c
blob: 12351a5372f99dbc3d0853103c510f98e305fcb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
 * SPDX-License-Identifier: BSD-3-Clause
 */


#include <config.h>

#include <string.h>

#include <stdarg.h>  // Required by <cmocka.h>
#include <stddef.h>  // Required by <cmocka.h>
#include <setjmp.h>  // Required by <cmocka.h>
#include <stdint.h>  // Required by <cmocka.h>
#include <cmocka.h>

#include "sizeof.h"
#include "string/strtcpy.h"


static void test_STRTCPY_trunc(void **state);
static void test_STRTCPY_ok(void **state);


int
main(void)
{
    const struct CMUnitTest  tests[] = {
        cmocka_unit_test(test_STRTCPY_trunc),
        cmocka_unit_test(test_STRTCPY_ok),
    };

    return cmocka_run_group_tests(tests, NULL, NULL);
}


static void
test_STRTCPY_trunc(void **state)
{
	char  buf[NITEMS("foo")];

	// Test that we're not returning SIZE_MAX
	assert_true(STRTCPY(buf, "fooo") < 0);
	assert_string_equal(buf, "foo");

	assert_int_equal(STRTCPY(buf, "barbaz"), -1);
	assert_string_equal(buf, "bar");
}


static void
test_STRTCPY_ok(void **state)
{
	char  buf[NITEMS("foo")];

	assert_int_equal(STRTCPY(buf, "foo"), strlen("foo"));
	assert_string_equal(buf, "foo");

	assert_int_equal(STRTCPY(buf, "fo"), strlen("fo"));
	assert_string_equal(buf, "fo");

	assert_int_equal(STRTCPY(buf, "f"), strlen("f"));
	assert_string_equal(buf, "f");

	assert_int_equal(STRTCPY(buf, ""), strlen(""));
	assert_string_equal(buf, "");
}