1 /* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 /**
17 * Unit test for jerry-libm
18 */
19
20 #include <math.h>
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <stdio.h>
24
25 static bool passed = true;
26
27 static void
check_int(const char * expr,int computed,int expected)28 check_int (const char *expr, int computed, int expected)
29 {
30 printf ("%s = %d [expected=%d] ", expr, computed, expected);
31
32 bool result = computed == expected;
33 printf ("%s\n", result ? "PASS" : "FAIL");
34
35 passed &= result;
36 } /* check_int */
37
38 typedef union
39 {
40 double value;
41 uint64_t bits64;
42 uint32_t bits32[2];
43 } double_bits_t;
44
45 static void
check_double(const char * expr,double computed,double expected)46 check_double (const char *expr, double computed, double expected)
47 {
48 double_bits_t computed_bits;
49 double_bits_t expected_bits;
50
51 computed_bits.value = computed;
52 expected_bits.value = expected;
53
54 printf ("%s = 0x%08x%08x [expected=0x%08x%08x] ", expr,
55 (unsigned int) computed_bits.bits32[1], (unsigned int) computed_bits.bits32[0],
56 (unsigned int) expected_bits.bits32[1], (unsigned int) expected_bits.bits32[0]);
57
58 bool result;
59 if (isnan (computed) && isnan (expected))
60 {
61 result = true;
62 }
63 else
64 {
65 int64_t diff = (int64_t) (computed_bits.bits64 - expected_bits.bits64);
66 if (diff < 0)
67 {
68 diff = -diff;
69 }
70
71 if (diff <= 1) /* tolerate 1 bit of differene in the last place */
72 {
73 result = true;
74 if (diff != 0)
75 {
76 printf ("APPROX ");
77 }
78 }
79 else
80 {
81 result = false;
82 }
83 }
84 printf ("%s\n", result ? "PASS" : "FAIL");
85
86 passed &= result;
87 } /* check_double */
88
89 int
main(void)90 main (void)
91 {
92 #define INF INFINITY
93 #include "test-libm.inc.h"
94
95 return passed ? 0 : 1;
96 } /* main */
97