openMSX
xrange_test.cc
Go to the documentation of this file.
1#include "catch.hpp"
2#include "xrange.hh"
3#include <algorithm>
4#include <type_traits>
5#include <vector>
6
7template<typename T, typename RANGE>
8static void test(const RANGE& range, const std::vector<T>& v)
9{
10 CHECK(std::equal(range.begin(), range.end(), v.begin(), v.end()));
11}
12
13TEST_CASE("xrange")
14{
15 test<int>(xrange( 0), {});
16 test<int>(xrange( 1), {0});
17 test<int>(xrange( 2), {0, 1});
18 test<int>(xrange(10), {0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
19
20 test<int>(xrange(5, 5), {});
21 test<int>(xrange(5, 6), {5});
22 test<int>(xrange(5, 7), {5, 6});
23 test<int>(xrange(5, 10), {5, 6, 7, 8, 9});
24
25 test<unsigned long long>(xrange(0ULL), {});
26 test<unsigned long long>(xrange(1ULL), {0ULL});
27 test<unsigned long long>(xrange(2ULL), {0ULL, 1ULL});
28 test<unsigned long long>(
29 xrange(10ULL),
30 {0ULL, 1ULL, 2ULL, 3ULL, 4ULL, 5ULL, 6ULL, 7ULL, 8ULL, 9ULL});
31
32 test<unsigned long long>(xrange(5ULL, 5ULL), {});
33 test<unsigned long long>(xrange(5ULL, 6ULL), {5ULL});
34 test<unsigned long long>(xrange(5ULL, 7ULL), {5ULL, 6ULL});
35 test<unsigned long long>(
36 xrange(5ULL, 10ULL),
37 {5ULL, 6ULL, 7ULL, 8ULL, 9ULL});
38 test<unsigned long long>(
39 xrange(0x112233445566ULL, 0x112233445568ULL),
40 {0x112233445566ULL, 0x112233445567ULL});
41
42 // undefined behavior
43 //test<int>(xrange(10, 5), {});
44}
45
46TEST_CASE("xrange, random-access")
47{
48 auto r = xrange(20, 45);
49 static_assert(std::is_same_v<decltype(r.begin())::iterator_category,
50 std::random_access_iterator_tag>);
51
52 auto b = r.begin(); REQUIRE(*b == 20);
53 auto m = b + 10;
54
55 auto i1 = b; auto j1 = i1++; CHECK(*i1 == 21); CHECK(*j1 == 20); CHECK(i1 != j1);
56 auto i2 = b; auto j2 = ++i2; CHECK(*i2 == 21); CHECK(*j2 == 21); CHECK(i2 == j2);
57 auto i3 = m; auto j3 = i3--; CHECK(*i3 == 29); CHECK(*j3 == 30); CHECK(i3 != j3);
58 auto i4 = m; auto j4 = --i4; CHECK(*i4 == 29); CHECK(*j4 == 29); CHECK(i4 == j4);
59
60 auto i5 = b; i5 += 7; CHECK(*i5 == 27);
61 auto i6 = m; i6 -= 7; CHECK(*i6 == 23);
62
63 auto i7 = b + 4; CHECK(*i7 == 24);
64 auto i8 = 6 + m; CHECK(*i8 == 36);
65 auto i9 = m - 5; CHECK(*i9 == 25);
66
67 CHECK((m - b) == 10);
68 CHECK(m[4] == 34);
69
70 CHECK(b < m); CHECK(!(b < b));
71 CHECK(b <= m); CHECK( b <= b );
72 CHECK(m > b); CHECK(!(b > b));
73 CHECK(m >= b); CHECK( b >= b );
74}
void test(const IterableBitSet< N > &s, std::initializer_list< size_t > list)
CHECK(m3==m3)
imat4 i4(ivec4(1, 2, 3, 4), ivec4(3, 4, 5, 6), ivec4(5, 6, 7, 8), ivec4(7, 8, 9, 0))
imat4 j4(ivec4(1, 0, 3, 4), ivec4(3, 4, 5, 6), ivec4(5, 6, 7, 8), ivec4(7, 8, 9, 0))
imat3 j3(ivec3(1, 2, 3), ivec3(4, 5, 6), ivec3(7, 0, 9))
imat3 i3(ivec3(1, 2, 3), ivec3(4, 5, 6), ivec3(7, 8, 9))
constexpr auto xrange(T e)
Definition xrange.hh:132
TEST_CASE("xrange")