1 #include "../Src/th_lua.h"
2 #include "catch2/catch.hpp"
3 
4 TEST_CASE("luaT_rotate works", "[luaT_rotate]") {
5   lua_State* L = luaL_newstate();
6   lua_pushnumber(L, 1);
7   lua_pushnumber(L, 2);
8   lua_pushnumber(L, 3);
9   lua_pushnumber(L, 4);
10   lua_pushnumber(L, 5);
11   lua_pushnumber(L, 6);
12   lua_pushnumber(L, 7);
13 
14   REQUIRE(lua_gettop(L) == 7);
15 
16   SECTION("rotate 0") {
17     luaT_rotate(L, 1, 0);
18 
19     REQUIRE(lua_tonumber(L, 1) == 1);
20     REQUIRE(lua_tonumber(L, 2) == 2);
21     REQUIRE(lua_tonumber(L, 3) == 3);
22     REQUIRE(lua_tonumber(L, 4) == 4);
23     REQUIRE(lua_tonumber(L, 5) == 5);
24     REQUIRE(lua_tonumber(L, 6) == 6);
25     REQUIRE(lua_tonumber(L, 7) == 7);
26   }
27 
28   SECTION("rotate all up 1") {
29     luaT_rotate(L, 1, 1);
30 
31     REQUIRE(lua_tonumber(L, 1) == 7);
32     REQUIRE(lua_tonumber(L, 2) == 1);
33     REQUIRE(lua_tonumber(L, 3) == 2);
34     REQUIRE(lua_tonumber(L, 4) == 3);
35     REQUIRE(lua_tonumber(L, 5) == 4);
36     REQUIRE(lua_tonumber(L, 6) == 5);
37     REQUIRE(lua_tonumber(L, 7) == 6);
38   }
39 
40   SECTION("rotate all down 1") {
41     luaT_rotate(L, 1, -1);
42 
43     REQUIRE(lua_tonumber(L, 1) == 2);
44     REQUIRE(lua_tonumber(L, 2) == 3);
45     REQUIRE(lua_tonumber(L, 3) == 4);
46     REQUIRE(lua_tonumber(L, 4) == 5);
47     REQUIRE(lua_tonumber(L, 5) == 6);
48     REQUIRE(lua_tonumber(L, 6) == 7);
49     REQUIRE(lua_tonumber(L, 7) == 1);
50   }
51 
52   SECTION("rotate all up 2") {
53     luaT_rotate(L, 1, 2);
54 
55     REQUIRE(lua_tonumber(L, 1) == 6);
56     REQUIRE(lua_tonumber(L, 2) == 7);
57     REQUIRE(lua_tonumber(L, 3) == 1);
58     REQUIRE(lua_tonumber(L, 4) == 2);
59     REQUIRE(lua_tonumber(L, 5) == 3);
60     REQUIRE(lua_tonumber(L, 6) == 4);
61     REQUIRE(lua_tonumber(L, 7) == 5);
62   }
63 
64   SECTION("rotate all down 2") {
65     luaT_rotate(L, 1, -2);
66 
67     REQUIRE(lua_tonumber(L, 1) == 3);
68     REQUIRE(lua_tonumber(L, 2) == 4);
69     REQUIRE(lua_tonumber(L, 3) == 5);
70     REQUIRE(lua_tonumber(L, 4) == 6);
71     REQUIRE(lua_tonumber(L, 5) == 7);
72     REQUIRE(lua_tonumber(L, 6) == 1);
73     REQUIRE(lua_tonumber(L, 7) == 2);
74   }
75 
76   SECTION("rotate from 3rd up 1") {
77     luaT_rotate(L, 3, 1);
78 
79     REQUIRE(lua_tonumber(L, 1) == 1);
80     REQUIRE(lua_tonumber(L, 2) == 2);
81     REQUIRE(lua_tonumber(L, 3) == 7);
82     REQUIRE(lua_tonumber(L, 4) == 3);
83     REQUIRE(lua_tonumber(L, 5) == 4);
84     REQUIRE(lua_tonumber(L, 6) == 5);
85     REQUIRE(lua_tonumber(L, 7) == 6);
86   }
87 
88   SECTION("rotate from -3rd up 1") {
89     luaT_rotate(L, -3, 1);
90 
91     REQUIRE(lua_tonumber(L, 1) == 1);
92     REQUIRE(lua_tonumber(L, 2) == 2);
93     REQUIRE(lua_tonumber(L, 3) == 3);
94     REQUIRE(lua_tonumber(L, 4) == 4);
95     REQUIRE(lua_tonumber(L, 5) == 7);
96     REQUIRE(lua_tonumber(L, 6) == 5);
97     REQUIRE(lua_tonumber(L, 7) == 6);
98   }
99 
100   SECTION("rotate from -5th down 2") {
101     luaT_rotate(L, -5, -2);
102 
103     REQUIRE(lua_tonumber(L, 1) == 1);
104     REQUIRE(lua_tonumber(L, 2) == 2);
105     REQUIRE(lua_tonumber(L, 3) == 5);
106     REQUIRE(lua_tonumber(L, 4) == 6);
107     REQUIRE(lua_tonumber(L, 5) == 7);
108     REQUIRE(lua_tonumber(L, 6) == 3);
109     REQUIRE(lua_tonumber(L, 7) == 4);
110   }
111 
112   lua_close(L);
113 }
114