Skip to content

Commit 2fcf222

Browse files
math_tests: trig and exponential
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).
1 parent af260c5 commit 2fcf222

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

test/unit/stdlib/math/math.c3

+68-1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,40 @@ fn void! test_ceil() @test
167167
assert(math::ceil(vec) == double[<5>] { -123, 124, 1, 0, 0 });
168168
}
169169

170+
fn void! test_cos() @test
171+
{
172+
int [<5>] in = { 231, 1, 0, -1, -231 };
173+
double [<5>] out = { 0.09280621889587707, 0.54030230586813972 , 1., 0.54030230586813972, 0.09280621889587707 };
174+
assert(@typeis(math::cos(in[0]), double));
175+
assert(@typeis(math::cos((float)in[0]), float));
176+
assert(@typeis(math::cos((double)in[0]), double));
177+
for (int i = 0; i < 5; i++)
178+
{
179+
assert(math::cos(in[i]) == out[i], "cos(%d) is not equal to %f", in[i], out[i]);
180+
assert(math::cos((float)in[i]) == (float)out[i], "cos(%f) is not equal to %f", in[i], out[i]);
181+
assert(math::cos((double)in[i]) == out[i], "cos(%f) is not equal to %f", in[i], out[i]);
182+
}
183+
assert(math::cos(double[<2>] { math::PI, 0. }) == double[<2>] { -1., 1. });
184+
assert(math::cos(float[<2>] { math::PI, 0. }) == float[<2>] { -1.f, 1.f });
185+
}
186+
187+
fn void! test_exp() @test
188+
{
189+
int [<5>] in = { 2, 1, 0, -1, -2 };
190+
double [<5>] out = { 7.389056098930650, math::E , 1., 0.36787944117144233, 0.1353352832366127 };
191+
assert(@typeis(math::exp(in[0]), double));
192+
assert(@typeis(math::exp((float)in[0]), float));
193+
assert(@typeis(math::exp((double)in[0]), double));
194+
for (int i = 0; i < 5; i++)
195+
{
196+
assert(math::exp(in[i]) == out[i], "exp(%d) is not equal to %f", in[i], out[i]);
197+
assert(math::exp((float)in[i]) == (float)out[i], "exp(%f) is not equal to %f", in[i], out[i]);
198+
assert(math::exp((double)in[i]) == out[i], "exp(%f) is not equal to %f", in[i], out[i]);
199+
}
200+
assert(math::exp(double[<2>] { 1., 0. }) == double[<2>] { math::E, 1. });
201+
assert(math::exp(float[<2>] { 1., 0. }) == float[<2>] { math::E, 1.f });
202+
}
203+
170204
fn void! test_floor() @test
171205
{
172206
double d = -123.1;
@@ -221,6 +255,40 @@ fn void! test_sign() @test
221255
assert(@typeis(math::sign(y), uint));
222256
}
223257

258+
fn void! test_sin() @test
259+
{
260+
int [<5>] in = { 231, 1, 0, -1, -231 };
261+
double [<5>] out = { -0.99568418975810324, 0.84147098480789651 , 0., -0.84147098480789651, 0.99568418975810324 };
262+
assert(@typeis(math::sin(in[0]), double));
263+
assert(@typeis(math::sin((float)in[0]), float));
264+
assert(@typeis(math::sin((double)in[0]), double));
265+
for (int i = 0; i < 5; i++)
266+
{
267+
assert(math::sin(in[i]) == out[i], "sin(%d) is not equal to %f", in[i], out[i]);
268+
assert(math::sin((float)in[i]) == (float)out[i], "sin(%f) is not equal to %f", in[i], out[i]);
269+
assert(math::sin((double)in[i]) == out[i], "sin(%f) is not equal to %f", in[i], out[i]);
270+
}
271+
assert(math::sin(double[<2>] { math::PI_2, -math::PI_2 }) == double[<2>] { 1., -1. });
272+
assert(math::sin(float[<2>] { math::PI_2, -math::PI_2 }) == float[<2>] { 1.f, -1.f });
273+
}
274+
275+
fn void! test_tan() @test
276+
{
277+
int [<5>] in = { 231, 1, 0, -1, -231 };
278+
double [<5>] out = { -10.728636524619113, 1.5574077246549022 , 0., -1.5574077246549022, 10.728636524619113 };
279+
assert(@typeis(math::tan(in[0]), double));
280+
assert(@typeis(math::tan((float)in[0]), float));
281+
assert(@typeis(math::tan((double)in[0]), double));
282+
for (int i = 0; i < 5; i++)
283+
{
284+
assert(math::tan(in[i]) == out[i], "tan(%d) is not equal to %f", in[i], out[i]);
285+
assert(math::tan((float)in[i]) == (float)out[i], "tan(%f) is not equal to %f", in[i], out[i]);
286+
assert(math::tan((double)in[i]) == out[i], "tan(%f) is not equal to %f", in[i], out[i]);
287+
}
288+
assert(math::tan(double[<2>] { 0., 0. }) == double[<2>] { 0., 0. });
289+
assert(math::tan(float[<2>] { 0., 0. }) == float[<2>] { 0.f, 0.f });
290+
}
291+
224292
fn void! test_trunc() @test
225293
{
226294
double d = -123.9;
@@ -317,4 +385,3 @@ fn void! test_lcm() @test
317385
assert(math::lcm(11,17) == 11*17);
318386
assert(math::lcm(11,17,227,263) == 11*17*227*263);
319387
}
320-

testrun

-602 KB
Binary file not shown.

0 commit comments

Comments
 (0)