FunctionParams.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. //
  2. // Copyright (c) 2009 Brandon Jones
  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. int f0()
  28. {
  29. return 1;
  30. }
  31. int f1(int a1)
  32. {
  33. return (a1 == 1);
  34. }
  35. int f2(int a1, int a2)
  36. {
  37. return (a1 == 1) && (a2 == 2);
  38. }
  39. int f3(int a1, int a2, int a3)
  40. {
  41. return (a1 == 1) && (a2 == 2) && (a3 == 3);
  42. }
  43. int f4(int a1, int a2, int a3, int a4)
  44. {
  45. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4);
  46. }
  47. int f5(int a1, int a2, int a3, int a4, int a5)
  48. {
  49. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4) && (a5 == 5);
  50. }
  51. int f6(int a1, int a2, int a3, int a4, int a5, int a6)
  52. {
  53. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4) && (a5 == 5)
  54. && (a6 == 6);
  55. }
  56. int f7(int a1, int a2, int a3, int a4, int a5, int a6, int a7)
  57. {
  58. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4) && (a5 == 5)
  59. && (a6 == 6) && (a7 == 7);
  60. }
  61. int f8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  62. {
  63. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4) && (a5 == 5)
  64. && (a6 == 6) && (a7 == 7) && (a8 == 8);
  65. }
  66. int f9(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8,
  67. int a9)
  68. {
  69. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4) && (a5 == 5)
  70. && (a6 == 6) && (a7 == 7) && (a8 == 8) && (a9 == 9);
  71. }
  72. int f10(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8,
  73. int a9, int a10)
  74. {
  75. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4) && (a5 == 5)
  76. && (a6 == 6) && (a7 == 7) && (a8 == 8) && (a9 == 9) && (a10 == 10);
  77. }
  78. int f11(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8,
  79. int a9, int a10, int a11)
  80. {
  81. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4) && (a5 == 5)
  82. && (a6 == 6) && (a7 == 7) && (a8 == 8)&& (a9 == 9) && (a10 == 10)
  83. && (a11 == 11);
  84. }
  85. int f12(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8,
  86. int a9, int a10, int a11, int a12)
  87. {
  88. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4) && (a5 == 5)
  89. && (a6 == 6) && (a7 == 7) && (a8 == 8)&& (a9 == 9) && (a10 == 10)
  90. && (a11 == 11) && (a12 == 12);
  91. }
  92. int f13(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8,
  93. int a9, int a10, int a11, int a12, int a13)
  94. {
  95. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4) && (a5 == 5)
  96. && (a6 == 6) && (a7 == 7) && (a8 == 8) && (a9 == 9) && (a10 == 10)
  97. && (a11 == 11) && (a12 == 12) && (a13 == 13);
  98. }
  99. int f14(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8,
  100. int a9, int a10, int a11, int a12, int a13, int a14)
  101. {
  102. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4) && (a5 == 5)
  103. && (a6 == 6) && (a7 == 7) && (a8 == 8)&& (a9 == 9) && (a10 == 10)
  104. && (a11 == 11) && (a12 == 12) && (a13 == 13) && (a14 == 14);
  105. }
  106. TEST_F(SqratTest, GlobalFunction) {
  107. DefaultVM::Set(vm);
  108. RootTable().Func(_SC("f0"), &f0);
  109. RootTable().Func(_SC("f1"), &f1);
  110. RootTable().Func(_SC("f2"), &f2);
  111. RootTable().Func(_SC("f3"), &f3);
  112. RootTable().Func(_SC("f4"), &f4);
  113. RootTable().Func(_SC("f5"), &f5);
  114. RootTable().Func(_SC("f6"), &f6);
  115. RootTable().Func(_SC("f7"), &f7);
  116. RootTable().Func(_SC("f8"), &f8);
  117. RootTable().Func(_SC("f9"), &f9);
  118. RootTable().Func(_SC("f10"), &f10);
  119. RootTable().Func(_SC("f11"), &f11);
  120. RootTable().Func(_SC("f12"), &f12);
  121. RootTable().Func(_SC("f13"), &f13);
  122. RootTable().Func(_SC("f14"), &f14);
  123. Script script;
  124. script.CompileString(_SC(" \
  125. gTest.EXPECT_INT_EQ(1, f0()); print(0);\
  126. gTest.EXPECT_INT_EQ(1, f1(1)); print(1); \
  127. gTest.EXPECT_INT_EQ(1, f2(1, 2)); print(2);\
  128. gTest.EXPECT_INT_EQ(1, f3(1, 2, 3)); print(3);\
  129. gTest.EXPECT_INT_EQ(1, f4(1, 2, 3, 4)); print(4);\
  130. gTest.EXPECT_INT_EQ(1, f5(1, 2, 3, 4, 5)); print(5);\
  131. gTest.EXPECT_INT_EQ(1, f6(1, 2, 3, 4, 5, 6)); print(6);\
  132. gTest.EXPECT_INT_EQ(1, f7(1, 2, 3, 4, 5, 6, 7)); print(7);\
  133. gTest.EXPECT_INT_EQ(1, f8(1, 2, 3, 4, 5, 6, 7, 8)); print(8);\
  134. gTest.EXPECT_INT_EQ(1, f9(1, 2, 3, 4, 5, 6, 7, 8, 9)); print(9);\
  135. gTest.EXPECT_INT_EQ(1, f10(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); print(10);\
  136. gTest.EXPECT_INT_EQ(1, f11(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)); print(11);\
  137. gTest.EXPECT_INT_EQ(1, f12(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)); print(12);\
  138. gTest.EXPECT_INT_EQ(1, f13(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)); print(13);\
  139. gTest.EXPECT_INT_EQ(1, f14(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)); print(14);\
  140. "));
  141. if (Sqrat::Error::Occurred(vm)) {
  142. FAIL() << _SC("Compile Failed: ") << Sqrat::Error::Message(vm);
  143. }
  144. script.Run();
  145. if (Sqrat::Error::Occurred(vm)) {
  146. FAIL() << _SC("Run Failed: ") << Sqrat::Error::Message(vm);
  147. }
  148. }
  149. class C
  150. {
  151. public:
  152. int f0();
  153. int f1(int a1);
  154. int f2(int a1, int a2);
  155. int f3(int a1, int a2, int a3);
  156. int f4(int a1, int a2, int a3, int a4);
  157. int f5(int a1, int a2, int a3, int a4, int a5);
  158. int f6(int a1, int a2, int a3, int a4, int a5, int a6);
  159. int f7(int a1, int a2, int a3, int a4, int a5, int a6, int a7);
  160. int f8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8);
  161. int f9(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8,
  162. int a9);
  163. int f10(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8,
  164. int a9, int a10);
  165. int f11(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8,
  166. int a9, int a10, int a11);
  167. int f12(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8,
  168. int a9, int a10, int a11, int a12);
  169. int f13(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8,
  170. int a9, int a10, int a11, int a12, int a13);
  171. int f14(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8,
  172. int a9, int a10, int a11, int a12, int a13, int a14);
  173. };
  174. int C::f0()
  175. {
  176. return 1;
  177. }
  178. int C::f1(int a1)
  179. {
  180. return (a1 == 1);
  181. }
  182. int C::f2(int a1, int a2)
  183. {
  184. return (a1 == 1) && (a2 == 2);
  185. }
  186. int C::f3(int a1, int a2, int a3)
  187. {
  188. return (a1 == 1) && (a2 == 2) && (a3 == 3);
  189. }
  190. int C::f4(int a1, int a2, int a3, int a4)
  191. {
  192. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4);
  193. }
  194. int C::f5(int a1, int a2, int a3, int a4, int a5)
  195. {
  196. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4) && (a5 == 5);
  197. }
  198. int C::f6(int a1, int a2, int a3, int a4, int a5, int a6)
  199. {
  200. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4) && (a5 == 5)
  201. && (a6 == 6);
  202. }
  203. int C::f7(int a1, int a2, int a3, int a4, int a5, int a6, int a7)
  204. {
  205. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4) && (a5 == 5)
  206. && (a6 == 6) && (a7 == 7);
  207. }
  208. int C::f8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  209. {
  210. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4) && (a5 == 5)
  211. && (a6 == 6) && (a7 == 7) && (a8 == 8);
  212. }
  213. int C::f9(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8,
  214. int a9)
  215. {
  216. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4) && (a5 == 5)
  217. && (a6 == 6) && (a7 == 7) && (a8 == 8) && (a9 == 9);
  218. }
  219. int C::f10(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8,
  220. int a9, int a10)
  221. {
  222. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4) && (a5 == 5)
  223. && (a6 == 6) && (a7 == 7) && (a8 == 8) && (a9 == 9) && (a10 == 10);
  224. }
  225. int C::f11(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8,
  226. int a9, int a10, int a11)
  227. {
  228. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4) && (a5 == 5)
  229. && (a6 == 6) && (a7 == 7) && (a8 == 8)&& (a9 == 9) && (a10 == 10)
  230. && (a11 == 11);
  231. }
  232. int C::f12(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8,
  233. int a9, int a10, int a11, int a12)
  234. {
  235. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4) && (a5 == 5)
  236. && (a6 == 6) && (a7 == 7) && (a8 == 8)&& (a9 == 9) && (a10 == 10)
  237. && (a11 == 11) && (a12 == 12);
  238. }
  239. int C::f13(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8,
  240. int a9, int a10, int a11, int a12, int a13)
  241. {
  242. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4) && (a5 == 5)
  243. && (a6 == 6) && (a7 == 7) && (a8 == 8) && (a9 == 9) && (a10 == 10)
  244. && (a11 == 11) && (a12 == 12) && (a13 == 13);
  245. }
  246. int C::f14(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8,
  247. int a9, int a10, int a11, int a12, int a13, int a14)
  248. {
  249. return (a1 == 1)&& (a2 == 2) && (a3 == 3) && (a4 == 4) && (a5 == 5)
  250. && (a6 == 6) && (a7 == 7) && (a8 == 8)&& (a9 == 9) && (a10 == 10)
  251. && (a11 == 11) && (a12 == 12) && (a13 == 13) && (a14 == 14);
  252. }
  253. TEST_F(SqratTest, MemberFunction) {
  254. DefaultVM::Set(vm);
  255. Class<C> CC(vm, _SC("C"));
  256. CC.Func(_SC("f0"), &C::f0);
  257. CC.Func(_SC("f1"), &C::f1);
  258. CC.Func(_SC("f2"), &C::f2);
  259. CC.Func(_SC("f3"), &C::f3);
  260. CC.Func(_SC("f4"), &C::f4);
  261. CC.Func(_SC("f5"), &C::f5);
  262. CC.Func(_SC("f6"), &C::f6);
  263. CC.Func(_SC("f7"), &C::f7);
  264. CC.Func(_SC("f8"), &C::f8);
  265. CC.Func(_SC("f9"), &C::f9);
  266. CC.Func(_SC("f10"), &C::f10);
  267. CC.Func(_SC("f11"), &C::f11);
  268. CC.Func(_SC("f12"), &C::f12);
  269. CC.Func(_SC("f13"), &C::f13);
  270. CC.Func(_SC("f14"), &C::f14);
  271. RootTable().Bind(_SC("C"), CC);
  272. Script script;
  273. script.CompileString(_SC(" \
  274. c <- C(); \
  275. gTest.EXPECT_INT_EQ(1, c.f0()); print(0);\
  276. gTest.EXPECT_INT_EQ(1, c.f1(1)); print(1); \
  277. gTest.EXPECT_INT_EQ(1, c.f2(1, 2)); print(2);\
  278. gTest.EXPECT_INT_EQ(1, c.f3(1, 2, 3)); print(3);\
  279. gTest.EXPECT_INT_EQ(1, c.f4(1, 2, 3, 4)); print(4);\
  280. gTest.EXPECT_INT_EQ(1, c.f5(1, 2, 3, 4, 5)); print(5);\
  281. gTest.EXPECT_INT_EQ(1, c.f6(1, 2, 3, 4, 5, 6)); print(6);\
  282. gTest.EXPECT_INT_EQ(1, c.f7(1, 2, 3, 4, 5, 6, 7)); print(7);\
  283. gTest.EXPECT_INT_EQ(1, c.f8(1, 2, 3, 4, 5, 6, 7, 8)); print(8);\
  284. gTest.EXPECT_INT_EQ(1, c.f9(1, 2, 3, 4, 5, 6, 7, 8, 9)); print(9);\
  285. gTest.EXPECT_INT_EQ(1, c.f10(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); print(10);\
  286. gTest.EXPECT_INT_EQ(1, c.f11(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)); print(11);\
  287. gTest.EXPECT_INT_EQ(1, c.f12(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)); print(12);\
  288. gTest.EXPECT_INT_EQ(1, c.f13(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)); print(13);\
  289. gTest.EXPECT_INT_EQ(1, c.f14(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)); print(14);\
  290. "));
  291. if (Sqrat::Error::Occurred(vm)) {
  292. FAIL() << _SC("Compile Failed: ") << Sqrat::Error::Message(vm);
  293. }
  294. script.Run();
  295. if (Sqrat::Error::Occurred(vm)) {
  296. FAIL() << _SC("Run Failed: ") << Sqrat::Error::Message(vm);
  297. }
  298. }