summaryrefslogtreecommitdiff
path: root/source/test/unittests/lower_bound.test.c
diff options
context:
space:
mode:
authorMitya Selivanov <0x7fffff@guattari.ru>2022-08-07 08:33:25 +0400
committerMitya Selivanov <0x7fffff@guattari.ru>2022-08-07 08:33:25 +0400
commitfd458396b376d0f9fff3dd5a03b1ef95c8cf870e (patch)
treed9418d90e288695bf54735419fd24bbe9e558426 /source/test/unittests/lower_bound.test.c
parentd9e0da408325e36852ca80643cb25fa9b3717889 (diff)
downloadkit-fd458396b376d0f9fff3dd5a03b1ef95c8cf870e.zip
Lower bound
Diffstat (limited to 'source/test/unittests/lower_bound.test.c')
-rw-r--r--source/test/unittests/lower_bound.test.c178
1 files changed, 178 insertions, 0 deletions
diff --git a/source/test/unittests/lower_bound.test.c b/source/test/unittests/lower_bound.test.c
new file mode 100644
index 0000000..275acd6
--- /dev/null
+++ b/source/test/unittests/lower_bound.test.c
@@ -0,0 +1,178 @@
+#include "../../kit/lower_bound.h"
+#include "../../kit/array_ref.h"
+
+#define KIT_TEST_FILE lower_bound
+#include "../../kit_test/test.h"
+
+static int kit_less_int(int left, int right) {
+ return left < right;
+}
+
+static int kit_less_int_ref(int const *left, int const *right) {
+ return *left < *right;
+}
+
+TEST("lower bound empty") {
+ AR_CONST(ref, int) = { .size = 0, .values = NULL };
+
+ ptrdiff_t index;
+ KIT_LOWER_BOUND(index, ref, 42, kit_less_int);
+ REQUIRE(index == 0);
+}
+
+TEST("lower bound single left") {
+ int const v[1] = { 42 };
+ AR_CONST(ref, int) = { .size = 1, .values = v };
+
+ ptrdiff_t index;
+ KIT_LOWER_BOUND(index, ref, 42, kit_less_int);
+ REQUIRE(index == 0);
+}
+
+TEST("lower bound single right") {
+ int const v[1] = { 42 };
+ AR_CONST(ref, int) = { .size = 1, .values = v };
+
+ ptrdiff_t index;
+ KIT_LOWER_BOUND(index, ref, 43, kit_less_int);
+ REQUIRE(index == 1);
+}
+
+TEST("lower bound first of four") {
+ int const v[4] = { 1, 2, 3, 4 };
+ AR_CONST(ref, int) = { .size = 4, .values = v };
+
+ ptrdiff_t index;
+ KIT_LOWER_BOUND(index, ref, 1, kit_less_int);
+ REQUIRE(index == 0);
+}
+
+TEST("lower bound second of four") {
+ int const v[4] = { 1, 2, 3, 4 };
+ AR_CONST(ref, int) = { .size = 4, .values = v };
+
+ ptrdiff_t index;
+ KIT_LOWER_BOUND(index, ref, 2, kit_less_int);
+ REQUIRE(index == 1);
+}
+
+TEST("lower bound third of four") {
+ int const v[4] = { 1, 2, 3, 4 };
+ AR_CONST(ref, int) = { .size = 4, .values = v };
+
+ ptrdiff_t index;
+ KIT_LOWER_BOUND(index, ref, 3, kit_less_int);
+ REQUIRE(index == 2);
+}
+
+TEST("lower bound forth of four") {
+ int const v[4] = { 1, 2, 3, 4 };
+ AR_CONST(ref, int) = { .size = 4, .values = v };
+
+ ptrdiff_t index;
+ KIT_LOWER_BOUND(index, ref, 4, kit_less_int);
+ REQUIRE(index == 3);
+}
+
+TEST("lower bound fifth of four") {
+ int const v[4] = { 1, 2, 3, 4 };
+ AR_CONST(ref, int) = { .size = 4, .values = v };
+
+ ptrdiff_t index;
+ KIT_LOWER_BOUND(index, ref, 5, kit_less_int);
+ REQUIRE(index == 4);
+}
+
+TEST("lower bound first of five") {
+ int const v[5] = { 1, 2, 3, 4, 5 };
+ AR_CONST(ref, int) = { .size = 5, .values = v };
+
+ ptrdiff_t index;
+ KIT_LOWER_BOUND(index, ref, 1, kit_less_int);
+ REQUIRE(index == 0);
+}
+
+TEST("lower bound second of five") {
+ int const v[5] = { 1, 2, 3, 4, 5 };
+ AR_CONST(ref, int) = { .size = 5, .values = v };
+
+ ptrdiff_t index;
+ KIT_LOWER_BOUND(index, ref, 2, kit_less_int);
+ REQUIRE(index == 1);
+}
+
+TEST("lower bound third of five") {
+ int const v[5] = { 1, 2, 3, 4, 5 };
+ AR_CONST(ref, int) = { .size = 5, .values = v };
+
+ ptrdiff_t index;
+ KIT_LOWER_BOUND(index, ref, 3, kit_less_int);
+ REQUIRE(index == 2);
+}
+
+TEST("lower bound forth of five") {
+ int const v[5] = { 1, 2, 3, 4, 5 };
+ AR_CONST(ref, int) = { .size = 5, .values = v };
+
+ ptrdiff_t index;
+ KIT_LOWER_BOUND(index, ref, 4, kit_less_int);
+ REQUIRE(index == 3);
+}
+
+TEST("lower bound fifth of five") {
+ int const v[5] = { 1, 2, 3, 4, 5 };
+ AR_CONST(ref, int) = { .size = 5, .values = v };
+
+ ptrdiff_t index;
+ KIT_LOWER_BOUND(index, ref, 5, kit_less_int);
+ REQUIRE(index == 4);
+}
+
+TEST("lower bound sixth of five") {
+ int const v[5] = { 1, 2, 3, 4, 5 };
+ AR_CONST(ref, int) = { .size = 5, .values = v };
+
+ ptrdiff_t index;
+ KIT_LOWER_BOUND(index, ref, 6, kit_less_int);
+ REQUIRE(index == 5);
+}
+
+TEST("lower bound ref first of four") {
+ int const v[4] = { 1, 2, 3, 4 };
+ int const value = 1;
+ AR_CONST(ref, int) = { .size = 4, .values = v };
+
+ ptrdiff_t index;
+ KIT_LOWER_BOUND_REF(index, ref, value, kit_less_int_ref);
+ REQUIRE(index == 0);
+}
+
+TEST("lower bound ref second of four") {
+ int const v[4] = { 1, 2, 3, 4 };
+ int const value = 2;
+ AR_CONST(ref, int) = { .size = 4, .values = v };
+
+ ptrdiff_t index;
+ KIT_LOWER_BOUND_REF(index, ref, value, kit_less_int_ref);
+ REQUIRE(index == 1);
+}
+
+TEST("lower bound ref fifth of five") {
+ int const v[5] = { 1, 2, 3, 4, 5 };
+ int const value = 5;
+ AR_CONST(ref, int) = { .size = 5, .values = v };
+
+ ptrdiff_t index;
+ KIT_LOWER_BOUND_REF(index, ref, value, kit_less_int_ref);
+ REQUIRE(index == 4);
+}
+
+TEST("lower bound ref sixth of five") {
+ int const v[5] = { 1, 2, 3, 4, 5 };
+ int const value = 6;
+ AR_CONST(ref, int) = { .size = 5, .values = v };
+
+ ptrdiff_t index;
+ KIT_LOWER_BOUND_REF(index, ref, value, kit_less_int_ref);
+ REQUIRE(index == 5);
+}