yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
rom_test.cc
Go to the documentation of this file.
1#include "app/rom.h"
2
3#include <gmock/gmock.h>
4#include <gtest/gtest.h>
5
6#include "absl/status/status.h"
7#include "absl/status/statusor.h"
8#include "test/core/testing.h"
10
11namespace yaze {
12namespace test {
13
14using ::testing::_;
15using ::testing::DoAll;
16using ::testing::Return;
17
18const static std::vector<uint8_t> kMockRomData = {
19 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
20 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
21 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
22};
23
24class MockRom : public app::Rom {
25 public:
26 MOCK_METHOD(absl::Status, WriteHelper, (const WriteAction&), (override));
27
28 MOCK_METHOD2(ReadHelper, absl::Status(uint8_t&, int));
29 MOCK_METHOD2(ReadHelper, absl::Status(uint16_t&, int));
30 MOCK_METHOD2(ReadHelper, absl::Status(std::vector<uint8_t>&, int));
31
32 MOCK_METHOD(absl::StatusOr<uint8_t>, ReadByte, (int));
33 MOCK_METHOD(absl::StatusOr<uint16_t>, ReadWord, (int));
34 MOCK_METHOD(absl::StatusOr<uint32_t>, ReadLong, (int));
35};
36
37class RomTest : public ::testing::Test {
38 protected:
40};
41
42TEST_F(RomTest, Uninitialized) {
43 EXPECT_EQ(rom_.size(), 0);
44 EXPECT_EQ(rom_.data(), nullptr);
45}
46
47TEST_F(RomTest, LoadFromFile) {
48#if defined(__linux__)
49 GTEST_SKIP();
50#endif
51 EXPECT_OK(rom_.LoadFromFile("test.sfc"));
52 EXPECT_EQ(rom_.size(), 0x200000);
53 EXPECT_NE(rom_.data(), nullptr);
54}
55
56TEST_F(RomTest, LoadFromFileInvalid) {
57 EXPECT_THAT(rom_.LoadFromFile("invalid.sfc"),
58 StatusIs(absl::StatusCode::kNotFound));
59 EXPECT_EQ(rom_.size(), 0);
60 EXPECT_EQ(rom_.data(), nullptr);
61}
62
63TEST_F(RomTest, LoadFromFileEmpty) {
64 EXPECT_THAT(rom_.LoadFromFile(""),
65 StatusIs(absl::StatusCode::kInvalidArgument));
66}
67
68TEST_F(RomTest, ReadByteOk) {
69 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
70
71 for (size_t i = 0; i < kMockRomData.size(); ++i) {
72 uint8_t byte;
73 ASSERT_OK_AND_ASSIGN(byte, rom_.ReadByte(i));
74 EXPECT_EQ(byte, kMockRomData[i]);
75 }
76}
77
78TEST_F(RomTest, ReadByteInvalid) {
79 EXPECT_THAT(rom_.ReadByte(0).status(),
80 StatusIs(absl::StatusCode::kFailedPrecondition));
81}
82
83TEST_F(RomTest, ReadWordOk) {
84 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
85
86 for (size_t i = 0; i < kMockRomData.size(); i += 2) {
87 // Little endian
88 EXPECT_THAT(
89 rom_.ReadWord(i),
90 IsOkAndHolds<uint16_t>((kMockRomData[i]) | kMockRomData[i + 1] << 8));
91 }
92}
93
94TEST_F(RomTest, ReadWordInvalid) {
95 EXPECT_THAT(rom_.ReadWord(0).status(),
96 StatusIs(absl::StatusCode::kFailedPrecondition));
97}
98
99TEST_F(RomTest, ReadLongOk) {
100 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
101
102 for (size_t i = 0; i < kMockRomData.size(); i += 4) {
103 // Little endian
104 EXPECT_THAT(rom_.ReadLong(i),
105 IsOkAndHolds<uint32_t>((kMockRomData[i]) | kMockRomData[i] |
106 kMockRomData[i + 1] << 8 |
107 kMockRomData[i + 2] << 16));
108 }
109}
110
111TEST_F(RomTest, ReadLongInvalid) {
112 EXPECT_THAT(rom_.ReadLong(0).status(),
113 StatusIs(absl::StatusCode::kFailedPrecondition));
114}
115
116TEST_F(RomTest, ReadBytesOk) {
117 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
118
119 std::vector<uint8_t> bytes;
120 ASSERT_OK_AND_ASSIGN(bytes, rom_.ReadByteVector(0, kMockRomData.size()));
121 EXPECT_THAT(bytes, ::testing::ContainerEq(kMockRomData));
122}
123
124TEST_F(RomTest, ReadBytesInvalid) {
125 EXPECT_THAT(rom_.ReadByteVector(0, 1).status(),
126 StatusIs(absl::StatusCode::kFailedPrecondition));
127}
128
129TEST_F(RomTest, ReadBytesOutOfRange) {
130 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
131
132 std::vector<uint8_t> bytes;
133 EXPECT_THAT(rom_.ReadByteVector(kMockRomData.size() + 1, 1).status(),
134 StatusIs(absl::StatusCode::kOutOfRange));
135}
136
137TEST_F(RomTest, WriteByteOk) {
138 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
139
140 for (size_t i = 0; i < kMockRomData.size(); ++i) {
141 EXPECT_OK(rom_.WriteByte(i, 0xFF));
142 uint8_t byte;
143 ASSERT_OK_AND_ASSIGN(byte, rom_.ReadByte(i));
144 EXPECT_EQ(byte, 0xFF);
145 }
146}
147
148TEST_F(RomTest, WriteByteInvalid) {
149 EXPECT_THAT(rom_.WriteByte(0, 0xFF),
150 StatusIs(absl::StatusCode::kFailedPrecondition));
151
152 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
153 EXPECT_THAT(rom_.WriteByte(kMockRomData.size(), 0xFF),
154 StatusIs(absl::StatusCode::kOutOfRange));
155}
156
157TEST_F(RomTest, WriteWordOk) {
158 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
159
160 for (size_t i = 0; i < kMockRomData.size(); i += 2) {
161 EXPECT_OK(rom_.WriteWord(i, 0xFFFF));
162 uint16_t word;
163 ASSERT_OK_AND_ASSIGN(word, rom_.ReadWord(i));
164 EXPECT_EQ(word, 0xFFFF);
165 }
166}
167
168TEST_F(RomTest, WriteWordInvalid) {
169 EXPECT_THAT(rom_.WriteWord(0, 0xFFFF),
170 StatusIs(absl::StatusCode::kFailedPrecondition));
171
172 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
173 EXPECT_THAT(rom_.WriteWord(kMockRomData.size(), 0xFFFF),
174 StatusIs(absl::StatusCode::kOutOfRange));
175}
176
177TEST_F(RomTest, WriteLongOk) {
178 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
179
180 for (size_t i = 0; i < kMockRomData.size(); i += 4) {
181 EXPECT_OK(rom_.WriteLong(i, 0xFFFFFF));
182 uint32_t word;
183 ASSERT_OK_AND_ASSIGN(word, rom_.ReadLong(i));
184 EXPECT_EQ(word, 0xFFFFFF);
185 }
186}
187
188TEST_F(RomTest, WriteLongInvalid) {
189 EXPECT_THAT(rom_.WriteLong(0, 0xFFFFFF),
190 StatusIs(absl::StatusCode::kFailedPrecondition));
191
192 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
193 EXPECT_THAT(rom_.WriteLong(kMockRomData.size(), 0xFFFFFFFF),
194 StatusIs(absl::StatusCode::kOutOfRange));
195}
196
197TEST_F(RomTest, WriteTransactionSuccess) {
198 MockRom mock_rom;
199 EXPECT_OK(mock_rom.LoadFromBytes(kMockRomData));
200
201 EXPECT_CALL(mock_rom, WriteHelper(_))
202 .WillRepeatedly(Return(absl::OkStatus()));
203
205 app::Rom::WriteAction{0x1000, uint8_t{0xFF}},
206 app::Rom::WriteAction{0x1001, uint16_t{0xABCD}},
207 app::Rom::WriteAction{0x1002, std::vector<uint8_t>{0x12, 0x34}}));
208}
209
210TEST_F(RomTest, WriteTransactionFailure) {
211 MockRom mock_rom;
212 EXPECT_OK(mock_rom.LoadFromBytes(kMockRomData));
213
214 EXPECT_CALL(mock_rom, WriteHelper(_))
215 .WillOnce(Return(absl::OkStatus()))
216 .WillOnce(Return(absl::InternalError("Write failed")));
217
218 EXPECT_EQ(mock_rom.WriteTransaction(
219 app::Rom::WriteAction{0x1000, uint8_t{0xFF}},
220 app::Rom::WriteAction{0x1001, uint16_t{0xABCD}}),
221 absl::InternalError("Write failed"));
222}
223
224TEST_F(RomTest, ReadTransactionSuccess) {
225 MockRom mock_rom;
226 EXPECT_OK(mock_rom.LoadFromBytes(kMockRomData));
227 uint8_t byte_val;
228 uint16_t word_val;
229
230 EXPECT_OK(mock_rom.ReadTransaction(byte_val, 0x0000, word_val, 0x0001));
231
232 EXPECT_EQ(byte_val, 0x00);
233 EXPECT_EQ(word_val, 0x0201);
234}
235
236TEST_F(RomTest, ReadTransactionFailure) {
237 MockRom mock_rom;
238 EXPECT_OK(mock_rom.LoadFromBytes(kMockRomData));
239 uint8_t byte_val;
240
241 EXPECT_EQ(mock_rom.ReadTransaction(byte_val, 0x1000),
242 absl::FailedPreconditionError("Offset out of range"));
243}
244
245} // namespace test
246} // namespace yaze
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:136
absl::Status WriteTransaction(Args... args)
Definition rom.h:418
absl::Status ReadTransaction(T &var, int address, Args &&... args)
Definition rom.h:426
absl::StatusOr< uint32_t > ReadLong(int offset)
Definition rom.h:270
virtual absl::Status WriteHelper(const WriteAction &action)
Definition rom.h:499
absl::StatusOr< uint16_t > ReadWord(int offset)
Definition rom.h:257
absl::Status ReadHelper(T &var, int address)
Definition rom.h:521
absl::StatusOr< uint8_t > ReadByte(int offset)
Definition rom.h:249
absl::Status LoadFromBytes(const std::vector< uint8_t > &data)
Definition rom.cc:266
MOCK_METHOD2(ReadHelper, absl::Status(uint8_t &, int))
MOCK_METHOD(absl::StatusOr< uint16_t >, ReadWord,(int))
MOCK_METHOD(absl::StatusOr< uint8_t >, ReadByte,(int))
MOCK_METHOD2(ReadHelper, absl::Status(std::vector< uint8_t > &, int))
MOCK_METHOD(absl::Status, WriteHelper,(const WriteAction &),(override))
MOCK_METHOD2(ReadHelper, absl::Status(uint16_t &, int))
MOCK_METHOD(absl::StatusOr< uint32_t >, ReadLong,(int))
TEST_F(RomTest, Uninitialized)
Definition rom_test.cc:42
::testing::AssertionResult IsOkAndHolds(const absl::StatusOr< T > &status_or, const T &value)
Definition testing.h:30
Definition common.cc:22
#define EXPECT_OK(expr)
Definition testing.h:10
#define ASSERT_OK_AND_ASSIGN(lhs, rexpr)
Definition testing.h:14