Skip to content

Add unit testing of fdlibm #967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/unit/test-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include "jrt.h"

#include <math.h>
#include "fdlibm-math.h"
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down
83 changes: 83 additions & 0 deletions tests/unit/test-fdlibm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* Copyright 2016 Samsung Electronics Co., Ltd.
* Copyright 2016 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Unit test for fdlibm
*/

#include "test-common.h"

static bool passed = true;

static void
check_int (const char *expr, int computed, int expected)
{
printf ("%s = %d [expected=%d] ", expr, computed, expected);

bool result = computed == expected;
printf ("%s\n", result ? "PASS" : "FAIL");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would only print result information if:
a) I'm running the unit tests with verbose option to see all results (I know we don't have such option now)
b) or there is a failure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree here. Mostly exactly because we don't have verbose/quiet modes for unit tests. These tests are to catch errors and if something is caught then help finding its root. Thus, for debugging, I do need to know what did I test successfully and what failed (positive results are just as important as the negative ones in locating the error). Moreover and/or fortunately, the way unit test execution works (make test-unit) only dumps the output of this test if it fails. If everything is OK, then all the above output is swallowed.


passed &= result;
} /* check_int */

static void
check_double (const char *expr, double computed, double expected)
{
uint64_t *computed_bits_p = (uint64_t *) &computed;
uint64_t *expected_bits_p = (uint64_t *) &expected;

printf ("%s = 0x%lx [expected=0x%lx] ", expr, *computed_bits_p, *expected_bits_p);

bool result;
if (isnan (computed) && isnan (expected))
{
result = true;
}
else
{
int64_t diff = (int64_t) (*computed_bits_p - *expected_bits_p);
if (diff < 0)
{
diff = -diff;
}

if (diff <= 1) /* tolerate 1 bit of differene in the last place */
{
result = true;
if (diff != 0)
{
printf ("APPROX ");
}
}
else
{
result = false;
}
}
printf ("%s\n", result ? "PASS" : "FAIL");

passed &= result;
} /* check_double */

int
main (int __attr_unused___ argc,
char __attr_unused___ **argv)
{
#define INF INFINITY
#include "test-fdlibm.inc.h"

return passed ? 0 : 1;
} /* main */
563 changes: 563 additions & 0 deletions tests/unit/test-fdlibm.inc.h

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tools/cppcheck/suppressions-list
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
operatorEqVarError
noConstructor
duplicateExpression
wrongmathcall:tests/unit/test-fdlibm.inc.h

// FIXME: false positive in cppcheck 1.61 (will disappear once distro ships with 1.69)
variableScope:jerry-core/ecma/builtin-objects/ecma-builtin-helpers.c
20 changes: 20 additions & 0 deletions tools/gen-test-fdlibm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

# Copyright 2016 Samsung Electronics Co., Ltd.
# Copyright 2016 University of Szeged
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

make -C tools/unit-tests build
tools/unit-tests/gen-test-fdlibm >tests/unit/test-fdlibm.inc.h
make -C tools/unit-tests clean
29 changes: 29 additions & 0 deletions tools/unit-tests/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2016 Samsung Electronics Co., Ltd.
# Copyright 2016 University of Szeged
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

CC=gcc
LDFLAGS=-lm

GENS=gen-test-fdlibm

.PHONY: build
build: $(GENS)

.PHONY: clean
clean:
rm -f $(GENS)

gen-test-fdlibm: gen-test-fdlibm.c
$(CC) $< -o $@ $(LDFLAGS)
Loading