/test.cpp
C++ | 210 lines | 141 code | 30 blank | 39 comment | 23 complexity | b33823c5a55cfd446cab897f766486b8 MD5 | raw file
1#include <iostream> 2#include <map> 3#include <vector> 4#include "LuaContext.h" 5 6#ifdef NDEBUG 7# error This code uses assert() and shouldnt be compiled with NDEBUG 8#endif 9 10// these are basic tests just to see if I broke something when modifying the code 11void test() { 12 // test 1: writing basic types and reading again 13 { 14 Lua::LuaContext lua; 15 lua.writeVariable("a", 5); 16 lua.writeVariable("b", true); 17 lua.writeVariable("c", 5.2); 18 lua.writeVariable("d", "test"); 19 lua.executeCode("a = a + 2;"); 20 lua.executeCode("b = not b;"); 21 lua.executeCode("c = c * 2;"); 22 lua.executeCode("d = d .. \"test\";"); 23 lua.executeCode("e = \"7\";"); 24 assert(lua.readVariable<int>("a") == 7); 25 assert(lua.readVariable<bool>("b") == false); 26 assert(lua.readVariable<float>("c") == 10.4f); 27 assert(lua.readVariable<std::string>("d") == "testtest"); 28 assert(lua.readVariable<double>("e") == 7.); 29 } 30 31 // test 2: writing a function and calling it within lua 32 // strangely enough, GCC terminates the program because of the exception voluntary triggered inside the try-catch block 33 { 34# ifndef __GNUC__ 35 Lua::LuaContext lua; 36 lua.writeVariable("increment", [](int x) { return x + 1; }); 37 lua.executeCode("x = increment(3);"); 38 try { lua.executeCode("x = increment(increment);"); assert(false); } catch(...) { } 39 assert(lua.readVariable<int>("x") == 4); 40# endif 41 } 42 43 // test 3: calling a lua function 44 { 45 Lua::LuaContext lua; 46 lua.writeVariable("increment", [](int x) { return x + 1; }); 47 lua.executeCode("function test(a) \r return increment(a); \r end"); 48 assert(lua.callLuaFunction<int>("test", 3) == 4); 49 assert(lua.callLuaFunction<int>("test", "3.1") == 4); 50 assert(lua.callLuaFunction<int>("test", 3, 8, 12, true, "hello") == 4); 51 } 52 53 // test 4: calling a lua function returning multiple values 54 { 55 Lua::LuaContext lua; 56 std::shared_ptr<std::vector<char>> ptr(new std::vector<char>()); 57 lua.writeVariable("obj", ptr); 58 lua.executeCode("function test() \r return 4, 8, \"test\", obj; \r end"); 59 const auto r = lua.callLuaFunction<std::tuple<std::string,float,std::string,std::shared_ptr<std::vector<char>>>>("test"); 60 assert(std::get<0>(r) == "4"); 61 assert(std::get<1>(r) == 8.f); 62 assert(std::get<2>(r) == "test"); 63 assert(std::get<3>(r) == ptr); 64 assert(std::get<3>(r)); 65 } 66 67 // test 5: using a custom type (std::vector<char>) 68 { 69 class MyType { 70 public: 71 MyType(int x) : _variable(x) {} 72 73 74 void increment() { _variable++; } 75 void decrement() { _variable--; } 76 int& getVariable() { return _variable; } 77 int read() const { return _variable; } 78 79 80 private: 81 int _variable; 82 }; 83 84 Lua::LuaContext lua; 85 lua.writeVariable("increment", [](std::shared_ptr<MyType> ptr) { ptr->increment(); }); 86 lua.registerFunction("increment", &MyType::increment); 87 lua.registerFunction("decrement", &MyType::decrement); 88 lua.registerFunction("read", &MyType::read); 89 lua.registerFunction("getValue", [](std::shared_ptr<MyType> ptr) -> int { return ptr->getVariable(); }); 90 std::shared_ptr<MyType> ptr(new MyType(5)); 91 lua.writeVariable("create", [ptr]() { return ptr; }); 92 lua.executeCode("t = create(); increment(t); x = t:getValue(); t:increment();"); 93 assert(ptr); 94 assert(ptr->getVariable() == 7); 95 assert(lua.readVariable<int>("x") == 6); 96 } 97 98 // test 6: reading arrays 99 { 100 Lua::LuaContext lua; 101 lua.writeArrayIntoVariable("a"); 102 lua.writeArrayIntoVariable("a.b"); 103 lua.writeVariable("a.b.c", 9); 104 std::shared_ptr<std::vector<char>> ptr(new std::vector<char>()); 105 lua.writeVariable("a.b.d", ptr); 106 107 lua.executeCode("x = a[\"b\"][\"c\"];"); 108 lua.executeCode("y = a[\"b\"][\"d\"];"); 109 110 assert(lua.readVariable<int>("x") == 9); 111 assert(ptr); 112 assert(lua.readVariable<std::shared_ptr<std::vector<char>>>("y") == ptr); 113 } 114 115 // test 7: member functions that return a tuple 116 { 117 class A { 118 public: 119 std::tuple<int,float> f1() const { return std::make_tuple(2, 3.7f); } 120 }; 121 122 Lua::LuaContext lua; 123 lua.registerFunction("f1", &A::f1); 124 lua.writeVariable("f", []() { return std::make_tuple(2, 3.7); }); 125 } 126 127 // test 8: executeCode returning a value 128 { 129 Lua::LuaContext lua; 130 assert(lua.executeCode<int>("return 5;") == 5); 131 } 132 133 // test 9: thread safety 134 { 135 // GCC seems to have some problems with their <thread> implementation 136 // this has nothing to do with lua 137# ifndef __GNUC__ 138 /*Lua::LuaContext lua; 139 lua.executeCode("a = 1; b = 2;"); 140 std::thread checkA([&]() { 141 for (int i = 0; i < 100000; ++i) { 142 lua.writeVariable("f", [](int x) { return x + 1; }); 143 assert(lua.readVariable<int>("a") == 1); 144 } 145 }); 146 std::thread checkB([&]() { 147 for (int i = 0; i < 100000; ++i) 148 assert(lua.readVariable<int>("b") == 2); 149 }); 150 checkA.join(); 151 checkB.join();*/ 152# endif 153 } 154 155 // test 10: writing entire arrays 156 { 157 Lua::LuaContext lua; 158 159 Lua::LuaContext::Table table; 160 table.insert("test", 5); 161 table.insert(5, "test"); 162 assert(table.read<int>("test") == 5); 163 lua.writeVariable("t", table); 164 165 Lua::LuaContext::Table table2; 166 table2.insert("f", [](int x) { return x + 1; }); 167 lua.writeVariable("f", table2); 168 169 assert(lua.executeCode<int>("return t[\"test\"];") == 5); 170 assert(lua.executeCode<std::string>("return t[5];") == "test"); 171 assert(lua.executeCode<int>("return f.f(2);") == 3); 172 } 173 174 // test 11: reading entire arrays 175 { 176 /*Lua::LuaContext lua; 177 178 lua.writeVariable("handleTable", [](Lua::LuaContext::Table table) { 179 assert(table.read<std::string>(0) == "value 0"); 180 assert(table.read<std::string>(1) == "value 1"); 181 assert(table.read<std::string>(2) == "value 2"); 182 }); 183 184 lua.executeCode("handleTable({ [0] = \"value 0\", [1] = \"value 1\", [2] = \"value 2\" });");*/ 185 } 186 187 // test 12: reading/writing maps 188 { 189 Lua::LuaContext lua; 190 191 std::map<std::string,int> map; 192 map["test"] = 5; 193 map["haha"] = 3; 194 195 lua.writeVariable("arr", map); 196 197 assert(lua.readVariable<std::string>("arr.haha") == "3"); 198 } 199} 200 201 202int main() { 203 test(); 204 std::cout << "All tests passed successfully" << std::endl; 205# ifdef _WIN32 206 system("pause"); 207# endif 208 return 0; 209} 210