File: rlmachine_test.cc

package info (click to toggle)
rlvm 0.14-5.2
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 17,104 kB
  • sloc: cpp: 91,574; ansic: 39,346; perl: 768; sh: 320; python: 181; makefile: 8
file content (278 lines) | stat: -rw-r--r-- 9,169 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// -*- Mode: C++; tab-width:2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi:tw=80:et:ts=2:sts=2
//
// -----------------------------------------------------------------------
//
// This file is part of RLVM, a RealLive virtual machine clone.
//
// -----------------------------------------------------------------------
//
// Copyright (C) 2007 Elliot Glaysher
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
//
// -----------------------------------------------------------------------

#include "gtest/gtest.h"

#include <iostream>
#include <utility>
#include <string>
#include <vector>

#include "machine/memory.h"
#include "machine/rlmachine.h"
#include "machine/serialization.h"
#include "modules/module_str.h"
#include "utilities/exception.h"
#include "libreallive/intmemref.h"
#include "test_utils.h"

using namespace std;
using namespace libreallive;

class RLMachineTest : public FullSystemTest {
 protected:
  void setIntMemoryCountingFrom(RLMachine& saveMachine,
                                const vector<pair<int, char>>& banks,
                                int count) {
    for (vector<pair<int, char>>::const_iterator it = banks.begin();
         it != banks.end();
         ++it) {
      for (int i = 0; i < SIZE_OF_MEM_BANK; ++i) {
        saveMachine.SetIntValue(IntMemRef(it->second, i), count);
        count++;
      }
    }
  }

  void setStrMemoryCountingFrom(RLMachine& saveMachine, int type, int count) {
    for (int i = 0; i < SIZE_OF_MEM_BANK; ++i) {
      saveMachine.SetStringValue(type, i, std::to_string(count));
      count++;
    }
  }

  void verifyIntMemoryCountingFrom(RLMachine& loadMachine,
                                   const vector<pair<int, char>>& banks,
                                   int count) {
    for (vector<pair<int, char>>::const_iterator it = banks.begin();
         it != banks.end();
         ++it) {
      for (int i = 0; i < SIZE_OF_MEM_BANK; ++i) {
        EXPECT_EQ(count, loadMachine.GetIntValue(IntMemRef(it->second, i)));
        count++;
      }
    }
  }

  void verifyStrMemoryCountingFrom(RLMachine& loadMachine,
                                   int type,
                                   int count) {
    for (int i = 0; i < SIZE_OF_MEM_BANK; ++i) {
      EXPECT_EQ(std::to_string(count), loadMachine.GetStringValue(type, i));
      count++;
    }
  }
};

TEST_F(RLMachineTest, RejectsDoubleAttachs) {
  rlmachine.AttachModule(new StrModule);
  EXPECT_THROW({ rlmachine.AttachModule(new StrModule); }, rlvm::Exception);
}

TEST_F(RLMachineTest, ReturnFromFarcallMismatch) {
  EXPECT_THROW({ rlmachine.ReturnFromFarcall(); }, rlvm::Exception);
}

TEST_F(RLMachineTest, ReturnFromGosubMismatch) {
  EXPECT_THROW({ rlmachine.ReturnFromGosub(); }, rlvm::Exception);
}

TEST_F(RLMachineTest, Halts) {
  EXPECT_TRUE(!rlmachine.halted()) << "Machine does not start halted.";
  rlmachine.Halt();
  EXPECT_TRUE(rlmachine.halted()) << "Machine is halted.";
}

TEST_F(RLMachineTest, RegisterStore) {
  for (int i = 0; i < 10; ++i) {
    rlmachine.set_store_register(i);
    EXPECT_EQ(i, rlmachine.store_register());
  }
}

// Test valid string memory access.
TEST_F(RLMachineTest, StringMemory) {
  vector<int> types = {STRK_LOCATION, STRM_LOCATION, STRS_LOCATION};

  for (vector<int>::const_iterator it = types.begin(); it != types.end();
       ++it) {
    const string str = "Stored at " + std::to_string(*it);
    rlmachine.SetStringValue(*it, 0, str);
    EXPECT_EQ(str, rlmachine.GetStringValue(*it, 0));
  }
}

// Test error-inducing, string memory access.
TEST_F(RLMachineTest, StringMemoryErrors) {
  EXPECT_THROW({ rlmachine.SetStringValue(STRM_LOCATION, 2000, "Blah"); },
               rlvm::Exception);
  EXPECT_THROW({ rlmachine.GetStringValue(STRM_LOCATION, 2000); },
               rlvm::Exception);
}

// Test valid integer access of all types.
//
// For reference to understand the following
// signed 32-bit integer: 10281 (0010 1000 0010 1001b)
//        8-bit integers: 0,0,40,41
//
// For    8-bit integers: 38,39,40,41
TEST_F(RLMachineTest, IntegerMemory) {
  vector<char> banks = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'L', 'Z'};

  const int in8b[] = {38, 39, 40, 41};
  const int base = (in8b[0] << 24) | (in8b[1] << 16) | (in8b[2] << 8) | in8b[3];

  const int rc = 8;
  const int final = (rc << 24) | (rc << 16) | (rc << 8) | rc;

  for (vector<char>::const_iterator it = banks.begin(); it != banks.end();
       ++it) {
    IntMemRef wordRef(*it, 0);
    rlmachine.SetIntValue(wordRef, base);
    EXPECT_EQ(base, rlmachine.GetIntValue(wordRef))
        << "Didn't record full value";

    for (int i = 0; i < 4; ++i) {
      IntMemRef comp(*it, "8b", i);
      EXPECT_EQ(in8b[3 - i], rlmachine.GetIntValue(comp))
          << "Could get partial value";

      rlmachine.SetIntValue(comp, rc);
    }

    EXPECT_EQ(final, rlmachine.GetIntValue(wordRef))
        << "Changing the components didn't change the full value!";
  }
}

TEST_F(RLMachineTest, IntegerMemoryErrors) {
  EXPECT_THROW({ rlmachine.GetIntValue(IntMemRef(10, 0, 0)); },
               rlvm::Exception);
  EXPECT_NO_THROW({ rlmachine.GetIntValue(IntMemRef('A', 1999)); });  // NOLINT
  EXPECT_THROW({ rlmachine.GetIntValue(IntMemRef('A', 2000)); },
               rlvm::Exception);
}

TEST_F(RLMachineTest, CheckNameLetterIndex) {
  EXPECT_EQ(0, Memory::ConvertLetterIndexToInt("A"));
  EXPECT_EQ(25, Memory::ConvertLetterIndexToInt("Z"));
  EXPECT_EQ(26, Memory::ConvertLetterIndexToInt("AA"));
  EXPECT_EQ(52, Memory::ConvertLetterIndexToInt("BA"));
  EXPECT_EQ(701, Memory::ConvertLetterIndexToInt("ZZ"));
}

TEST_F(RLMachineTest, NameStorage) {
  Memory& memory = rlmachine.memory();
  EXPECT_EQ("Bob", memory.GetName(Memory::ConvertLetterIndexToInt("A")));
  EXPECT_EQ("Alice",
            memory.GetLocalName(Memory::ConvertLetterIndexToInt("AB")));
}

TEST_F(RLMachineTest, Serialization) {
  stringstream ss;
  libreallive::Archive arc(locateTestCase("Module_Str_SEEN/strcpy_0.TXT"));
  // Save data
  {
    RLMachine saveMachine(system, arc);
    setIntMemoryCountingFrom(saveMachine, GLOBAL_INTEGER_BANKS, 0);
    setStrMemoryCountingFrom(saveMachine, STRM_LOCATION, 0);

    Serialization::saveGlobalMemoryTo(ss, saveMachine);
  }

  // Load data
  {
    RLMachine loadMachine(system, arc);
    Serialization::loadGlobalMemoryFrom(ss, loadMachine);
    verifyIntMemoryCountingFrom(loadMachine, GLOBAL_INTEGER_BANKS, 0);
    verifyStrMemoryCountingFrom(loadMachine, STRM_LOCATION, 0);
  }
}

// Tests serialization of the kidoku table.
TEST_F(RLMachineTest, SerializationOfKidoku) {
  stringstream ss;
  libreallive::Archive arc(locateTestCase("Module_Str_SEEN/strcpy_0.TXT"));

  // Save data
  {
    RLMachine saveMachine(system, arc);

    for (int i = 0; i < 10; i += 2) {
      saveMachine.memory().RecordKidoku(5, i);
    }

    Serialization::saveGlobalMemoryTo(ss, saveMachine);
  }

  // Load data
  {
    RLMachine loadMachine(system, arc);
    Serialization::loadGlobalMemoryFrom(ss, loadMachine);

    for (int i = 0; i < 10; i++) {
      EXPECT_EQ(!(i % 2), loadMachine.memory().HasBeenRead(5, i))
          << "Didn't save kidoku table correctly!";
    }
  }
}

TEST_F(RLMachineTest, SerializationOfSavepointValues) {
  stringstream ss;
  libreallive::Archive arc(locateTestCase("Module_Str_SEEN/strcpy_0.TXT"));
  // Save data
  {
    RLMachine saveMachine(system, arc);

    // Write the values we're going to check for.
    setIntMemoryCountingFrom(saveMachine, LOCAL_INTEGER_BANKS, 0);
    setStrMemoryCountingFrom(saveMachine, STRS_LOCATION, 0);
    saveMachine.MarkSavepoint();

    // Verify that those values are written.
    verifyIntMemoryCountingFrom(saveMachine, LOCAL_INTEGER_BANKS, 0);
    verifyStrMemoryCountingFrom(saveMachine, STRS_LOCATION, 0);

    // Scribble different values on top, immediately check to make sure we can
    // still read them, but don't commit them.
    setIntMemoryCountingFrom(saveMachine, LOCAL_INTEGER_BANKS, 5);
    setStrMemoryCountingFrom(saveMachine, STRS_LOCATION, 5);
    verifyIntMemoryCountingFrom(saveMachine, LOCAL_INTEGER_BANKS, 5);
    verifyStrMemoryCountingFrom(saveMachine, STRS_LOCATION, 5);

    Serialization::saveGameTo(ss, saveMachine);
  }

  // Load data. Assure that we only have the committed values.
  {
    RLMachine loadMachine(system, arc);
    Serialization::loadGameFrom(ss, loadMachine);
    verifyIntMemoryCountingFrom(loadMachine, LOCAL_INTEGER_BANKS, 0);
    verifyStrMemoryCountingFrom(loadMachine, STRS_LOCATION, 0);
  }
}