ArrayBinding.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // Copyright 2013 Li-Cheng (Andy) Tai
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. //
  8. // Permission is granted to anyone to use this software for any purpose,
  9. // including commercial applications, and to alter it and redistribute it
  10. // freely, subject to the following restrictions:
  11. //
  12. // 1. The origin of this software must not be misrepresented; you must not
  13. // claim that you wrote the original software. If you use this software
  14. // in a product, an acknowledgment in the product documentation would be
  15. // appreciated but is not required.
  16. //
  17. // 2. Altered source versions must be plainly marked as such, and must not be
  18. // misrepresented as being the original software.
  19. //
  20. // 3. This notice may not be removed or altered from any source
  21. // distribution.
  22. //
  23. #include <gtest/gtest.h>
  24. #include <sqrat.h>
  25. #include "Fixture.h"
  26. using namespace Sqrat;
  27. TEST_F(SqratTest, ArrayGet) {
  28. static const SQChar *sq_code = _SC("\
  29. local i; \
  30. for (i = 0; i < 12; i++) \
  31. a.append(i);\
  32. \
  33. ");
  34. int i;
  35. DefaultVM::Set(vm);
  36. Array array(vm);
  37. RootTable(vm).Bind(_SC("a"), array);
  38. Script script;
  39. script.CompileString(sq_code);
  40. if (Sqrat::Error::Occurred(vm)) {
  41. FAIL() << _SC("Compile Failed: ") << Sqrat::Error::Message(vm);
  42. }
  43. script.Run();
  44. if (Sqrat::Error::Occurred(vm)) {
  45. FAIL() << _SC("Run Failed: ") << Sqrat::Error::Message(vm);
  46. }
  47. const int length = array.Length();
  48. EXPECT_EQ(length, 12);
  49. for ( i = 0; i < length; i++)
  50. {
  51. SharedPtr<int> t = array.GetValue<int>(i);
  52. EXPECT_EQ(t != NULL, 1);
  53. EXPECT_EQ(*t, i);
  54. SharedPtr<float> f;
  55. f = array.GetValue<float>(i);
  56. EXPECT_EQ(f != NULL, 1);
  57. EXPECT_EQ((float) i, *f);
  58. }
  59. int t[length];
  60. array.GetArray(t, sizeof(t) / sizeof(t[0]));
  61. EXPECT_FALSE(Sqrat::Error::Occurred(vm));
  62. for (i = 0; i < length; i++)
  63. {
  64. EXPECT_EQ(t[i], i);
  65. }
  66. double d[length];
  67. array.GetArray(d, sizeof(d) / sizeof(d[0]));
  68. EXPECT_FALSE(Sqrat::Error::Occurred(vm));
  69. for (i = 0; i < length; i++)
  70. {
  71. EXPECT_EQ(d[i], (double) i);
  72. }
  73. double d2[15];
  74. array.GetArray(d2, sizeof(d2) / sizeof(d2[0]));
  75. EXPECT_TRUE(Sqrat::Error::Occurred(vm));
  76. Sqrat::Error::Clear(vm);
  77. double d3[5];
  78. array.GetArray(d3, sizeof(d3) / sizeof(d3[0]));
  79. EXPECT_FALSE(Sqrat::Error::Occurred(vm));
  80. }
  81. void touch_element(Sqrat::Array & a, int index, int val)
  82. {
  83. a.SetValue(index, val);
  84. }
  85. void touch_element2(Sqrat::Array a, int index, int val)
  86. {
  87. a.SetValue(index, val);
  88. }
  89. TEST_F(SqratTest, PassingArrayIn) {
  90. static const int SIZE = 56;
  91. static const SQChar *sq_code = _SC("\
  92. local i; \
  93. for (i = 0; i < a.len(); i++) \
  94. touch_element2(a, i, 5 - i);\
  95. \
  96. for (i = 0; i < a.len(); i++) \
  97. gTest.EXPECT_INT_EQ( a[i], 5 - i);\
  98. \
  99. for (i = 0; i < a.len(); i++) \
  100. touch_element(a, i, -i);\
  101. \
  102. for (i = 0; i < a.len(); i++) \
  103. gTest.EXPECT_INT_EQ( a[i], - i);\
  104. \
  105. ");
  106. DefaultVM::Set(vm);
  107. RootTable().Func(_SC("touch_element"), &touch_element);
  108. RootTable().Func(_SC("touch_element2"), &touch_element2);
  109. int i;
  110. Array array(vm, SIZE);
  111. RootTable(vm).Bind(_SC("a"), array);
  112. for (i = 0; i < SIZE; i++)
  113. touch_element(array, i, i);
  114. int length = array.Length();
  115. EXPECT_EQ(length, SIZE);
  116. for (i = 0; i < length; i++)
  117. {
  118. SharedPtr<int> t = array.GetValue<int>(i);
  119. EXPECT_EQ(t != NULL, 1);
  120. EXPECT_EQ(*t, i);
  121. }
  122. Script script;
  123. script.CompileString(sq_code);
  124. if (Sqrat::Error::Occurred(vm)) {
  125. FAIL() << _SC("Compile Failed: ") << Sqrat::Error::Message(vm);
  126. }
  127. script.Run();
  128. if (Sqrat::Error::Occurred(vm)) {
  129. FAIL() << _SC("Run Failed: ") << Sqrat::Error::Message(vm);
  130. }
  131. length = array.Length();
  132. EXPECT_EQ(length, SIZE);
  133. for (i = 0; i < length; i++)
  134. {
  135. SharedPtr<int> t = array.GetValue<int>(i);
  136. EXPECT_EQ(t != NULL, 1);
  137. EXPECT_EQ(*t, -i);
  138. }
  139. }
  140. TEST_F(SqratTest, PassingArrayIn2) {
  141. static const int SIZE = 56;
  142. static const SQChar *sq_code = _SC("\
  143. local i; \
  144. local a2 = array(12); \
  145. for (i = 0; i < a2.len(); i++) \
  146. touch_element2(a2, i, 1 - i); \
  147. \
  148. for (i = 0; i < a2.len(); i++) \
  149. gTest.EXPECT_INT_EQ( a2[i], 1 - i); \
  150. \
  151. for (i = 0; i < a2.len(); i++) \
  152. touch_element(a2, i, 1 + i); \
  153. \
  154. for (i = 0; i < a2.len(); i++) \
  155. gTest.EXPECT_INT_EQ( a2[i], 1 + i); \
  156. \
  157. ");
  158. DefaultVM::Set(vm);
  159. RootTable().Func(_SC("touch_element2"), &touch_element2);
  160. RootTable().Func(_SC("touch_element"), &touch_element);
  161. Script script;
  162. script.CompileString(sq_code);
  163. if (Sqrat::Error::Occurred(vm)) {
  164. FAIL() << _SC("Compile Failed: ") << Sqrat::Error::Message(vm);
  165. }
  166. script.Run();
  167. if (Sqrat::Error::Occurred(vm)) {
  168. FAIL() << _SC("Run Failed: ") << Sqrat::Error::Message(vm);
  169. }
  170. }