]> git.sur5r.net Git - cc65/blob - testcode/lib/snprintf-test.c
un-remove TABs in doc/using-make.sgml
[cc65] / testcode / lib / snprintf-test.c
1 /*
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
5 ** properly.
6 **
7 ** 2015-07-17, Greg King
8 */
9
10 #include <conio.h>
11 #include <errno.h>
12 #include <stdio.h>
13 #include <string.h>
14
15 static const char format[] = "1234567890\nabcdefghijklmnopqrstuvwxyz\n%u\n%s\n\n";
16 #define FORMAT_SIZE (sizeof format - 2u - 2u - 1u)
17
18 #define arg1 12345u
19 #define ARG1_SIZE (5u)
20
21 static const char arg2[] = "!@#$%^&*()-+";
22 #define ARG2_SIZE (sizeof arg2 - 1u)
23
24 #define STRING_SIZE (FORMAT_SIZE + ARG1_SIZE + ARG2_SIZE)
25
26 static char buf[256];
27 static int size;
28
29
30 static void fillbuf(void)
31 {
32     memset(buf, 0xFF, sizeof buf - 1u);
33     buf[sizeof buf - 1u] = '\0';
34 }
35
36
37 unsigned char main(void)
38 {
39     static unsigned char failures = 0;
40
41     /* Show what sprintf() should create. */
42
43     if ((size = printf(format, arg1, arg2)) != STRING_SIZE) {
44         ++failures;
45         printf("printf() gave the wrong size: %d.\n", size);
46     }
47
48     /* Test the normal behavior of sprintf(). */
49
50     fillbuf();
51     size = sprintf(buf, format, arg1, arg2);
52     fputs(buf, stdout);
53     if (size != STRING_SIZE) {
54         ++failures;
55         printf("sprintf() gave the wrong size: %d.\n", size);
56     }
57
58     /* Test the normal behavior of snprintf(). */
59
60     fillbuf();
61     size = snprintf(buf, sizeof buf, format, arg1, arg2);
62     fputs(buf, stdout);
63     if (size != STRING_SIZE) {
64         ++failures;
65         printf("snprintf(sizeof buf) gave the wrong size:\n %d.\n", size);
66     }
67
68     /* Does snprintf() return the full-formatted size even when the buffer
69     ** is short?  Does it write beyond the end of that buffer?
70     */
71
72     fillbuf();
73     size = snprintf(buf, STRING_SIZE - 5u, format, arg1, arg2);
74     if (size != STRING_SIZE) {
75         ++failures;
76         printf("snprintf(STRING_SIZE-5) gave the wrong size:\n %d.\n", size);
77     }
78     if (buf[STRING_SIZE - 5u - 1u] != '\0' || buf[STRING_SIZE - 5u] != 0xFF) {
79         ++failures;
80         printf("snprintf(STRING_SIZE-5) wrote beyond\n the end of the buffer.\n");
81     }
82
83     /* Does snprintf() detect a buffer size that is too big? */
84
85     fillbuf();
86     errno = 0;
87     size = snprintf(buf, 0x8000, format, arg1, arg2);
88     if (size >= 0) {
89         ++failures;
90         printf("snprintf(0x8000) didn't give an error:\n %d; errno=%d.\n", size, errno);
91     } else {
92         printf("snprintf(0x8000) did give an error:\n errno=%d.\n", errno);
93     }
94     if (buf[0] != 0xFF) {
95         ++failures;
96         printf("snprintf(0x8000) wrote into the buffer.\n");
97     }
98
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.
101     */
102
103     fillbuf();
104     size = snprintf(buf, 0, format, arg1, arg2);
105     if (size != STRING_SIZE) {
106         ++failures;
107         printf("snprintf(0) gave the wrong size:\n %d.\n", size);
108     }
109     if (buf[0] != 0xFF) {
110         ++failures;
111         printf("snprintf(0) wrote into the buffer.\n");
112     }
113
114     /* Does sprintf() detect a zero buffer-pointer? */
115
116     errno = 0;
117     size = sprintf(NULL, format, arg1, arg2);
118     if (size >= 0) {
119         ++failures;
120         printf("sprintf(NULL) didn't give an error:\n %d; errno=%d.\n", size, errno);
121     } else {
122         printf("sprintf(NULL) did give an error:\n errno=%d.\n", errno);
123     }
124
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.
127     */
128
129     size = snprintf(NULL, 0, format, arg1, arg2);
130     if (size != STRING_SIZE) {
131         ++failures;
132         printf("snprintf(NULL,0) gave the wrong size:\n %d.\n", size);
133     }
134
135     if (failures != 0) {
136         printf("There were %u", failures);
137     } else {
138         printf("There were no");
139     }
140     printf(" failures.\nTap a key. ");
141     cgetc();
142
143     return failures;
144 }