Skip to content

Commit

Permalink
Add unit test for math.c
Browse files Browse the repository at this point in the history
  • Loading branch information
DreamPearl committed Oct 24, 2021
1 parent 32c933f commit 8abb49c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/usr/include/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ int isnan(double x);
double floor(double x);
double round(double x);
double fmod(double x, double y);
double fabs(double x);

double sin(double x);
double cos(double x);
Expand Down
2 changes: 2 additions & 0 deletions src/usr/lib/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ double round(double x) { return floor(x + 0.5); }

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

double fabs(double x) { return (x >= 0) ? x : -x; }

double sin(double x) {
const int terms = 32;
const double x2 = x * x;
Expand Down
68 changes: 68 additions & 0 deletions src/usr/local/src/test-math.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <math.h>
#include <stdio.h>
#include <testing.h>

double EPSILON = 1e-9;

double check_double_equal(double x, double y) {
return std::fabs(x - y) < EPSILON;
}

double to_radian(double x) { return x * (M_PI / 180); }

void test_isnan() {
ASSERT_TRUE(std::isnan(16.0) == 0);
ASSERT_TRUE(std::isnan(-16.0) == 0);
ASSERT_TRUE(std::isnan(0.0 / 0.0) != 0);
ASSERT_TRUE(std::isnan(1.0 / 0.0) == 0);
}

void test_floor() {
ASSERT_TRUE(check_double_equal(std::floor(16.0), 16.0));
ASSERT_TRUE(check_double_equal(std::floor(16.6), 16.0));
ASSERT_TRUE(check_double_equal(std::floor(-16.0), -16.0));
ASSERT_TRUE(check_double_equal(std::floor(-16.9), -17));
}

void test_round() {
ASSERT_TRUE(check_double_equal(std::round(16.3), 16.0));
ASSERT_TRUE(check_double_equal(std::round(16.6), 17.0));
ASSERT_TRUE(check_double_equal(std::round(-16.0), -16.0));
ASSERT_TRUE(check_double_equal(std::round(-16.9), -17));
}

void test_fmod() {
ASSERT_TRUE(check_double_equal(std::fmod(1.6, 1.2), 0.4));
ASSERT_TRUE(check_double_equal(std::fmod(7.0, 5.0), 2.0));
}

void test_fabs() {
ASSERT_TRUE(check_double_equal(std::fabs(1.6), 1.6));
ASSERT_TRUE(check_double_equal(std::fabs(-7.7), 7.7));
ASSERT_TRUE(check_double_equal(std::fabs(0.0), 0.0));
}

void test_sin() {
ASSERT_TRUE(check_double_equal(std::sin(to_radian(45.0)), 0.7071067812));
ASSERT_TRUE(check_double_equal(std::sin(to_radian(90.0)), 1.0));
}

void test_cos() {
ASSERT_TRUE(check_double_equal(std::cos(to_radian(60.0)), 0.5));
ASSERT_TRUE(check_double_equal(std::cos(to_radian(90.0)), 0.0));
}

int main(int argc, char *argv[]) {
TEST_INIT();

RUN_TEST(test_isnan);
RUN_TEST(test_floor);
RUN_TEST(test_round);
RUN_TEST(test_fmod);
RUN_TEST(test_fabs);
RUN_TEST(test_sin);
RUN_TEST(test_cos);

TEST_SUMMARY();
return 0;
}
1 change: 1 addition & 0 deletions tests/app/libraries/usr_lib_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ function add_library_unit_test {
add_library_unit_test "test-string"
add_library_unit_test "test-stdlib"
add_library_unit_test "test-vector"
add_library_unit_test "test-math"

0 comments on commit 8abb49c

Please sign in to comment.