File: rloperation_test.cc

package info (click to toggle)
rlvm 0.14-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,920 kB
  • ctags: 33,581
  • sloc: cpp: 91,570; ansic: 39,346; perl: 768; python: 181; sh: 170; makefile: 8
file content (283 lines) | stat: -rw-r--r-- 8,603 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
279
280
281
282
283
// -*- 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) 2009 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 <algorithm>
#include <string>
#include <vector>

#include "libreallive/archive.h"
#include "libreallive/expression.h"
#include "libreallive/intmemref.h"
#include "machine/rlmachine.h"
#include "machine/rloperation.h"
#include "machine/rloperation/argc_t.h"
#include "machine/rloperation/complex_t.h"
#include "machine/rloperation/default_value.h"
#include "machine/rloperation/references.h"
#include "test_system/test_system.h"

#include "test_utils.h"

using namespace std;
using namespace std::placeholders;
using namespace libreallive;

// -----------------------------------------------------------------------

template <class T>
void runDataTest(T& t, RLMachine& machine, const vector<string>& input) {
  ExpressionPiecesVector expression_pieces;
  vector<string> binary_strings;
  transform(input.begin(),
            input.end(),
            back_inserter(binary_strings),
            bind(&PrintableToParsableString, _1));

  t.ParseParameters(binary_strings, expression_pieces);
  t.Dispatch(machine, expression_pieces);
}

// -----------------------------------------------------------------------

class RLOperationTest : public FullSystemTest {};

// -----------------------------------------------------------------------

// Tests that we can parse an IntConstant_T.
struct IntcIntcCapturer : public RLOp_Void_2<IntConstant_T, IntConstant_T> {
  int& one_;
  int& two_;

  IntcIntcCapturer(int& one, int& two) : one_(one), two_(two) {}

  virtual void operator()(RLMachine& machine, int in_one, int in_two) {
    one_ = in_one;
    two_ = in_two;
  }
};

TEST_F(RLOperationTest, TestIntConstant_T) {
  int one = -1;
  int two = -1;
  IntcIntcCapturer capturer(one, two);

  vector<string> unparsed = {"$ FF 01 00 00 00", "$ FF 02 00 00 00"};
  runDataTest(capturer, rlmachine, unparsed);

  EXPECT_EQ(1, one);
  EXPECT_EQ(2, two);
}

// -----------------------------------------------------------------------

// Tests that we can parse an IntReference_T.
struct IntRefIntRefCapturer
    : public RLOp_Void_2<IntReference_T, IntReference_T> {
  int& one_;
  int& two_;

  IntRefIntRefCapturer(int& one, int& two) : one_(one), two_(two) {}

  virtual void operator()(RLMachine& machine,
                          IntReferenceIterator in_one,
                          IntReferenceIterator in_two) {
    one_ = *in_one;
    two_ = *in_two;
  }
};

TEST_F(RLOperationTest, TestIntReference_T) {
  rlmachine.SetIntValue(IntMemRef('A', 0), 1);
  rlmachine.SetIntValue(IntMemRef('B', 5), 2);

  int one = -1;
  int two = -1;
  IntRefIntRefCapturer capturer(one, two);

  vector<string> unparsed = {"$ 00 [ $ FF 00 00 00 00 ]",
                             "$ 01 [ $ FF 05 00 00 00 ]"};
  runDataTest(capturer, rlmachine, unparsed);

  EXPECT_EQ(1, one);
  EXPECT_EQ(2, two);
}

// -----------------------------------------------------------------------

struct StringcStringcCapturer
    : public RLOp_Void_2<StrConstant_T, StrConstant_T> {
  std::string& one_;
  std::string& two_;

  StringcStringcCapturer(std::string& one, std::string& two)
      : one_(one), two_(two) {}

  virtual void operator()(RLMachine& machine,
                          std::string in_one,
                          std::string in_two) {
    one_ = in_one;
    two_ = in_two;
  }
};

TEST_F(RLOperationTest, TestStringConstant_T) {
  std::string one = "empty";
  std::string two = "empty";
  StringcStringcCapturer capturer(one, two);

  vector<string> unparsed = {"\"string one\"", "\"string two\""};
  ExpressionPiecesVector expression_pieces;
  capturer.ParseParameters(unparsed, expression_pieces);
  capturer.Dispatch(rlmachine, expression_pieces);

  EXPECT_EQ("string one", one);
  EXPECT_EQ("string two", two);
}

// -----------------------------------------------------------------------

// Tests that we can parse an StrReference_T.
struct StrRefStrRefCapturer
    : public RLOp_Void_2<StrReference_T, StrReference_T> {
  std::string& one_;
  std::string& two_;

  StrRefStrRefCapturer(std::string& one, std::string& two)
      : one_(one), two_(two) {}

  virtual void operator()(RLMachine& machine,
                          StringReferenceIterator in_one,
                          StringReferenceIterator in_two) {
    one_ = *in_one;
    two_ = *in_two;
  }
};

TEST_F(RLOperationTest, TestStringReference_T) {
  rlmachine.SetStringValue(STRM_LOCATION, 0, "string one");
  rlmachine.SetStringValue(STRS_LOCATION, 5, "string two");

  std::string one = "empty";
  std::string two = "empty";
  StrRefStrRefCapturer capturer(one, two);

  vector<string> unparsed = {"$ 0C [ $ FF 00 00 00 00 ]",
                             "$ 12 [ $ FF 05 00 00 00 ]"};
  runDataTest(capturer, rlmachine, unparsed);

  EXPECT_EQ("string one", one);
  EXPECT_EQ("string two", two);
}

// -----------------------------------------------------------------------

struct ArgcCapturer : public RLOp_Void_1<Argc_T<IntConstant_T>> {
  std::vector<int>& out_;
  explicit ArgcCapturer(std::vector<int>& out) : out_(out) {}

  virtual void operator()(RLMachine& machine, std::vector<int> inputs) {
    copy(inputs.begin(), inputs.end(), back_inserter(out_));
  }
};

TEST_F(RLOperationTest, TestArgc_T) {
  vector<int> output;
  ArgcCapturer capturer(output);

  vector<string> unparsed = {"$ FF 09 00 00 00", "$ FF 03 00 00 00",
                             "$ FF 07 00 00 00", "$ FF 00 00 00 00"};
  runDataTest(capturer, rlmachine, unparsed);

  EXPECT_EQ(4, output.size());
  EXPECT_EQ(9, output[0]);
  EXPECT_EQ(3, output[1]);
  EXPECT_EQ(7, output[2]);
  EXPECT_EQ(0, output[3]);
}

// -----------------------------------------------------------------------

struct DefaultValueCapturer : public RLOp_Void_1<DefaultIntValue_T<18>> {
  int& out_;
  explicit DefaultValueCapturer(int& out) : out_(out) {}

  virtual void operator()(RLMachine& machine, int in) { out_ = in; }
};

TEST_F(RLOperationTest, TestDefaultIntValue_T) {
  int output = -1;
  DefaultValueCapturer capturer(output);
  vector<string> unparsed;
  runDataTest(capturer, rlmachine, unparsed);
  EXPECT_EQ(18, output) << "Uses default value with no arguments";

  unparsed.push_back("$ FF 04 00 00 00");
  runDataTest(capturer, rlmachine, unparsed);
  EXPECT_EQ(4, output) << "Returns argument";
}

// -----------------------------------------------------------------------

struct ComplexCapturer
    : public RLOp_Void_2<Complex2_T<IntConstant_T, IntConstant_T>,
                         Complex2_T<IntConstant_T, IntConstant_T>> {
  int& one_;
  int& two_;
  int& three_;
  int& four_;

  ComplexCapturer(int& one, int& two, int& three, int& four)
      : one_(one), two_(two), three_(three), four_(four) {}

  virtual void operator()(RLMachine& machine,
                          Complex2_T<IntConstant_T, IntConstant_T>::type one,
                          Complex2_T<IntConstant_T, IntConstant_T>::type two) {
    one_ = get<0>(one);
    two_ = get<1>(one);
    three_ = get<0>(two);
    four_ = get<1>(two);
  }
};

TEST_F(RLOperationTest, TestComplex2_T) {
  int one = -1;
  int two = -1;
  int three = -1;
  int four = -1;
  ComplexCapturer capturer(one, two, three, four);

  vector<string> unparsed = {"( $ FF 01 00 00 00 $ FF 02 00 00 00 )",
                             "( $ FF 03 00 00 00 $ FF 04 00 00 00 )"};
  runDataTest(capturer, rlmachine, unparsed);

  EXPECT_EQ(1, one);
  EXPECT_EQ(2, two);

  EXPECT_EQ(3, three);
  EXPECT_EQ(4, four);
}