Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / rive-cpp / test / span_test.cpp
1 /*
2  * Copyright 2022 Rive
3  */
4
5 #include <rive/span.hpp>
6 #include <catch.hpp>
7 #include <cstdio>
8 #include <vector>
9
10 using namespace rive;
11
12 TEST_CASE("basics", "[span]") {
13     Span<int> span;
14     REQUIRE(span.empty());
15     REQUIRE(span.size() == 0);
16     REQUIRE(span.size_bytes() == 0);
17     REQUIRE(span.begin() == span.end());
18
19     int array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
20
21     span = {array, 4};
22     REQUIRE(!span.empty());
23     REQUIRE(span.data() == array);
24     REQUIRE(span.size() == 4);
25     REQUIRE(span.size_bytes() == 4 * sizeof(int));
26     REQUIRE(span.begin() + span.size() == span.end());
27
28     int counter = 0;
29     int sum = 0;
30     for (auto s : span) {
31         counter += 1;
32         sum += s;
33     }
34     REQUIRE(counter == 4);
35     REQUIRE(sum == 0 + 1 + 2 + 3);
36
37     auto sub = span.subset(1, 2);
38     REQUIRE(!sub.empty());
39     REQUIRE(sub.data() == array + 1);
40     REQUIRE(sub.size() == 2);
41
42     sub = sub.subset(1, 0);
43     REQUIRE(sub.empty());
44     REQUIRE(sub.size() == 0);
45 }
46
47 static void funca(Span<int> span) {}
48 static void funcb(Span<const int> span) {}
49
50 TEST_CASE("const-and-containers", "[span]") {
51     const int carray[] = {1, 2, 3, 4};
52     funcb({carray, 4});
53
54     int array[] = {1, 2, 3, 4};
55     funca({array, 4});
56     funcb({array, 4});
57
58     std::vector<int> v;
59     funca(toSpan(v));
60     funcb(toSpan(v));
61 }