Skip to content

Commit 3b269fa

Browse files
DreamPearlscopeInfinity
authored andcommitted
Add unit test for math.c
1 parent 32c933f commit 3b269fa

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

src/usr/include/math.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ int isnan(double x);
1111
double floor(double x);
1212
double round(double x);
1313
double fmod(double x, double y);
14+
double fabs(double x);
1415

1516
double sin(double x);
1617
double cos(double x);

src/usr/lib/math.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ double round(double x) { return floor(x + 0.5); }
2424

2525
double fmod(double x, double y) { return x - floor(x / y) * y; }
2626

27+
double fabs(double x) { return (x >= 0) ? x : -x; }
28+
2729
double sin(double x) {
2830
const int terms = 32;
2931
const double x2 = x * x;

src/usr/local/src/test-math.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <math.h>
2+
#include <stdio.h>
3+
#include <testing.h>
4+
5+
double EPSILON = 1e-9;
6+
7+
double check_double_equal(double x, double y) {
8+
return std::fabs(x - y) < EPSILON;
9+
}
10+
11+
double to_radian(double x) { return x * (M_PI / 180); }
12+
13+
void test_isnan() {
14+
ASSERT_TRUE(std::isnan(16.0) == 0);
15+
ASSERT_TRUE(std::isnan(-16.0) == 0);
16+
ASSERT_TRUE(std::isnan(0.0 / 0.0) != 0);
17+
ASSERT_TRUE(std::isnan(1.0 / 0.0) == 0);
18+
}
19+
20+
void test_floor() {
21+
ASSERT_TRUE(check_double_equal(std::floor(16.0), 16.0));
22+
ASSERT_TRUE(check_double_equal(std::floor(16.6), 16.0));
23+
ASSERT_TRUE(check_double_equal(std::floor(-16.0), -16.0));
24+
ASSERT_TRUE(check_double_equal(std::floor(-16.9), -17));
25+
}
26+
27+
void test_round() {
28+
ASSERT_TRUE(check_double_equal(std::round(16.3), 16.0));
29+
ASSERT_TRUE(check_double_equal(std::round(16.6), 17.0));
30+
ASSERT_TRUE(check_double_equal(std::round(-16.0), -16.0));
31+
ASSERT_TRUE(check_double_equal(std::round(-16.9), -17));
32+
}
33+
34+
void test_fmod() {
35+
ASSERT_TRUE(check_double_equal(std::fmod(1.6, 1.2), 0.4));
36+
ASSERT_TRUE(check_double_equal(std::fmod(7.0, 5.0), 2.0));
37+
}
38+
39+
void test_fabs() {
40+
ASSERT_TRUE(check_double_equal(std::fabs(1.6), 1.6));
41+
ASSERT_TRUE(check_double_equal(std::fabs(-7.7), 7.7));
42+
ASSERT_TRUE(check_double_equal(std::fabs(0.0), 0.0));
43+
}
44+
45+
void test_sin() {
46+
ASSERT_TRUE(check_double_equal(std::sin(to_radian(45.0)), 0.7071067812));
47+
ASSERT_TRUE(check_double_equal(std::sin(to_radian(90.0)), 1.0));
48+
}
49+
50+
void test_cos() {
51+
ASSERT_TRUE(check_double_equal(std::cos(to_radian(60.0)), 0.5));
52+
ASSERT_TRUE(check_double_equal(std::cos(to_radian(90.0)), 0.0));
53+
}
54+
55+
int main(int argc, char *argv[]) {
56+
TEST_INIT();
57+
58+
RUN_TEST(test_isnan);
59+
RUN_TEST(test_floor);
60+
RUN_TEST(test_round);
61+
RUN_TEST(test_fmod);
62+
RUN_TEST(test_fabs);
63+
RUN_TEST(test_sin);
64+
RUN_TEST(test_cos);
65+
66+
TEST_SUMMARY();
67+
return 0;
68+
}

tests/app/libraries/usr_lib_test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ function add_library_unit_test {
1414
add_library_unit_test "test-string"
1515
add_library_unit_test "test-stdlib"
1616
add_library_unit_test "test-vector"
17+
add_library_unit_test "test-math"

0 commit comments

Comments
 (0)