1 #include "UnitTest++/UnitTestPP.h"
2 #include "UnitTest++/TimeHelpers.h"
3
4 #include "RecordingReporter.h"
5 #include "ScopedCurrentTest.h"
6
7 namespace {
8
TEST(TimeConstraintMacroQualifiesNamespace)9 TEST(TimeConstraintMacroQualifiesNamespace)
10 {
11 // If this compiles without a "using namespace UnitTest;", all is well.
12 UNITTEST_TIME_CONSTRAINT(1);
13 }
14
TEST(TimeConstraintMacroUsesCorrectInfo)15 TEST(TimeConstraintMacroUsesCorrectInfo)
16 {
17 int testLine = 0;
18 RecordingReporter reporter;
19
20 {
21 UnitTest::TestResults testResults(&reporter);
22 ScopedCurrentTest scopedResults(testResults);
23
24 UNITTEST_TIME_CONSTRAINT(10); testLine = __LINE__;
25 UnitTest::TimeHelpers::SleepMs(20);
26 }
27
28 using namespace std;
29
30 CHECK_EQUAL(1, reporter.testFailedCount);
31 CHECK(strstr(reporter.lastFailedFile, __FILE__));
32 CHECK_EQUAL(testLine, reporter.lastFailedLine);
33 CHECK(strstr(reporter.lastFailedTest, "TimeConstraintMacroUsesCorrectInfo"));
34 }
35
TEST(TimeConstraintMacroComparesAgainstPreciseActual)36 TEST(TimeConstraintMacroComparesAgainstPreciseActual)
37 {
38 int testLine = 0;
39 RecordingReporter reporter;
40
41 {
42 UnitTest::TestResults testResults(&reporter);
43 ScopedCurrentTest scopedResults(testResults);
44
45 UNITTEST_TIME_CONSTRAINT(1); testLine = __LINE__;
46
47 // start a new timer and run until we're as little over the 1 msec
48 // threshold as we can achieve; this should guarantee that the "test"
49 // runs in some very small amount of time > 1 msec
50 UnitTest::Timer myTimer;
51 myTimer.Start();
52
53 while (myTimer.GetTimeInMs() < 1.001)
54 UnitTest::TimeHelpers::SleepMs(0);
55 }
56
57 using namespace std;
58
59 CHECK_EQUAL(1, reporter.testFailedCount);
60 CHECK(strstr(reporter.lastFailedFile, __FILE__));
61 CHECK_EQUAL(testLine, reporter.lastFailedLine);
62 CHECK(strstr(reporter.lastFailedTest, "TimeConstraintMacroComparesAgainstPreciseActual"));
63 }
64
65 struct EmptyFixture {};
66
TEST_FIXTURE(EmptyFixture,TimeConstraintMacroWorksInFixtures)67 TEST_FIXTURE(EmptyFixture, TimeConstraintMacroWorksInFixtures)
68 {
69 int testLine = 0;
70 RecordingReporter reporter;
71
72 {
73 UnitTest::TestResults testResults(&reporter);
74 ScopedCurrentTest scopedResults(testResults);
75
76 UNITTEST_TIME_CONSTRAINT(10); testLine = __LINE__;
77 UnitTest::TimeHelpers::SleepMs(20);
78 }
79
80 using namespace std;
81
82 CHECK_EQUAL(1, reporter.testFailedCount);
83 CHECK(strstr(reporter.lastFailedFile, __FILE__));
84 CHECK_EQUAL(testLine, reporter.lastFailedLine);
85 CHECK(strstr(reporter.lastFailedTest, "TimeConstraintMacroWorksInFixtures"));
86 }
87
88 }
89