From fef0a487a119a5cbcd2fd95c6a35e0a9c4ab8ac1 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Fri, 24 May 2024 11:35:20 -0300 Subject: src/math.c: Add mul_size() --- tests/math.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tests/math.c (limited to 'tests/math.c') diff --git a/tests/math.c b/tests/math.c new file mode 100644 index 0000000..34f54ce --- /dev/null +++ b/tests/math.c @@ -0,0 +1,67 @@ +#include "../src/math.c" + +#include "../src/testing.h" + + +static void +test_mul_size(void) { + test_start("mul_size()"); + + size_t val; + { + testing("zero values"); + + assert(mul_size(1U, 1U, &val) == 0); + assert(val == 1U); + + test_ok(); + } + { + testing("small values"); + + assert(mul_size(2U, 3U, &val) == 0); + assert(val == 6U); + + assert(mul_size(10U, 10U, &val) == 0); + assert(val == 100U); + + assert(mul_size(15U, 15U, &val) == 0); + assert(val == 225U); + + test_ok(); + } + { + testing("big values"); + + assert(mul_size(SIZE_MAX, 1U, &val) == 0); + assert(val == SIZE_MAX); + + assert(mul_size(1U, SIZE_MAX, &val) == 0); + assert(val == SIZE_MAX); + + assert(mul_size(SIZE_MAX / 2U, 2U, &val) == 0); + assert(val == SIZE_MAX - 1U); + + test_ok(); + } + { + testing("overflowing values"); + + val = 123U; + + assert(mul_size(SIZE_MAX, 2U, &val) != 0); + assert(val == 123U); + + assert(mul_size(3U, SIZE_MAX / 2U, &val) != 0); + assert(val == 123U); + + test_ok(); + } +} + + +int +main(void) { + test_mul_size(); + return 0; +} -- cgit v1.2.3