From bec55635f9d9dbc70c29be595d4c9726b64cb3c1 Mon Sep 17 00:00:00 2001 From: Andrew Conlin Date: Wed, 18 Dec 2024 12:28:00 +0000 Subject: [PATCH] [2024-12-18] Adding tests for new syntax - Moving old tests into `testInitSyntax.py` - This syntax is still supported - `testInitSyntax.py` also tests calling tic() before toc(), which is not possible with the setup of `test.py`. This should be moved to its own file soon. --- tests/test.py | 107 +++++++++++++++++++++------------------- tests/testInitSyntax.py | 82 ++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 52 deletions(-) create mode 100644 tests/testInitSyntax.py diff --git a/tests/test.py b/tests/test.py index b019fd2..7fbfdd4 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1,43 +1,41 @@ import pytest -import tictoc +from tictoc import tic,toc import time import math - -@pytest.fixture -def t(): - t = tictoc.init() - return t - - class testFunctionality: - def testBasic(self, t): - t.tic() + def testBasic(self): + tic() print("test") - t.toc() - assert t.results.seconds > 0 + results = toc() + assert results.seconds > 0 - def testOverwrite(self, t): - t.tic() + def testMultipleGlobalCalls(self): + tic() print("test") - t.toc() - firstResult = t.results.seconds + results = toc() print("test2") - t.toc() - secondResult = t.results.seconds + results2 = toc() - assert firstResult < secondResult + assert results.seconds < results2.seconds + + def testMultipleCalls(self): + first = tic() + print("test") + second = tic() + print("test2") + secondResult = toc(second).seconds + firstResult = toc(first).seconds + + assert firstResult > secondResult class testInvalid: - def testNoInit(self): + def testNonTicInputForToc(self): with pytest.raises(Exception): - t.tic() - - def testTocBeforeTic(self, t): - with pytest.raises(Exception): - t.toc() - + tic() + print("test") + toc(1) @pytest.mark.parametrize("sleepTime", [0.05, 0.5, 1]) class testAccuracy: @@ -45,42 +43,47 @@ class testAccuracy: def tol(self): return 0.0006 - def testSingleCall(self, t, sleepTime, tol): - t.tic() + def testSingleCall(self, sleepTime, tol): + tic() time.sleep(sleepTime) - t.toc() - assert (t.results.seconds > sleepTime) & ( - t.results.seconds < (t.results.seconds + tol) - ) + results = toc() + assert (results.seconds < sleepTime+tol) - def testMultipleCalls(self, t, sleepTime, tol): - t.tic() + def testMultipleGlobalCalls(self, sleepTime, tol): + tic() time.sleep(sleepTime) - t.toc() + toc() time.sleep(sleepTime) - t.toc() - assert (t.results.seconds > sleepTime * 2) & ( - t.results.seconds < (t.results.seconds + tol) - ) + results = toc() + assert (results.seconds < (sleepTime * 2)+tol) + def testMultipleCalls(self, sleepTime, tol): + first = tic() + time.sleep(sleepTime) + second = tic() + time.sleep(sleepTime) + results2 = toc(second) + results = toc(first) + assert (results.seconds < (sleepTime * 2)+tol) + assert (results2.seconds < sleepTime+tol) class testConsistency: - def testMicros(self, t): - t.tic() + def testMicros(self): + tic() print("test") - t.toc() - assert t.results.micros == (math.floor(t.results.nanos * pow(10, -3))) + results = toc() + assert results.micros == (math.floor(results.nanos * pow(10, -3))) - def testMillis(self, t): - t.tic() + def testMillis(self): + tic() print("test") - t.toc() - assert t.results.millis == (math.floor(t.results.nanos * pow(10, -6))) + results = toc() + assert results.millis == (math.floor(results.nanos * pow(10, -6))) - def testSeconds(self, t): - t.tic() + def testSeconds(self): + tic() print("test") - t.toc() - assert t.results.seconds == round( - (t.results.nanos * pow(10, -9)), 9 + results = toc() + assert results.seconds == round( + (results.nanos * pow(10, -9)), 9 ) # f64 vs u128, hence the round diff --git a/tests/testInitSyntax.py b/tests/testInitSyntax.py new file mode 100644 index 0000000..97b7557 --- /dev/null +++ b/tests/testInitSyntax.py @@ -0,0 +1,82 @@ +import pytest +import tictoc +import time +import math + + +@pytest.fixture +def t(): + t = tictoc.init() + return t + + +class testFunctionality: + def testBasic(self, t): + t.tic() + print("test") + t.toc() + assert t.results.seconds > 0 + + def testOverwrite(self, t): + t.tic() + print("test") + t.toc() + firstResult = t.results.seconds + print("test2") + t.toc() + secondResult = t.results.seconds + + assert firstResult < secondResult + + +class testInvalid: + def testNoInit(self): + with pytest.raises(Exception): + t.tic() + + def testTocBeforeTic(self, t): + with pytest.raises(Exception): + t.toc() + + +@pytest.mark.parametrize("sleepTime", [0.05, 0.5, 1]) +class testAccuracy: + @pytest.fixture(scope="class") + def tol(self): + return 0.0006 + + def testSingleCall(self, t, sleepTime, tol): + t.tic() + time.sleep(sleepTime) + t.toc() + assert (t.results.seconds < sleepTime+tol) + + def testMultipleCalls(self, t, sleepTime, tol): + t.tic() + time.sleep(sleepTime) + t.toc() + time.sleep(sleepTime) + t.toc() + assert (t.results.seconds < (sleepTime * 2)+tol) + + +class testConsistency: + def testMicros(self, t): + t.tic() + print("test") + t.toc() + assert t.results.micros == (math.floor(t.results.nanos * pow(10, -3))) + + def testMillis(self, t): + t.tic() + print("test") + t.toc() + assert t.results.millis == (math.floor(t.results.nanos * pow(10, -6))) + + def testSeconds(self, t): + t.tic() + print("test") + t.toc() + assert t.results.seconds == round( + (t.results.nanos * pow(10, -9)), 9 + ) # f64 vs u128, hence the round