Skip to content

Commit 593e1f0

Browse files
committed
Add unit testing of fdlibm
The project is relying on a variant of fdlibm, which has aleady been edited but never verified for correctness. This patch adds unit testing of fdlibm by: * introducing a test generator that uses a trusted libm implementation to calculate correct and expected results of math functions (tools/gen-test-fdlibm.sh and tools/unit-tests/gen-test-fdlibm.c), * adding tests created with the generator that stress all publicly exported functions of jerry's fdlibm (tests/unit/test-fdlibm.inc.h), and * adding a unit test file to drive the generated tests (tests/unit/test-fdlibm.c). Note: The test generator is not expected to be executed often, thus it is not wired into the build system. If it gets edited, it must be used locally to re-generate the .inc.h file. During development, it turned out that tests/unit/test-common.h included the system header math.h, which was only a bad smell until now but became a real header conflict issue with the introduction of the fdlibm unit test. Thus, this patch also changes the include to fdlibm-math.h. JerryScript-DCO-1.0-Signed-off-by: Akos Kiss [email protected]
1 parent e1f20ad commit 593e1f0

File tree

6 files changed

+1425
-1
lines changed

6 files changed

+1425
-1
lines changed

tests/unit/test-common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#include "jrt.h"
2020

21-
#include <math.h>
21+
#include "fdlibm-math.h"
2222
#include <setjmp.h>
2323
#include <stdio.h>
2424
#include <stdlib.h>

tests/unit/test-fdlibm.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* Copyright 2016 Samsung Electronics Co., Ltd.
2+
* Copyright 2016 University of Szeged.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Unit test for fdlibm
19+
*/
20+
21+
#include "test-common.h"
22+
23+
static bool passed = true;
24+
25+
static void
26+
check_int (const char *expr, int computed, int expected)
27+
{
28+
printf ("%s = %d [expected=%d] ", expr, computed, expected);
29+
30+
bool result = computed == expected;
31+
printf ("%s\n", result ? "PASS" : "FAIL");
32+
33+
passed &= result;
34+
} /* check_int */
35+
36+
static void
37+
check_double (const char *expr, double computed, double expected)
38+
{
39+
uint64_t *computed_bits_p = (uint64_t *) &computed;
40+
uint64_t *expected_bits_p = (uint64_t *) &expected;
41+
42+
printf ("%s = 0x%lx [expected=0x%lx] ", expr, *computed_bits_p, *expected_bits_p);
43+
44+
bool result;
45+
if (isnan (computed) && isnan (expected))
46+
{
47+
result = true;
48+
}
49+
else
50+
{
51+
int64_t diff = (int64_t) (*computed_bits_p - *expected_bits_p);
52+
if (diff < 0)
53+
{
54+
diff = -diff;
55+
}
56+
57+
if (diff <= 1) /* tolerate 1 bit of differene in the last place */
58+
{
59+
result = true;
60+
if (diff != 0)
61+
{
62+
printf ("APPROX ");
63+
}
64+
}
65+
else
66+
{
67+
result = false;
68+
}
69+
}
70+
printf ("%s\n", result ? "PASS" : "FAIL");
71+
72+
passed &= result;
73+
} /* check_double */
74+
75+
int
76+
main (int __attr_unused___ argc,
77+
char __attr_unused___ **argv)
78+
{
79+
#define INF INFINITY
80+
#include "test-fdlibm.inc.h"
81+
82+
return passed ? 0 : 1;
83+
} /* main */

0 commit comments

Comments
 (0)