?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. // https://github.com/vinniefalco/LuaBridge
  2. // Copyright 2019, Dmitry Tarakanov
  3. // SPDX-License-Identifier: MIT
  4.  
  5. #include "TestBase.h"
  6. #include "TestTypes.h"
  7.  
  8. #include "LuaBridge/Array.h"
  9.  
  10. #include <array>
  11.  
  12. template<class T>
  13. struct ArrayTest : TestBase
  14. {
  15. };
  16.  
  17. TYPED_TEST_CASE_P(ArrayTest);
  18.  
  19. TYPED_TEST_P(ArrayTest, LuaRef)
  20. {
  21.     using Traits = TypeTraits<TypeParam>;
  22.  
  23.     this->runLua("result = {" + Traits::list() + "}");
  24.  
  25.     std::array<TypeParam, 3> expected;
  26.     std::copy_n(Traits::values().begin(), 3, expected.begin());
  27.  
  28.     std::array<TypeParam, 3> actual = this->result();
  29.     ASSERT_EQ(expected, actual);
  30. }
  31.  
  32. REGISTER_TYPED_TEST_CASE_P(ArrayTest, LuaRef);
  33.  
  34. INSTANTIATE_TYPED_TEST_CASE_P(ArrayTest, ArrayTest, TestTypes);
  35.  
  36. namespace {
  37.  
  38. struct Data
  39. {
  40.     /* explicit */ Data(int i = -1000) : i(i) {}
  41.  
  42.     int i;
  43. };
  44.  
  45. bool operator==(const Data& lhs, const Data& rhs)
  46. {
  47.     return lhs.i == rhs.i;
  48. }
  49.  
  50. std::ostream& operator<<(std::ostream& lhs, const Data& rhs)
  51. {
  52.     lhs << "{" << rhs.i << "}";
  53.     return lhs;
  54. }
  55.  
  56. template<std::size_t S>
  57. std::array<Data, S> processValues(const std::array<Data, S>& data)
  58. {
  59.     return data;
  60. }
  61.  
  62. template<std::size_t S>
  63. std::array<Data, S> processPointers(const std::array<const Data*, S>& data)
  64. {
  65.     std::array<Data, S> result;
  66.     std::size_t arrayIndex = 0;
  67.     for (const auto* item : data)
  68.     {
  69.         result[arrayIndex] = *item;
  70.         ++arrayIndex;
  71.     }
  72.     return result;
  73. }
  74.  
  75. } // namespace
  76.  
  77. struct ArrayTests : TestBase
  78. {
  79. };
  80.  
  81. TEST_F(ArrayTests, PassFromLua)
  82. {
  83.     luabridge::getGlobalNamespace(L)
  84.         .beginClass<Data>("Data")
  85.         .addConstructor<void (*)(int)>()
  86.         .endClass()
  87.         .addFunction("processValues", &processValues<3>)
  88.         .addFunction("processPointers", &processPointers<3>);
  89.  
  90.     resetResult();
  91.     runLua("result = processValues ({Data (-1), Data (2), Data (5)})");
  92.  
  93.     ASSERT_EQ((std::array<Data, 3>({-1, 2, 5})), (result<std::array<Data, 3>>()));
  94.  
  95.     resetResult();
  96.     runLua("result = processPointers ({Data (-3), Data (4), Data (9)})");
  97.  
  98.     ASSERT_EQ((std::array<Data, 3>({-3, 4, 9})), (result<std::array<Data, 3>>()));
  99. }
  100.