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);
13 void test_msnprintf(void);
14 void test_msnprintfLiteralPercentm(void);
15 void test_msnprintfBackslashLiteralPercentm(void);
16 void test_msnprintfBackslashPercent(void);
17 void test_msnprintfHangingPercent(void);
18 void test_format_errmsgHangingPercent(void);
19 void test_msnprintfNullTarget(void);
20 void test_msnprintfTruncate(void);
24 test_msnprintf(void) {
25 #define FMT_PREFIX "msyslog.cpp ENOENT: "
31 exp_cnt = snprintf(exp_buf, sizeof(exp_buf), FMT_PREFIX "%s",
34 act_cnt = msnprintf(act_buf, sizeof(act_buf), FMT_PREFIX "%m");
36 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
37 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
41 test_msnprintfLiteralPercentm(void)
48 exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "%%m");
50 act_cnt = msnprintf(act_buf, sizeof(act_buf), "%%m");
52 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
53 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
57 test_msnprintfBackslashLiteralPercentm(void) {
63 exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "\%%m");
65 act_cnt = msnprintf(act_buf, sizeof(act_buf), "\%%m");
67 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
68 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
72 test_msnprintfBackslashPercent(void) {
78 exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "\%s",
81 act_cnt = msnprintf(act_buf, sizeof(act_buf), "\%m");
83 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
84 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
88 test_msnprintfHangingPercent(void) {
89 static char fmt[] = "percent then nul term then non-nul %\0oops!";
97 exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "%s", fmt);
98 act_cnt = msnprintf(act_buf, sizeof(act_buf), "%s", fmt);
100 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
101 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
102 TEST_ASSERT_EQUAL_STRING("", act_buf + 1 + strlen(act_buf));
106 test_format_errmsgHangingPercent(void) {
107 #ifndef VSNPRINTF_PERCENT_M
108 static char fmt[] = "percent then nul term then non-nul %\0oops!";
112 format_errmsg(act_buf, sizeof(act_buf), fmt, ENOENT);
114 TEST_ASSERT_EQUAL_STRING(fmt, act_buf);
115 TEST_ASSERT_EQUAL_STRING("", act_buf + 1 + strlen(act_buf));
117 TEST_IGNORE_MESSAGE("VSNPRINTF_PERCENT_M is defined")
122 test_msnprintfNullTarget(void) {
126 exp_cnt = snprintf(NULL, 0, "%d", 123);
128 act_cnt = msnprintf(NULL, 0, "%d", 123);
130 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
134 test_msnprintfTruncate(void) {
135 char undist[] = "undisturbed";
141 memcpy(exp_buf + 3, undist, sizeof(undist));
142 memcpy(act_buf + 3, undist, sizeof(undist));
143 exp_cnt = snprintf(exp_buf, 3, "%s", strerror(ENOENT));
145 act_cnt = msnprintf(act_buf, 3, "%m");
147 TEST_ASSERT_EQUAL('\0', exp_buf[2]);
148 TEST_ASSERT_EQUAL('\0', act_buf[2]);
149 TEST_ASSERT_TRUE(act_cnt > 0);
150 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
151 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
152 TEST_ASSERT_EQUAL_STRING(exp_buf + 3, undist);
153 TEST_ASSERT_EQUAL_STRING(act_buf + 3, undist);