forked from Brill-Power/thingsetplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestBinaryEncoder.cpp
More file actions
103 lines (91 loc) · 3.4 KB
/
TestBinaryEncoder.cpp
File metadata and controls
103 lines (91 loc) · 3.4 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Copyright (c) 2024 Brill Power.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "thingset++/ThingSetBinaryEncoder.hpp"
#include "gtest/gtest.h"
using namespace ThingSet;
#define SETUP(size) \
uint8_t buffer[size]; \
FixedDepthThingSetBinaryEncoder encoder(buffer, size);
#define ASSERT_BUFFER_EQ(expected, actual, actual_len) \
ASSERT_EQ(sizeof(expected), actual_len); \
for (size_t i = 0; i < sizeof(expected); i++) { \
ASSERT_EQ(expected[i], actual[i]); \
}
TEST(BinaryEncoder, EncodeFloat)
{
SETUP(128)
float f = 1.23;
encoder.encode(f);
uint8_t expected[] = { 0xFA, 0x3F, 0x9D, 0x70, 0xA4 };
ASSERT_BUFFER_EQ(expected, buffer, encoder.getEncodedLength());
}
TEST(BinaryEncoder, EncodeArrayOfFloats)
{
SETUP(128)
std::array f = { 1.23f, 4.56f, 7.89f };
encoder.encode(f);
uint8_t expected[] = { 0x83, 0xFA, 0x3F, 0x9D, 0x70, 0xA4, 0xFA, 0x40,
0x91, 0xEB, 0x85, 0xFA, 0x40, 0xFC, 0x7A, 0xE1 };
ASSERT_BUFFER_EQ(expected, buffer, encoder.getEncodedLength());
}
TEST(BinaryEncoder, EncodeString)
{
SETUP(128)
const char *hello = "world";
encoder.encode(hello);
uint8_t expected[] = { 0x65, 0x77, 0x6F, 0x72, 0x6C, 0x64 };
ASSERT_BUFFER_EQ(expected, buffer, encoder.getEncodedLength());
}
TEST(BinaryEncoder, EncodeStdString)
{
SETUP(128)
const std::string bonjour = "monde";
encoder.encode(bonjour);
uint8_t expected[] = { 0x65, 0x6D, 0x6F, 0x6E, 0x64, 0x65 };
ASSERT_BUFFER_EQ(expected, buffer, encoder.getEncodedLength());
}
TEST(BinaryEncoder, EncodeStdStringView)
{
SETUP(128)
const std::string_view nihao = "世界";
encoder.encode(nihao);
uint8_t expected[] = { 0x66, 0xE4, 0xB8, 0x96, 0xE7, 0x95, 0x8C };
ASSERT_BUFFER_EQ(expected, buffer, encoder.getEncodedLength());
}
TEST(BinaryEncoder, EncodeMap)
{
SETUP(128)
ASSERT_TRUE(encoder.encodeMapStart(2));
ASSERT_TRUE(encoder.encode(0x01));
ASSERT_TRUE(encoder.encode("hello"));
ASSERT_TRUE(encoder.encode(0x02));
std::array<uint32_t, 3> i32 = { { 1, 2, 3 } };
ASSERT_TRUE(encoder.encode(i32));
ASSERT_TRUE(encoder.encodeMapEnd(2));
uint8_t expected[] = { 0xA2, 0x01, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x02, 0x83, 0x01, 0x02, 0x03 };
ASSERT_BUFFER_EQ(expected, buffer, encoder.getEncodedLength());
}
TEST(BinaryEncoder, EncodeBag)
{
SETUP(128)
encoder.encodeList(1.23f, 123, "123");
uint8_t expected[] = { 0x83, 0xFA, 0x3F, 0x9D, 0x70, 0xA4, 0x18, 0x7B, 0x63, 0x31, 0x32, 0x33 };
ASSERT_BUFFER_EQ(expected, buffer, encoder.getEncodedLength());
}
TEST(BinaryEncoder, EncodeNull)
{
SETUP(128)
encoder.encodeNull();
uint8_t expected[] = { 0xF6 };
ASSERT_BUFFER_EQ(expected, buffer, encoder.getEncodedLength());
}
TEST(BinaryEncoder, EncodePreamble)
{
SETUP(128)
encoder.encodePreamble();
uint8_t expected[] = { 0xF6 };
ASSERT_BUFFER_EQ(expected, buffer, encoder.getEncodedLength());
}