forked from Brill-Power/thingsetplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestStreamingBinaryEncoder.cpp
More file actions
63 lines (55 loc) · 1.6 KB
/
TestStreamingBinaryEncoder.cpp
File metadata and controls
63 lines (55 loc) · 1.6 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
/*
* Copyright (c) 2024 Brill Power.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "thingset++/StreamingThingSetBinaryEncoder.hpp"
#include "gtest/gtest.h"
using namespace ThingSet;
constexpr size_t MessageSize = 8;
constexpr size_t TotalSize = 64;
class InMemoryStreamingThingSetBinaryEncoder : public StreamingThingSetBinaryEncoder<MessageSize>
{
public:
std::array<uint8_t, TotalSize> output;
size_t position = 0;
InMemoryStreamingThingSetBinaryEncoder() : StreamingThingSetBinaryEncoder()
{
zcbor_new_encode_state(_state, BINARY_ENCODER_DEFAULT_MAX_DEPTH, &_buffer[0], _buffer.size(), 2);
}
protected:
bool write(size_t length, bool flushing) override
{
if (length > MessageSize) {
return false;
}
memcpy(&output[position], &_buffer[0], length);
position += length;
return true;
}
};
TEST(StreamingBinaryEncoder, EncodeFloat)
{
InMemoryStreamingThingSetBinaryEncoder encoder;
encoder.encode(3.14f);
encoder.flush();
ASSERT_EQ(5, encoder.position);
}
TEST(StreamingBinaryEncoder, EncodeIntArray)
{
InMemoryStreamingThingSetBinaryEncoder encoder;
std::array<int, 4> array = { 1, 2, 3, 4 };
encoder.encode(array);
encoder.flush();
ASSERT_EQ(5, encoder.position);
}
TEST(StreamingBinaryEncoder, EncodeOverflow)
{
// rather contrived way of getting the encoder to not write before a flush
InMemoryStreamingThingSetBinaryEncoder encoder;
encoder.encode("Hello!");
ASSERT_EQ(0, encoder.position);
encoder.encode(32768);
encoder.flush();
ASSERT_EQ(10, encoder.position);
}