1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-17 21:35:27 +03:00
opentx/radio/src/tests/lua.cpp
2019-05-07 23:35:12 +02:00

166 lines
5 KiB
C++

/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <math.h>
#include "gtests.h"
#if defined(LUA)
#define SWAP_DEFINED
#include "opentx.h"
::testing::AssertionResult __luaExecStr(const char * str)
{
extern lua_State * lsScripts;
if (!lsScripts) luaInit();
if (!lsScripts) return ::testing::AssertionFailure() << "No Lua state!";
if (luaL_dostring(lsScripts, str)) {
return ::testing::AssertionFailure() << "lua error: " << lua_tostring(lsScripts, -1);
}
return ::testing::AssertionSuccess();
}
#define luaExecStr(test) EXPECT_TRUE(__luaExecStr(test))
TEST(Lua, testSetModelInfo)
{
luaExecStr("info = model.getInfo()");
// luaExecStr("print('model name: '..info.name..' id: '..info.id)");
luaExecStr("info.name = 'modelA'");
luaExecStr("model.setInfo(info)");
// luaExecStr("print('model name: '..info.name..' id: '..info.id)");
EXPECT_ZSTREQ("modelA", g_model.header.name);
luaExecStr("info.name = 'Model 1'");
luaExecStr("model.setInfo(info)");
// luaExecStr("print('model name: '..info.name..' id: '..info.id)");
EXPECT_ZSTREQ("Model 1", g_model.header.name);
}
TEST(Lua, testPanicProtection)
{
bool passed = false;
PROTECT_LUA() {
PROTECT_LUA() {
// simulate panic
longjmp(global_lj->b, 1);
}
else {
// we should come here
passed = true;
}
UNPROTECT_LUA();
}
else {
// and not here
// TRACE("testLuaProtection: test 1 FAILED");
FAIL() << "Failed test 1";
}
UNPROTECT_LUA()
EXPECT_EQ(passed, true);
passed = false;
PROTECT_LUA() {
PROTECT_LUA() {
int a = 5;
UNUSED(a);
}
else {
// we should not come here
// TRACE("testLuaProtection: test 2 FAILED");
FAIL() << "Failed test 2";
}
UNPROTECT_LUA()
// simulate panic
longjmp(global_lj->b, 1);
}
else {
// we should come here
passed = true;
}
UNPROTECT_LUA()
EXPECT_EQ(passed, true);
}
TEST(Lua, testModelInputs)
{
MODEL_RESET();
luaExecStr("noInputs = model.getInputsCount(0)");
luaExecStr("if noInputs > 0 then error('getInputsCount()') end");
// add one line on Input4
luaExecStr("model.insertInput(3, 0, {name='test1', source=MIXSRC_Thr, weight=56, offset=3, switch=2})");
EXPECT_EQ(3, (int)g_model.expoData[0].chn);
EXPECT_ZSTREQ("test1", g_model.expoData[0].name);
EXPECT_EQ(MIXSRC_Thr, g_model.expoData[0].srcRaw);
EXPECT_EQ(56, g_model.expoData[0].weight);
EXPECT_EQ(3, g_model.expoData[0].offset);
EXPECT_EQ(2, g_model.expoData[0].swtch);
// add another one before existing line on Input4
luaExecStr("model.insertInput(3, 0, {name='test2', source=MIXSRC_Rud, weight=-56})");
EXPECT_EQ(3, (int)g_model.expoData[0].chn);
EXPECT_ZSTREQ("test2", g_model.expoData[0].name);
EXPECT_EQ(MIXSRC_Rud, g_model.expoData[0].srcRaw);
EXPECT_EQ(-56, g_model.expoData[0].weight);
EXPECT_EQ(0, g_model.expoData[0].offset);
EXPECT_EQ(0, g_model.expoData[0].swtch);
EXPECT_EQ(3, (int)g_model.expoData[1].chn);
EXPECT_ZSTREQ("test1", g_model.expoData[1].name);
EXPECT_EQ(MIXSRC_Thr, g_model.expoData[1].srcRaw);
EXPECT_EQ(56, g_model.expoData[1].weight);
EXPECT_EQ(3, g_model.expoData[1].offset);
EXPECT_EQ(2, g_model.expoData[1].swtch);
// add another line after existing lines on Input4
luaExecStr("model.insertInput(3, model.getInputsCount(3), {name='test3', source=MIXSRC_Ail, weight=100})");
EXPECT_EQ(3, (int)g_model.expoData[0].chn);
EXPECT_ZSTREQ("test2", g_model.expoData[0].name);
EXPECT_EQ(MIXSRC_Rud, g_model.expoData[0].srcRaw);
EXPECT_EQ(-56, g_model.expoData[0].weight);
EXPECT_EQ(0, g_model.expoData[0].offset);
EXPECT_EQ(0, g_model.expoData[0].swtch);
EXPECT_EQ(3, (int)g_model.expoData[1].chn);
EXPECT_ZSTREQ("test1", g_model.expoData[1].name);
EXPECT_EQ(MIXSRC_Thr, g_model.expoData[1].srcRaw);
EXPECT_EQ(56, g_model.expoData[1].weight);
EXPECT_EQ(3, g_model.expoData[1].offset);
EXPECT_EQ(2, g_model.expoData[1].swtch);
EXPECT_EQ(3, (int)g_model.expoData[2].chn);
EXPECT_ZSTREQ("test3", g_model.expoData[2].name);
EXPECT_EQ(MIXSRC_Ail, g_model.expoData[2].srcRaw);
EXPECT_EQ(100, g_model.expoData[2].weight);
EXPECT_EQ(0, g_model.expoData[2].offset);
EXPECT_EQ(0, g_model.expoData[2].swtch);
// verify number of lines for Input4
luaExecStr("noInputs = model.getInputsCount(3)");
luaExecStr("if noInputs ~= 3 then error('getInputsCount()') end");
}
#endif // #if defined(LUA)