diff options
author | EuAndreh <eu@euandre.org> | 2024-06-14 14:07:05 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-06-14 14:07:05 -0300 |
commit | f28724811ceea6b95b471774665845c916626d68 (patch) | |
tree | c7f4fa1d88a0216e84bbbfaee20d3c6008619c61 | |
parent | src/trace.h: Init tracing functions (diff) | |
download | pindaiba-f28724811ceea6b95b471774665845c916626d68.tar.gz pindaiba-f28724811ceea6b95b471774665845c916626d68.tar.xz |
src/cmp.{c,h}: Add minimal implementation of cmp_size()
-rw-r--r-- | deps.mk | 9 | ||||
-rw-r--r-- | src/cmp.c | 20 | ||||
-rw-r--r-- | src/cmp.h | 10 | ||||
-rw-r--r-- | tests/cmp.c | 47 |
4 files changed, 86 insertions, 0 deletions
@@ -4,6 +4,7 @@ catalogs.msg = $(catalogs.en.msg) sources.c = \ src/catalog.c \ + src/cmp.c \ src/hash.c \ src/i18n.c \ src/lib.c \ @@ -20,6 +21,7 @@ sources.c = \ tests.c = \ tests/catalog.c \ + tests/cmp.c \ tests/hash.c \ tests/i18n.c \ tests/lib.c \ @@ -35,6 +37,7 @@ tests.c = \ tests/vector.c \ src/catalog.o: src/catalog.h +src/cmp.o: src/cmp.h src/hash.o: src/hash.h src/i18n.o: src/i18n.h src/lib.o: src/lib.h @@ -50,6 +53,7 @@ src/util.o: src/util.h src/vector.o: src/vector.h tests/catalog.o: src/catalog.c src/catalog.h +tests/cmp.o: src/cmp.c src/cmp.h tests/hash.o: src/hash.c src/hash.h tests/i18n.o: src/i18n.c src/i18n.h tests/lib.o: src/lib.c src/lib.h @@ -65,6 +69,7 @@ tests/util.o: src/util.c src/util.h tests/vector.o: src/vector.c src/vector.h tests/catalog.a: tests/catalog.o +tests/cmp.a: tests/cmp.o tests/hash.a: tests/hash.o tests/i18n.a: tests/i18n.o tests/lib.a: tests/lib.o @@ -80,6 +85,7 @@ tests/util.a: tests/util.o tests/vector.a: tests/vector.o tests/catalog.bin-check: tests/catalog.bin +tests/cmp.bin-check: tests/cmp.bin tests/hash.bin-check: tests/hash.bin tests/i18n.bin-check: tests/i18n.bin tests/lib.bin-check: tests/lib.bin @@ -96,6 +102,7 @@ tests/vector.bin-check: tests/vector.bin src/catalog.o: src/logerr.h +src/cmp.o: src/hash.o: src/logerr.h src/random.h src/i18n.o: src/catalog.h src/lib.o: src/logerr.h @@ -111,6 +118,7 @@ src/util.o: src/logerr.h src/vector.o: src/catalog.h src/i18n.h src/logerr.h src/math.h src/util.h tests/catalog.o: src/logerr.h src/testing.h src/util.h tests/slurp.h +tests/cmp.o: src/testing.h tests/hash.o: src/logerr.h src/random.h src/testing.h tests/i18n.o: src/catalog.h src/logerr.h tests/lib.o: src/logerr.h @@ -126,6 +134,7 @@ tests/util.o: src/logerr.h src/testing.h tests/slurp.h tests/vector.o: src/catalog.h src/i18n.h src/logerr.h src/math.h src/util.h src/testing.h tests/catalog.a: src/logerr.o src/testing.o src/util.o tests/slurp.o +tests/cmp.a: src/testing.o tests/hash.a: src/logerr.o src/random.o src/testing.o src/util.o tests/i18n.a: src/catalog.o src/logerr.o tests/lib.a: src/logerr.o diff --git a/src/cmp.c b/src/cmp.c new file mode 100644 index 0000000..9784aae --- /dev/null +++ b/src/cmp.c @@ -0,0 +1,20 @@ +#include "config.h" + +#include <stddef.h> + +#include "cmp.h" + + + +enum Comparison +cmp_size(const size_t a, const size_t b) { + if (a > b) { + return Comparison_GREATER; + } + + if (a < b) { + return Comparison_LESSER; + } + + return Comparison_EQUAL; +} diff --git a/src/cmp.h b/src/cmp.h new file mode 100644 index 0000000..0fbfe39 --- /dev/null +++ b/src/cmp.h @@ -0,0 +1,10 @@ +enum Comparison { + Comparison_LESSER, + Comparison_EQUAL, + Comparison_GREATER, +}; + + + +enum Comparison +cmp_size(const size_t a, const size_t b); diff --git a/tests/cmp.c b/tests/cmp.c new file mode 100644 index 0000000..943402f --- /dev/null +++ b/tests/cmp.c @@ -0,0 +1,47 @@ +#include "../src/cmp.c" + +#include <assert.h> +#include <stdint.h> + +#include "../src/testing.h" + + + +static void +test_cmp_size(void) { + test_start("cmp_size()"); + + { + testing("equal values"); + + assert(cmp_size(0U, 0U) == Comparison_EQUAL); + assert(cmp_size(SIZE_MAX, SIZE_MAX) == Comparison_EQUAL); + assert(cmp_size(123U, 122U + 1U) == Comparison_EQUAL); + + test_ok(); + } + { + testing("greater values"); + + assert(cmp_size(1U, 0U) == Comparison_GREATER); + assert(cmp_size(1234U, 1222U) == Comparison_GREATER); + assert(cmp_size(SIZE_MAX, SIZE_MAX - 1U) == Comparison_GREATER); + + test_ok(); + } + { + testing("lesser values"); + + assert(cmp_size(0U, 1U) == Comparison_LESSER); + assert(cmp_size(12U, 34U) == Comparison_LESSER); + assert(cmp_size(0U, SIZE_MAX) == Comparison_LESSER); + + test_ok(); + } +} + +int +main(void) { + test_cmp_size(); + return 0; +} |