1 // madronalib: a C++ framework for DSP applications. 2 // Copyright (c) 2020 Madrona Labs LLC. http://www.madronalabs.com 3 // Distributed under the MIT license: http://madrona-labs.mit-license.org/ 4 5 // a unit test made using the Catch framework in catch.hpp / tests.cpp. 6 7 #include <chrono> 8 #include <iostream> 9 #include <map> 10 #include <thread> 11 #include <unordered_map> 12 #include <vector> 13 14 #include "catch.hpp" 15 #include "madronalib.h" 16 17 using namespace ml; 18 using namespace std::chrono; 19 20 TEST_CASE("madronalib/core/timer/basic", "[timer][basic]") 21 { 22 // call this once in an application. 23 bool deferToMainThread = false; 24 SharedResourcePointer<ml::Timers> t; 25 t->start(deferToMainThread); 26 27 // calling start again should not break anything. 28 SharedResourcePointer<ml::Timers> t2; 29 t2->start(deferToMainThread); 30 31 int sum = 0; 32 33 // test number of calls correct 34 const int testSize = 10; 35 std::vector<std::unique_ptr<Timer> > v; 36 for (int i = 0; i < testSize; ++i) 37 { 38 v.emplace_back(std::unique_ptr<Timer>(new Timer)); 39 v[i]->callNTimes( __anon7a89d6ba0102() 40 [&sum]() { 41 sum += 1; 42 std::cout << "."; 43 }, 44 milliseconds(10 + 20 * i), 2); 45 } 46 std::this_thread::sleep_for(milliseconds(500)); 47 std::cout << "timer sum: " << sum << "\n"; 48 REQUIRE(sum == 20); 49 50 // test deleting timers while running (no REQUIRE) 51 const int test2Size = 10; 52 { 53 std::vector<std::unique_ptr<Timer> > v2; 54 for (int i = 0; i < test2Size; ++i) 55 { 56 v2.emplace_back(std::unique_ptr<Timer>(new Timer)); __anon7a89d6ba0202() 57 v2[i]->start([=]() { std::cout << i << " "; }, milliseconds(10 * i)); 58 } 59 std::this_thread::sleep_for(milliseconds(500)); 60 } 61 62 // test stopping timers while running 63 const int test3Size = 10; 64 { 65 int sum{0}; 66 std::cout << "\n----\n"; 67 std::vector<std::unique_ptr<Timer> > v3; 68 for (int i = 0; i < test3Size; ++i) 69 { 70 v3.emplace_back(std::unique_ptr<Timer>(new Timer)); 71 v3[i]->start( __anon7a89d6ba0302() 72 [&sum, i]() { 73 sum++; 74 std::cout << i << " "; 75 }, 76 milliseconds(10)); 77 } 78 for (int i = 0; i < test3Size; ++i) 79 { 80 std::this_thread::sleep_for(milliseconds(10)); 81 v3[i]->stop(); 82 } 83 84 // make sure all timers have stopped 85 std::this_thread::sleep_for(milliseconds(100)); 86 sum = 0; 87 std::this_thread::sleep_for(milliseconds(100)); 88 REQUIRE(sum == 0); 89 } 90 91 // temp 92 typedef SmallStackBuffer<float, 64> vBuf; 93 vBuf va(16); 94 vBuf vb(128); 95 vBuf *pvc = new vBuf(16); 96 vBuf *pvd = new vBuf(128); 97 98 std::cout << "\n\n a: " << &va << " b: " << &vb << " c: " << pvc 99 << " d: " << pvd << "\n"; 100 std::cout << "\n\n a: " << va.data() << " b: " << vb.data() 101 << " c: " << pvc->data() << " d: " << pvd->data() << "\n"; 102 103 #ifdef _WINDOWS 104 system("pause"); 105 #endif 106 } 107