x9e with horus bt module (#5214)
[opentx.git] / radio / src / tests / lua.cpp
bloba23568c84c7e7f513a7a2cebd6a5c3192798eba8
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include <math.h>
22 #include "gtests.h"
24 #if defined(LUA)
26 #define SWAP_DEFINED
27 #include "opentx.h"
29 extern const char * zchar2string(const char * zstring, int size);
30 #define EXPECT_ZSTREQ(c_string, z_string) EXPECT_STREQ(c_string, zchar2string(z_string, sizeof(z_string)))
32 ::testing::AssertionResult __luaExecStr(const char * str)
34 extern lua_State * lsScripts;
35 if (!lsScripts) luaInit();
36 if (!lsScripts) return ::testing::AssertionFailure() << "No Lua state!";
37 if (luaL_dostring(lsScripts, str)) {
38 return ::testing::AssertionFailure() << "lua error: " << lua_tostring(lsScripts, -1);
40 return ::testing::AssertionSuccess();
43 #define luaExecStr(test) EXPECT_TRUE(__luaExecStr(test))
45 TEST(Lua, testSetModelInfo)
47 luaExecStr("info = model.getInfo()");
48 // luaExecStr("print('model name: '..info.name..' id: '..info.id)");
49 luaExecStr("info.name = 'modelA'");
50 luaExecStr("model.setInfo(info)");
51 // luaExecStr("print('model name: '..info.name..' id: '..info.id)");
52 EXPECT_ZSTREQ("modelA", g_model.header.name);
54 luaExecStr("info.name = 'Model 1'");
55 luaExecStr("model.setInfo(info)");
56 // luaExecStr("print('model name: '..info.name..' id: '..info.id)");
57 EXPECT_ZSTREQ("Model 1", g_model.header.name);
60 TEST(Lua, testPanicProtection)
62 bool passed = false;
63 PROTECT_LUA() {
64 PROTECT_LUA() {
65 //simulate panic
66 longjmp(global_lj->b, 1);
68 else {
69 //we should come here
70 passed = true;
72 UNPROTECT_LUA();
74 else {
75 // an not here
76 // TRACE("testLuaProtection: test 1 FAILED");
77 FAIL() << "Failed test 1";
79 UNPROTECT_LUA()
81 EXPECT_EQ(passed, true);
83 passed = false;
85 PROTECT_LUA() {
86 PROTECT_LUA() {
87 int a = 5;
88 a = a; // avoids the warning
90 else {
91 //we should not come here
92 // TRACE("testLuaProtection: test 2 FAILED");
93 FAIL() << "Failed test 2";
95 UNPROTECT_LUA()
96 //simulate panic
97 longjmp(global_lj->b, 1);
99 else {
100 // we should come here
101 passed = true;
103 UNPROTECT_LUA()
105 EXPECT_EQ(passed, true);
108 TEST(Lua, testModelInputs)
110 MODEL_RESET();
111 luaExecStr("noInputs = model.getInputsCount(0)");
112 luaExecStr("if noInputs > 0 then error('getInputsCount()') end");
114 // add one line on Input4
115 luaExecStr("model.insertInput(3, 0, {name='test1', source=MIXSRC_Thr, weight=56, offset=3, switch=2})");
116 EXPECT_EQ(3, (int)g_model.expoData[0].chn);
117 EXPECT_ZSTREQ("test1", g_model.expoData[0].name);
118 EXPECT_EQ(MIXSRC_Thr, g_model.expoData[0].srcRaw);
119 EXPECT_EQ(56, g_model.expoData[0].weight);
120 EXPECT_EQ(3, g_model.expoData[0].offset);
121 EXPECT_EQ(2, g_model.expoData[0].swtch);
123 // add another one before existing line on Input4
124 luaExecStr("model.insertInput(3, 0, {name='test2', source=MIXSRC_Rud, weight=-56})");
125 EXPECT_EQ(3, (int)g_model.expoData[0].chn);
126 EXPECT_ZSTREQ("test2", g_model.expoData[0].name);
127 EXPECT_EQ(MIXSRC_Rud, g_model.expoData[0].srcRaw);
128 EXPECT_EQ(-56, g_model.expoData[0].weight);
129 EXPECT_EQ(0, g_model.expoData[0].offset);
130 EXPECT_EQ(0, g_model.expoData[0].swtch);
132 EXPECT_EQ(3, (int)g_model.expoData[1].chn);
133 EXPECT_ZSTREQ("test1", g_model.expoData[1].name);
134 EXPECT_EQ(MIXSRC_Thr, g_model.expoData[1].srcRaw);
135 EXPECT_EQ(56, g_model.expoData[1].weight);
136 EXPECT_EQ(3, g_model.expoData[1].offset);
137 EXPECT_EQ(2, g_model.expoData[1].swtch);
139 // add another line after existing lines on Input4
140 luaExecStr("model.insertInput(3, model.getInputsCount(3), {name='test3', source=MIXSRC_Ail, weight=100})");
141 EXPECT_EQ(3, (int)g_model.expoData[0].chn);
142 EXPECT_ZSTREQ("test2", g_model.expoData[0].name);
143 EXPECT_EQ(MIXSRC_Rud, g_model.expoData[0].srcRaw);
144 EXPECT_EQ(-56, g_model.expoData[0].weight);
145 EXPECT_EQ(0, g_model.expoData[0].offset);
146 EXPECT_EQ(0, g_model.expoData[0].swtch);
148 EXPECT_EQ(3, (int)g_model.expoData[1].chn);
149 EXPECT_ZSTREQ("test1", g_model.expoData[1].name);
150 EXPECT_EQ(MIXSRC_Thr, g_model.expoData[1].srcRaw);
151 EXPECT_EQ(56, g_model.expoData[1].weight);
152 EXPECT_EQ(3, g_model.expoData[1].offset);
153 EXPECT_EQ(2, g_model.expoData[1].swtch);
155 EXPECT_EQ(3, (int)g_model.expoData[2].chn);
156 EXPECT_ZSTREQ("test3", g_model.expoData[2].name);
157 EXPECT_EQ(MIXSRC_Ail, g_model.expoData[2].srcRaw);
158 EXPECT_EQ(100, g_model.expoData[2].weight);
159 EXPECT_EQ(0, g_model.expoData[2].offset);
160 EXPECT_EQ(0, g_model.expoData[2].swtch);
162 // verify number of lines for Input4
163 luaExecStr("noInputs = model.getInputsCount(3)");
164 luaExecStr("if noInputs ~= 3 then error('getInputsCount()') end");
168 #endif // #if defined(LUA)