summaryrefslogtreecommitdiff
path: root/src/range/slice_test.cpp
blob: 5bd27489396888ff47d8cb5412533c79e51aa3db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "slice.hpp"

#include <gtest/gtest.h>

TEST(slice, slice)
{
    int init[] = {1, 2, 3, 4, 5};

    Slice<int> slice(std::begin(init), std::end(init));
    EXPECT_EQ(slice.data(), init);
    EXPECT_EQ(slice.size(), 5);

    Slice<int> head = slice.slice_h(2);
    Slice<int> tail = slice.slice_t(2);
    EXPECT_EQ(head.size(), 2);
    EXPECT_EQ(tail.size(), 3);
    EXPECT_EQ(head.front(), 1);
    EXPECT_EQ(head.back(), 2);
    EXPECT_EQ(tail.front(), 3);
    EXPECT_EQ(tail.back(), 5);

    head = slice.rslice_h(3);
    tail = slice.rslice_t(3);
    EXPECT_EQ(head.size(), 2);
    EXPECT_EQ(tail.size(), 3);
    EXPECT_EQ(head.front(), 1);
    EXPECT_EQ(head.back(), 2);
    EXPECT_EQ(tail.front(), 3);
    EXPECT_EQ(tail.back(), 5);

    head = slice.islice_h(slice.begin() + 2);
    tail = slice.islice_t(slice.end() - 3);
    EXPECT_EQ(head.size(), 2);
    EXPECT_EQ(tail.size(), 3);
    EXPECT_EQ(head.front(), 1);
    EXPECT_EQ(head.back(), 2);
    EXPECT_EQ(tail.front(), 3);
    EXPECT_EQ(tail.back(), 5);

    tail = slice.lslice(1, 3);
    EXPECT_EQ(tail.size(), 3);
    EXPECT_EQ(tail.front(), 2);
    EXPECT_EQ(tail.back(), 4);

    tail = slice.pslice(1, 4);
    EXPECT_EQ(tail.size(), 3);
    EXPECT_EQ(tail.front(), 2);
    EXPECT_EQ(tail.back(), 4);

    tail = slice.islice(slice.begin() + 1, slice.end() - 1);
    EXPECT_EQ(tail.size(), 3);
    EXPECT_EQ(tail.front(), 2);
    EXPECT_EQ(tail.back(), 4);

    head = slice;
    while (head)
    {
        size_t headsize = head.size();
        EXPECT_EQ(head.back(), headsize);
        EXPECT_EQ(head.pop_back(), headsize);
    }

    tail = slice;
    while (!!tail)
    {
        size_t tailsize = tail.size();
        EXPECT_EQ(tail.front(), 6 - tailsize);
        EXPECT_EQ(tail.pop_front(), 6 - tailsize);
    }
}

TEST(slice, cast)
{
    struct Foo
    {
        int x;
    };
    struct Bar : Foo
    {
    };

    Bar bars[2] = {Bar(), Bar()};

    Slice<Bar> slice(bars, 2);
    Slice<Foo> foos(slice);

    EXPECT_EQ(foos.size(), slice.size());
    EXPECT_EQ(&foos.end()[-1], &slice.end()[-1]);
}