1 //===- skypat.h -----------------------------------------------------------===//
2 //
3 // The SkyPat Team
4 //
5 // This file is distributed under the New BSD License.
6 // See LICENSE for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #ifndef SKYPAT_SKYPAT_H
10 #define SKYPAT_SKYPAT_H
11 #include <stdint.h>
12 #include <string>
13 #include <vector>
14 #include <map>
15 #include <skypat/Support/OStrStream.h>
16
17 #define SKYPAT_VERNUM 0x24
18
19 namespace skypat {
20 enum PerfEvent {
21 // type is PERF_TYPE_HARDWARE
22 CPU_CYCLES, // = 0
23 INSTRUCTIONS,
24 CACHE_REFERENCES,
25 CACHE_MISSES,
26 BRANCH_INSTRUCTIONS,
27 BRANCH_MISSES,
28 BUS_CYCLES,
29 STALLED_CYCLES_FRONTEND,
30 STALLED_CYCLES_BACKEND,
31 REF_CPU_CYCLES, // = 9
32 // type is PERF_TYPE_SOFTWARE
33 CPU_CLOCK, // = 10
34 TASK_CLOCK,
35 PAGE_FAULTS,
36 CONTEXT_SWITCHES,
37 CPU_MIGRATIONS,
38 PAGE_FAULTS_MIN,
39 PAGE_FAULTS_MAJ,
40 ALIGNMENT_FAULTS,
41 EMULATION_FAULTS // = 18
42 };
43
44 enum PerfEventCache{
45 // type is PERF_TYPE_HW_CACHE
46 // TODO: add CPU cache events
47 };
48
49 extern char const *Perf_event_name[];
50
51 class Test;
52
53 namespace testing {
54 namespace internal {
55 class Timer;
56 class Perf;
57
58 //===----------------------------------------------------------------------===//
59 // ADT
60 //===----------------------------------------------------------------------===//
61 class Uncopyable
62 {
63 protected:
Uncopyable()64 Uncopyable() { }
~Uncopyable()65 ~Uncopyable() { }
66
67 private:
68 Uncopyable(const Uncopyable&); /// NOT TO IMPLEMENT
69 Uncopyable& operator=(const Uncopyable&); /// NOT TO IMPLEMENT
70 };
71
72 } // namespace of internal
73
74 //===----------------------------------------------------------------------===//
75 // Forward Declaration
76 //===----------------------------------------------------------------------===//
77 class AssertionResult;
78 class TestCase;
79 class TestInfo;
80 class TestResult;
81 class TestFactoryBase;
82 class PartResult;
83 class TestPartResult;
84 class PerfPartResult;
85 class UnitTest;
86
87 // Expands to the name of the class that implements the given test.
88 #define SKYPAT_TEST_CLASS_NAME_(case_name, test_name) \
89 case_name##_##test_name##_Test
90
91 /// Helper macro for defining Cases.
92 #define SKYPAT_TEST_CASE(case_name, test_name, parent_class)\
93 class SKYPAT_TEST_CLASS_NAME_(case_name, test_name) : public parent_class {\
94 public:\
95 SKYPAT_TEST_CLASS_NAME_(case_name, test_name)() {}\
96 private:\
97 virtual void TestBody();\
98 static skypat::testing::TestInfo* const m_TestInfo;\
99 };\
100 \
101 skypat::testing::TestInfo* const SKYPAT_TEST_CLASS_NAME_(case_name, test_name)\
102 ::m_TestInfo =\
103 skypat::testing::MakeAndRegisterTestInfo(\
104 #case_name, #test_name, \
105 new skypat::testing::TestFactory<\
106 SKYPAT_TEST_CLASS_NAME_(case_name, test_name)>);\
107 void SKYPAT_TEST_CLASS_NAME_(case_name, test_name)::TestBody()
108
109 // The message handling macros. The assignment is used to trigger recording a
110 // partial result.
111 #define SKYPAT_MESSAGE_AT(file, line, message, result_type) \
112 skypat::testing::AssertHelper(result_type, file, line, message) = \
113 skypat::testing::Message()
114
115 #define SKYPAT_MESSAGE(message, result_type) \
116 SKYPAT_MESSAGE_AT(__FILE__, __LINE__, message, result_type)
117
118 // SKYPAT_FATAL_FAILURE is a run-time result of fatal error
119 #define SKYPAT_FATAL_FAILURE(message) \
120 return SKYPAT_MESSAGE(message, skypat::testing::TestPartResult::kFatalFailure)
121
122 // SKYPAT_NON_FATAL_FAILURE is a run-time result of expectation error
123 #define SKYPAT_NONFATAL_FAILURE(message) \
124 SKYPAT_MESSAGE(message, skypat::testing::TestPartResult::kNonFatalFailure)
125
126 // SKYPAT_SUCCESS is a run-time result of success.
127 #define SKYPAT_SUCCESS(message) \
128 SKYPAT_MESSAGE(message, skypat::testing::TestPartResult::kSuccess)
129
130 // The GNU compiler emits a warning if nested "if" statements are followed by
131 // an "else" statement and braces are not used to explicitly disambiguate the
132 // "else" binding.
133 #ifdef __INTEL_COMPILER
134 # define SKYPAT_UNAMBIGUOUS_ELSE_BLOCKER
135 #else
136 # define SKYPAT_UNAMBIGUOUS_ELSE_BLOCKER switch (0) case 0: default: // NOLINT
137 #endif
138
139 // Implements Boolean test assertions such as EXPECT_TRUE. expression can be
140 // either a boolean expression or an AssertionResult. text is a textual
141 // represenation of expression as it was passed into the EXPECT_TRUE.
142 //
143 // The last parameter 'fail' is the run-time result of the testing
144 #define SKYPAT_TEST_BOOLEAN(expression, text, actual, expected, fail) \
145 SKYPAT_UNAMBIGUOUS_ELSE_BLOCKER \
146 if (const skypat::testing::AssertionResult _ar_ = \
147 skypat::testing::AssertionResult(expression)) \
148 SKYPAT_SUCCESS(""); \
149 else \
150 fail(skypat::testing::GetBoolAssertionFailureMessage(\
151 _ar_, text, #actual, #expected))
152
153 // Implements Predicate test assertions such as EXPECT_EQ.
154 //
155 // The last parameter 'fail' is the run-time result of the testing
156 #define SKYPAT_TEST_PREDICATE(expression, text, actual, expected, fail) \
157 SKYPAT_UNAMBIGUOUS_ELSE_BLOCKER \
158 if (const skypat::testing::AssertionResult _ar_ = \
159 skypat::testing::AssertionResult(expression)) \
160 SKYPAT_SUCCESS(""); \
161 else \
162 fail(skypat::testing::GetPredAssertionFailureMessage(\
163 _ar_, text, actual, #actual, expected, #expected))
164
165 //===----------------------------------------------------------------------===//
166 // Supports
167 //===----------------------------------------------------------------------===//
168 /// Interval - the unit of time.
169 typedef uint64_t Interval;
170
171 //===----------------------------------------------------------------------===//
172 // Core
173 //===----------------------------------------------------------------------===//
174 class TestFactoryBase
175 {
176 public:
~TestFactoryBase()177 virtual ~TestFactoryBase() { }
178 virtual skypat::Test* CreateTest() = 0;
179 };
180
181 template<typename SingleTest> struct TestFactory : public TestFactoryBase {
CreateTestTestFactory182 virtual skypat::Test* CreateTest() { return new SingleTest; }
183 };
184
185 /** \class PerfIterator
186 * \brief PerfIterator is used to calculate the computing time of a
187 * performance test.
188 */
189 class PerfIterator
190 {
191 public:
192 /// @param pFileName the source file name.
193 /// @param pLoC the line of code.
194 PerfIterator(const char* pFileName, int pLoC);
195
196 /// @param pFileName the source file name.
197 /// @param pLoC the line of code.
198 /// @param pFlavor the flavor of event (COUNT / SAMPLE)
199 /// @param pType the type of event (HW / SW)
200 /// @param pEvent the name of event
201 PerfIterator(const char* pFileName, int pLoC,\
202 enum PerfEvent pEvent);
203
204 /// Destructor. The place to sum up the time.
205 ~PerfIterator();
206
207 /// increase counter
208 PerfIterator& next();
209
210 /// @return true if we should go to the next step.
211 bool hasNext() const;
212
213 private:
214 int m_Counter;
215 internal::Timer* m_pTimer;
216 internal::Perf* m_pPerf;
217 PerfPartResult* m_pPerfResult;
218 };
219
220 /** \class PartResult
221 * \brief The partial result of a single test
222 */
223 class PartResult
224 {
225 public:
226 PartResult(const std::string& pFileName, int pLoC);
227
228 PartResult(const std::string& pFileName, int pLoC,
229 const std::string& pMessage);
230
~PartResult()231 virtual ~PartResult() { }
232
filename()233 const std::string& filename() const { return m_FileName; }
234
lineNumber()235 int lineNumber() const { return m_LoC; }
236
message()237 const std::string& message() const { return m_Message; }
238
239 protected:
update_message(const std::string & pMessage)240 void update_message(const std::string& pMessage) { m_Message = pMessage; }
241
242 protected:
243 std::string m_FileName;
244 int m_LoC;
245 std::string m_Message;
246 };
247
248 /** \class TestPartResult
249 * \brief The partial result of a single test
250 */
251 class TestPartResult : public PartResult
252 {
253 public:
254 enum Type {
255 kSuccess, ///< Succeeded.
256 kNonFatalFailure, ///< Failed but the test can continue.
257 kFatalFailure ///< Failed and the test should be terminated.
258 };
259
260 public:
261 TestPartResult(Type pType, const std::string& pFileName,
262 int pLoC, const std::string& pMessage);
263
type()264 Type type() const { return m_Type; }
265
266 TestPartResult& appendUserMessage(const std::string& pMessage);
267
268 private:
269 Type m_Type;
270 };
271
272 /** \class TestPerfPartResult
273 * \brief The performance result
274 */
275 class PerfPartResult : public PartResult
276 {
277 public:
278 PerfPartResult(const std::string& pFileName, int pLoC);
279
280 Interval getTimerNum() const;
281 Interval getPerfEventNum() const;
282 Interval getPerfEventType() const;
283
284 void setTimerNum(Interval pTime);
285 void setPerfEventNum(Interval pEventNum);
286 void setPerfEventType(Interval pEventType);
287
288 private:
289 Interval m_PerfTimerNum;
290 Interval m_PerfEventNum;
291 Interval m_PerfEventType;
292 };
293
294 /** \class TestResult
295 * \brief The result of a single test.
296 *
297 * TestResult concludes the result of a single test in summary.
298 */
299 class TestResult : private skypat::testing::internal::Uncopyable
300 {
301 public:
302 typedef std::vector<const TestPartResult*> Reliability;
303 typedef std::vector<const PerfPartResult*> Performance;
304
305 public:
306 enum Conclusion {
307 kPassed, ///< we pass all assertions
308 kFailed, ///< we fail some assertions
309 kNotTested ///< we do not have any assertions
310 };
311
312 public:
313 TestResult(const TestInfo& pInfo);
314
315 ~TestResult();
316
317 bool isPassed() const;
318
319 bool isFailed() const;
320
conclusion()321 Conclusion conclusion() const { return m_Conclusion; }
322
setConclusion(Conclusion pConclusion)323 void setConclusion(Conclusion pConclusion) { m_Conclusion = pConclusion; }
324
325 const Performance& performance() const;
326
327 const Reliability& reliability() const;
328
329 private:
330 const TestInfo& m_Info;
331 Conclusion m_Conclusion;
332 };
333
334 /** \class TestCase
335 * \brief The information of a test case (a set of tests)
336 */
337 class TestCase
338 {
339 private:
340 typedef std::vector<testing::TestInfo*> InfoList;
341
342 public:
343 typedef InfoList::iterator iterator;
344 typedef InfoList::const_iterator const_iterator;
345
begin()346 const_iterator begin() const { return m_InfoList.begin(); }
begin()347 iterator begin() { return m_InfoList.begin(); }
end()348 const_iterator end() const { return m_InfoList.end(); }
end()349 iterator end() { return m_InfoList.end(); }
350
351 public:
352 TestCase(const std::string& pCaseName);
353
354 ~TestCase();
355
356 testing::TestInfo* addTestInfo(const std::string& pTestName,
357 testing::TestFactoryBase& pFactory);
358
getCaseName()359 const std::string& getCaseName() const { return m_CaseName; }
360
getNumOfTests()361 unsigned int getNumOfTests() const { return m_InfoList.size(); }
362
363 private:
364 InfoList m_InfoList;
365 std::string m_CaseName;
366 };
367
368 /** \class TestInfo
369 * \brief The information of a single test.
370 *
371 * TestInfo stores the information of a single test. A test case contains
372 * multiple tests which is represented by TestInfos.
373 *
374 * TestInfo is created at static time and gathers partial results at run-time.
375 */
376 class TestInfo
377 {
378 public:
379 TestInfo(TestCase* pTestCase,
380 const std::string& pTestName,
381 TestFactoryBase& pFactory);
382
383 ~TestInfo();
384
testCase()385 const TestCase* testCase() const { return m_pTestCase; }
getCaseName()386 const std::string& getCaseName() const { return m_pTestCase->getCaseName(); }
getTestName()387 const std::string& getTestName() const { return m_TestName; }
result()388 const TestResult& result() const { return m_Result; }
389
390 /// run - run a single test function and notifiy repeater.
391 void run();
392
393 void addTestPartResult(const TestPartResult& pResult);
394 PerfPartResult* addPerfPartResult(const char* pFile, int pLine);
395
396 private:
397 friend class TestResult;
398
399 typedef std::vector<const TestPartResult*> TestPartResultList;
400 typedef std::vector<const PerfPartResult*> PerfPartResultList;
401
402 private:
getTestResults()403 const TestPartResultList& getTestResults() const { return m_TestResultList; }
getTestResults()404 TestPartResultList& getTestResults() { return m_TestResultList; }
405
getPerfResults()406 const PerfPartResultList& getPerfResults() const { return m_PerfResultList; }
getPerfResults()407 PerfPartResultList& getPerfResults() { return m_PerfResultList; }
408
409 private:
410 TestCase* m_pTestCase;
411 std::string m_TestName;
412 TestResult m_Result;
413 TestFactoryBase* m_pFactory;
414 TestPartResultList m_TestResultList;
415 PerfPartResultList m_PerfResultList;
416 };
417
418 /** \class AssertionResult
419 * \brief The result of an assertion.
420 */
421 class AssertionResult
422 {
423 public:
424 AssertionResult(const AssertionResult& other);
425
426 explicit AssertionResult(bool pSuccess);
427
428 operator bool() const { return m_bSuccess; } // NOLINT
429
430 AssertionResult operator!() const;
431
message()432 const std::string& message() const { return m_Message; }
433
434 template <typename T> AssertionResult& operator<<(const T& pValue);
435
436 AssertionResult& operator<<(
437 ::std::ostream& (*basic_manipulator)(::std::ostream& stream));
438
hasMessage()439 bool hasMessage() const { return !m_Message.empty(); }
440
441 private:
442 bool m_bSuccess;
443 std::string m_Message;
444 };
445
446 /** \class Message
447 * \brief Message is a carrier to pass user-defined message to AssertionResult
448 */
449 class Message
450 {
451 public:
452 Message();
453
454 template<typename T>
455 inline Message& operator <<(const T& pValue) {
456 m_OSS << pValue;
457 return *this;
458 }
459
460 template<typename T>
461 inline Message& operator <<(T* const& pPointer) {
462 if (NULL == pPointer)
463 m_OSS << "(null)";
464 else
465 m_OSS << pPointer;
466 return *this;
467 }
468
str()469 const std::string& str() const { return m_Message; }
470
471 private:
472 std::string m_Message;
473 OStrStream m_OSS;
474 };
475
476 /** \class AssertHelper
477 * \brief AssertHelper carries all information to UnitTest.
478 */
479 class AssertHelper
480 {
481 public:
482 AssertHelper(TestPartResult::Type pType,
483 const std::string& pFile,
484 int pLineOfCode,
485 const std::string& pMessage);
486
487 // Message assignment is a semantic trick to enable assertion
488 // streaming; see the SKYPAT_MESSAGE_AT macro below.
489 // This method may update the messages stored in TestPartResult.
490 void operator=(const Message& pMesg);
491
492 private:
493 TestPartResult m_Result;
494 };
495
496 TestInfo* MakeAndRegisterTestInfo(
497 const char* pCaseName,
498 const char* pTestName,
499 TestFactoryBase* pFactory);
500
501 std::string GetBoolAssertionFailureMessage(
502 const AssertionResult& pAssertionResult,
503 const char* pExpressionText,
504 const char* pActualPredicateValue,
505 const char* pExpectedPredicateValue);
506
507 template<typename T1, typename T2>
GetPredAssertionFailureMessage(const AssertionResult & pAssertionResult,const char * pExpressionText,const T1 & pActualPredicateValue,const char * pActualPredicate,const T2 & pExpectedPredicateValue,const char * pExpectedPredicate)508 std::string GetPredAssertionFailureMessage(
509 const AssertionResult& pAssertionResult,
510 const char* pExpressionText,
511 const T1& pActualPredicateValue,
512 const char* pActualPredicate,
513 const T2& pExpectedPredicateValue,
514 const char* pExpectedPredicate)
515 {
516 std::string result;
517 OStrStream OS(result);
518 OS << "Value of: " << pExpressionText
519 << "\n Actual: " << pActualPredicateValue;
520 if (pAssertionResult.hasMessage())
521 OS << "(" << pAssertionResult.message() << ")";
522 OS << "\n Expected: " << pExpectedPredicateValue;
523 return result;
524 }
525 //===----------------------------------------------------------------------===//
526 // Listener
527 //===----------------------------------------------------------------------===//
528 /** \class Listener
529 * \brief Listener provides interfaces for objects who wants UnitTest's events.
530 */
531 class Listener
532 {
533 public:
~Listener()534 virtual ~Listener() { }
535
536 // Fired before any test activity starts.
OnTestProgramStart(const testing::UnitTest & pUnitTest)537 virtual void OnTestProgramStart(const testing::UnitTest& pUnitTest) {}
538
539 // Fired before each test case (a set of tests) of test start.
OnTestCaseStart(const TestCase & pTestCase)540 virtual void OnTestCaseStart(const TestCase& pTestCase) {}
541
542 // Fired before set-up for each iteration of test start.
OnSetUpStart(const UnitTest & pUnitTest)543 virtual void OnSetUpStart(const UnitTest& pUnitTest) {}
544
545 // Fired before set-up for each iteration of test end.
OnSetUpEnd(const UnitTest & pUnitTest)546 virtual void OnSetUpEnd(const UnitTest& pUnitTest) {}
547
548 // Fired before the test starts.
OnTestStart(const TestInfo & pTestInfo)549 virtual void OnTestStart(const TestInfo& pTestInfo) {}
550
551 // Fired after a failed assertion or a SUCCEED() invocation.
OnTestPartResult(const TestPartResult & pTestPartResult)552 virtual void OnTestPartResult(const TestPartResult& pTestPartResult) {}
553
554 // Fired after a PERFORM invocation
OnPerfPartResult(const PerfPartResult & pPerfPartResult)555 virtual void OnPerfPartResult(const PerfPartResult& pPerfPartResult) {}
556
557 // Fired after the test ends.
OnTestEnd(const TestInfo & pTestInfo)558 virtual void OnTestEnd(const TestInfo& pTestInfo) {}
559
560 // Fired before environment tear-down for each iteration of tests starts.
OnTearDownStart(const UnitTest & pUnitTest)561 virtual void OnTearDownStart(const UnitTest& pUnitTest) {}
562
563 // Fired after environment tear-down for each iteration of tests ends.
OnTearDownEnd(const UnitTest & pUnitTest)564 virtual void OnTearDownEnd(const UnitTest& pUnitTest) {}
565
566 // Fired after each test case (a set of tests) of test ends.
OnTestCaseEnd(const TestCase & pTestCase)567 virtual void OnTestCaseEnd(const TestCase& pTestCase) {}
568
569 // Fired after all test activities have ended.
OnTestProgramEnd(const UnitTest & pUnitTest)570 virtual void OnTestProgramEnd(const UnitTest& pUnitTest) {}
571 };
572
573 class Log
574 {
575 public:
576 enum Severity {
577 kInfo,
578 kWarning,
579 kError,
580 kFatal
581 };
582
583 public:
584 Log(Severity pSeverity, const std::string& pFileName, int pLoC);
585
586 /// Destructor. Flush the buffers and if the Severity is fatal, aborts the
587 /// program.
588 ~Log();
589
590 static ::std::ostream& getOStream();
591
592 static std::string FormatFileLocation(const std::string& pFileName, int pLoC);
593
594 private:
595 Severity m_Severity;
596 };
597
598 //===----------------------------------------------------------------------===//
599 // Repeater
600 //===----------------------------------------------------------------------===//
601 /** \class Repeater
602 * \brief Repeater dispatches events to all listeners.
603 */
604 class Repeater : public Listener
605 {
606 public:
607 typedef std::vector<Listener*> ListenerList;
608
609 public:
610 Repeater();
611
612 ~Repeater();
613
614 void add(Listener* pListener);
615
616 void release(Listener& pListener);
617
isForward()618 bool isForward() const { return m_bForward; }
619
620 void setForward(bool pEnable = true) { m_bForward = pEnable; }
621
622 void OnTestProgramStart(const testing::UnitTest& pUnitTest);
623 void OnTestCaseStart(const TestCase& pTestCase);
624 void OnSetUpStart(const UnitTest& pUnitTest);
625 void OnSetUpEnd(const UnitTest& pUnitTest);
626 void OnTestStart(const TestInfo& pTestInfo);
627 void OnTestPartResult(const TestPartResult& pTestPartResult);
628 void OnPerfPartResult(const PerfPartResult& pPerfPartResult);
629 void OnTestEnd(const TestInfo& pTestInfo);
630 void OnTearDownStart(const UnitTest& pUnitTest);
631 void OnTearDownEnd(const UnitTest& pUnitTest);
632 void OnTestCaseEnd(const TestCase& pTestCase);
633 void OnTestProgramEnd(const UnitTest& pUnitTest);
634
635 private:
636 ListenerList m_Listeners;
637 bool m_bForward;
638 };
639
640 //===----------------------------------------------------------------------===//
641 // UnitTest
642 //===----------------------------------------------------------------------===//
643 class UnitTest
644 {
645 private:
646 typedef std::map<std::string, testing::TestCase*> CaseMap;
647
648 // RunCases stores all runnable cases
649 typedef std::vector<testing::TestCase*> RunCases;
650
651 public:
self()652 static UnitTest* self() {
653 static UnitTest instance;
654 return &instance;
655 }
656
657 void RunAll();
658
659 /// addRunCase - add the test case to run
660 /// @return true if the case exists (be stored at static-time).
661 bool addRunCase(const std::string& pCaseName);
662
663 /// addAllRunCase - add all test cases to run
664 void addAllRunCases();
665
666 /// addTestInfo - store a TestInfo at static-time
667 testing::TestInfo* addTestInfo(const std::string& pCaseName,
668 const std::string& pTestName,
669 testing::TestFactoryBase& pFactory);
670
671 /// addTestPartResult - add partial test result at run-time.
672 void addTestPartResult(const testing::TestPartResult& pPartResult);
673
674 /// addPerfPartResult - add partial performance result at run-time.
675 testing::PerfPartResult* addPerfPartResult(const char* pFile, int pLine);
676
repeater()677 const Repeater& repeater() const { return m_Repeater; }
repeater()678 Repeater& repeater() { return m_Repeater; }
679
getNumOfCases()680 unsigned int getNumOfCases() const { return m_CaseMap.size(); }
getNumOfTests()681 unsigned int getNumOfTests() const { return m_NumOfTests; }
getNumOfFails()682 unsigned int getNumOfFails() const { return m_NumOfFails; }
683
getNumOfRunCases()684 unsigned int getNumOfRunCases() const { return m_RunCases.size(); }
685
686 private:
687 UnitTest();
688 ~UnitTest();
689 UnitTest(const UnitTest& pCopy); // DO NOT IMPLEMENT
690 UnitTest& operator=(const UnitTest& pCopy); // DO NOT IMPLEMENT
691
692 private:
693 CaseMap m_CaseMap;
694 RunCases m_RunCases;
695 testing::Repeater m_Repeater;
696 testing::TestInfo* m_pCurrentInfo;
697 unsigned int m_NumOfTests;
698 unsigned int m_NumOfFails;
699 };
700
701 } // namespace of testing
702
703 /** \class Test
704 * \brief Test is the abstract class that all tests inherit from.
705 *
706 * In skypat, a performance test program contains one or many TestCases, and
707 * each TestCase contains one or many Tests.
708 *
709 * When you define a test using the TEST macro, you don't need to explicitly
710 * derive from Test - the PERFORM macro automatically does this for you.
711 *
712 * The only one time you derive from Test is when defining a test fixture
713 * to be used a PERFORM_F. For example:
714 *
715 * class FooTest : public skypat::Test
716 * {
717 * public:
718 * virtual void SetUp() { ... }
719 * virtual void TearDown() { ... }
720 * ...
721 * };
722 *
723 * SKYPAT_C( FooTest, Bar1) { ... }
724 * SKYPAT_C( FooTest, Bar2) { ... }
725 */
726 class Test : private skypat::testing::internal::Uncopyable
727 {
728 friend class skypat::testing::TestInfo;
729 private:
730 void run();
731
732 public:
733 /// Destructor
734 virtual ~Test() = 0;
735
736 /// SetUp - set up the test fixture.
SetUp()737 virtual void SetUp() { }
738
739 /// TearDown - tears down the test fixture.
TearDown()740 virtual void TearDown() { }
741
742 public:
743 /// @name Static Member Functions
744 /// @{
745 /// Initialize - initialize environment for TestCases.
746 static void Initialize(const int& pArgc, char* pArgv[]);
747
748 /// Initialize - use the pretty printer
749 /// @param[out] pProgName The program name.
750 static void Initialize(const std::string& pProgName);
751
752 /// Initialize - initialize environment for TestCases.
753 /// @param[out] pProgName The program name.
754 /// @param[out] pCSVResult The resulting CSV file.
755 static void
756 Initialize(const std::string& pProgName, const std::string& pCSVResult);
757
758 /// RunAll - run all TestCases.
759 static void RunAll();
760
761 /// Sleep - sleep for micro seconds
762 static void Sleep(int pMS);
763 /// @}
764
765 virtual void TestBody() = 0;
766 };
767
768 // Defines a test that uses a test fixture.
769 //
770 // SKYPAT_C defines a skypat case.
771 // SKYPAT_F defines a skypat function.
772 //
773 // The first parameter is the name of the test fixture class, which
774 // also doubles as the test case name. The second parameter is the
775 // name of the test within the test case.
776 //
777 // A test fixture class must be declared earlier. The user should put
778 // his test code between braces after using this macro. Example:
779 //
780 // class FooTest : public skypat::Test {
781 // protected:
782 // virtual void SetUp() { m_B.AddElement(3); }
783 //
784 // Foo m_A;
785 // Foo m_B;
786 // };
787 //
788 // SKYPAT_C(FooTest, Initializes) {
789 // m_A.StatusIsOK();
790 // }
791 //
792 #define SKYPAT_C(test_fixture, test_name) \
793 SKYPAT_TEST_CASE(test_fixture, test_name, test_fixture)
794
795 #define SKYPAT_F(test_fixture, test_name) \
796 SKYPAT_TEST_CASE(test_fixture, test_name, skypat::Test)
797
798 // Boolean assertions.
799 #define EXPECT_TRUE(condition) \
800 SKYPAT_TEST_BOOLEAN(condition, #condition, false, true, \
801 SKYPAT_NONFATAL_FAILURE)
802 #define EXPECT_FALSE(condition) \
803 SKYPAT_TEST_BOOLEAN(!(condition), #condition, true, false, \
804 SKYPAT_NONFATAL_FAILURE)
805 #define ASSERT_TRUE(condition) \
806 SKYPAT_TEST_BOOLEAN(condition, #condition, false, true, \
807 SKYPAT_FATAL_FAILURE)
808 #define ASSERT_FALSE(condition) \
809 SKYPAT_TEST_BOOLEAN(!(condition), #condition, true, false, \
810 SKYPAT_FATAL_FAILURE)
811
812 // Boolean assertions.
813 #define EXPECT_TRUE_MSG(condition, msg) \
814 SKYPAT_TEST_BOOLEAN(condition, #condition, false, true, \
815 SKYPAT_NONFATAL_FAILURE) << msg
816 #define EXPECT_FALSE_MSG(condition, msg) \
817 SKYPAT_TEST_BOOLEAN(!(condition), #condition, true, false, \
818 SKYPAT_NONFATAL_FAILURE) << msg
819 #define ASSERT_TRUE_MSG(condition, msg) \
820 SKYPAT_TEST_BOOLEAN(condition, #condition, false, true, \
821 SKYPAT_FATAL_FAILURE) << msg
822 #define ASSERT_FALSE_MSG(condition, msg) \
823 SKYPAT_TEST_BOOLEAN(!(condition), #condition, true, false, \
824 SKYPAT_FATAL_FAILURE) << msg
825
826 #define SKYPAT_EXPECT_PRED(condition, actual, expected) \
827 SKYPAT_TEST_PREDICATE(condition, #condition, \
828 actual, expected, \
829 SKYPAT_NONFATAL_FAILURE)
830
831 #define SKYPAT_ASSERT_PRED(condition, actual, expected) \
832 SKYPAT_TEST_PREDICATE(condition, #condition, \
833 actual, expected, \
834 SKYPAT_FATAL_FAILURE)
835
836 #define EXPECT_EQ(actual, expected) \
837 SKYPAT_EXPECT_PRED((actual == expected), actual, expected)
838 #define EXPECT_NE(actual, expected) \
839 SKYPAT_EXPECT_PRED((actual != expected), actual, expected)
840 #define EXPECT_LE(actual, expected) \
841 SKYPAT_EXPECT_PRED((actual <= expected), actual, expected)
842 #define EXPECT_LT(actual, expected) \
843 SKYPAT_EXPECT_PRED((actual < expected), actual, expected)
844 #define EXPECT_GE(actual, expected) \
845 SKYPAT_EXPECT_PRED((actual >= expected), actual, expected)
846 #define EXPECT_GT(actual, expected) \
847 SKYPAT_EXPECT_PRED((actual > expected), actual, expected)
848
849 #define ASSERT_EQ(actual, expected) \
850 SKYPAT_ASSERT_PRED((actual == expected), actual, expected)
851 #define ASSERT_NE(actual, expected) \
852 SKYPAT_ASSERT_PRED((actual != expected), actual, expected)
853 #define ASSERT_LE(actual, expected) \
854 SKYPAT_ASSERT_PRED((actual <= expected), actual, expected)
855 #define ASSERT_LT(actual, expected) \
856 SKYPAT_ASSERT_PRED((actual < expected), actual, expected)
857 #define ASSERT_GE(actual, expected) \
858 SKYPAT_ASSERT_PRED((actual >= expected), actual, expected)
859 #define ASSERT_GT(actual, expected) \
860 SKYPAT_ASSERT_PRED((actual > expected), actual, expected)
861
862 #define EXPECT_EQ_MSG(actual, expected, mesg) \
863 SKYPAT_EXPECT_PRED((actual == expected), actual, expected) << mesg
864 #define EXPECT_NE_MSG(actual, expected, mesg) \
865 SKYPAT_EXPECT_PRED((actual != expected), actual, expected) << mesg
866 #define EXPECT_LE_MSG(actual, expected, mesg) \
867 SKYPAT_EXPECT_PRED((actual <= expected), actual, expected) << mesg
868 #define EXPECT_LT_MSG(actual, expected, mesg) \
869 SKYPAT_EXPECT_PRED((actual < expected), actual, expected) << mesg
870 #define EXPECT_GE_MSG(actual, expected, mesg) \
871 SKYPAT_EXPECT_PRED((actual >= expected), actual, expected) << mesg
872 #define EXPECT_GT_MSG(actual, expected, mesg) \
873 SKYPAT_EXPECT_PRED((actual > expected), actual, expected) << mesg
874
875 #define ASSERT_EQ_MSG(actual, expected, mesg) \
876 SKYPAT_ASSERT_PRED((actual == expected), actual, expected) << mesg
877 #define ASSERT_NE_MSG(actual, expected, mesg) \
878 SKYPAT_ASSERT_PRED((actual != expected), actual, expected) << mesg
879 #define ASSERT_LE_MSG(actual, expected, mesg) \
880 SKYPAT_ASSERT_PRED((actual <= expected), actual, expected) << mesg
881 #define ASSERT_LT_MSG(actual, expected, mesg) \
882 SKYPAT_ASSERT_PRED((actual < expected), actual, expected) << mesg
883 #define ASSERT_GE_MSG(actual, expected, mesg) \
884 SKYPAT_ASSERT_PRED((actual >= expected), actual, expected) << mesg
885 #define ASSERT_GT_MSG(actual, expected, mesg) \
886 SKYPAT_ASSERT_PRED((actual > expected), actual, expected) << mesg
887
888 #define PERFORM(event) \
889 for (skypat::testing::PerfIterator __loop(__FILE__, __LINE__, event); \
890 __loop.hasNext(); \
891 __loop.next() )
892
893 } // namespace of skypat
894
895 #endif
896