test.h (873B)
1 #pragma once 2 3 #include <cmath> 4 #include <cstdio> 5 #include <functional> 6 #include <iostream> 7 #include <string> 8 9 void run(const std::string& message, std::function<void ()> func); 10 11 extern int g_total; 12 extern int g_passes; 13 14 template<class T> bool equal(const T& a, const T& b) 15 { 16 return a == b; 17 } 18 19 template<> inline bool equal<double>(const double& a, const double& b) 20 { 21 return std::abs(a - b) < 1e-5; 22 } 23 24 template<class T> void test(const std::string& message, const T& expected, const T& actual) 25 { 26 g_total++; 27 if (equal(expected, actual)) { 28 g_passes++; 29 } else { 30 std::cerr << "\x1b[31;1mFAIL:\x1b[0m " << message; 31 std::cerr << ", expected: " << "\x1b[32;1m" << expected << "\x1b[0m"; 32 std::cerr << ", actual: " << "\x1b[31;1m" << actual << "\x1b[0m" << std::endl; 33 } 34 } 35 36 void test_audio(); 37 void test_fft(); 38 void test_utils();