summaryrefslogtreecommitdiff
path: root/external/bsd/ntp/dist/tests/libntp/sbprintf.c
blob: 0c96727c20667ecd8797a71d8ef758bcecd37ec6 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*	$NetBSD: sbprintf.c,v 1.2 2020/05/25 20:47:36 christos Exp $	*/

#include "config.h"
#include "ntp_stdlib.h"
#include "unity.h"

#include <errno.h>

extern void test_NullBuf1(void);
void test_NullBuf1(void)
{
	int rc = xsbprintf(NULL, NULL, "blah");
        TEST_ASSERT(rc == -1 && errno == EINVAL);
}

extern void test_NullBuf2(void);
void test_NullBuf2(void)
{
	char *bp = NULL;
	int   rc = xsbprintf(&bp, NULL, "blah");
        TEST_ASSERT(rc == -1 && errno == EINVAL);
	TEST_ASSERT_EQUAL_PTR(bp, NULL);
}

extern void test_EndBeyond(void);
void test_EndBeyond(void)
{
	char ba[2];
	char *bp = ba + 1;
	char *ep = ba;
	int rc = xsbprintf(&bp, ep, "blah");
        TEST_ASSERT(rc == -1 && errno == EINVAL);
}

extern void test_SmallBuf(void);
void test_SmallBuf(void)
{
	char  ba[4];
	char *bp = ba;
	char *ep = ba + sizeof(ba);
	int   rc = xsbprintf(&bp, ep, "1234");
        TEST_ASSERT(rc == 0 && strlen(ba) == 0);
	TEST_ASSERT_EQUAL_PTR(bp, ba);
}

extern void test_MatchBuf(void);
void test_MatchBuf(void)
{
	char  ba[5];
	char *bp = ba;
	char *ep = ba + sizeof(ba);
	int   rc = xsbprintf(&bp, ep, "1234");
        TEST_ASSERT(rc == 4 && strlen(ba) == 4);
	TEST_ASSERT_EQUAL_PTR(bp, ba + 4);
}

extern void test_BigBuf(void);
void test_BigBuf(void)
{
	char  ba[10];
	char *bp = ba;
	char *ep = ba + sizeof(ba);
	int   rc = xsbprintf(&bp, ep, "1234");
        TEST_ASSERT(rc == 4 && strlen(ba) == 4);
	TEST_ASSERT_EQUAL_PTR(bp, ba + 4);
}

extern void test_SimpleArgs(void);
void test_SimpleArgs(void)
{
	char  ba[10];
	char *bp = ba;
	char *ep = ba + sizeof(ba);
	int   rc = xsbprintf(&bp, ep, "%d%d%d%d", 1, 2, 3, 4);
        TEST_ASSERT(rc == 4 && strlen(ba) == 4);
	TEST_ASSERT_EQUAL_PTR(bp, ba + 4);
	TEST_ASSERT_FALSE(strcmp(ba, "1234"));
}

extern void test_Increment1(void);
void test_Increment1(void)
{
	char  ba[10];
	char *bp = ba;
	char *ep = ba + sizeof(ba);
	int   rc;

	rc = xsbprintf(&bp, ep, "%d%d%d%d", 1, 2, 3, 4);
        TEST_ASSERT(rc == 4 && strlen(ba) == 4);
	TEST_ASSERT_EQUAL_PTR(bp, ba + 4);

	rc = xsbprintf(&bp, ep, "%s", "frob");
        TEST_ASSERT(rc == 4 && strlen(ba) == 8);
	TEST_ASSERT_EQUAL_PTR(bp, ba + 8);
	TEST_ASSERT_FALSE(strcmp(ba, "1234frob"));
}