1 /*	$NetBSD: sbprintf.c,v 1.2 2020/05/25 20:47:36 christos Exp $	*/
2 
3 #include "config.h"
4 #include "ntp_stdlib.h"
5 #include "unity.h"
6 
7 #include <errno.h>
8 
9 extern void test_NullBuf1(void);
test_NullBuf1(void)10 void test_NullBuf1(void)
11 {
12 	int rc = xsbprintf(NULL, NULL, "blah");
13         TEST_ASSERT(rc == -1 && errno == EINVAL);
14 }
15 
16 extern void test_NullBuf2(void);
test_NullBuf2(void)17 void test_NullBuf2(void)
18 {
19 	char *bp = NULL;
20 	int   rc = xsbprintf(&bp, NULL, "blah");
21         TEST_ASSERT(rc == -1 && errno == EINVAL);
22 	TEST_ASSERT_EQUAL_PTR(bp, NULL);
23 }
24 
25 extern void test_EndBeyond(void);
test_EndBeyond(void)26 void test_EndBeyond(void)
27 {
28 	char ba[2];
29 	char *bp = ba + 1;
30 	char *ep = ba;
31 	int rc = xsbprintf(&bp, ep, "blah");
32         TEST_ASSERT(rc == -1 && errno == EINVAL);
33 }
34 
35 extern void test_SmallBuf(void);
test_SmallBuf(void)36 void test_SmallBuf(void)
37 {
38 	char  ba[4];
39 	char *bp = ba;
40 	char *ep = ba + sizeof(ba);
41 	int   rc = xsbprintf(&bp, ep, "1234");
42         TEST_ASSERT(rc == 0 && strlen(ba) == 0);
43 	TEST_ASSERT_EQUAL_PTR(bp, ba);
44 }
45 
46 extern void test_MatchBuf(void);
test_MatchBuf(void)47 void test_MatchBuf(void)
48 {
49 	char  ba[5];
50 	char *bp = ba;
51 	char *ep = ba + sizeof(ba);
52 	int   rc = xsbprintf(&bp, ep, "1234");
53         TEST_ASSERT(rc == 4 && strlen(ba) == 4);
54 	TEST_ASSERT_EQUAL_PTR(bp, ba + 4);
55 }
56 
57 extern void test_BigBuf(void);
test_BigBuf(void)58 void test_BigBuf(void)
59 {
60 	char  ba[10];
61 	char *bp = ba;
62 	char *ep = ba + sizeof(ba);
63 	int   rc = xsbprintf(&bp, ep, "1234");
64         TEST_ASSERT(rc == 4 && strlen(ba) == 4);
65 	TEST_ASSERT_EQUAL_PTR(bp, ba + 4);
66 }
67 
68 extern void test_SimpleArgs(void);
test_SimpleArgs(void)69 void test_SimpleArgs(void)
70 {
71 	char  ba[10];
72 	char *bp = ba;
73 	char *ep = ba + sizeof(ba);
74 	int   rc = xsbprintf(&bp, ep, "%d%d%d%d", 1, 2, 3, 4);
75         TEST_ASSERT(rc == 4 && strlen(ba) == 4);
76 	TEST_ASSERT_EQUAL_PTR(bp, ba + 4);
77 	TEST_ASSERT_FALSE(strcmp(ba, "1234"));
78 }
79 
80 extern void test_Increment1(void);
test_Increment1(void)81 void test_Increment1(void)
82 {
83 	char  ba[10];
84 	char *bp = ba;
85 	char *ep = ba + sizeof(ba);
86 	int   rc;
87 
88 	rc = xsbprintf(&bp, ep, "%d%d%d%d", 1, 2, 3, 4);
89         TEST_ASSERT(rc == 4 && strlen(ba) == 4);
90 	TEST_ASSERT_EQUAL_PTR(bp, ba + 4);
91 
92 	rc = xsbprintf(&bp, ep, "%s", "frob");
93         TEST_ASSERT(rc == 4 && strlen(ba) == 8);
94 	TEST_ASSERT_EQUAL_PTR(bp, ba + 8);
95 	TEST_ASSERT_FALSE(strcmp(ba, "1234frob"));
96 }
97 
98