comparison test_snprintf.c @ 0:e3088565a6b8 default tip

*: initial commit kinda dumb, but wifi was out and I was bored. most of this code is shit.
author Paper <paper@tflc.us>
date Wed, 03 Dec 2025 03:04:39 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e3088565a6b8
1 #include "printf.h"
2
3 #include <stdint.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <limits.h>
7 #include <float.h>
8
9 /* This is a smaller buffer size than test.h needs, because we want to test
10 * NUL-terminating behavior. */
11 #define BUFSZ 128
12
13 int test_snprintf(void)
14 {
15 int n1, n2, b1, b2;
16 char s1[BUFSZ], s2[BUFSZ];
17
18 b1 = my_snprintf(NULL, 0,
19 #include "test.h"
20 );
21 if (b1 < 0) {
22 fprintf(stderr, "my_snprintf(NULL, 0, ...): %s\n", my_strerror(b1));
23 return -1;
24 }
25
26 b2 = snprintf(NULL, 0,
27 #include "test.h"
28 );
29 if (b2 < 0) {
30 perror("snprintf(NULL, 0, ...)");
31 return -1;
32 }
33
34 if (b1 != b2) {
35 fprintf(stderr, "snprintf and my_snprintf returned different buffer sizes!\n");
36 return -1;
37 }
38
39 n1 = my_snprintf(s1, BUFSZ,
40 #include "test.h"
41 );
42 if (n1 < 0) {
43 fprintf(stderr, "my_snprintf: %s\n", my_strerror(n1));
44 return -1;
45 }
46 n2 = snprintf(s2, BUFSZ,
47 #include "test.h"
48 );
49 if (n2 < 0) {
50 my_free(s1);
51 perror("snprintf");
52 return -1;
53 }
54
55 if (n1 != b1) {
56 fprintf(stderr, "my_snprintf with and without a buffer returned different results\n");
57 return -1;
58 }
59
60 if (n1 != n2) {
61 printf("snprintf and my_snprintf returned different buffer sizes\n");
62 return -1;
63 }
64
65 if (strcmp(s1, s2)) {
66 fprintf(stderr, "Got different results!!\n");
67 fprintf(stderr, " snprintf: %s", s1);
68 fprintf(stderr, " my_snprintf: %s", s2);
69
70 return -1;
71 }
72
73 return 0;
74 }