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 #define FMT_PREFIX "msyslog.cpp ENOENT: "
21 exp_cnt = snprintf(exp_buf, sizeof(exp_buf), FMT_PREFIX "%s",
24 act_cnt = msnprintf(act_buf, sizeof(act_buf), FMT_PREFIX "%m");
25 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
26 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
30 test_msnprintfLiteralPercentm(void)
37 exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "%%m");
39 act_cnt = msnprintf(act_buf, sizeof(act_buf), "%%m");
40 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
41 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
45 test_msnprintfBackslashLiteralPercentm(void) {
51 exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "\%%m");
53 act_cnt = msnprintf(act_buf, sizeof(act_buf), "\%%m");
54 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
55 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
59 test_msnprintfBackslashPercent(void) {
65 exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "\%s",
68 act_cnt = msnprintf(act_buf, sizeof(act_buf), "\%m");
69 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
70 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
74 test_msnprintfHangingPercent(void) {
75 static char fmt[] = "percent then nul term then non-nul %\0oops!";
83 exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "%s", fmt);
84 act_cnt = msnprintf(act_buf, sizeof(act_buf), "%s", fmt);
85 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
86 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
87 TEST_ASSERT_EQUAL_STRING("", act_buf + 1 + strlen(act_buf));
91 test_format_errmsgHangingPercent(void) {
92 #ifndef VSNPRINTF_PERCENT_M
93 static char fmt[] = "percent then nul term then non-nul %\0oops!";
97 format_errmsg(act_buf, sizeof(act_buf), fmt, ENOENT);
98 TEST_ASSERT_EQUAL_STRING(fmt, act_buf);
99 TEST_ASSERT_EQUAL_STRING("", act_buf + 1 + strlen(act_buf));
101 TEST_IGNORE_MESSAGE("VSNPRINTF_PERCENT_M is defined")
106 test_msnprintfNullTarget(void) {
110 exp_cnt = snprintf(NULL, 0, "%d", 123);
112 act_cnt = msnprintf(NULL, 0, "%d", 123);
113 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
117 test_msnprintfTruncate(void) {
118 char undist[] = "undisturbed";
124 memcpy(exp_buf + 3, undist, sizeof(undist));
125 memcpy(act_buf + 3, undist, sizeof(undist));
126 exp_cnt = snprintf(exp_buf, 3, "%s", strerror(ENOENT));
128 act_cnt = msnprintf(act_buf, 3, "%m");
129 TEST_ASSERT_EQUAL('\0', exp_buf[2]);
130 TEST_ASSERT_EQUAL('\0', act_buf[2]);
131 TEST_ASSERT_TRUE(act_cnt > 0);
132 TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
133 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
134 TEST_ASSERT_EQUAL_STRING(exp_buf + 3, undist);
135 TEST_ASSERT_EQUAL_STRING(act_buf + 3, undist);