TEST: Add tests for Common::upperBound

This commit is contained in:
grisenti 2023-06-01 23:07:41 +02:00
parent 1a696c1d6f
commit 661e9373e3

View file

@ -230,4 +230,44 @@ public:
TS_ASSERT_EQUALS(it, nullptr);
}
}
void test_upper_bound_element_found() {
const auto test = [](const int *first, const int *last, int value, int expected) {
const auto it = Common::upperBound(first, last, value);
TS_ASSERT(first <= it && it < last);
TS_ASSERT_EQUALS(*it, expected);
};
const int one[] = {1};
const int values[] = {2, 3, 4, 10, 50, 100, 900, 1000};
test(one, one + ARRAYSIZE(one), 0, 1);
test(values, values + ARRAYSIZE(values), 1, 2);
test(values, values + ARRAYSIZE(values), 950, 1000);
test(values, values + ARRAYSIZE(values), 20, 50);
}
void test_upper_bound_nothing_found() {
const int values[] = {1, 2, 3, 6, 8, 10, 20, 50};
const auto last = values + ARRAYSIZE(values);
{
const auto it = Common::upperBound(values, last, 50);
TS_ASSERT_EQUALS(it, last);
}
{
const auto it = Common::upperBound(values, last, 100);
TS_ASSERT_EQUALS(it, last);
}
}
void test_upper_bound_empty_input() {
{
const int values[] = {1};
const auto last = values + ARRAYSIZE(values);
const auto it = Common::upperBound(last, last, 1);
TS_ASSERT_EQUALS(it, last);
}
{
const auto it = Common::upperBound((int *)nullptr, (int *)nullptr, 1);
TS_ASSERT_EQUALS(it, nullptr);
}
}
};