Files
dusk/test/util/test_math.c
Dominic Masters aec937b04b
Some checks failed
Build Dusk / build-psp (push) Has been cancelled
Build Dusk / build-linux (push) Has been cancelled
Add some tests
2026-01-05 16:13:14 -06:00

206 lines
7.6 KiB
C

/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "dusktest.h"
#include "util/math.h"
static void test_mathNextPowTwo(void **state) {
(void)state;
// Positive integers
assert_int_equal(mathNextPowTwo(0), 1);
assert_int_equal(mathNextPowTwo(1), 1);
assert_int_equal(mathNextPowTwo(2), 2);
assert_int_equal(mathNextPowTwo(3), 4);
assert_int_equal(mathNextPowTwo(4), 4);
assert_int_equal(mathNextPowTwo(5), 8);
assert_int_equal(mathNextPowTwo(15), 16);
assert_int_equal(mathNextPowTwo(16), 16);
assert_int_equal(mathNextPowTwo(17), 32);
assert_int_equal(mathNextPowTwo(31), 32);
assert_int_equal(mathNextPowTwo(32), 32);
assert_int_equal(mathNextPowTwo(33), 64);
assert_int_equal(mathNextPowTwo(63), 64);
assert_int_equal(mathNextPowTwo(64), 64);
assert_int_equal(mathNextPowTwo(65), 128);
// Large values
assert_int_equal(mathNextPowTwo(1000), 1024);
assert_int_equal(mathNextPowTwo(4095), 4096);
// Zero
assert_int_equal(mathNextPowTwo(0), 1);
// Max Value
assert_int_equal(mathNextPowTwo(0xFFFFFFFF), 1);
}
static void test_mathMax(void **state) {
(void)state;
// Test positive integers
assert_int_equal(mathMax(1, 2), 2);
assert_int_equal(mathMax(2, 1), 2);
// Test negative integers
assert_int_equal(mathMax(-1, -2), -1);
assert_int_equal(mathMax(-4, -2), -2);
// Test 0
assert_int_equal(mathMax(0, -1), 0);
assert_int_equal(mathMax(-1, 0), 0);
// Test large values
assert_int_equal(mathMax(1000000, 999999), 1000000);
assert_int_equal(mathMax(999999, 1000000), 1000000);
// Test mixed negative and positive
assert_int_equal(mathMax(-32, 5), 5);
assert_int_equal(mathMax(5, -32), 5);
// Floats with floats
assert_float_equal(mathMax(1.5f, 2.5f), 2.5f, 0.0001f);
assert_float_equal(mathMax(2.5f, 1.5f), 2.5f, 0.0001f);
assert_float_equal(mathMax(-1.5f, -2.5f), -1.5f, 0.0001f);
assert_float_equal(mathMax(-2.5f, -1.5f), -1.5f, 0.0001f);
assert_float_equal(mathMax(0.0f, -1.5f), 0.0f, 0.0001f);
assert_float_equal(mathMax(-1.5f, 0.0f), 0.0f, 0.0001f);
assert_float_equal(mathMax(500.0f, -333.0f), 500.0f, 0.0001f);
assert_float_equal(mathMax(-333.0f, 500.0f), 500.0f, 0.0001f);
// Floats with integers
assert_float_equal(mathMax(1, 2.5f), 2.5f, 0.0001f);
assert_float_equal(mathMax(2.5f, 1), 2.5f, 0.0001f);
assert_float_equal(mathMax(-1, -2.5f), -1.0f, 0.0001f);
assert_float_equal(mathMax(-2.5f, -1), -1.0f, 0.0001f);
assert_float_equal(mathMax(0, -1.5f), 0.0f, 0.0001f);
assert_float_equal(mathMax(-1.5f, 0), 0.0f, 0.0001f);
assert_float_equal(mathMax(500, -333.5f), 500.0f, 0.0001f);
assert_float_equal(mathMax(-333.5f, 500), 500.0f, 0.0001f);
}
static void test_mathMin(void **state) {
(void)state;
// Positive integers
assert_int_equal(mathMin(1, 2), 1);
assert_int_equal(mathMin(2, 1), 1);
assert_int_equal(mathMin(0, 5), 0);
assert_int_equal(mathMin(5, 0), 0);
// Negative integers
assert_int_equal(mathMin(-1, -2), -2);
assert_int_equal(mathMin(-2, -1), -2);
assert_int_equal(mathMin(-5, -10), -10);
assert_int_equal(mathMin(-10, -5), -10);
// Mixed negative and positive
assert_int_equal(mathMin(-5, 32), -5);
assert_int_equal(mathMin(32, -5), -5);
// Large values
assert_int_equal(mathMin(1000000, 999999), 999999);
assert_int_equal(mathMin(999999, 1000000), 999999);
// Floats with floats
assert_float_equal(mathMin(1.5f, 2.5f), 1.5f, 0.0001f);
assert_float_equal(mathMin(2.5f, 1.5f), 1.5f, 0.0001f);
assert_float_equal(mathMin(-1.5f, -2.5f), -2.5f, 0.0001f);
assert_float_equal(mathMin(-2.5f, -1.5f), -2.5f, 0.0001f);
assert_float_equal(mathMin(0.0f, -1.5f), -1.5f, 0.0001f);
assert_float_equal(mathMin(-1.5f, 0.0f), -1.5f, 0.0001f);
assert_float_equal(mathMin(500.0f, -333.0f), -333.0f, 0.0001f);
assert_float_equal(mathMin(-333.0f, 500.0f), -333.0f, 0.0001f);
// Floats with integers
assert_float_equal(mathMin(1, 2.5f), 1.0f, 0.0001f);
assert_float_equal(mathMin(2.5f, 1), 1.0f, 0.0001f);
assert_float_equal(mathMin(-1, -2.5f), -2.5f, 0.0001f);
assert_float_equal(mathMin(-2.5f, -1), -2.5f, 0.0001f);
assert_float_equal(mathMin(0, -1.5f), -1.5f, 0.0001f);
assert_float_equal(mathMin(-1.5f, 0), -1.5f, 0.0001f);
assert_float_equal(mathMin(500, -333.5f), -333.5f, 0.0001f);
assert_float_equal(mathMin(-333.5f, 500), -333.5f, 0.0001f);
}
static void test_mathClamp(void **state) {
(void)state;
// Positive Integer bounds.
assert_int_equal(mathClamp(5, 1, 10), 5); // Within bounds
assert_int_equal(mathClamp(0, 1, 10), 1); // Below lower bound
assert_int_equal(mathClamp(15, 1, 10), 10); // Above upper bound
// Negative Integer bounds.
assert_int_equal(mathClamp(-5, -10, -1), -5); // Within bounds
assert_int_equal(mathClamp(-15, -10, -1), -10); // Below lower bound
assert_int_equal(mathClamp(0, -10, -1), -1); // Above upper bound
// Mixed Integer bounds.
assert_int_equal(mathClamp(0, -5, 5), 0); // Within bounds
assert_int_equal(mathClamp(-10, -5, 5), -5); // Below lower bound
assert_int_equal(mathClamp(10, -5, 5), 5); // Above upper bound
// Positive Float bounds.
assert_float_equal(mathClamp(5.5f, 1.0f, 10.0f), 5.5f, 0.0001f); // Within
assert_float_equal(mathClamp(0.5f, 1.0f, 10.0f), 1.0f, 0.0001f); // Below
assert_float_equal(mathClamp(15.5f, 1.0f, 10.0f), 10.0f, 0.0001f); // Above
// Negative Float bounds.
assert_float_equal(mathClamp(-5.5f, -10.0f, -1.0f), -5.5f, 0.0001f); // Within
assert_float_equal(mathClamp(-15.5f, -10.0f, -1.0f), -10.0f, 0.0001f);// Below
assert_float_equal(mathClamp(0.0f, -10.0f, -1.0f), -1.0f, 0.0001f); // Above
// Mixed Float bounds.
assert_float_equal(mathClamp(0.0f, -5.0f, 5.0f), 0.0f, 0.0001f); // Within
assert_float_equal(mathClamp(-10.0f, -5.0f, 5.0f), -5.0f, 0.0001f); // Below
assert_float_equal(mathClamp(10.0f, -5.0f, 5.0f), 5.0f, 0.0001f); // Above
// With integers and floats mixed
assert_float_equal(mathClamp(5, 1.0f, 10.0f), 5.0f, 0.0001f); // Within
assert_float_equal(mathClamp(0, 1.0f, 10.0f), 1.0f, 0.0001f); // Below
assert_float_equal(mathClamp(15, 1.0f, 10.0f), 10.0f, 0.0001f); // Above
assert_float_equal(mathClamp(5.5f, 1, 10), 5.5f, 0.0001f); // Within
assert_float_equal(mathClamp(0.5f, 1, 10), 1.0f, 0.0001f); // Below
assert_float_equal(mathClamp(15.5f, 1, 10), 10.0f, 0.0001f); // Above
assert_float_equal(mathClamp(-5, -10.0f, -1.0f), -5.0f, 0.0001f); // Within
assert_float_equal(mathClamp(-15, -10.0f, -1.0f), -10.0f, 0.0001f);// Below
assert_float_equal(mathClamp(0, -10.0f, -1.0f), -1.0f, 0.0001f); // Above
assert_float_equal(mathClamp(-5.5f, -10, -1), -5.5f, 0.0001f); // Within
assert_float_equal(mathClamp(-15.5f, -10, -1), -10.0f, 0.0001f);// Below
assert_float_equal(mathClamp(0.0f, -10, -1), -1.0f, 0.0001f); // Above
}
static void test_mathAbs(void **state) {
(void)state;
// Positive integers
assert_int_equal(mathAbs(5), 5);
assert_int_equal(mathAbs(0), 0);
// Negative integers
assert_int_equal(mathAbs(-5), 5);
assert_int_equal(mathAbs(-100), 100);
// Positive floats
assert_float_equal(mathAbs(5.5f), 5.5f, 0.0001f);
assert_float_equal(mathAbs(0.0f), 0.0f, 0.0001f);
// Negative floats
assert_float_equal(mathAbs(-5.5f), 5.5f, 0.0001f);
assert_float_equal(mathAbs(-100.25f), 100.25f, 0.0001f);
}
int main(int argc, char **argv) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_mathNextPowTwo),
cmocka_unit_test(test_mathMax),
cmocka_unit_test(test_mathMin),
cmocka_unit_test(test_mathClamp),
cmocka_unit_test(test_mathAbs),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}