summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorbd <bdunahu@operationnull.com>2025-04-12 01:36:59 -0400
committerbd <bdunahu@operationnull.com>2025-04-12 01:36:59 -0400
commitb91eb002d4e6b2dc0c51b03df57c5089659ac669 (patch)
tree3ad405efc96f439a9e7af2bcac53c808a4667ef0 /tests
parent28a2788e2c59357d9269e558b0bd45db3241c42d (diff)
Add C11 test class header as base cache fixture class
Diffstat (limited to 'tests')
-rw-r--r--tests/c11.h47
-rw-r--r--tests/cache_1_1.cc (renamed from tests/cache.cc)78
-rw-r--r--tests/dram.cc38
3 files changed, 78 insertions, 85 deletions
diff --git a/tests/c11.h b/tests/c11.h
new file mode 100644
index 0000000..eb59cc0
--- /dev/null
+++ b/tests/c11.h
@@ -0,0 +1,47 @@
+#include "cache.h"
+#include "dram.h"
+#include "storage.h"
+#include <algorithm>
+#include <array>
+#include <catch2/catch_test_macros.hpp>
+#include <functional>
+
+class C11
+{
+ public:
+ C11() : m_delay(4), c_delay(2), mem(new int), fetch(new int)
+ {
+ this->c = new Cache(new Dram(this->m_delay), this->c_delay);
+ this->expected = {0, 0, 0, 0};
+ this->actual = this->c->view(0, 1)[0];
+ }
+
+ ~C11()
+ {
+ delete this->c;
+ delete this->mem;
+ delete this->fetch;
+ }
+
+ void
+ wait_then_do(int delay, std::function<int()> f)
+ {
+ for (int i = 0; i < delay; ++i) {
+ int r = f();
+
+ // check response
+ CHECK(!r);
+ // check for early modifications
+ actual = c->view(0, 1)[0];
+ REQUIRE(this->expected == this->actual);
+ }
+ }
+
+ int m_delay;
+ int c_delay;
+ Cache *c;
+ int *mem;
+ int *fetch;
+ std::array<signed int, LINE_SIZE> expected;
+ std::array<signed int, LINE_SIZE> actual;
+};
diff --git a/tests/cache.cc b/tests/cache_1_1.cc
index 313f93f..e3677a4 100644
--- a/tests/cache.cc
+++ b/tests/cache_1_1.cc
@@ -1,57 +1,9 @@
+#include "c11.h"
#include "cache.h"
#include "dram.h"
#include <catch2/catch_test_macros.hpp>
-class CacheFixture
-{
- public:
- CacheFixture()
- {
- this->m_delay = 4;
- this->c_delay = 2;
- this->d = new Dram(this->m_delay);
- this->c = new Cache(this->d, this->c_delay);
- this->mem = new int;
- this->fetch = new int;
- this->expected = {0, 0, 0, 0};
- this->actual = this->c->view(0, 1)[0];
- }
-
- ~CacheFixture()
- {
- delete this->c;
- delete this->mem;
- delete this->fetch;
- }
-
- /**
- * An operation that is done a lot.
- */
- void
- wait_for_storage(int delay, int expected, std::function<int()> f)
- {
- for (int i = 0; i < delay; ++i) {
- int r = f();
-
- // check response
- CHECK(r == expected);
- // check for early modifications
- actual = c->view(0, 1)[0];
- REQUIRE(this->expected == this->actual);
- }
- }
-
- int m_delay;
- int c_delay;
- Cache *c;
- Dram *d;
- int *mem;
- int *fetch;
- std::array<signed int, LINE_SIZE> expected;
- std::array<signed int, LINE_SIZE> actual;
-};
-
-TEST_CASE_METHOD(CacheFixture, "store 0th element in DELAY cycles", "[dram]")
+TEST_CASE_METHOD(C11, "store 0th element in DELAY cycles", "[dram]")
{
int r;
signed int w;
@@ -60,23 +12,19 @@ TEST_CASE_METHOD(CacheFixture, "store 0th element in DELAY cycles", "[dram]")
w = 0x11223344;
// delay + 1 due to internal logic, when mem
// finishes handle_miss still returns 'blocked'
- this->wait_for_storage(this->m_delay + this->c_delay + 1, 0, [this, w]() {
+ this->wait_then_do(this->m_delay + this->c_delay + 1, [this, w]() {
return this->c->write_word(this->mem, w, 0b0);
});
r = c->write_word(this->mem, w, 0b0);
CHECK(r);
- actual = this->d->view(0, 1)[0];
- // we do NOT write back now!
- REQUIRE(expected == actual);
-
expected.at(0) = w;
actual = c->view(0, 1)[0];
REQUIRE(expected == actual);
}
-TEST_CASE_METHOD(CacheFixture, "store 0th, 1st element in DELAY cycles, with conflict", "[cache]")
+TEST_CASE_METHOD(C11, "store 0th, 1st element in DELAY cycles, with conflict", "[cache]")
{
signed int w;
int r, i;
@@ -99,17 +47,13 @@ TEST_CASE_METHOD(CacheFixture, "store 0th, 1st element in DELAY cycles, with con
r = c->write_word(this->mem, w, 0b0);
CHECK(r);
- actual = d->view(0, 1)[0];
- // we do NOT write back now!
- REQUIRE(expected == actual);
-
expected.at(0) = w;
actual = c->view(0, 1)[0];
REQUIRE(expected == actual);
// this should have been loaded already!
- this->wait_for_storage(
- this->c_delay, 0, [this, w]() { return this->c->write_word(this->fetch, w, 0b1); });
+ this->wait_then_do(
+ this->c_delay, [this, w]() { return this->c->write_word(this->fetch, w, 0b1); });
r = c->write_word(this->fetch, w, 0b1);
CHECK(r);
@@ -120,7 +64,7 @@ TEST_CASE_METHOD(CacheFixture, "store 0th, 1st element in DELAY cycles, with con
}
TEST_CASE_METHOD(
- CacheFixture, "store 0th, 1st element different tags, in DELAY cycles, no conflict", "[cache]")
+ C11, "store 0th, 1st element different tags, in DELAY cycles, no conflict", "[cache]")
{
int r;
signed int w;
@@ -129,7 +73,7 @@ TEST_CASE_METHOD(
w = 0x11223344;
// delay + 1 due to internal logic, when mem
// finishes handle_miss still returns 'blocked'
- this->wait_for_storage(this->m_delay + this->c_delay + 1, 0, [this, w]() {
+ this->wait_then_do(this->m_delay + this->c_delay + 1, [this, w]() {
return this->c->write_word(this->mem, w, 0b0);
});
@@ -142,7 +86,7 @@ TEST_CASE_METHOD(
// write back to memory
// fetch new address (don't run the completion cycle yet)
- this->wait_for_storage(this->m_delay + this->m_delay + 1, 0, [this, w]() {
+ this->wait_then_do(this->m_delay + this->m_delay + 1, [this, w]() {
return this->c->write_word(this->fetch, w, 0b10000001);
});
@@ -154,8 +98,8 @@ TEST_CASE_METHOD(
actual = c->view(0, 1)[0];
CHECK(expected == actual);
- this->wait_for_storage(
- this->c_delay, 0, [this, w]() { return this->c->write_word(this->fetch, w, 0b10000001); });
+ this->wait_then_do(
+ this->c_delay, [this, w]() { return this->c->write_word(this->fetch, w, 0b10000001); });
r = c->write_word(this->fetch, w, 0b10000001);
CHECK(r);
diff --git a/tests/dram.cc b/tests/dram.cc
index 086ca4a..06a7720 100644
--- a/tests/dram.cc
+++ b/tests/dram.cc
@@ -2,10 +2,10 @@
#include <array>
#include <catch2/catch_test_macros.hpp>
-class DramFixture
+class D
{
public:
- DramFixture()
+ D()
{
this->delay = 3;
this->d = new Dram(this->delay);
@@ -15,7 +15,7 @@ class DramFixture
this->actual = this->d->view(0, 1)[0];
}
- ~DramFixture()
+ ~D()
{
delete this->d;
delete this->mem;
@@ -44,14 +44,15 @@ class DramFixture
std::array<signed int, LINE_SIZE> actual;
};
-TEST_CASE_METHOD(DramFixture, "store 0th element in DELAY cycles", "[dram]")
+TEST_CASE_METHOD(D, "store 0th element in DELAY cycles", "[dram]")
{
int r;
signed int w;
CHECK(expected == actual);
w = 0x11223344;
- this->wait_for_storage(this->delay, [this, w]() { return this->d->write_word(this->mem, w, 0x0); });
+ this->wait_for_storage(
+ this->delay, [this, w]() { return this->d->write_word(this->mem, w, 0x0); });
r = this->d->write_word(this->mem, w, 0x0);
@@ -61,14 +62,15 @@ TEST_CASE_METHOD(DramFixture, "store 0th element in DELAY cycles", "[dram]")
REQUIRE(expected == actual);
}
-TEST_CASE_METHOD(DramFixture, "store 0th, 1st element in DELAY cycles, no conflict", "[dram]")
+TEST_CASE_METHOD(D, "store 0th, 1st element in DELAY cycles, no conflict", "[dram]")
{
int r;
signed int w;
CHECK(expected == actual);
w = 0x11223344;
- this->wait_for_storage(this->delay, [this, w]() { return this->d->write_word(this->mem, w, 0x0); });
+ this->wait_for_storage(
+ this->delay, [this, w]() { return this->d->write_word(this->mem, w, 0x0); });
r = d->write_word(this->mem, w, 0x0);
REQUIRE(r);
@@ -77,7 +79,8 @@ TEST_CASE_METHOD(DramFixture, "store 0th, 1st element in DELAY cycles, no confli
actual = d->view(0, 1)[0];
REQUIRE(expected == actual);
- this->wait_for_storage(this->delay, [this, w]() { return this->d->write_word(this->fetch, w, 0x1); });
+ this->wait_for_storage(
+ this->delay, [this, w]() { return this->d->write_word(this->fetch, w, 0x1); });
r = d->write_word(this->fetch, w, 0x1);
CHECK(r);
@@ -87,7 +90,7 @@ TEST_CASE_METHOD(DramFixture, "store 0th, 1st element in DELAY cycles, no confli
REQUIRE(expected == actual);
}
-TEST_CASE_METHOD(DramFixture, "store 0th element in DELAY cycles with conflict", "[dram]")
+TEST_CASE_METHOD(D, "store 0th element in DELAY cycles with conflict", "[dram]")
{
int r, i;
signed int w;
@@ -112,7 +115,8 @@ TEST_CASE_METHOD(DramFixture, "store 0th element in DELAY cycles with conflict",
actual = d->view(0, 1)[0];
REQUIRE(expected == actual);
- this->wait_for_storage(this->delay, [this, w]() { return this->d->write_word(this->fetch, w, 0x1); });
+ this->wait_for_storage(
+ this->delay, [this, w]() { return this->d->write_word(this->fetch, w, 0x1); });
r = d->write_word(this->fetch, w, 0x1);
CHECK(r);
@@ -122,7 +126,7 @@ TEST_CASE_METHOD(DramFixture, "store 0th element in DELAY cycles with conflict",
REQUIRE(expected == actual);
}
-TEST_CASE_METHOD(DramFixture, "store line in DELAY cycles", "[dram]")
+TEST_CASE_METHOD(D, "store line in DELAY cycles", "[dram]")
{
int r;
signed int w;
@@ -142,7 +146,7 @@ TEST_CASE_METHOD(DramFixture, "store line in DELAY cycles", "[dram]")
REQUIRE(expected == actual);
}
-TEST_CASE_METHOD(DramFixture, "store line in DELAY cycles no conflict", "[dram]")
+TEST_CASE_METHOD(D, "store line in DELAY cycles no conflict", "[dram]")
{
int r;
signed int w;
@@ -173,7 +177,7 @@ TEST_CASE_METHOD(DramFixture, "store line in DELAY cycles no conflict", "[dram]"
REQUIRE(expected == actual);
}
-TEST_CASE_METHOD(DramFixture, "store line in DELAY cycles with conflict", "[dram]")
+TEST_CASE_METHOD(D, "store line in DELAY cycles with conflict", "[dram]")
{
int r, i;
signed int w;
@@ -212,8 +216,7 @@ TEST_CASE_METHOD(DramFixture, "store line in DELAY cycles with conflict", "[dram
REQUIRE(expected == actual);
}
-TEST_CASE_METHOD(
- DramFixture, "store line in DELAY cycles, read in DELAY cycles, no conflict", "[dram]")
+TEST_CASE_METHOD(D, "store line in DELAY cycles, read in DELAY cycles, no conflict", "[dram]")
{
int r, i, addr;
signed int w;
@@ -242,8 +245,7 @@ TEST_CASE_METHOD(
REQUIRE(expected == actual);
}
-TEST_CASE_METHOD(
- DramFixture, "store line in DELAY cycles, read in DELAY cycles with conflict", "[dram]")
+TEST_CASE_METHOD(D, "store line in DELAY cycles, read in DELAY cycles with conflict", "[dram]")
{
int r, i, addr;
signed int w;
@@ -276,7 +278,7 @@ TEST_CASE_METHOD(
}
TEST_CASE_METHOD(
- DramFixture,
+ D,
"store line in DELAY cycles, read one element at a time in DELAY cycles "
"with conflict",
"[dram]")