PageRenderTime 311ms CodeModel.GetById 34ms RepoModel.GetById 3ms app.codeStats 0ms

/test.cpp

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