Debian Codegraph @

matio-1.5.30/test/test_snprintf.c

raw symbols SCIP
1 #include "matioConfig.h"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <math.h>
6
7 #if !HAVE_SNPRINTF
8 int rpl_snprintf(char *, size_t, const char *, ...);
9 #define mat_snprintf rpl_snprintf
10 #else
11 #define mat_snprintf snprintf
12 #endif /* !HAVE_SNPRINTF */
13
14 int main(int argc, char **argv)
15 {
16 char buf1[1024];
17 char buf2[1024];
18 const char *fp_fmt[] = {
19 "%1.1f",
20 "%-1.5f",
21 "%1.5f",
22 "%123.9f",
23 "%10.5f",
24 "% 10.5f",
25 "%+22.9f",
26 "%+4.9f",
27 "%01.3f",
28 "%4f",
29 "%3.1f",
30 "%3.2f",
31 "%.0f",
32 "%f",
33 "-16.16f",
34 NULL
35 };
36 const double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 203.9, 0.96, 0.996,
37 0.9996, 1.996, 4.136, 5.030201, 0.00205,
38 /* END LIST */ 0};
39 const char *int_fmt[] = {
40 "%-1.5d",
41 "%1.5d",
42 "%123.9d",
43 "%5.5d",
44 "%10.5d",
45 "% 10.5d",
46 "%+22.33d",
47 "%01.3d",
48 "%4d",
49 "%d",
50 NULL
51 };
52 const long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
53 const char *str_fmt[] = {
54 "10.5s",
55 "5.10s",
56 "10.1s",
57 "0.10s",
58 "10.0s",
59 "1.10s",
60 "%s",
61 "%.1s",
62 "%.10s",
63 "%10s",
64 NULL
65 };
66 const char *str_vals[] = {"hello", "a", "", "a longer string", NULL};
67 int x, y;
68 int fail = 0;
69 int num = 0;
70
71 printf ("Testing snprintf format codes against system sprintf...\n");
72
73 for (x = 0; fp_fmt[x] ; x++) {
74 for (y = 0; fp_nums[y] != 0 ; y++) {
75 int l1 = mat_snprintf(NULL, 0, fp_fmt[x], fp_nums[y]);
76 int l2 = mat_snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
77 sprintf (buf2, fp_fmt[x], fp_nums[y]);
78 if (strcmp (buf1, buf2) != 0) {
79 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
80 fp_fmt[x], buf1, buf2);
81 fail++;
82 }
83 if (l1 != l2) {
84 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, fp_fmt[x]);
85 fail++;
86 }
87 num++;
88 }
89 }
90
91 for (x = 0; int_fmt[x] ; x++) {
92 for (y = 0; int_nums[y] != 0 ; y++) {
93 int l1 = mat_snprintf(NULL, 0, int_fmt[x], int_nums[y]);
94 int l2 = mat_snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
95 sprintf (buf2, int_fmt[x], int_nums[y]);
96 if (strcmp (buf1, buf2) != 0) {
97 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
98 int_fmt[x], buf1, buf2);
99 fail++;
100 }
101 if (l1 != l2) {
102 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, int_fmt[x]);
103 fail++;
104 }
105 num++;
106 }
107 }
108
109 for (x = 0; str_fmt[x] ; x++) {
110 for (y = 0; str_vals[y] != 0 ; y++) {
111 int l1 = mat_snprintf(NULL, 0, str_fmt[x], str_vals[y]);
112 int l2 = mat_snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
113 sprintf (buf2, str_fmt[x], str_vals[y]);
114 if (strcmp (buf1, buf2) != 0) {
115 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
116 str_fmt[x], buf1, buf2);
117 fail++;
118 }
119 if (l1 != l2) {
120 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, str_fmt[x]);
121 fail++;
122 }
123 num++;
124 }
125 }
126
127 printf ("%d tests failed out of %d.\n", fail, num);
128
129 printf("seeing how many digits we support\n");
130 {
131 double v0 = 0.12345678901234567890123456789012345678901;
132 for (x=0; x<100; x++) {
133 double p = pow(10, x);
134 double r = v0*p;
135 mat_snprintf(buf1, sizeof(buf1), "%1.1f", r);
136 sprintf(buf2, "%1.1f", r);
137 if (strcmp(buf1, buf2) != 0) {
138 printf("we seem to support %d digits\n", x-1);
139 break;
140 }
141 }
142 }
143
144 return 0;
145 }