Skip to content
Myr

test_sprintf.c

This file

Path

tests/test_sprintf.c

202 lines · 7.2 KiB · C

Last changed by

Merge branch 'working'Sean Barrettbefore adoptionf54acd4

History of this file

Take it

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

lines 1–19why, one step back
1
2#define USE_STB 134#if USE_STB5# include "stb_sprintf.h"6# define SPRINTF stbsp_sprintf7# define SNPRINTF stbsp_snprintf8#else9# include <locale.h>10# define SPRINTF sprintf11# define SNPRINTF snprintf12#endif1314#include <assert.h>15#include <math.h> // for INFINITY, NAN16#include <stddef.h> // for ptrdiff_t17#include <stdio.h> // for printf18#include <string.h> // for strcmp, strncmp, strlen19

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

lines 20–26why, one step back
20#if _MSC_VER && _MSC_VER <= 1600
21typedef int intmax_t;22typedef ptrdiff_t ssize_t;23#else24#include <stdint.h> // for intmax_t, ssize_t25#endif26

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

line 27why, one step back
27// stbsp_sprintf

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

lines 28–29why, one step back
28#define CHECK_END(str) \
29 if (strcmp(buf, str) != 0 || (unsigned) ret != strlen(str)) { \

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

lines 30–31why, one step back
30 printf("< '%s'\n> '%s'\n", str, buf); \
31 assert(!"Fail"); \

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

line 32why, one step back
32 }

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

line 33why, one step back
33

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

lines 34–44why, one step back
34#define CHECK9(str, v1, v2, v3, v4, v5, v6, v7, v8, v9) { int ret = SPRINTF(buf, v1, v2, v3, v4, v5, v6, v7, v8, v9); CHECK_END(str); }
35#define CHECK8(str, v1, v2, v3, v4, v5, v6, v7, v8 ) { int ret = SPRINTF(buf, v1, v2, v3, v4, v5, v6, v7, v8 ); CHECK_END(str); }36#define CHECK7(str, v1, v2, v3, v4, v5, v6, v7 ) { int ret = SPRINTF(buf, v1, v2, v3, v4, v5, v6, v7 ); CHECK_END(str); }37#define CHECK6(str, v1, v2, v3, v4, v5, v6 ) { int ret = SPRINTF(buf, v1, v2, v3, v4, v5, v6 ); CHECK_END(str); }38#define CHECK5(str, v1, v2, v3, v4, v5 ) { int ret = SPRINTF(buf, v1, v2, v3, v4, v5 ); CHECK_END(str); }39#define CHECK4(str, v1, v2, v3, v4 ) { int ret = SPRINTF(buf, v1, v2, v3, v4 ); CHECK_END(str); }40#define CHECK3(str, v1, v2, v3 ) { int ret = SPRINTF(buf, v1, v2, v3 ); CHECK_END(str); }41#define CHECK2(str, v1, v2 ) { int ret = SPRINTF(buf, v1, v2 ); CHECK_END(str); }42#define CHECK1(str, v1 ) { int ret = SPRINTF(buf, v1 ); CHECK_END(str); }4344#ifdef TEST_SPRINTF

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

lines 45–52why, one step back
45int main()
46{47 char buf[1024];48 int n = 0;49 const double pow_2_75 = 37778931862957161709568.0;50 const double pow_2_85 = 38685626227668133590597632.0;5152 // integers

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

lines 53–65why, one step back
53 CHECK4("a b 1", "%c %s %d", 'a', "b", 1);
54 CHECK2("abc ", "%-8.3s", "abcdefgh");55 CHECK2("+5", "%+2d", 5);56 CHECK2(" 6", "% 3i", 6);57 CHECK2("-7 ", "%-4d", -7);58 CHECK2("+0", "%+d", 0);59 CHECK3(" 00003: 00004", "%10.5d:%10.5d", 3, 4);60 CHECK2("-100006789", "%d", -100006789);61 CHECK3("20 0020", "%u %04u", 20u, 20u);62 CHECK4("12 1e 3C", "%o %x %X", 10u, 30u, 60u);63 CHECK4(" 12 1e 3C ", "%3o %2x %-3X", 10u, 30u, 60u);64 CHECK4("012 0x1e 0X3C", "%#o %#x %#X", 10u, 30u, 60u);65 CHECK2("", "%.0x", 0);

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

line 66why, one step back
66#if USE_STB

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

line 67why, one step back
67 CHECK2("0", "%.0d", 0); // stb_sprintf gives "0"

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

line 68why, one step back
68#else

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

line 69why, one step back
69 CHECK2("", "%.0d", 0); // glibc gives "" as specified by C99(?)

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

line 70why, one step back
70#endif

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

lines 71–75why, one step back
71 CHECK3("33 555", "%hi %ld", (short)33, 555l);
72#if !defined(_MSC_VER) || _MSC_VER >= 160073 CHECK2("9888777666", "%llu", 9888777666llu);74#endif75 CHECK4("-1 2 -3", "%ji %zi %ti", (intmax_t)-1, (ssize_t)2, (ptrdiff_t)-3);

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

lines 76–77why, one step back
76
77 // floating-point numbers

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

lines 78–85why, one step back
78 CHECK2("-3.000000", "%f", -3.0);
79 CHECK2("-8.8888888800", "%.10f", -8.88888888);80 CHECK2("880.0888888800", "%.10f", 880.08888888);81 CHECK2("4.1", "%.1f", 4.1);82 CHECK2(" 0", "% .0f", 0.1);83 CHECK2("0.00", "%.2f", 1e-4);84 CHECK2("-5.20", "%+4.2f", -5.2);85 CHECK2("0.0 ", "%-10.1f", 0.);

Merge branch 'fix_ub_shift' of https://github.com/wojdyr/stb into workingSean Barrettbefore adoption2805fe3

line 86why, one step back
86 CHECK2("-0.000000", "%f", -0.);

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

line 87why, one step back
87 CHECK2("0.000001", "%f", 9.09834e-07);

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

line 88why, one step back
88#if USE_STB // rounding differences

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

lines 89–90why, one step back
89 CHECK2("38685626227668133600000000.0", "%.1f", pow_2_85);
90 CHECK2("0.000000499999999999999978", "%.24f", 5e-7);

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

line 91why, one step back
91#else

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

lines 92–93why, one step back
92 CHECK2("38685626227668133590597632.0", "%.1f", pow_2_85); // exact
93 CHECK2("0.000000499999999999999977", "%.24f", 5e-7);

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

line 94why, one step back
94#endif

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

lines 95–108why, one step back
95 CHECK2("0.000000000000000020000000", "%.24f", 2e-17);
96 CHECK3("0.0000000100 100000000", "%.10f %.0f", 1e-8, 1e+8);97 CHECK2("100056789.0", "%.1f", 100056789.0);98 CHECK4(" 1.23 %", "%*.*f %%", 5, 2, 1.23);99 CHECK2("-3.000000e+00", "%e", -3.0);100 CHECK2("4.1E+00", "%.1E", 4.1);101 CHECK2("-5.20e+00", "%+4.2e", -5.2);102 CHECK3("+0.3 -3", "%+g %+g", 0.3, -3.0);103 CHECK2("4", "%.1G", 4.1);104 CHECK2("-5.2", "%+4.2g", -5.2);105 CHECK2("3e-300", "%g", 3e-300);106 CHECK2("1", "%.0g", 1.2);107 CHECK3(" 3.7 3.71", "% .3g %.3g", 3.704, 3.706);108 CHECK3("2e-315:1e+308", "%g:%g", 2e-315, 1e+308);

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

line 109why, one step back
109

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

line 110why, one step back
110#if __STDC_VERSION__ >= 199901L

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

line 111why, one step back
111#if USE_STB

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

line 112why, one step back
112 CHECK4("Inf Inf NaN", "%g %G %f", INFINITY, INFINITY, NAN);

sprintf warningsSean Barrettbefore adoptionf06f586

line 113why, one step back
113 CHECK2("N", "%.1g", NAN);

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

line 114why, one step back
114#else

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

line 115why, one step back
115 CHECK4("inf INF nan", "%g %G %f", INFINITY, INFINITY, NAN);

sprintf warningsSean Barrettbefore adoptionf06f586

line 116why, one step back
116 CHECK2("nan", "%.1g", NAN);

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

line 117why, one step back
117#endif

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

line 118why, one step back
118#endif

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

lines 119–120why, one step back
119
120 // %n

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

line 121why, one step back
121 CHECK3("aaa ", "%.3s %n", "aaaaaaaaaaaaa", &n);

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

lines 122–123why, one step back
122 assert(n == 4);
123

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

line 124why, one step back
124#if __STDC_VERSION__ >= 199901L

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

line 125why, one step back
125 // hex floats

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

lines 126–128why, one step back
126 CHECK2("0x1.fedcbap+98", "%a", 0x1.fedcbap+98);
127 CHECK2("0x1.999999999999a0p-4", "%.14a", 0.1);128 CHECK2("0x1.0p-1022", "%.1a", 0x1.ffp-1023);

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

line 129why, one step back
129#if USE_STB // difference in default precision and x vs X for %A

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

lines 130–131why, one step back
130 CHECK2("0x1.009117p-1022", "%a", 2.23e-308);
131 CHECK2("-0x1.AB0P-5", "%.3A", -0x1.abp-5);

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

line 132why, one step back
132#else

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

lines 133–134why, one step back
133 CHECK2("0x1.0091177587f83p-1022", "%a", 2.23e-308);
134 CHECK2("-0X1.AB0P-5", "%.3A", -0X1.abp-5);

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

line 135why, one step back
135#endif

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

line 136why, one step back
136#endif

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

lines 137–139why, one step back
137
138 // %p139#if USE_STB

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

line 140why, one step back
140 CHECK2("0000000000000000", "%p", (void*) NULL);

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

line 141why, one step back
141#else

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

line 142why, one step back
142 CHECK2("(nil)", "%p", (void*) NULL);

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

lines 143–151why, one step back
143#endif
144145 // snprintf146 assert(SNPRINTF(buf, 100, " %s %d", "b", 123) == 10);147 assert(strcmp(buf, " b 123") == 0);148 assert(SNPRINTF(buf, 100, "%f", pow_2_75) == 30);149 assert(strncmp(buf, "37778931862957161709568.000000", 17) == 0);150 n = SNPRINTF(buf, 10, "number %f", 123.456789);151 assert(strcmp(buf, "number 12") == 0);

sprintf warningsSean Barrettbefore adoptionf06f586

line 152why, one step back
152 assert(n == 17); // written vs would-be written bytes

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

line 153why, one step back
153 n = SNPRINTF(buf, 0, "7 chars");

sprintf warningsSean Barrettbefore adoptionf06f586

line 154why, one step back
154 assert(n == 7);

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

lines 155–168why, one step back
155 // stb_sprintf uses internal buffer of 512 chars - test longer string
156 assert(SPRINTF(buf, "%d %600s", 3, "abc") == 603);157 assert(strlen(buf) == 603);158 SNPRINTF(buf, 550, "%d %600s", 3, "abc");159 assert(strlen(buf) == 549);160 assert(SNPRINTF(buf, 600, "%510s %c", "a", 'b') == 516);161162 // length check163 assert(SNPRINTF(NULL, 0, " %s %d", "b", 123) == 10);164165 // ' modifier. Non-standard, but supported by glibc.166#if !USE_STB167 setlocale(LC_NUMERIC, ""); // C locale does not group digits168#endif

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

lines 169–176why, one step back
169 CHECK2("1,200,000", "%'d", 1200000);
170 CHECK2("-100,006,789", "%'d", -100006789);171#if !defined(_MSC_VER) || _MSC_VER >= 1600172 CHECK2("9,888,777,666", "%'lld", 9888777666ll);173#endif174 CHECK2("200,000,000.000000", "%'18f", 2e8);175 CHECK2("100,056,789", "%'.0f", 100056789.0);176 CHECK2("100,056,789.0", "%'.1f", 100056789.0);

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

line 177why, one step back
177#if USE_STB // difference in leading zeros

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

line 178why, one step back
178 CHECK2("000,001,200,000", "%'015d", 1200000);

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

line 179why, one step back
179#else

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

line 180why, one step back
180 CHECK2("0000001,200,000", "%'015d", 1200000);

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

lines 181–184why, one step back
181#endif
182183 // things not supported by glibc184#if USE_STB

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

lines 185–193why, one step back
185 CHECK2("null", "%s", (char*) NULL);
186 CHECK2("123,4abc:", "%'x:", 0x1234ABC);187 CHECK2("100000000", "%b", 256);188 CHECK3("0b10 0B11", "%#b %#B", 2, 3);189#if !defined(_MSC_VER) || _MSC_VER >= 1600190 CHECK4("2 3 4", "%I64d %I32d %Id", 2ll, 3, 4ll);191#endif192 CHECK3("1k 2.54 M", "%$_d %$.2d", 1000, 2536000);193 CHECK3("2.42 Mi 2.4 M", "%$$.2d %$$$d", 2536000, 2536000);

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

lines 194–196why, one step back
194
195 // different separators196 stbsp_set_separators(' ', ',');

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

line 197why, one step back
197 CHECK2("12 345,678900", "%'f", 12345.6789);

Merge branch 'test_sprintf' of https://github.com/wojdyr/stb into work2Sean Barrettbefore adoption3223673

lines 198–201why, one step back
198#endif
199200 return 0;201}

test_sprintf: clean up for MSVC6 & integration to codebaseSean Barrettbefore adoption81b4fd7

line 202why, one step back
202#endif