?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. // https://github.com/vinniefalco/LuaBridge
  2. // Copyright 2019, Dmitry Tarakanov
  3. // SPDX-License-Identifier: MIT
  4.  
  5. #include "TestBase.h"
  6.  
  7. #include "LuaBridge/UnorderedMap.h"
  8.  
  9. #include <unordered_map>
  10.  
  11. struct UnorderedMapTests : TestBase
  12. {
  13. };
  14.  
  15. namespace {
  16.  
  17. struct Data
  18. {
  19.     /* explicit */ Data(int i) : i(i) {}
  20.  
  21.     int i;
  22. };
  23.  
  24. } // namespace
  25.  
  26. namespace std {
  27.  
  28. template<>
  29. struct hash<Data>
  30. {
  31.     size_t operator()(const Data& value) const noexcept
  32.     {
  33.         return 0; // Don't care about hash collisions
  34.     }
  35. };
  36.  
  37. template<>
  38. struct hash<::luabridge::LuaRef>
  39. {
  40.     size_t operator()(const ::luabridge::LuaRef& value) const
  41.     {
  42.         return 0; // Don't care about hash collisions
  43.     }
  44. };
  45.  
  46. } // namespace std
  47.  
  48. TEST_F(UnorderedMapTests, LuaRef)
  49. {
  50.     {
  51.         runLua("result = {[false] = true, a = 'abc', [1] = 5, [3.14] = -1.1}");
  52.  
  53.         using Map = std::unordered_map<luabridge::LuaRef, luabridge::LuaRef>;
  54.         Map expected{
  55.             {luabridge::LuaRef(L, false), luabridge::LuaRef(L, true)},
  56.             {luabridge::LuaRef(L, 'a'), luabridge::LuaRef(L, "abc")},
  57.             {luabridge::LuaRef(L, 1), luabridge::LuaRef(L, 5)},
  58.             {luabridge::LuaRef(L, 3.14), luabridge::LuaRef(L, -1.1)},
  59.         };
  60.         Map actual = result();
  61.         ASSERT_EQ(expected, actual);
  62.         ASSERT_EQ(expected, result<Map>());
  63.     }
  64.  
  65.     {
  66.         runLua("result = {'a', 'b', 'c'}");
  67.  
  68.         using Int2Char = std::unordered_map<int, char>;
  69.         Int2Char expected{{1, 'a'}, {2, 'b'}, {3, 'c'}};
  70.         Int2Char actual = result();
  71.         ASSERT_EQ(expected, actual);
  72.         ASSERT_EQ(expected, result<Int2Char>());
  73.     }
  74. }
  75.  
  76. TEST_F(UnorderedMapTests, PassToFunction)
  77. {
  78.     runLua("function foo (map) "
  79.            "  result = map "
  80.            "end");
  81.  
  82.     auto foo = luabridge::getGlobal(L, "foo");
  83.     using Int2Bool = std::unordered_map<int, bool>;
  84.  
  85.     resetResult();
  86.  
  87.     Int2Bool lvalue{{10, false}, {20, true}, {30, true}};
  88.     foo(lvalue);
  89.     ASSERT_TRUE(result().isTable());
  90.     ASSERT_EQ(lvalue, result<Int2Bool>());
  91.  
  92.     resetResult();
  93.  
  94.     const Int2Bool constLvalue = lvalue;
  95.     foo(constLvalue);
  96.     ASSERT_TRUE(result().isTable());
  97.     ASSERT_EQ(constLvalue, result<Int2Bool>());
  98. }
  99.  
  100. namespace {
  101.  
  102. bool operator==(const Data& lhs, const Data& rhs)
  103. {
  104.     return lhs.i == rhs.i;
  105. }
  106.  
  107. bool operator<(const Data& lhs, const Data& rhs)
  108. {
  109.     return lhs.i < rhs.i;
  110. }
  111.  
  112. std::ostream& operator<<(std::ostream& lhs, const Data& rhs)
  113. {
  114.     lhs << "{" << rhs.i << "}";
  115.     return lhs;
  116. }
  117.  
  118. std::unordered_map<Data, Data> processValues(const std::unordered_map<Data, Data>& data)
  119. {
  120.     return data;
  121. }
  122.  
  123. std::unordered_map<Data, Data> processPointers(const std::unordered_map<Data, const Data*>& data)
  124. {
  125.     std::unordered_map<Data, Data> result;
  126.     for (const auto& item : data)
  127.     {
  128.         result.emplace(item.first, *item.second);
  129.     }
  130.     return result;
  131. }
  132.  
  133. } // namespace
  134.  
  135. TEST_F(UnorderedMapTests, PassFromLua)
  136. {
  137.     luabridge::getGlobalNamespace(L)
  138.         .beginClass<Data>("Data")
  139.         .addConstructor<void (*)(int)>()
  140.         .endClass()
  141.         .addFunction("processValues", &processValues)
  142.         .addFunction("processPointers", &processPointers);
  143.  
  144.     {
  145.         resetResult();
  146.         runLua("result = processValues ({[Data (-1)] = Data (2)})");
  147.         std::unordered_map<Data, Data> expected{{Data(-1), Data(2)}};
  148.         const auto actual = result<std::unordered_map<Data, Data>>();
  149.         ASSERT_EQ(expected, actual);
  150.     }
  151.  
  152.     {
  153.         resetResult();
  154.         runLua("result = processPointers ({[Data (3)] = Data (-4)})");
  155.         std::unordered_map<Data, Data> expected{{Data(3), Data(-4)}};
  156.         const auto actual = result<std::unordered_map<Data, Data>>();
  157.         ASSERT_EQ(expected, actual);
  158.     }
  159. }
  160.