3#include <gmock/gmock.h>
4#include <gtest/gtest.h>
6#include "absl/status/status.h"
7#include "absl/status/statusor.h"
15using ::testing::DoAll;
16using ::testing::Return;
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,
43 EXPECT_EQ(rom_.size(), 0);
44 EXPECT_EQ(rom_.data(),
nullptr);
52 EXPECT_EQ(rom_.size(), 0x200000);
53 EXPECT_NE(rom_.data(),
nullptr);
57 EXPECT_THAT(rom_.LoadFromFile(
"invalid.sfc"),
58 StatusIs(absl::StatusCode::kNotFound));
59 EXPECT_EQ(rom_.size(), 0);
60 EXPECT_EQ(rom_.data(),
nullptr);
64 EXPECT_THAT(rom_.LoadFromFile(
""),
65 StatusIs(absl::StatusCode::kInvalidArgument));
69 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
71 for (
size_t i = 0; i < kMockRomData.size(); ++i) {
74 EXPECT_EQ(
byte, kMockRomData[i]);
79 EXPECT_THAT(rom_.ReadByte(0).status(),
80 StatusIs(absl::StatusCode::kFailedPrecondition));
84 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
86 for (
size_t i = 0; i < kMockRomData.size(); i += 2) {
95 EXPECT_THAT(rom_.ReadWord(0).status(),
96 StatusIs(absl::StatusCode::kFailedPrecondition));
100 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
102 for (
size_t i = 0; i < kMockRomData.size(); i += 4) {
104 EXPECT_THAT(rom_.ReadLong(i),
106 kMockRomData[i + 1] << 8 |
107 kMockRomData[i + 2] << 16));
112 EXPECT_THAT(rom_.ReadLong(0).status(),
113 StatusIs(absl::StatusCode::kFailedPrecondition));
117 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
119 std::vector<uint8_t> bytes;
121 EXPECT_THAT(bytes, ::testing::ContainerEq(kMockRomData));
125 EXPECT_THAT(rom_.ReadByteVector(0, 1).status(),
126 StatusIs(absl::StatusCode::kFailedPrecondition));
130 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
132 std::vector<uint8_t> bytes;
133 EXPECT_THAT(rom_.ReadByteVector(kMockRomData.size() + 1, 1).status(),
134 StatusIs(absl::StatusCode::kOutOfRange));
138 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
140 for (
size_t i = 0; i < kMockRomData.size(); ++i) {
144 EXPECT_EQ(
byte, 0xFF);
149 EXPECT_THAT(rom_.WriteByte(0, 0xFF),
150 StatusIs(absl::StatusCode::kFailedPrecondition));
152 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
153 EXPECT_THAT(rom_.WriteByte(kMockRomData.size(), 0xFF),
154 StatusIs(absl::StatusCode::kOutOfRange));
158 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
160 for (
size_t i = 0; i < kMockRomData.size(); i += 2) {
164 EXPECT_EQ(word, 0xFFFF);
169 EXPECT_THAT(rom_.WriteWord(0, 0xFFFF),
170 StatusIs(absl::StatusCode::kFailedPrecondition));
172 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
173 EXPECT_THAT(rom_.WriteWord(kMockRomData.size(), 0xFFFF),
174 StatusIs(absl::StatusCode::kOutOfRange));
178 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
180 for (
size_t i = 0; i < kMockRomData.size(); i += 4) {
184 EXPECT_EQ(word, 0xFFFFFF);
189 EXPECT_THAT(rom_.WriteLong(0, 0xFFFFFF),
190 StatusIs(absl::StatusCode::kFailedPrecondition));
192 EXPECT_OK(rom_.LoadFromBytes(kMockRomData));
193 EXPECT_THAT(rom_.WriteLong(kMockRomData.size(), 0xFFFFFFFF),
194 StatusIs(absl::StatusCode::kOutOfRange));
201 EXPECT_CALL(mock_rom, WriteHelper(_))
202 .WillRepeatedly(Return(absl::OkStatus()));
207 app::Rom::WriteAction{0x1002, std::vector<uint8_t>{0x12, 0x34}}));
214 EXPECT_CALL(mock_rom, WriteHelper(_))
215 .WillOnce(Return(absl::OkStatus()))
216 .WillOnce(Return(absl::InternalError(
"Write failed")));
221 absl::InternalError(
"Write failed"));
232 EXPECT_EQ(byte_val, 0x00);
233 EXPECT_EQ(word_val, 0x0201);
242 absl::FailedPreconditionError(
"Offset out of range"));
The Rom class is used to load, save, and modify Rom data.
absl::Status WriteTransaction(Args... args)
absl::Status ReadTransaction(T &var, int address, Args &&... args)
absl::StatusOr< uint32_t > ReadLong(int offset)
virtual absl::Status WriteHelper(const WriteAction &action)
absl::StatusOr< uint16_t > ReadWord(int offset)
absl::Status ReadHelper(T &var, int address)
absl::StatusOr< uint8_t > ReadByte(int offset)
absl::Status LoadFromBytes(const std::vector< uint8_t > &data)
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)
::testing::AssertionResult IsOkAndHolds(const absl::StatusOr< T > &status_or, const T &value)
#define ASSERT_OK_AND_ASSIGN(lhs, rexpr)