Various fixes around Companion trainer mode (#7116)
[opentx.git] / radio / src / tests / functions.cpp
blob087af20e11bdeb684fd6855680c7ef4dc4e7ec5f
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 "gtests.h"
23 class SpecialFunctionsTest : public OpenTxTest {};
25 TEST_F(SpecialFunctionsTest, SwitchFiledSize)
27 // test the size of swtch member
28 g_model.customFn[0].swtch = SWSRC_LAST;
29 EXPECT_EQ(g_model.customFn[0].swtch, SWSRC_LAST) << "CustomFunctionData.swtch member is too small to hold all possible values";
30 g_model.customFn[0].swtch = -SWSRC_LAST;
31 EXPECT_EQ(g_model.customFn[0].swtch, -SWSRC_LAST) << "CustomFunctionData.swtch member is too small to hold all possible values";
34 #if defined(PCBTARANIS) || defined(PCBHORUS)
35 TEST_F(SpecialFunctionsTest, FlightReset)
37 g_model.customFn[0].swtch = SWSRC_SA0;
38 g_model.customFn[0].func = FUNC_RESET;
39 g_model.customFn[0].all.val = FUNC_RESET_FLIGHT;
40 g_model.customFn[0].active = true;
42 mainRequestFlags = 0;
43 simuSetSwitch(0, 0);
44 evalFunctions(g_model.customFn, modelFunctionsContext);
45 EXPECT_EQ((bool)(mainRequestFlags & (1 << REQUEST_FLIGHT_RESET)), false);
47 // now trigger SA0
48 simuSetSwitch(0, -1);
50 // flightReset() should be called
51 evalFunctions(g_model.customFn, modelFunctionsContext);
52 EXPECT_EQ((bool)(mainRequestFlags & (1 << REQUEST_FLIGHT_RESET)), true);
54 // now reset mainRequestFlags, and it should stay reset (flightReset() should not be called again)
55 mainRequestFlags = 0;
56 evalFunctions(g_model.customFn, modelFunctionsContext);
57 EXPECT_EQ((bool)(mainRequestFlags & (1 << REQUEST_FLIGHT_RESET)), false);
60 #if defined(GVARS)
61 TEST_F(SpecialFunctionsTest, GvarsInc)
63 simuSetSwitch(0, 0); // SA-
65 g_model.customFn[0].swtch = SWSRC_SA0;
66 g_model.customFn[0].func = FUNC_ADJUST_GVAR;
67 g_model.customFn[0].all.mode = FUNC_ADJUST_GVAR_INCDEC;
68 g_model.customFn[0].all.param = 0; // GV1
69 g_model.customFn[0].all.val = -1; // inc/dec value
70 g_model.customFn[0].active = true;
72 g_model.flightModeData[0].gvars[0] = 10; // GV1 = 10;
73 evalFunctions(g_model.customFn, modelFunctionsContext);
74 EXPECT_EQ(g_model.flightModeData[0].gvars[0], 10);
76 // now trigger SA0
77 simuSetSwitch(0, -1); // SAdown
78 evalFunctions(g_model.customFn, modelFunctionsContext);
79 EXPECT_EQ(g_model.flightModeData[0].gvars[0], 9);
80 evalFunctions(g_model.customFn, modelFunctionsContext);
81 EXPECT_EQ(g_model.flightModeData[0].gvars[0], 9);
83 simuSetSwitch(0, 0); // SA-
84 evalFunctions(g_model.customFn, modelFunctionsContext);
85 EXPECT_EQ(g_model.flightModeData[0].gvars[0], 9);
87 simuSetSwitch(0, -1); // SAdown
88 evalFunctions(g_model.customFn, modelFunctionsContext);
89 EXPECT_EQ(g_model.flightModeData[0].gvars[0], 8);
90 evalFunctions(g_model.customFn, modelFunctionsContext);
91 EXPECT_EQ(g_model.flightModeData[0].gvars[0], 8);
93 simuSetSwitch(0, 0); // SA-
94 evalFunctions(g_model.customFn, modelFunctionsContext);
95 EXPECT_EQ(g_model.flightModeData[0].gvars[0], 8);
97 g_model.customFn[0].all.val = 10; // inc/dec value
99 simuSetSwitch(0, -1); // SAdown
100 evalFunctions(g_model.customFn, modelFunctionsContext);
101 EXPECT_EQ(g_model.flightModeData[0].gvars[0], 18);
102 evalFunctions(g_model.customFn, modelFunctionsContext);
103 EXPECT_EQ(g_model.flightModeData[0].gvars[0], 18);
105 simuSetSwitch(0, 0); // SA-
106 evalFunctions(g_model.customFn, modelFunctionsContext);
107 EXPECT_EQ(g_model.flightModeData[0].gvars[0], 18);
109 simuSetSwitch(0, -1); // SAdown
110 evalFunctions(g_model.customFn, modelFunctionsContext);
111 EXPECT_EQ(g_model.flightModeData[0].gvars[0], 28);
112 evalFunctions(g_model.customFn, modelFunctionsContext);
113 EXPECT_EQ(g_model.flightModeData[0].gvars[0], 28);
115 #endif // #if defined(GVARS)
117 #endif // #if defined(PCBTARANIS) || defined(PCBHORUS)