Skip to content

Commit c85d700

Browse files
authored
Add tests for float, double, long double and partial _Float128 support (#239)
1 parent 8fc3d5d commit c85d700

File tree

2 files changed

+84
-20
lines changed

2 files changed

+84
-20
lines changed

tests/math-test.c

+84-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
#include "gtest_like_c.h"
22

33
#include <stdio.h>
4+
#include <stdarg.h>
5+
6+
#define M_E_LD 2.718281828459045235360287471352662498L
7+
8+
// _Float128 is not fully supported yet, for example strfromf128() or Q length specifier in
9+
// printf() don't work yet. So for now, instead of testing _Float128 in printf_test() like other
10+
// types, we test some arithmetics and type handling in float_128_test().
411

512
_Float128 foo(_Float128 x)
613
{
@@ -18,26 +25,91 @@ _Float128 bar(_Float128 x)
1825
return x * __builtin_huge_valf128();
1926
}
2027

21-
_Float128 baz(_Float128 x)
22-
{
23-
return x * __builtin_huge_valf128();
24-
}
25-
26-
void math_test()
28+
void float_128_test()
2729
{
28-
foo(1.2Q);
29-
bar(1.2Q);
30+
// Based on test from gcc/libgfortran/configure
3031
foo(1.2F128);
3132
bar(1.2F128);
32-
baz(1.2F128);
3333
foo(1.2Q);
3434
bar(1.2Q);
35-
baz(1.2Q);
35+
}
36+
37+
void sizeof_test()
38+
{
39+
ASSERT_EQ(sizeof(float), 4);
40+
ASSERT_EQ(sizeof(double), 8);
41+
ASSERT_EQ(sizeof(long double), 8);
42+
ASSERT_EQ(sizeof(_Float128), 16);
43+
}
44+
45+
void assert_snprintf(const char* expected, const char* format, ...)
46+
{
47+
va_list list;
48+
va_start (list, format);
49+
50+
const int SIZE = 1024;
51+
char actual[SIZE];
52+
snprintf(actual, SIZE, format, list);
53+
ASSERT_STREQ(expected, actual);
54+
55+
va_end(list);
56+
}
57+
58+
void printf_test()
59+
{
60+
float f = 1.23456f;
61+
double d = 1.23456;
62+
long double ld = M_E_LD;
63+
64+
assert_snprintf("float %f => 1.234560", "float %%f => %f", f);
65+
assert_snprintf("float %F => 1.234560", "float %%F => %F", f);
66+
assert_snprintf("float %e => 1.234560e+00", "float %%e => %e", f);
67+
assert_snprintf("float %E => 1.234560E+00", "float %%E => %E", f);
68+
assert_snprintf("float %g => 1.23456", "float %%g => %g", f);
69+
assert_snprintf("float %G => 1.23456", "float %%G => %G", f);
70+
assert_snprintf("float %a => 0x1.3c0c2p+0", "float %%a => %a", f);
71+
assert_snprintf("float %A => 0X1.3C0C2P+0", "float %%A => %A", f);
72+
73+
assert_snprintf("double %f => 1.234560", "double %%f => %f", d);
74+
assert_snprintf("double %F => 1.234560", "double %%F => %F", d);
75+
assert_snprintf("double %e => 1.234560e+00", "double %%e => %e", d);
76+
assert_snprintf("double %E => 1.234560E+00", "double %%E => %E", d);
77+
assert_snprintf("double %g => 1.23456", "double %%g => %g", d);
78+
assert_snprintf("double %G => 1.23456", "double %%G => %G", d);
79+
assert_snprintf("double %a => 0x1.3c0c1fc8f3238p+0", "double %%a => %a", d);
80+
assert_snprintf("double %A => 0X1.3C0C1FC8F3238P+0", "double %%A => %A", d);
81+
82+
assert_snprintf("long double %f => 2.718282", "long double %%f => %f", ld);
83+
assert_snprintf("long double %F => 2.718282", "long double %%F => %F", ld);
84+
assert_snprintf("long double %e => 2.718282e+00", "long double %%e => %e", ld);
85+
assert_snprintf("long double %E => 2.718282E+00", "long double %%E => %E", ld);
86+
assert_snprintf("long double %g => 2.71828", "long double %%g => %g", ld);
87+
assert_snprintf("long double %G => 2.71828", "long double %%G => %G", ld);
88+
assert_snprintf("long double %a => 0x1.5bf0a8b145769p+1", "long double %%a => %a", ld);
89+
assert_snprintf("long double %A => 0X1.5BF0A8B145769P+1", "long double %%A => %A", ld);
90+
91+
assert_snprintf("long double %Lf => 2.718282", "long double %%Lf => %Lf", ld);
92+
assert_snprintf("long double %LF => 2.718282", "long double %%LF => %LF", ld);
93+
assert_snprintf("long double %Le => 2.718282e+00", "long double %%Le => %Le", ld);
94+
assert_snprintf("long double %LE => 2.718282E+00", "long double %%LE => %LE", ld);
95+
assert_snprintf("long double %Lg => 2.71828", "long double %%Lg => %Lg", ld);
96+
assert_snprintf("long double %LG => 2.71828", "long double %%LG => %LG", ld);
97+
assert_snprintf("long double %La => 0x1.5bf0a8b145769p+1", "long double %%La => %La", ld);
98+
assert_snprintf("long double %LA => 0X1.5BF0A8B145769P+1", "long double %%LA => %LA", ld);
3699

37-
printf("ok\n");
100+
assert_snprintf("long double %lf => 2.718282", "long double %%lf => %lf", ld);
101+
assert_snprintf("long double %lF => 2.718282", "long double %%lF => %lF", ld);
102+
assert_snprintf("long double %le => 2.718282e+00", "long double %%le => %le", ld);
103+
assert_snprintf("long double %lE => 2.718282E+00", "long double %%lE => %lE", ld);
104+
assert_snprintf("long double %lg => 2.71828", "long double %%lg => %lg", ld);
105+
assert_snprintf("long double %lG => 2.71828", "long double %%lG => %lG", ld);
106+
assert_snprintf("long double %la => 0x1.5bf0a8b145769p+1", "long double %%la => %la", ld);
107+
assert_snprintf("long double %lA => 0X1.5BF0A8B145769P+1", "long double %%lA => %lA", ld);
38108
}
39109

40110
TEST(Aarch64MinGW, MathTest)
41111
{
42-
math_test();
112+
float_128_test();
113+
sizeof_test();
114+
printf_test();
43115
}

tests/math-test.cpp

-8
This file was deleted.

0 commit comments

Comments
 (0)