diff options
author | EuAndreh <eu@euandre.org> | 2024-05-24 11:35:20 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-05-24 13:03:56 -0300 |
commit | fef0a487a119a5cbcd2fd95c6a35e0a9c4ab8ac1 (patch) | |
tree | 542e59d44cf82c988e90cc05217cb4b35b22a76d | |
parent | Add some version of vector.c (diff) | |
download | pindaiba-fef0a487a119a5cbcd2fd95c6a35e0a9c4ab8ac1.tar.gz pindaiba-fef0a487a119a5cbcd2fd95c6a35e0a9c4ab8ac1.tar.xz |
src/math.c: Add mul_size()
-rw-r--r-- | deps.mk | 9 | ||||
-rw-r--r-- | src/math.c | 27 | ||||
-rw-r--r-- | src/math.h | 2 | ||||
-rw-r--r-- | tests/math.c | 67 |
4 files changed, 105 insertions, 0 deletions
@@ -7,6 +7,7 @@ sources.c = \ src/i18n.c \ src/lib.c \ src/logerr.c \ + src/math.c \ src/random.c \ src/string.c \ src/testing.c \ @@ -18,6 +19,7 @@ tests.c = \ tests/i18n.c \ tests/lib.c \ tests/logerr.c \ + tests/math.c \ tests/random.c \ tests/string.c \ tests/testing.c \ @@ -28,6 +30,7 @@ src/catalog.o: src/catalog.h src/i18n.o: src/i18n.h src/lib.o: src/lib.h src/logerr.o: src/logerr.h +src/math.o: src/math.h src/random.o: src/random.h src/string.o: src/string.h src/testing.o: src/testing.h @@ -38,6 +41,7 @@ tests/catalog.o: src/catalog.c src/catalog.h tests/i18n.o: src/i18n.c src/i18n.h tests/lib.o: src/lib.c src/lib.h tests/logerr.o: src/logerr.c src/logerr.h +tests/math.o: src/math.c src/math.h tests/random.o: src/random.c src/random.h tests/string.o: src/string.c src/string.h tests/testing.o: src/testing.c src/testing.h @@ -48,6 +52,7 @@ tests/catalog.a: tests/catalog.o tests/i18n.a: tests/i18n.o tests/lib.a: tests/lib.o tests/logerr.a: tests/logerr.o +tests/math.a: tests/math.o tests/random.a: tests/random.o tests/string.a: tests/string.o tests/testing.a: tests/testing.o @@ -58,6 +63,7 @@ tests/catalog.bin-check: tests/catalog.bin tests/i18n.bin-check: tests/i18n.bin tests/lib.bin-check: tests/lib.bin tests/logerr.bin-check: tests/logerr.bin +tests/math.bin-check: tests/math.bin tests/random.bin-check: tests/random.bin tests/string.bin-check: tests/string.bin tests/testing.bin-check: tests/testing.bin @@ -69,6 +75,7 @@ src/catalog.o: src/logerr.h src/i18n.o: src/catalog.h src/lib.o: src/logerr.h src/logerr.o: +src/math.o: src/random.o: src/logerr.h src/string.o: src/testing.o: @@ -79,6 +86,7 @@ tests/catalog.o: src/logerr.h src/testing.h tests/slurp.h tests/i18n.o: src/catalog.h src/logerr.h tests/lib.o: src/logerr.h tests/logerr.o: src/testing.h tests/slurp.h +tests/math.o: src/testing.h tests/random.o: src/logerr.h src/testing.h tests/string.o: tests/testing.o: @@ -89,6 +97,7 @@ tests/catalog.a: src/logerr.o src/testing.o tests/slurp.o tests/i18n.a: src/catalog.o src/logerr.o tests/lib.a: src/logerr.o tests/logerr.a: src/testing.o tests/slurp.o +tests/math.a: src/testing.o tests/random.a: src/logerr.o src/testing.o tests/string.a: tests/testing.a: diff --git a/src/math.c b/src/math.c new file mode 100644 index 0000000..366bd5e --- /dev/null +++ b/src/math.c @@ -0,0 +1,27 @@ +#include "config.h" + +#include <assert.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> + +#include "math.h" + + +int +mul_size(const size_t x, const size_t y, size_t *const out) { + int rc = -1; + + assert(x != 0U); + assert(y != 0U); + + const bool overflows = x > (SIZE_MAX / y); + if (overflows) { + goto out; + } + + *out = x * y; + rc = 0; +out: + return rc; +} diff --git a/src/math.h b/src/math.h new file mode 100644 index 0000000..13f1795 --- /dev/null +++ b/src/math.h @@ -0,0 +1,2 @@ +int +mul_size(const size_t x, const size_t y, size_t *const out); 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; +} |