Imported Upstream version 2.9.0
[debian/cc1111] / support / regression / tests / simplefloat.c
1 /** Simple set of tests for floating pt.
2  */
3 #include <testfwk.h>
4 #include <math.h>
5
6 #if (PORT_HOST)
7 #  define FCOMP(a,b) (fabsf((a) - (b)) < ((b) * 1e-7))
8 #else
9    /* Testing floats for equality is normally a bug,
10       but too keep this test simple we dare it. And
11       it works with the exception of the division on
12       the host port. */
13 #  define FCOMP(a,b) ((a) == (b))
14 #endif
15
16 void
17 testCmp(void)
18 {
19   volatile float left, right;
20
21   left = 5;
22   right = 13;
23   ASSERT(left + right == 18);
24   ASSERT(left + right <= 18);
25   ASSERT(left + right >= 18);
26   ASSERT(left + right > 17.9);
27   ASSERT(left + right < 18.1);
28 }
29
30 void
31 testDiv(void)
32 {
33 #if defined (__mcs51) && !defined (SDCC_STACK_AUTO)
34   idata at 0xd0
35 #endif
36   volatile float left;
37   volatile float right;
38
39   left = 17;
40   right = 343;
41
42   ASSERT(FCOMP(left/right, (17.0/343.0)));
43   ASSERT(FCOMP(right/left, (343.0/17.0)));
44
45   right = 17;
46   ASSERT(FCOMP(left/right, 1.0));
47 }
48
49 void
50 testDivNearOne(void)
51 {
52   volatile float left, right, result;
53
54   left = 12392.4;
55   right = 12392.4;
56   result = left / right;
57
58   if (result > 0.999999)
59     {
60       /* Fine */
61     }
62   else
63     {
64       FAIL();
65     }
66   if (result < 1.00001)
67     {
68       /* Fine */
69     }
70   else
71     {
72       FAIL();
73     }
74   if (result > 0.999999 && result < 1.00001)
75     {
76       /* Fine */
77     }
78   else
79     {
80       FAIL();
81     }
82 }