File: test_snprintf.c

package info (click to toggle)
shadow 1%3A4.19.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 66,808 kB
  • sloc: sh: 44,185; ansic: 34,184; xml: 12,350; exp: 3,691; makefile: 1,655; python: 1,409; perl: 120; sed: 16
file content (66 lines) | stat: -rw-r--r-- 1,486 bytes parent folder | download
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
// SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#include "config.h"

#include <stdio.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/sprintf/snprintf.h"


static void test_stprintf_a_trunc(void **);
static void test_stprintf_a_ok(void **);


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

    return cmocka_run_group_tests(tests, NULL, NULL);
}


static void
test_stprintf_a_trunc(void **)
{
	char  buf[countof("foo")];

	// Test that we're not returning SIZE_MAX
	assert_true(stprintf_a(buf, "f%su", "oo") < 0);
	assert_true(strcmp(buf, "foo") == 0);

	assert_true(stprintf_a(buf, "barbaz") == -1);
	assert_true(strcmp(buf, "bar") == 0);
}


static void
test_stprintf_a_ok(void **)
{
	char  buf[countof("foo")];

	assert_true(stprintf_a(buf, "%s", "foo") == strlen("foo"));
	assert_true(strcmp(buf, "foo") == 0);

	assert_true(stprintf_a(buf, "%do", 1) == strlen("1o"));
	assert_true(strcmp(buf, "1o") == 0);

	assert_true(stprintf_a(buf, "f") == strlen("f"));
	assert_true(strcmp(buf, "f") == 0);

	assert_true(stprintf_a(buf, "") == strlen(""));
	assert_true(strcmp(buf, "") == 0);
}