forked from M-Labs/nac3
92 lines
2.1 KiB
C++
92 lines
2.1 KiB
C++
|
#pragma once
|
||
|
|
||
|
#include <irrt_everything.hpp>
|
||
|
#include <test/includes.hpp>
|
||
|
|
||
|
namespace test {
|
||
|
namespace slice {
|
||
|
void test_slice_normal() {
|
||
|
// Normal situation
|
||
|
BEGIN_TEST();
|
||
|
|
||
|
UserSlice user_slice;
|
||
|
user_slice.set_stop(5);
|
||
|
|
||
|
Slice slice = user_slice.indices(100);
|
||
|
|
||
|
printf("%d, %d, %d\n", slice.start, slice.stop, slice.step);
|
||
|
|
||
|
assert_values_match(0, slice.start);
|
||
|
assert_values_match(5, slice.stop);
|
||
|
assert_values_match(1, slice.step);
|
||
|
}
|
||
|
|
||
|
void test_slice_start_too_large() {
|
||
|
// Start is too large and should be clamped to length
|
||
|
BEGIN_TEST();
|
||
|
|
||
|
UserSlice user_slice;
|
||
|
user_slice.set_start(400);
|
||
|
|
||
|
Slice slice = user_slice.indices(100);
|
||
|
|
||
|
assert_values_match(100, slice.start);
|
||
|
assert_values_match(100, slice.stop);
|
||
|
assert_values_match(1, slice.step);
|
||
|
}
|
||
|
|
||
|
void test_slice_negative_start_stop() {
|
||
|
// Negative start/stop should be resolved
|
||
|
BEGIN_TEST();
|
||
|
|
||
|
UserSlice user_slice;
|
||
|
user_slice.set_start(-10);
|
||
|
user_slice.set_stop(-5);
|
||
|
|
||
|
Slice slice = user_slice.indices(100);
|
||
|
|
||
|
assert_values_match(90, slice.start);
|
||
|
assert_values_match(95, slice.stop);
|
||
|
assert_values_match(1, slice.step);
|
||
|
}
|
||
|
|
||
|
void test_slice_only_negative_step() {
|
||
|
// Things like `[::-5]` should be handled correctly
|
||
|
BEGIN_TEST();
|
||
|
|
||
|
UserSlice user_slice;
|
||
|
user_slice.set_step(-5);
|
||
|
|
||
|
Slice slice = user_slice.indices(100);
|
||
|
|
||
|
assert_values_match(99, slice.start);
|
||
|
assert_values_match(-1, slice.stop);
|
||
|
assert_values_match(-5, slice.step);
|
||
|
}
|
||
|
|
||
|
void test_slice_step_zero() {
|
||
|
// Step = 0 is a value error
|
||
|
BEGIN_TEST();
|
||
|
|
||
|
ErrorContext errctx = create_testing_errctx();
|
||
|
|
||
|
UserSlice user_slice;
|
||
|
user_slice.set_start(2);
|
||
|
user_slice.set_stop(12);
|
||
|
user_slice.set_step(0);
|
||
|
|
||
|
Slice slice;
|
||
|
user_slice.indices_checked(&errctx, 100, &slice);
|
||
|
|
||
|
assert_errctx_has_exception(&errctx, errctx.exceptions->value_error);
|
||
|
}
|
||
|
|
||
|
void run() {
|
||
|
test_slice_normal();
|
||
|
test_slice_start_too_large();
|
||
|
test_slice_negative_start_stop();
|
||
|
test_slice_only_negative_step();
|
||
|
test_slice_step_zero();
|
||
|
}
|
||
|
} // namespace slice
|
||
|
} // namespace test
|