3 #include "ntp_stdlib.h"
7 #ifndef VSNPRINTF_PERCENT_M
8 // format_errmsg() is normally private to msyslog.c
9 void format_errmsg(char *, size_t, const char *, int);
14 void test_msnprintf(void);
15 void test_msnprintfLiteralPercentm(void);
16 void test_msnprintfBackslashLiteralPercentm(void);
17 void test_msnprintfBackslashPercent(void);
18 void test_msnprintfHangingPercent(void);
19 void test_format_errmsgHangingPercent(void);
20 void test_msnprintfNullTarget(void);
21 void test_msnprintfTruncate(void);
34 test_msnprintf(void) {
35 #define FMT_PREFIX "msyslog.cpp ENOENT: "
41 exp_cnt = snprintf(exp_buf, sizeof(exp_buf), FMT_PREFIX "%s",
44 act_cnt = msnprintf(act_buf, sizeof(act_buf), FMT_PREFIX "%m");
46 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
47 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
51 test_msnprintfLiteralPercentm(void)
58 exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "%%m");
60 act_cnt = msnprintf(act_buf, sizeof(act_buf), "%%m");
62 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
63 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
67 test_msnprintfBackslashLiteralPercentm(void) {
73 exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "\%%m");
75 act_cnt = msnprintf(act_buf, sizeof(act_buf), "\%%m");
77 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
78 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
82 test_msnprintfBackslashPercent(void) {
88 exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "\%s",
91 act_cnt = msnprintf(act_buf, sizeof(act_buf), "\%m");
93 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
94 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
98 test_msnprintfHangingPercent(void) {
99 static char fmt[] = "percent then nul term then non-nul %\0oops!";
107 exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "%s", fmt);
108 act_cnt = msnprintf(act_buf, sizeof(act_buf), "%s", fmt);
110 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
111 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
112 TEST_ASSERT_EQUAL_STRING("", act_buf + 1 + strlen(act_buf));
116 test_format_errmsgHangingPercent(void) {
117 #ifndef VSNPRINTF_PERCENT_M
118 static char fmt[] = "percent then nul term then non-nul %\0oops!";
122 format_errmsg(act_buf, sizeof(act_buf), fmt, ENOENT);
124 TEST_ASSERT_EQUAL_STRING(fmt, act_buf);
125 TEST_ASSERT_EQUAL_STRING("", act_buf + 1 + strlen(act_buf));
127 TEST_IGNORE_MESSAGE("VSNPRINTF_PERCENT_M is defined")
132 test_msnprintfNullTarget(void) {
136 exp_cnt = snprintf(NULL, 0, "%d", 123);
138 act_cnt = msnprintf(NULL, 0, "%d", 123);
140 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
144 test_msnprintfTruncate(void) {
145 char undist[] = "undisturbed";
151 memcpy(exp_buf + 3, undist, sizeof(undist));
152 memcpy(act_buf + 3, undist, sizeof(undist));
153 exp_cnt = snprintf(exp_buf, 3, "%s", strerror(ENOENT));
155 act_cnt = msnprintf(act_buf, 3, "%m");
157 TEST_ASSERT_EQUAL('\0', exp_buf[2]);
158 TEST_ASSERT_EQUAL('\0', act_buf[2]);
159 TEST_ASSERT_TRUE(act_cnt > 0);
160 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
161 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
162 TEST_ASSERT_EQUAL_STRING(exp_buf + 3, undist);
163 TEST_ASSERT_EQUAL_STRING(act_buf + 3, undist);