2 ** Test a function that formats and writes characters into a string buffer.
3 ** This program does not test formatting. It tests some behaviors that are
4 ** specific to the buffer. It tests that certain conditions are handled
7 ** 2015-07-17, Greg King
15 static const char format[] = "1234567890\nabcdefghijklmnopqrstuvwxyz\n%u\n%s\n\n";
16 #define FORMAT_SIZE (sizeof format - 2u - 2u - 1u)
19 #define ARG1_SIZE (5u)
21 static const char arg2[] = "!@#$%^&*()-+";
22 #define ARG2_SIZE (sizeof arg2 - 1u)
24 #define STRING_SIZE (FORMAT_SIZE + ARG1_SIZE + ARG2_SIZE)
30 static void fillbuf(void)
32 memset(buf, 0xFF, sizeof buf - 1u);
33 buf[sizeof buf - 1u] = '\0';
37 unsigned char main(void)
39 static unsigned char failures = 0;
41 /* Show what sprintf() should create. */
43 if ((size = printf(format, arg1, arg2)) != STRING_SIZE) {
45 printf("printf() gave the wrong size: %d.\n", size);
48 /* Test the normal behavior of sprintf(). */
51 size = sprintf(buf, format, arg1, arg2);
53 if (size != STRING_SIZE) {
55 printf("sprintf() gave the wrong size: %d.\n", size);
58 /* Test the normal behavior of snprintf(). */
61 size = snprintf(buf, sizeof buf, format, arg1, arg2);
63 if (size != STRING_SIZE) {
65 printf("snprintf(sizeof buf) gave the wrong size:\n %d.\n", size);
68 /* Does snprintf() return the full-formatted size even when the buffer
69 ** is short? Does it write beyond the end of that buffer?
73 size = snprintf(buf, STRING_SIZE - 5u, format, arg1, arg2);
74 if (size != STRING_SIZE) {
76 printf("snprintf(STRING_SIZE-5) gave the wrong size:\n %d.\n", size);
78 if (buf[STRING_SIZE - 5u - 1u] != '\0' || buf[STRING_SIZE - 5u] != 0xFF) {
80 printf("snprintf(STRING_SIZE-5) wrote beyond\n the end of the buffer.\n");
83 /* Does snprintf() detect a buffer size that is too big? */
87 size = snprintf(buf, 0x8000, format, arg1, arg2);
90 printf("snprintf(0x8000) didn't give an error:\n %d; errno=%d.\n", size, errno);
92 printf("snprintf(0x8000) did give an error:\n errno=%d.\n", errno);
96 printf("snprintf(0x8000) wrote into the buffer.\n");
99 /* snprintf() must measure the length of the formatted output even when the
100 ** buffer size is zero. But, it must not touch the buffer.
104 size = snprintf(buf, 0, format, arg1, arg2);
105 if (size != STRING_SIZE) {
107 printf("snprintf(0) gave the wrong size:\n %d.\n", size);
109 if (buf[0] != 0xFF) {
111 printf("snprintf(0) wrote into the buffer.\n");
114 /* Does sprintf() detect a zero buffer-pointer? */
117 size = sprintf(NULL, format, arg1, arg2);
120 printf("sprintf(NULL) didn't give an error:\n %d; errno=%d.\n", size, errno);
122 printf("sprintf(NULL) did give an error:\n errno=%d.\n", errno);
125 /* snprintf() must measure the length of the formatted output even when the
126 ** buffer size is zero. A zero pointer is not an error, in that case.
129 size = snprintf(NULL, 0, format, arg1, arg2);
130 if (size != STRING_SIZE) {
132 printf("snprintf(NULL,0) gave the wrong size:\n %d.\n", size);
136 printf("There were %u", failures);
138 printf("There were no");
140 printf(" failures.\nTap a key. ");