Skip to content

Commit 0ba866e

Browse files
math_tests: trig and exponential
Wrote tests for the trigonometric macros were 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).
1 parent cb0eba5 commit 0ba866e

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

test/unit/stdlib/math/math.c3

+69
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,41 @@ fn void! test_sign() @test
221255
assert(@typeis(math::sign(y), uint));
222256
}
223257

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

0 commit comments

Comments
 (0)