Skip to content

Commit

Permalink
Make getTimestamp use monotonic time (#36)
Browse files Browse the repository at this point in the history
* monotonic time test

* switch to steady_clock

* remove sleep

* allow equal time

* tweaks

* see if fails with system_clock

* Revert "see if fails with system_clock"
still passes, whatever
This reverts commit 616f302.
  • Loading branch information
kh9sd authored and anushkakulk committed Jun 12, 2024
1 parent 3e726db commit 2d61658
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
14 changes: 13 additions & 1 deletion catch_tests/utils-and-constants/ECSUtilsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ namespace {
};
}

// run a bunch of asserts, try to catch a leap backwards
TEST_CASE("getTimeStamp monotonic time fuzz test") {
auto prevTime = getTimeStamp();

for (int i = 0; i < 20; i++){
auto curTime = getTimeStamp();

assert(prevTime <= curTime);
prevTime = curTime;
}
}

TEST_CASE("make_vector_unique", "[unit]") {
std::vector<std::unique_ptr<Foo>> result = make_vector_unique<Foo>(std::make_unique<Foo>(21312),
std::make_unique<Foo>(45342),
Expand All @@ -34,4 +46,4 @@ TEST_CASE("make_vector_unique", "[unit]") {
TEST_CASE("filterDoubleNan", "[unit]") {
REQUIRE_THAT(filterDoubleNan(1230.0), Catch::WithinRel(1230.0, 0.1));
REQUIRE_THAT(filterDoubleNan(std::nan("")), Catch::WithinRel(0.0, 0.1));
}
}
4 changes: 2 additions & 2 deletions src/utils/ECSUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ std::string get_date(){

uint64_t getTimeStamp() {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()
std::chrono::steady_clock::now().time_since_epoch()
).count();
}

Expand All @@ -63,4 +63,4 @@ double filterDoubleNan(double check) {

#ifdef _MSC_VER
#undef _CRT_SECURE_NO_WARNINGS
#endif
#endif

0 comments on commit 2d61658

Please sign in to comment.