1 // -*- Mode: C++; tab-width:2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi:tw=80:et:ts=2:sts=2
3 //
4 // -----------------------------------------------------------------------
5 //
6 // This file is part of RLVM, a RealLive virtual machine clone.
7 //
8 // -----------------------------------------------------------------------
9 //
10 // Copyright (C) 2006, 2007 Elliot Glaysher
11 //
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
25 //
26 // -----------------------------------------------------------------------
27
28 #include "gtest/gtest.h"
29
30 #include "libreallive/archive.h"
31 #include "libreallive/intmemref.h"
32 #include "machine/rlmachine.h"
33 #include "modules/module_mem.h"
34
35 #include "test_system/test_system.h"
36
37 #include "test_utils.h"
38
39 #include <iostream>
40 using namespace std;
41 using libreallive::IntMemRef;
42
43 // Tests setarray_0.
44 //
45 // Corresponding kepago listing:
46 //
47 // intA[3] = -1
48 // setarray(intA[0], 1, 2, 3)
49 //
TEST(LargeMemTest,setarray_0)50 TEST(LargeMemTest, setarray_0) {
51 libreallive::Archive arc(locateTestCase("Module_Mem_SEEN/setarray_0.TXT"));
52 TestSystem system;
53 RLMachine rlmachine(system, arc);
54 rlmachine.AttachModule(new MemModule);
55 rlmachine.ExecuteUntilHalted();
56
57 EXPECT_EQ(1, rlmachine.GetIntValue(IntMemRef('A', 0)))
58 << "setarray returned wrong value for intA[0]";
59 EXPECT_EQ(2, rlmachine.GetIntValue(IntMemRef('A', 1)))
60 << "setarray returned wrong value for intA[1]";
61 EXPECT_EQ(3, rlmachine.GetIntValue(IntMemRef('A', 2)))
62 << "setarray returned wrong value for intA[2]";
63 EXPECT_EQ(-1, rlmachine.GetIntValue(IntMemRef('A', 3)))
64 << "setarray touched the value in intA[3]!!";
65 }
66
67 // Tests setrng_0.
68 //
69 // Corresponding kepago listing:
70 //
71 // intA[4] = -1
72 // intA[0] = -1
73 // setrng(intA[0], intA[3])
TEST(LargeMemTest,setrng_0)74 TEST(LargeMemTest, setrng_0) {
75 libreallive::Archive arc(locateTestCase("Module_Mem_SEEN/setrng_0.TXT"));
76 TestSystem system;
77 RLMachine rlmachine(system, arc);
78 rlmachine.AttachModule(new MemModule);
79 rlmachine.ExecuteUntilHalted();
80
81 EXPECT_EQ(0, rlmachine.GetIntValue(IntMemRef('A', 0)))
82 << "setrng returned wrong value for intA[0]";
83 EXPECT_EQ(0, rlmachine.GetIntValue(IntMemRef('A', 1)))
84 << "setrng returned wrong value for intA[1]";
85 EXPECT_EQ(0, rlmachine.GetIntValue(IntMemRef('A', 2)))
86 << "setrng returned wrong value for intA[2]";
87 EXPECT_EQ(0, rlmachine.GetIntValue(IntMemRef('A', 3)))
88 << "setrng returned wrong value for intA[3]";
89 EXPECT_EQ(-1, rlmachine.GetIntValue(IntMemRef('A', 4)))
90 << "setrng touched the value in intA[4]!!!";
91 }
92
93 // Tests setrng_1.
94 //
95 // intA[4] = -1
96 // intA[0] = -1
97 // setrng(intA[0], intA[3], 4)
TEST(LargeMemTest,setrng_1)98 TEST(LargeMemTest, setrng_1) {
99 libreallive::Archive arc(locateTestCase("Module_Mem_SEEN/setrng_1.TXT"));
100 TestSystem system;
101 RLMachine rlmachine(system, arc);
102 rlmachine.AttachModule(new MemModule);
103 rlmachine.ExecuteUntilHalted();
104
105 EXPECT_EQ(4, rlmachine.GetIntValue(IntMemRef('A', 0)))
106 << "setrng returned wrong value for intA[0]";
107 EXPECT_EQ(4, rlmachine.GetIntValue(IntMemRef('A', 1)))
108 << "setrng returned wrong value for intA[1]";
109 EXPECT_EQ(4, rlmachine.GetIntValue(IntMemRef('A', 2)))
110 << "setrng returned wrong value for intA[2]";
111 EXPECT_EQ(4, rlmachine.GetIntValue(IntMemRef('A', 3)))
112 << "setrng returned wrong value for intA[3]";
113 EXPECT_EQ(-1, rlmachine.GetIntValue(IntMemRef('A', 4)))
114 << "setrng touched the value in intA[4]!!!";
115 }
116
117 // Tests cpyrng_0.
TEST(LargeMemTest,cpyrng_0)118 TEST(LargeMemTest, cpyrng_0) {
119 libreallive::Archive arc(locateTestCase("Module_Mem_SEEN/cpyrng_0.TXT"));
120 TestSystem system;
121 RLMachine rlmachine(system, arc);
122 rlmachine.AttachModule(new MemModule);
123 rlmachine.ExecuteUntilHalted();
124
125 // First make sure setarray did what we expected it to...
126 EXPECT_EQ(1, rlmachine.GetIntValue(IntMemRef('A', 0)))
127 << "setarray returned wrong value for intA[0]";
128 EXPECT_EQ(2, rlmachine.GetIntValue(IntMemRef('A', 1)))
129 << "setarray returned wrong value for intA[1]";
130 EXPECT_EQ(3, rlmachine.GetIntValue(IntMemRef('A', 2)))
131 << "setarray returned wrong value for intA[2]";
132
133 // Now make sure cpyrng did its job.
134 EXPECT_EQ(1, rlmachine.GetIntValue(IntMemRef('B', 0)))
135 << "cpyrng returned wrong value for intB[0]";
136 EXPECT_EQ(2, rlmachine.GetIntValue(IntMemRef('B', 1)))
137 << "cpyrng returned wrong value for intB[1]";
138 EXPECT_EQ(3, rlmachine.GetIntValue(IntMemRef('B', 2)))
139 << "cpyrng returned wrong value for intB[2]";
140 }
141
142 // Tests setarray_stepped_0.
143 //
144 // setrng(intA[0], intA[5], -1)
145 // setarray_stepped(intA[0], 2, 1, 2, 3)
146 //
TEST(LargeMemTest,setarray_stepped_0)147 TEST(LargeMemTest, setarray_stepped_0) {
148 libreallive::Archive arc(
149 locateTestCase("Module_Mem_SEEN/setarray_stepped_0.TXT"));
150 TestSystem system;
151 RLMachine rlmachine(system, arc);
152 rlmachine.AttachModule(new MemModule);
153 rlmachine.ExecuteUntilHalted();
154
155 // First make sure setarray_stepped did what we expected it to, and that it
156 // didn't overwrite the work of setrng
157 EXPECT_EQ(1, rlmachine.GetIntValue(IntMemRef('A', 0)))
158 << "setarray_stepped returned wrong value for intA[0]";
159 EXPECT_EQ(-1, rlmachine.GetIntValue(IntMemRef('A', 1)))
160 << "setarray_stepped touched the value of intA[1]";
161 EXPECT_EQ(2, rlmachine.GetIntValue(IntMemRef('A', 2)))
162 << "setarray_stepped returned wrong value for intA[2]";
163 EXPECT_EQ(-1, rlmachine.GetIntValue(IntMemRef('A', 3)))
164 << "setarray_stepped touched the value for intA[3]";
165 EXPECT_EQ(3, rlmachine.GetIntValue(IntMemRef('A', 4)))
166 << "setarray_stepped returned wrong value for intA[4]";
167 EXPECT_EQ(-1, rlmachine.GetIntValue(IntMemRef('A', 5)))
168 << "setarray_stepped touched the value for intA[5]";
169 }
170
171 // Tests setrng_stepped_0.
172 //
173 // setrng(intA[0], intA[5], -1)
174 // setrng_stepped(intA[0], 2, 3)
175 //
TEST(LargeMemTest,setrng_stepped_0)176 TEST(LargeMemTest, setrng_stepped_0) {
177 libreallive::Archive arc(
178 locateTestCase("Module_Mem_SEEN/setrng_stepped_0.TXT"));
179 TestSystem system;
180 RLMachine rlmachine(system, arc);
181 rlmachine.AttachModule(new MemModule);
182 rlmachine.ExecuteUntilHalted();
183
184 // First make sure setrng_stepped did what we expected it to, and that it
185 // didn't overwrite the work of setrng
186 EXPECT_EQ(0, rlmachine.GetIntValue(IntMemRef('A', 0)))
187 << "setrng_stepped returned wrong value for intA[0]";
188 EXPECT_EQ(-1, rlmachine.GetIntValue(IntMemRef('A', 1)))
189 << "setrng_stepped touched the value of intA[1]";
190 EXPECT_EQ(0, rlmachine.GetIntValue(IntMemRef('A', 2)))
191 << "setrng_stepped returned wrong value for intA[2]";
192 EXPECT_EQ(-1, rlmachine.GetIntValue(IntMemRef('A', 3)))
193 << "setrng_stepped touched the value for intA[3]";
194 EXPECT_EQ(0, rlmachine.GetIntValue(IntMemRef('A', 4)))
195 << "setrng_stepped returned wrong value for intA[4]";
196 EXPECT_EQ(-1, rlmachine.GetIntValue(IntMemRef('A', 5)))
197 << "setrng_stepped touched the value for intA[5]";
198 }
199
200 // Tests setrng_stepped_1.
201 //
202 // setrng(intA[0], intA[5], -1)
203 // setrng_stepped(intA[0], 2, 3, 5)
204 //
TEST(LargeMemTest,setrng_stepped_1)205 TEST(LargeMemTest, setrng_stepped_1) {
206 libreallive::Archive arc(
207 locateTestCase("Module_Mem_SEEN/setrng_stepped_1.TXT"));
208 TestSystem system;
209 RLMachine rlmachine(system, arc);
210 rlmachine.AttachModule(new MemModule);
211 rlmachine.ExecuteUntilHalted();
212
213 // First make sure setrng_stepped did what we expected it to, and that it
214 // didn't overwrite the work of setrng
215 EXPECT_EQ(5, rlmachine.GetIntValue(IntMemRef('A', 0)))
216 << "setrng_stepped returned wrong value for intA[0]";
217 EXPECT_EQ(-1, rlmachine.GetIntValue(IntMemRef('A', 1)))
218 << "setrng_stepped touched the value of intA[1]";
219 EXPECT_EQ(5, rlmachine.GetIntValue(IntMemRef('A', 2)))
220 << "setrng_stepped returned wrong value for intA[2]";
221 EXPECT_EQ(-1, rlmachine.GetIntValue(IntMemRef('A', 3)))
222 << "setrng_stepped touched the value for intA[3]";
223 EXPECT_EQ(5, rlmachine.GetIntValue(IntMemRef('A', 4)))
224 << "setrng_stepped returned wrong value for intA[4]";
225 EXPECT_EQ(-1, rlmachine.GetIntValue(IntMemRef('A', 5)))
226 << "setrng_stepped touched the value for intA[5]";
227 }
228
229 // Tests cpyvars.
230 //
231 // intB[3] = 5
232 // intB[5] = 1
233 // intB[8] = 2
234 // cpyvars(intA[0], 2, intB[1], intB[3], intB[6])
TEST(LargeMemTest,cpyvars)235 TEST(LargeMemTest, cpyvars) {
236 libreallive::Archive arc(locateTestCase("Module_Mem_SEEN/cpyvars_0.TXT"));
237 TestSystem system;
238 RLMachine rlmachine(system, arc);
239 rlmachine.AttachModule(new MemModule);
240 rlmachine.ExecuteUntilHalted();
241
242 // First make sure cpyvars did what we expected it to...
243 EXPECT_EQ(5, rlmachine.GetIntValue(IntMemRef('A', 0)))
244 << "cpyvars set wrong value for intA[0]";
245 EXPECT_EQ(1, rlmachine.GetIntValue(IntMemRef('A', 1)))
246 << "cpyvars set wrong value for intA[1]";
247 EXPECT_EQ(2, rlmachine.GetIntValue(IntMemRef('A', 2)))
248 << "cpyvars set wrong value for intA[2]";
249 }
250
251 // Tests sum.
252 //
253 // intA[0] = 0
254 // intA[1] = 1
255 // intA[2] = 2
256 // intA[3] = 3
257 // intA[10] = sum(intA[0], intA[3])
258 //
TEST(LargeMemTest,sum_0)259 TEST(LargeMemTest, sum_0) {
260 libreallive::Archive arc(locateTestCase("Module_Mem_SEEN/sum_0.TXT"));
261 TestSystem system;
262 RLMachine rlmachine(system, arc);
263 rlmachine.AttachModule(new MemModule);
264 rlmachine.ExecuteUntilHalted();
265
266 // First make sure sum did what we expected it to...
267 EXPECT_EQ(6, rlmachine.GetIntValue(IntMemRef('A', 10)))
268 << "sum returned the wrong value for intA[10]";
269 }
270