blob: 12640b125e753224d9501ec0ba6e13d10b881c16 [file] [log] [blame]
Ben Clayton63eb5c82023-11-22 21:11:19 +00001// Copyright 2023 The Dawn & Tint Authors
2//
3// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
5//
6// 1. Redistributions of source code must retain the above copyright notice, this
7// list of conditions and the following disclaimer.
8//
9// 2. Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12//
13// 3. Neither the name of the copyright holder nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "src/tint/utils/bytes/decoder.h"
29
30#include <string>
31#include <tuple>
32#include <unordered_map>
33#include <utility>
34
35#include "gmock/gmock.h"
36
37namespace tint::bytes {
38namespace {
39
40template <typename... ARGS>
41auto Data(ARGS&&... args) {
42 return std::array{std::byte{static_cast<uint8_t>(args)}...};
43}
44
45TEST(BytesDecoderTest, Uint8) {
46 auto data = Data(0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80);
Ben Clayton0a8dc812023-12-04 20:52:16 +000047 auto reader = BufferReader{Slice{data}};
Ben Clayton63eb5c82023-11-22 21:11:19 +000048 EXPECT_EQ(Decode<uint8_t>(reader).Get(), 0x10u);
49 EXPECT_EQ(Decode<uint8_t>(reader).Get(), 0x20u);
50 EXPECT_EQ(Decode<uint8_t>(reader).Get(), 0x30u);
51 EXPECT_EQ(Decode<uint8_t>(reader).Get(), 0x40u);
52 EXPECT_EQ(Decode<uint8_t>(reader).Get(), 0x50u);
53 EXPECT_EQ(Decode<uint8_t>(reader).Get(), 0x60u);
54 EXPECT_EQ(Decode<uint8_t>(reader).Get(), 0x70u);
55 EXPECT_EQ(Decode<uint8_t>(reader).Get(), 0x80u);
56 EXPECT_FALSE(Decode<uint8_t>(reader));
57}
58
59TEST(BytesDecoderTest, Uint16) {
60 auto data = Data(0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80);
Ben Clayton0a8dc812023-12-04 20:52:16 +000061 auto reader = BufferReader{Slice{data}};
Ben Clayton63eb5c82023-11-22 21:11:19 +000062 EXPECT_EQ(Decode<uint16_t>(reader).Get(), 0x2010u);
63 EXPECT_EQ(Decode<uint16_t>(reader).Get(), 0x4030u);
64 EXPECT_EQ(Decode<uint16_t>(reader).Get(), 0x6050u);
65 EXPECT_EQ(Decode<uint16_t>(reader).Get(), 0x8070u);
66 EXPECT_FALSE(Decode<uint16_t>(reader));
67}
68
69TEST(BytesDecoderTest, Uint32) {
70 auto data = Data(0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80);
Ben Clayton0a8dc812023-12-04 20:52:16 +000071 auto reader = BufferReader{Slice{data}};
72 EXPECT_EQ(Decode<uint32_t>(reader, Endianness::kBig).Get(), 0x10203040u);
73 EXPECT_EQ(Decode<uint32_t>(reader, Endianness::kBig).Get(), 0x50607080u);
Ben Clayton63eb5c82023-11-22 21:11:19 +000074 EXPECT_FALSE(Decode<uint32_t>(reader));
75}
76
77TEST(BytesDecoderTest, Float) {
78 auto data = Data(0x00, 0x00, 0x08, 0x41);
Ben Clayton0a8dc812023-12-04 20:52:16 +000079 auto reader = BufferReader{Slice{data}};
Ben Clayton63eb5c82023-11-22 21:11:19 +000080 EXPECT_EQ(Decode<float>(reader).Get(), 8.5f);
81 EXPECT_FALSE(Decode<float>(reader));
82}
83
84TEST(BytesDecoderTest, Bool) {
85 auto data = Data(0x0, 0x1, 0x2, 0x1, 0x0);
Ben Clayton0a8dc812023-12-04 20:52:16 +000086 auto reader = BufferReader{Slice{data}};
Ben Clayton63eb5c82023-11-22 21:11:19 +000087 EXPECT_EQ(Decode<bool>(reader).Get(), false);
88 EXPECT_EQ(Decode<bool>(reader).Get(), true);
89 EXPECT_EQ(Decode<bool>(reader).Get(), true);
90 EXPECT_EQ(Decode<bool>(reader).Get(), true);
91 EXPECT_EQ(Decode<bool>(reader).Get(), false);
92 EXPECT_FALSE(Decode<bool>(reader));
93}
94
95TEST(BytesDecoderTest, String) {
Ben Clayton0a8dc812023-12-04 20:52:16 +000096 auto data = Data(0x5, 0x0, 'h', 'e', 'l', 'l', 'o', 0x5, 0x0, 'w', 'o', 'r', 'l', 'd');
97 auto reader = BufferReader{Slice{data}};
Ben Clayton63eb5c82023-11-22 21:11:19 +000098 EXPECT_EQ(Decode<std::string>(reader).Get(), "hello");
99 EXPECT_EQ(Decode<std::string>(reader).Get(), "world");
100 EXPECT_FALSE(Decode<std::string>(reader));
101}
102
103struct S {
104 uint8_t a;
105 uint16_t b;
106 uint32_t c;
107 TINT_REFLECT(a, b, c);
108};
109
110TEST(BytesDecoderTest, ReflectedObject) {
111 auto data = Data(0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80);
Ben Clayton0a8dc812023-12-04 20:52:16 +0000112 auto reader = BufferReader{Slice{data}};
Ben Clayton63eb5c82023-11-22 21:11:19 +0000113 auto got = Decode<S>(reader);
114 EXPECT_EQ(got->a, 0x10u);
Ben Clayton0a8dc812023-12-04 20:52:16 +0000115 EXPECT_EQ(got->b, 0x3020u);
116 EXPECT_EQ(got->c, 0x70605040u);
Ben Clayton63eb5c82023-11-22 21:11:19 +0000117 EXPECT_FALSE(Decode<S>(reader));
118}
119
120TEST(BytesDecoderTest, UnorderedMap) {
121 using M = std::unordered_map<uint8_t, uint16_t>;
122 auto data = Data(0x00, 0x10, 0x02, 0x20, //
123 0x00, 0x30, 0x04, 0x40, //
124 0x00, 0x50, 0x06, 0x60, //
125 0x00, 0x70, 0x08, 0x80, //
126 0x01);
Ben Clayton0a8dc812023-12-04 20:52:16 +0000127 auto reader = BufferReader{Slice{data}};
Ben Clayton63eb5c82023-11-22 21:11:19 +0000128 auto got = Decode<M>(reader);
129 EXPECT_THAT(got.Get(), testing::ContainerEq(M{
Ben Clayton0a8dc812023-12-04 20:52:16 +0000130 std::pair<uint8_t, uint32_t>(0x10u, 0x2002u),
131 std::pair<uint8_t, uint32_t>(0x30u, 0x4004u),
132 std::pair<uint8_t, uint32_t>(0x50u, 0x6006u),
133 std::pair<uint8_t, uint32_t>(0x70u, 0x8008u),
Ben Clayton63eb5c82023-11-22 21:11:19 +0000134 }));
135 EXPECT_FALSE(Decode<M>(reader));
136}
137
138TEST(BytesDecoderTest, Tuple) {
139 using T = std::tuple<uint8_t, uint16_t, uint32_t>;
140 auto data = Data(0x10, //
141 0x20, 0x30, //
142 0x40, 0x50, 0x60, 0x70, //
143 0x80);
Ben Clayton0a8dc812023-12-04 20:52:16 +0000144 auto reader = BufferReader{Slice{data}};
145 EXPECT_THAT(Decode<T>(reader).Get(), (T{0x10u, 0x3020u, 0x70605040u}));
Ben Clayton63eb5c82023-11-22 21:11:19 +0000146 EXPECT_FALSE(Decode<T>(reader));
147}
148
149} // namespace
150} // namespace tint::bytes