utils.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #ifndef __XRCU_TESTS_UTILS__
  2. #define __XRCU_TESTS_UTILS__ 1
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. #include <utility>
  7. #include <initializer_list>
  8. #include <cstdlib>
  9. #include <cstdio>
  10. #define ASSERT(Cond) \
  11. do \
  12. { \
  13. if (!(Cond)) \
  14. { \
  15. std::cerr << "\n\nAssertion failed: " << #Cond << \
  16. "\nLine: " << __LINE__ << "\nFile: " << __FILE__ << "\n\n"; \
  17. exit (EXIT_FAILURE); \
  18. } \
  19. } \
  20. while (0)
  21. struct test_fn
  22. {
  23. std::string msg;
  24. void (*fct) (void);
  25. test_fn (const std::string& m, void (*f) (void)) : msg (m), fct (f)
  26. {
  27. }
  28. void run () const
  29. {
  30. std::cout << "Testing " << this->msg << "...";
  31. this->fct ();
  32. std::cout << " OK\n";
  33. }
  34. };
  35. std::vector<test_fn>& test_suite ()
  36. {
  37. static std::vector<test_fn> ts;
  38. return (ts);
  39. }
  40. void run_tests ()
  41. {
  42. for (auto& tst : test_suite ())
  43. tst.run ();
  44. std::cout << "Done\n";
  45. }
  46. struct test_module
  47. {
  48. typedef std::pair<std::string, void (*) (void)> pair_type;
  49. test_module (const char *name, std::initializer_list<pair_type> tests)
  50. {
  51. std::string nm = std::string (" (") + name + ") ";
  52. for (auto pair : tests)
  53. test_suite().push_back (test_fn (pair.first + nm, pair.second));
  54. }
  55. };
  56. // Common constants for all tests.
  57. static const int INSERTER_LOOPS = 2000;
  58. static const int INSERTER_THREADS = 32;
  59. static const int ERASER_THREADS = 16;
  60. static const int ERASER_LOOPS = 2000;
  61. static const int MUTATOR_KEY_SIZE = 200;
  62. static const int MUTATOR_THREADS = 32;
  63. inline std::string mkstr (int i)
  64. {
  65. char buf[100];
  66. sprintf (buf, "%d", i);
  67. return (std::string (buf));
  68. }
  69. #endif