forked from Brill-Power/thingsetplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestFunctions.cpp
More file actions
120 lines (105 loc) · 4.13 KB
/
TestFunctions.cpp
File metadata and controls
120 lines (105 loc) · 4.13 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Copyright (c) 2024 Brill Power.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <thingset++/ThingSet.hpp>
#include "Buffer.hpp"
#include "gtest/gtest.h"
using namespace ThingSet;
static bool called = false;
static bool calledVoid = false;
static void test(int argi, float argf)
{
called = true;
}
static void voidvoid()
{
calledVoid = true;
}
ThingSetUserFunction<0x400, 0x0, "xTest", void, int, float> xTestFunc(test);
ThingSetUserFunction<0x410, 0x0, "xVoid", void> xVoidVoidFunc(voidvoid);
ThingSetUserFunction<0x420, 0x0, "xLambda", float, float, float> xFloatLambda([](float x, float y) { return x * y; });
ThingSetUserFunction<0x430, 0x0, "xMap", std::map<std::string, float>> xGetMap([]() {
auto map = std::map<std::string, float>();
map.insert_or_assign("hello", 1.0f);
map.insert_or_assign("world", 2.0f);
return map;
});
ThingSetFunction<0x440, 0x0, "xTestCustom", ThingSetAccess::anyWrite, 0x4401, void, int, float> xTestCustomFunc(test);
ThingSetUserFunction<0x450, 0x0, "xString", int, std::string &> xString([](std::string &) { return 0; });
TEST(Functions, FunctionTypes)
{
ASSERT_EQ("(i32,f32)->()", xTestFunc.getType());
ASSERT_EQ("()->()", xVoidVoidFunc.getType());
ASSERT_EQ("(f32,f32)->(f32)", xFloatLambda.getType());
ASSERT_EQ("(string)->(i32)", xString.getType());
}
TEST(Functions, FunctionParameters)
{
ThingSetNode *node;
ASSERT_TRUE(ThingSetRegistry::findById(0x401, &node));
ASSERT_EQ(0x400, node->getParentId());
ASSERT_EQ("xTesti32_1", node->getName());
ASSERT_TRUE(ThingSetRegistry::findById(0x402, &node));
ASSERT_EQ(0x400, node->getParentId());
ASSERT_EQ("xTestf32_2", node->getName());
ASSERT_TRUE(ThingSetRegistry::findById(0x421, &node));
ASSERT_EQ(0x420, node->getParentId());
ASSERT_EQ("xLambdaf32_1", node->getName());
ASSERT_TRUE(ThingSetRegistry::findById(0x451, &node));
ASSERT_EQ(0x450, node->getParentId());
ASSERT_EQ("xStringstring_1", node->getName());
ASSERT_EQ("string", node->getType());
}
TEST(Functions, FunctionParametersWithCustomId)
{
ThingSetNode *node;
ASSERT_TRUE(ThingSetRegistry::findById(0x4401, &node));
ASSERT_EQ(0x440, node->getParentId());
ASSERT_EQ("xTestCustomi32_1", node->getName());
ASSERT_TRUE(ThingSetRegistry::findById(0x4402, &node));
ASSERT_EQ(0x440, node->getParentId());
ASSERT_EQ("xTestCustomf32_2", node->getName());
}
TEST(Functions, InvokeLambda)
{
uint8_t request[] = { 0x82, 0xF9, 0x40, 0x00, 0xF9, 0x42, 0x00 };
uint8_t response[64];
FixedDepthThingSetBinaryDecoder decoder(request, sizeof(request));
FixedDepthThingSetBinaryEncoder encoder(response, sizeof(response));
ASSERT_TRUE(xFloatLambda.invoke(decoder, encoder));
uint8_t expected[] = { 0xFA, 0x40, 0xC0, 0x00, 0x00 }; // 6.0
ASSERT_EQ(0, memcmp(response, expected, sizeof(expected)));
}
TEST(Functions, InvokeGetMap)
{
uint8_t request[] = { 0x80 };
uint8_t response[64];
FixedDepthThingSetBinaryDecoder decoder(request, sizeof(request));
FixedDepthThingSetBinaryEncoder encoder(response, sizeof(response));
ASSERT_TRUE(xGetMap.invoke(decoder, encoder));
uint8_t expected[] = {
0xA2, // 2 element map
0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F, // hello
0xFA, 0x3F, 0x80, 0x00, 0x00, // 1.0
0x65, 0x77, 0x6F, 0x72, 0x6C, 0x64, // world
0xFA, 0x40, 0x00, 0x00, 0x00 // 2.0
};
ASSERT_EQ(0, memcmp(response, expected, sizeof(expected)));
}
static uint32_t getLength(Buffer<1024> &buffer)
{
return buffer.size();
}
ThingSetUserFunction<0x460, 0x0, "xGetLength", uint32_t, Buffer<1024> &> xGetLength(getLength);
TEST(Functions, InvokeFunctionWithDecodableParameter)
{
uint8_t request[] = { 0x81, 0x44, 0x00, 0x01, 0x02, 0x03 };
uint8_t response[64];
FixedDepthThingSetBinaryDecoder decoder(request, sizeof(request));
FixedDepthThingSetBinaryEncoder encoder(response, sizeof(response));
ASSERT_TRUE(xGetLength.invoke(decoder, encoder));
uint8_t expected[] = { 0x04 }; // function returns length of bytes passed in
ASSERT_EQ(0, memcmp(response, expected, sizeof(expected)));
}