Skip to content

Commit

Permalink
math_tests: trig and exponential
Browse files Browse the repository at this point in the history
Tests for the trigonometric macros were written as well as the
exponential macro. The hyperbolic trig macros use the exponential macro
rather than any LLVM instrinsics (for now at least, LLVM 19 has the proper
compiler intrinsics). So for now I personally opted not to write any
tests specifically for the hyperbolic trig functions (unlike the inverse
hyperbolic trig functions).
  • Loading branch information
MendocinoWhiteDeer committed Dec 20, 2024
1 parent accd3f7 commit 25a5d78
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion test/unit/stdlib/math/math.c3
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,40 @@ fn void! test_ceil() @test
assert(math::ceil(vec) == double[<5>] { -123, 124, 1, 0, 0 });
}

fn void! test_cos() @test
{
int [<5>] in = { 231, 1, 0, -1, -231 };
double [<5>] out = { 0.09280621889587707, 0.54030230586813972 , 1., 0.54030230586813972, 0.09280621889587707 };
assert(@typeis(math::cos(in[0]), double));
assert(@typeis(math::cos((float)in[0]), float));
assert(@typeis(math::cos((double)in[0]), double));
for (int i = 0; i < 5; i++)
{
assert(math::cos(in[i]) == out[i], "cos(%d) is not equal to %f", in[i], out[i]);
assert(math::cos((float)in[i]) == (float)out[i], "cos(%f) is not equal to %f", in[i], out[i]);
assert(math::cos((double)in[i]) == out[i], "cos(%f) is not equal to %f", in[i], out[i]);
}
assert(math::cos(double[<2>] { math::PI, 0. }) == double[<2>] { -1., 1. });
assert(math::cos(float[<2>] { math::PI, 0. }) == float[<2>] { -1.f, 1.f });
}

fn void! test_exp() @test
{
int [<5>] in = { 2, 1, 0, -1, -2 };
double [<5>] out = { 7.389056098930650, math::E , 1., 0.36787944117144233, 0.1353352832366127 };
assert(@typeis(math::exp(in[0]), double));
assert(@typeis(math::exp((float)in[0]), float));
assert(@typeis(math::exp((double)in[0]), double));
for (int i = 0; i < 5; i++)
{
assert(math::exp(in[i]) == out[i], "exp(%d) is not equal to %f", in[i], out[i]);
assert(math::exp((float)in[i]) == (float)out[i], "exp(%f) is not equal to %f", in[i], out[i]);
assert(math::exp((double)in[i]) == out[i], "exp(%f) is not equal to %f", in[i], out[i]);
}
assert(math::exp(double[<2>] { 1., 0. }) == double[<2>] { math::E, 1. });
assert(math::exp(float[<2>] { 1., 0. }) == float[<2>] { math::E, 1.f });
}

fn void! test_floor() @test
{
double d = -123.1;
Expand Down Expand Up @@ -221,6 +255,40 @@ fn void! test_sign() @test
assert(@typeis(math::sign(y), uint));
}

fn void! test_sin() @test
{
int [<5>] in = { 231, 1, 0, -1, -231 };
double [<5>] out = { -0.99568418975810324, 0.84147098480789651 , 0., -0.84147098480789651, 0.99568418975810324 };
assert(@typeis(math::sin(in[0]), double));
assert(@typeis(math::sin((float)in[0]), float));
assert(@typeis(math::sin((double)in[0]), double));
for (int i = 0; i < 5; i++)
{
assert(math::sin(in[i]) == out[i], "sin(%d) is not equal to %f", in[i], out[i]);
assert(math::sin((float)in[i]) == (float)out[i], "sin(%f) is not equal to %f", in[i], out[i]);
assert(math::sin((double)in[i]) == out[i], "sin(%f) is not equal to %f", in[i], out[i]);
}
assert(math::sin(double[<2>] { math::PI_2, -math::PI_2 }) == double[<2>] { 1., -1. });
assert(math::sin(float[<2>] { math::PI_2, -math::PI_2 }) == float[<2>] { 1.f, -1.f });
}

fn void! test_tan() @test
{
int [<5>] in = { 231, 1, 0, -1, -231 };
double [<5>] out = { -10.728636524619113, 1.5574077246549022 , 0., -1.5574077246549022, 10.728636524619113 };
assert(@typeis(math::tan(in[0]), double));
assert(@typeis(math::tan((float)in[0]), float));
assert(@typeis(math::tan((double)in[0]), double));
for (int i = 0; i < 5; i++)
{
assert(math::tan(in[i]) == out[i], "tan(%d) is not equal to %f", in[i], out[i]);
assert(math::tan((float)in[i]) == (float)out[i], "tan(%f) is not equal to %f", in[i], out[i]);
assert(math::tan((double)in[i]) == out[i], "tan(%f) is not equal to %f", in[i], out[i]);
}
assert(math::tan(double[<2>] { 0., 0. }) == double[<2>] { 0., 0. });
assert(math::tan(float[<2>] { 0., 0. }) == float[<2>] { 0.f, 0.f });
}

fn void! test_trunc() @test
{
double d = -123.9;
Expand Down Expand Up @@ -317,4 +385,3 @@ fn void! test_lcm() @test
assert(math::lcm(11,17) == 11*17);
assert(math::lcm(11,17,227,263) == 11*17*227*263);
}

0 comments on commit 25a5d78

Please sign in to comment.