forked from UoB-HPC/SimEng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlatMemoryInterfaceTest.cc
81 lines (65 loc) · 2.76 KB
/
FlatMemoryInterfaceTest.cc
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
#include "gtest/gtest.h"
#include "simeng/memory/FlatMemoryInterface.hh"
namespace {
class FlatMemoryInterfaceTest : public testing::Test {
public:
FlatMemoryInterfaceTest() : memory(memoryData.data(), memorySize) {}
protected:
static constexpr uint16_t memorySize = 4;
std::array<char, memorySize> memoryData = {(char)0xFE, (char)0xCA, (char)0xBA,
(char)0xAB};
simeng::RegisterValue value = {0xDEADBEEF, 4};
simeng::RegisterValue value_oversized = {0xDEADBEEFDEADBEEF, 8};
simeng::memory::MemoryAccessTarget target = {0, 4};
simeng::memory::MemoryAccessTarget target_OutOfBound1 = {1000, 4};
simeng::memory::MemoryAccessTarget target_OutOfBound2 = {0, 8};
const std::string writeOverflowStr =
"Attempted to write beyond memory limit.";
simeng::memory::FlatMemoryInterface memory;
};
// Test that we can read data and it completes after zero cycles.
TEST_F(FlatMemoryInterfaceTest, FixedReadData) {
// Read a 32-bit value
memory.requestRead(target, 1);
auto entries = memory.getCompletedReads();
EXPECT_EQ(entries.size(), 1);
EXPECT_EQ(entries[0].requestId, 1);
EXPECT_EQ(entries[0].data, simeng::RegisterValue(0xABBACAFE, 4));
EXPECT_EQ(entries[0].target, target);
}
// Test that we can write data and it completes after zero cycles.
TEST_F(FlatMemoryInterfaceTest, FixedWriteData) {
// Write a 32-bit value to memory
memory.requestWrite(target, value);
EXPECT_EQ(reinterpret_cast<uint32_t*>(memoryData.data())[0], 0xDEADBEEF);
}
// Test that out-of-bounds memory reads are correctly handled.
TEST_F(FlatMemoryInterfaceTest, OutofBoundsRead) {
// Create a target such that address + size will overflow
memory.requestRead(target_OutOfBound1, 1);
// Create a regular out-of-bounds target
memory.requestRead(target_OutOfBound2, 2);
auto entries = memory.getCompletedReads();
EXPECT_EQ(entries.size(), 2);
auto overflowResult = entries[0];
EXPECT_EQ(overflowResult.requestId, 1);
EXPECT_FALSE(overflowResult.data);
EXPECT_EQ(overflowResult.target, target_OutOfBound1);
overflowResult = entries[1];
EXPECT_EQ(overflowResult.requestId, 2);
EXPECT_FALSE(overflowResult.data);
EXPECT_EQ(overflowResult.target, target_OutOfBound2);
}
// Test that out-of-bounds memory writes are correctly handled.
TEST_F(FlatMemoryInterfaceTest, OutofBoundsWrite_1) {
// Create a target such that address + size will overflow
ASSERT_DEATH(memory.requestWrite(target_OutOfBound1, value),
writeOverflowStr);
}
// Test that out-of-bounds memory writes are correctly handled.
TEST_F(FlatMemoryInterfaceTest, OutofBoundsWrite_2) {
// Create a regular out-of-bounds target
ASSERT_DEATH(memory.requestWrite(target_OutOfBound2, value_oversized),
writeOverflowStr);
}
} // namespace