test.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef UNIT_UNITTESTING_H
  23. #define UNIT_UNITTESTING_H
  24. #ifndef _PLATFORMINTRINSICS_H_
  25. # include "platform/platformIntrinsics.h"
  26. #endif
  27. namespace UnitTesting {
  28. //-----------------------------------------------------------------------------
  29. struct UnitMargin
  30. {
  31. static void Push(int margin);
  32. static void Pop();
  33. static int Current();
  34. };
  35. void UnitPrint(const char* msg);
  36. //-----------------------------------------------------------------------------
  37. class UnitTest {
  38. int _testCount;
  39. int _failureCount;
  40. int _warningCount;
  41. bool _lastTestResult;
  42. public:
  43. UnitTest();
  44. virtual ~UnitTest() {};
  45. /// Test an assertion and note if it has failed.
  46. bool test(bool a,const char* msg) {
  47. dFetchAndAdd( _testCount, 1 );
  48. if (!a)
  49. fail(msg);
  50. _lastTestResult = a;
  51. return a;
  52. }
  53. /// Report a failture condition.
  54. void fail(const char* msg);
  55. /// Report a warning
  56. void warn(const char* msg);
  57. int getTestCount() const { return _testCount; }
  58. int getFailureCount() const { return _failureCount; }
  59. int getWarningCount() const { return _warningCount; }
  60. bool lastTestPassed() const { return _lastTestResult; }
  61. /// Implement this with the specific test.
  62. virtual void run() = 0;
  63. };
  64. //-----------------------------------------------------------------------------
  65. class TestRegistry
  66. {
  67. friend class DynamicTestRegistration; // Bless me, Father, for I have sinned, but this is damn cool
  68. static TestRegistry *_list;
  69. TestRegistry *_next;
  70. const char *_name;
  71. const char *_className;
  72. bool _isInteractive;
  73. public:
  74. TestRegistry(const char* name, bool interactive, const char *className);
  75. virtual ~TestRegistry() {}
  76. static TestRegistry* getFirst() { return _list; }
  77. TestRegistry* getNext() { return _next; }
  78. const char* getName() { return _name; }
  79. const bool isInteractive() { return _isInteractive; }
  80. virtual UnitTest* newTest() = 0;
  81. };
  82. template<class T>
  83. class TestRegistration: public TestRegistry
  84. {
  85. public:
  86. virtual ~TestRegistration()
  87. {
  88. }
  89. TestRegistration(const char* name, bool interactive, const char *className)
  90. : TestRegistry(name, interactive, className)
  91. {
  92. }
  93. virtual UnitTest* newTest()
  94. {
  95. return new T;
  96. }
  97. };
  98. class DynamicTestRegistration : public TestRegistry
  99. {
  100. UnitTest *mUnitTest;
  101. public:
  102. DynamicTestRegistration( const char *name, UnitTest *test );
  103. virtual ~DynamicTestRegistration();
  104. virtual UnitTest *newTest() { return mUnitTest; }
  105. };
  106. //-----------------------------------------------------------------------------
  107. class TestRun {
  108. int _testCount;
  109. int _subCount;
  110. int _failureCount;
  111. int _warningCount;
  112. void test(TestRegistry* reg);
  113. public:
  114. TestRun();
  115. void printStats();
  116. bool test(const char* module, bool skipInteractive = false);
  117. };
  118. #define CreateUnitTest(Class,Name) \
  119. class Class; \
  120. static UnitTesting::TestRegistration<Class> _UnitTester##Class (Name, false, #Class); \
  121. class Class : public UnitTesting::UnitTest
  122. #define CreateInteractiveTest(Class,Name) \
  123. class Class; \
  124. static UnitTesting::TestRegistration<Class> _UnitTester##Class (Name, true, #Class); \
  125. class Class : public UnitTesting::UnitTest
  126. } // Namespace
  127. #endif