blob: 619952f15f796dc826f18c65bdb51c88ea37e93c [file] [log] [blame]
Matt Spinler113ad282019-07-09 14:44:13 -05001#include "extensions/openpower-pels/stream.hpp"
2
3#include <iostream>
4
5#include <gtest/gtest.h>
6
7using namespace openpower::pels;
8
9TEST(StreamTest, TestExtract)
10{
11 std::vector<uint8_t> data{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
12 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
13 0x08, 'h', 'e', 'l', 'l', 'o'};
14 Stream stream{data};
15
16 {
17 uint8_t v;
18 stream >> v;
19 EXPECT_EQ(v, 0x11);
20 }
21 {
22 uint16_t v;
23 stream >> v;
24 EXPECT_EQ(v, 0x2233);
25 }
26 {
27 uint32_t v;
28 stream >> v;
29 EXPECT_EQ(v, 0x44556677);
30 }
31 {
32 uint64_t v;
33 stream >> v;
34 EXPECT_EQ(v, 0x0102030405060708);
35 }
36 {
37 char v[6] = {0};
38 stream.read(v, 5);
39 EXPECT_EQ(memcmp(v, "hello", 5), 0);
40 }
41
42 EXPECT_EQ(stream.remaining(), 0);
43
44 // At the end, so should throw.
45 uint8_t v;
46 EXPECT_THROW(stream >> v, std::out_of_range);
47}
48
49TEST(StreamTest, InputTestNoExpansion)
50{
51 std::vector<uint8_t> data(256, 0);
52 Stream stream(data);
53 uint8_t v1 = 0x11;
54 uint16_t v2 = 0x2233;
55 uint64_t v3 = 0x445566778899AABB;
56 uint32_t v4 = 0xCCDDEEFF;
57
58 stream << v3 << v2 << v4 << v1;
59
60 uint8_t e1;
61 uint16_t e2;
62 uint64_t e3;
63 uint32_t e4;
64
65 stream.offset(0);
66 stream >> e3 >> e2 >> e4 >> e1;
67
68 EXPECT_EQ(v1, e1);
69 EXPECT_EQ(v2, e2);
70 EXPECT_EQ(v3, e3);
71 EXPECT_EQ(v4, e4);
72}
73
74TEST(StreamTest, InputTestExpansion)
75{
76 // The stream will expand the underlying vector
77 std::vector<uint8_t> data;
78 Stream stream(data);
79
80 uint32_t v1 = 0xAABBCCDD;
81 stream << v1;
82
83 stream.offset(0);
84 uint32_t e1;
85 stream >> e1;
86 EXPECT_EQ(data.size(), 4);
87 EXPECT_EQ(v1, e1);
88
89 stream.offset(2);
90
91 uint64_t v2 = 0x0102030405060708;
92 stream << v2;
93
94 EXPECT_EQ(data.size(), 10);
95 uint64_t e2;
96 stream.offset(2);
97 stream >> e2;
98
99 EXPECT_EQ(v2, e2);
100
101 auto origSize = data.size();
102 uint8_t v3 = 0xCC;
103 stream << v3;
104
105 EXPECT_EQ(origSize + 1, data.size());
106 stream.offset(stream.offset() - 1);
107 uint8_t e3;
108 stream >> e3;
109 EXPECT_EQ(v3, e3);
110}
111
112TEST(StreamTest, ReadWriteTest)
113{
114 std::vector<uint8_t> data{0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
115 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc};
116 Stream stream{data};
117 uint8_t buf[data.size()];
118
119 stream.read(buf, data.size());
120
121 for (size_t i = 0; i < data.size(); i++)
122 {
123 EXPECT_EQ(buf[i], data[i]);
124
125 // for the next test
126 buf[i] = 0x20 + i;
127 }
128
129 stream.offset(6);
130 stream.write(buf, 6);
131 for (size_t i = 0; i < 6; i++)
132 {
133 EXPECT_EQ(buf[i], data[i + 6]);
134 }
135}
136
137TEST(StreamTest, TestOffsets)
138{
139 std::vector<uint8_t> data{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
140 Stream stream{data, 3};
141
142 {
143 uint8_t v;
144 stream >> v;
145 EXPECT_EQ(v, 0x44);
146 EXPECT_EQ(stream.offset(), 4);
147 }
148
149 stream.offset(6);
150
151 {
152 uint8_t v;
153 stream >> v;
154 EXPECT_EQ(v, 0x77);
155 EXPECT_EQ(stream.offset(), 7);
156 EXPECT_EQ(stream.remaining(), 0);
157 }
158
159 EXPECT_THROW(stream.offset(100), std::out_of_range);
160}
Matt Spinlerd3777932019-09-24 13:38:47 -0500161
162TEST(StreamTest, TestVectorInsertExtract)
163{
164 std::vector<uint8_t> toInsert{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
165 std::vector<uint8_t> data;
166
167 // Insert
168 Stream stream{data};
169 stream << toInsert;
170 EXPECT_EQ(data, toInsert);
171
172 // Extract
173 std::vector<uint8_t> toExtract;
174 toExtract.resize(toInsert.size());
175 stream.offset(0);
176 stream >> toExtract;
177
178 EXPECT_EQ(data, toExtract);
179
180 // Go off the end
181 EXPECT_THROW(stream >> toExtract, std::out_of_range);
182}