Skip to content

Commit

Permalink
Complete clock class tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wfd3 committed Feb 28, 2024
1 parent a2b3d83 commit 5715418
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
10 changes: 6 additions & 4 deletions clock/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ class BusClock_t {
_accumulatedCycles = 0;
}

uint64_t delay(const uint64_t cycles = 1) {
bool delay(const uint64_t cycles = 1) {
_accumulatedCycles += cycles;

if (_accumulatedCycles > _cyclesInDelayTime) {
if (_accumulatedCycles >= _cyclesInDelayTime) {
_accumulatedCycles -= _cyclesInDelayTime;
if (_emulateTiming)
if (_emulateTiming) {
std::this_thread::sleep_for(_delayMs);
return true;
}
}

return _accumulatedCycles;
return false;
}

freq_t getFrequencyMHz() { return _MHz; }
Expand Down
63 changes: 59 additions & 4 deletions tests/clock_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,71 @@ class ClockTests : public testing::Test {
virtual void TearDown() {}
};

TEST_F(ClockTests, BelowThresholdTakesNoTime) {
TEST_F(ClockTests, BelowThresholdDoesNotDelay) {
uint64_t count = 100;
BusClock_t clock(1);
bool didDelay = false;

clock.enableTimingEmulation();

while(count--) {
didDelay |= clock.delay(1);
}

EXPECT_FALSE(didDelay);
}

TEST_F(ClockTests, AboveThresholdDelays) {
BusClock_t clock(1);
uint64_t count = clock.getCyclesInDelayTime();
bool didDelay = false;

clock.enableTimingEmulation();

while(count--) {
clock.delay(1);
didDelay |= clock.delay(1);
}

EXPECT_TRUE(didDelay);
}

TEST_F(ClockTests, BelowThresholdDoesNotDelayAt4MHz) {
uint64_t count = 100;
BusClock_t clock(4);
bool didDelay = false;

clock.enableTimingEmulation();

while(count--) {
didDelay |= clock.delay(1);
}

EXPECT_FALSE(didDelay);
}

TEST_F(ClockTests, AboveThresholdDelaysAt4MHz) {
BusClock_t clock(4);
uint64_t count = clock.getCyclesInDelayTime();
bool didDelay = false;

clock.enableTimingEmulation();

while(count--) {
didDelay |= clock.delay(1);
}

EXPECT_TRUE(didDelay);
}

TEST_F(ClockTests, CanGetClockFrequency) {
static constexpr uint16_t _MHz = 4;

BusClock_t clock(_MHz);

EXPECT_EQ(clock.getFrequencyMHz(), _MHz);
}

TEST_F(ClockTests, CanGetAccumulatedClockCycles) {
static constexpr uint16_t _MHz = 4;

BusClock_t clock(_MHz);

clock.delay(10000);
Expand All @@ -63,3 +108,13 @@ TEST_F(ClockTests, DelayConsumesAccumulatedCycles) {

EXPECT_EQ(clock.getAccumulatedCycles(), constant);
}

TEST_F(ClockTests, CantSetLowMHz) {
BusClock_t clock(0);
EXPECT_EQ(clock.getFrequencyMHz(), 1);
}

TEST_F(ClockTests, CantSetHighMHz) {
BusClock_t clock(1001);
EXPECT_EQ(clock.getFrequencyMHz(), 1000);
}

0 comments on commit 5715418

Please sign in to comment.