Browse Source

clock and tests

Kajetan Johannes Hammerle 3 years ago
parent
commit
375ceefe32
6 changed files with 110 additions and 1 deletions
  1. 2 0
      Main.cpp
  2. 3 1
      meson.build
  3. 40 0
      tests/ClockTests.cpp
  4. 8 0
      tests/ClockTests.h
  5. 29 0
      utils/Clock.cpp
  6. 28 0
      utils/Clock.h

+ 2 - 0
Main.cpp

@@ -15,6 +15,7 @@
 #include "tests/QuaternionTests.h"
 #include "tests/UtilsTests.h"
 #include "tests/ColorTests.h"
+#include "tests/ClockTests.h"
 
 int main() {
     ArrayTests::test();
@@ -34,5 +35,6 @@ int main() {
     QuaternionTests::test();
     UtilsTests::test();
     ColorTests::test();
+    ClockTests::test();
     return 0;
 }

+ 3 - 1
meson.build

@@ -25,7 +25,9 @@ sources = ['Main.cpp',
     'tests/QuaternionTests.cpp',
     'math/Quaternion.cpp',
     'tests/UtilsTests.cpp',
-    'tests/ColorTests.cpp']
+    'tests/ColorTests.cpp',
+    'utils/Clock.cpp',
+    'tests/ClockTests.cpp']
 
 executable('tests', 
     sources: sources,

+ 40 - 0
tests/ClockTests.cpp

@@ -0,0 +1,40 @@
+#include "tests/ClockTests.h"
+#include "tests/Test.h"
+#include "utils/Clock.h"
+
+static void testUpdate(Test& test) {
+    Clock c;
+    Clock::Nanos n1 = c.update();
+    Clock::Nanos n2 = c.update();
+    Clock::Nanos n3 = c.update();
+    Clock::Nanos n4 = c.update();
+    test.checkEqual(true, n1 > 0, "time measurement is positive 1");
+    test.checkEqual(true, n2 > 0, "time measurement is positive 2");
+    test.checkEqual(true, n3 > 0, "time measurement is positive 3");
+    test.checkEqual(true, n4 > 0, "time measurement is positive 4");
+}
+
+static void testUpdatesPerSecond(Test& test) {
+    Clock c;
+    for(int i = 0; i < 1000; i++) {
+        c.update();
+    }
+    test.checkEqual(true, c.getUpdatesPerSecond() > 0.0f, "updates per second are a positive");
+}
+
+static void testWait(Test& test) {
+    const Clock::Nanos wait = 50'000'000;
+    Clock c;
+    c.update();
+    c.wait(wait);
+    Clock::Nanos n2 = c.update();
+    test.checkEqual(true, n2 >= wait, "wait passes time");
+}
+
+void ClockTests::test() {
+    Test test("Clock");
+    testUpdate(test);
+    testUpdatesPerSecond(test);
+    testWait(test);
+    test.finalize();
+}

+ 8 - 0
tests/ClockTests.h

@@ -0,0 +1,8 @@
+#ifndef CLOCKTESTS_H
+#define CLOCKTESTS_H
+
+namespace ClockTests {
+    void test();
+}
+
+#endif

+ 29 - 0
utils/Clock.cpp

@@ -0,0 +1,29 @@
+#include <chrono>
+#include <thread>
+
+#include "utils/Clock.h"
+
+Clock::Clock() : index(0), last(getNanos()), sum(0), time(0) {
+}
+
+Clock::Nanos Clock::update() {
+    index = (index + 1) & (LENGTH - 1);
+    Nanos current = getNanos();
+    sum -= time[index];
+    time[index] = current - last;
+    sum += time[index];
+    last = current;
+    return time[index];
+}
+
+float Clock::getUpdatesPerSecond() const {
+    return LENGTH * (1000000000.0f / sum);
+}
+
+Clock::Nanos Clock::getNanos() const {
+    return std::chrono::high_resolution_clock::now().time_since_epoch().count();
+}
+
+void Clock::wait(Nanos nanos) const {
+    std::this_thread::sleep_for(std::chrono::nanoseconds(nanos));
+}

+ 28 - 0
utils/Clock.h

@@ -0,0 +1,28 @@
+#ifndef CLOCK_H
+#define CLOCK_H
+
+#include "utils/Types.h"
+#include "utils/Array.h"
+
+struct Clock final {
+    typedef int64 Nanos;
+    
+private:
+    static constexpr int BITS = 7;
+    static constexpr int LENGTH = 1 << BITS;
+    int index;
+    Nanos last;
+    Nanos sum;
+    Array<Nanos, LENGTH> time;
+
+public:
+    Clock();
+    Nanos update();
+    float getUpdatesPerSecond() const;
+    void wait(Nanos nanos) const;
+
+private:
+    Nanos getNanos() const;
+};
+
+#endif