Line data Source code
1 : // Copyright 2024 Accenture.
2 :
3 : #include "estd/slice.h"
4 :
5 : #include "estd/array.h"
6 : #include "estd/assert.h"
7 : #include "estd/vector.h"
8 :
9 : #include <gtest/gtest.h>
10 :
11 : #pragma GCC diagnostic ignored "-Wunused-variable"
12 :
13 : // clang-format off
14 : namespace
15 : {
16 1 : void example_construction()
17 : {
18 1 : {
19 : // [EXAMPLE_BEGIN estd static construction from C array]
20 1 : uint8_t data[] = {0, 1, 2, 3, 4};
21 :
22 : // statically-sized slice of size 5
23 1 : ::estd::slice<uint8_t, 5U> slice5(data);
24 :
25 : // statically-sized slice of size 3 which points to the first three elements
26 1 : ::estd::slice<uint8_t, 3U> slice3(data);
27 : // [EXAMPLE_END estd static construction from C array]
28 : }
29 :
30 1 : {
31 : // [EXAMPLE_BEGIN estd dynamic construction from C array]
32 1 : uint8_t data[] = {0, 1, 2, 3, 4};
33 :
34 : // dynamically-sized slice of size 5
35 1 : ::estd::slice<uint8_t> dynSizeSlice(data);
36 :
37 : // dynamically-sized slice can be constructed from a statically sized slice:
38 1 : ::estd::slice<uint8_t, 5U> slice5(data);
39 :
40 : // [EXAMPLE_END estd dynamic construction from C array]
41 : }
42 :
43 1 : {
44 : // [EXAMPLE_BEGIN estd construction from an estd array]
45 1 : ::estd::array<int32_t, 4U> data = {0, 1, 2, 3};
46 1 : ::estd::slice<int32_t, 4U> slice4(data);
47 : // EXAMPLE_END estd construction from an estd array
48 : }
49 :
50 1 : {
51 : // [EXAMPLE_BEGIN estd construction from pointer]
52 1 : uint8_t data[] = {0, 1, 2, 3, 4};
53 1 : uint8_t* pointer = data;
54 1 : ::estd::slice<uint8_t, 5U> slice5_fp = ::estd::slice<uint8_t, 5U>::from_pointer(pointer);
55 : // [EXAMPLE_END estd construction from pointer]
56 : }
57 :
58 1 : {
59 : // [EXAMPLE_BEGIN estd dynamic construction from estd vector]
60 1 : ::estd::declare::vector<int32_t, 10> v1;
61 1 : ::estd::slice<int32_t> s1(v1);
62 :
63 1 : ::estd::declare::vector<int32_t, 20> v2;
64 1 : ::estd::vector<int32_t>& v3 = v2;
65 1 : ::estd::slice<int32_t> s3(v3);
66 : // [EXAMPLE_END estd dynamic construction from estd vector]
67 1 : }
68 :
69 : #if 0 // this won't compile, therefore disabled
70 : // [EXAMPLE_BEGIN estd array too small]
71 : ::estd::slice<uint8_t, 6U> slice6(data); // won't compile - array is smaller than the size of slice
72 : // [EXAMPLE_END estd array too small]
73 : #endif
74 1 : }
75 :
76 1 : void example_make_slice()
77 : {
78 : // [EXAMPLE_BEGIN estd make_slice]
79 :
80 1 : uint32_t data[] = {0, 1, 2, 3, 4};
81 1 : auto staticSlice = ::estd::make_static_slice(data); // staticSlice has type slice<uint32_t, 5>
82 :
83 : // [EXAMPLE_END estd make_slice]
84 : }
85 :
86 1 : void example_iterate()
87 : {
88 : // [EXAMPLE_BEGIN estd iterate]
89 1 : uint32_t data[] = {0, 1, 2, 3, 4};
90 1 : ::estd::slice<uint32_t> slice5{data};
91 :
92 : // iterate over all elements, incrementing each of them:
93 1 : for (auto& i : slice5)
94 : {
95 : ++i;
96 : }
97 :
98 : // iterate over elements in reverse order
99 1 : for (auto rev_it = slice5.rbegin(); rev_it != slice5.rend(); ++rev_it)
100 : {
101 : *rev_it = *rev_it + 10;
102 : }
103 :
104 : // [EXAMPLE_END estd iterate]
105 : }
106 :
107 1 : void example_access_at()
108 : {
109 : // [EXAMPLE_BEGIN estd access_at]
110 1 : uint8_t data[] = {0, 1, 2, 3, 4};
111 1 : ::estd::slice<uint8_t, 5> const slice5(data);
112 :
113 1 : slice5.at<0>() = 42; // assign 42 to the first element
114 :
115 : // [EXAMPLE_END estd access_at]
116 : }
117 :
118 1 : void example_subviews()
119 : {
120 : // [EXAMPLE_BEGIN estd subviews]
121 1 : uint8_t data[] = {0, 1, 2, 3, 4};
122 1 : ::estd::slice<uint8_t> const slice(data);
123 :
124 : // [EXAMPLE_END estd subviews]
125 : }
126 :
127 1 : void example_subviews_static()
128 : {
129 : // [EXAMPLE_BEGIN estd subviews_static]
130 1 : uint8_t data[] = {0, 1, 2, 3, 4};
131 1 : ::estd::slice<uint8_t, 5U> const slice5(data);
132 1 : ::estd::slice<uint8_t, 4U> slice4 = slice5.offset<1>(); // skip 1 element: {1, 2, 3, 4}
133 1 : ::estd::slice<uint8_t, 3U> slice3 = slice5.subslice<3>(); // first 3 elements: {0, 1, 2}
134 : // [EXAMPLE_END estd subviews_static]
135 : }
136 :
137 1 : void example_modify()
138 : {
139 : // [EXAMPLE_BEGIN estd modify]
140 1 : uint8_t data[] = {0, 1, 2, 3, 4};
141 1 : ::estd::slice<uint8_t> slice(data);
142 :
143 1 : slice.advance(1U); // slice's pointer incremented by one, now points to {1, 2, 3, 4}
144 1 : slice.trim(2U); // slice's size decreased to 2, now points to {1, 2}
145 : // [EXAMPLE_END estd modify]
146 : }
147 :
148 : } // namespace
149 :
150 3 : TEST(Slice, run_examples)
151 : {
152 1 : example_construction();
153 1 : example_make_slice();
154 1 : example_iterate();
155 1 : example_subviews();
156 1 : example_subviews_static();
157 1 : example_modify();
158 1 : example_access_at();
159 1 : }
160 :
161 : // clang-format off
|