struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / float.c.in
blob6a7cb68295b6923e0c20b361095db7350f4e5632
1 /* Float tests.
3 operation: ADD, SUB, MUL, DIV, REVDIV
4 */
6 #if 1
7 // we are in the regression tests
8 #include <testfwk.h>
9 #define DEBUG(x)
11 #define {operation} 1
12 #else
13 // we are standalone
14 #include <stdio.h>
15 #define DEBUG(x) x
16 #define ASSERT(x)
17 #define ADD 1
18 #define SUB 1
19 #define MUL 1
20 #define DIV 1
21 #define REVDIV 1
22 #endif
24 #ifdef __SDCC_mcs51
25 # define STORAGE __xdata
26 # define XDATA __xdata
27 #elif __SDCC_pic16
28 # define STORAGE __code
29 # define XDATA
30 #else
31 # define STORAGE
32 # define XDATA
33 #endif
35 #if !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) // Lack of memory
36 XDATA volatile float left, right, result;
38 struct {
39 float left, right, add, sub, mul, div, revdiv;
40 } STORAGE cases[]={
41 // left right add sub mul div revdiv
42 { 12.8, 25.6, 38.4, -12.8, 327.68, 0.5, 2},
43 { 12.8, -25.6, -12.8, 38.4, -327.68, -0.5, -2},
44 { -12.8, 25.6, 12.8, -38.4, -327.68, -0.5, -2},
45 { -12.8, -25.6, -38.4, 12.8, 327.68, 0.5, 2},
46 { 100.0, 10.0, 110.0, 90.0, 1000.00, 10.0, 0.1},
47 { 1000.0, 10.0, 1010.0, 990.0, 10000.00, 100.0, 0.01},
48 { 10000.0, 10.0, 10010.0, 9990.0, 100000.00, 1000.0, 0.001},
49 { 100000.0, 10.0, 100010.0, 99990.0, 1000000.00, 10000.0, 0.0001},
50 { 1000000.0, 10.0, 1000010.0, 999990.0, 10000000.00, 100000.0, 0.00001},
51 {10000000.0, 10.0,10000010.0, 9999990.0,100000000.00, 1000000.0, 0.000001},
52 { 0x100, 0x10, 0x110, 0xf0, 0x1000, 0x10, 0.0625},
53 { 0x1000, 0x10, 0x1010, 0xff0, 0x10000, 0x100, 0.00390625},
54 { 0x10000, 0x10, 0x10010, 0xfff0, 0x100000, 0x1000, 0.00024414},
55 { 0x100000, 0x10, 0x100010, 0xffff0, 0x1000000, 0x10000, 0 /* ignore */},
56 { 0x1000000, 0x10, 0x1000010, 0xfffff0, 0x10000000, 0x100000, 0 /* ignore */},
57 {0x10000000, 0x10,0x10000010, 0xffffff0, (float)0x10000000*0x10,
58 0x1000000, 0 /* ignore */},
61 XDATA int tests = 0, errors = 0;
63 char
64 compare (float is, float should)
66 float diff = should ? is / should : 0;
67 tests++;
68 DEBUG (printf (" %1.3f (%f %f) ", is, should, diff));
70 if (should == 0)
72 DEBUG (printf ("IGNORED!\n"));
73 return 0;
76 // skip the fp roundoff errors
77 if (diff > 0.999999 && diff < 1.00001)
79 DEBUG (printf ("OK!\n"));
80 ASSERT (1);
81 return 0;
83 else
85 errors++;
86 DEBUG (printf ("FAIL!\n"));
87 ASSERT (0);
88 return 1;
91 #endif
93 void
94 testFloatMath (void)
96 #if !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) // Lack of memory
97 int i;
98 int t = sizeof (cases) / sizeof (cases[0]);
99 float result;
101 for (i = 0; i < t; i++)
103 DEBUG (printf ("Case %d ", i));
104 #ifdef ADD
105 // add
106 result = cases[i].left + cases[i].right;
107 DEBUG (printf ("%1.3f + %1.3f =", cases[i].left, cases[i].right));
108 compare (result, cases[i].add);
109 #endif
110 #ifdef SUB
111 // sub
112 result = cases[i].left - cases[i].right;
113 DEBUG (printf ("%1.3f - %1.3f =", cases[i].left, cases[i].right));
114 compare (result, cases[i].sub);
115 #endif
116 #ifdef MUL
117 // mul
118 result = cases[i].left * cases[i].right;
119 DEBUG (printf ("%1.3f * %1.3f =", cases[i].left, cases[i].right));
120 compare (result, cases[i].mul);
121 #endif
122 #ifdef DIV
123 // div
124 result = cases[i].left / cases[i].right;
125 DEBUG (printf ("%1.3f / %1.3f =", cases[i].left, cases[i].right));
126 compare (result, cases[i].div);
127 #endif
128 #ifdef REVDIV
129 // revdiv
130 result = cases[i].right / cases[i].left;
131 DEBUG (printf ("%1.3f / %1.3f =", cases[i].right, cases[i].left));
132 compare(result, cases[i].revdiv);
133 #endif
135 DEBUG (printf ("%d tests, %d errors\n", tests, errors));
136 #endif
139 void
140 testFloatMulRound (void)
142 #if !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) // Lack of memory
143 right = 2.0 / 10.61;
144 result = 10.61 * right;
145 compare (result, 2.0);
146 #endif
149 #if 0
150 void
151 main (void)
153 testFloatMath ();
154 testFloatMulRound ();
156 #endif