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