1 // Copyright 2022 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "gtest/gtest.h"
16 #include "pw_perf_test/event_handler.h"
17 #include "pw_perf_test/perf_test.h"
18
19 namespace pw::perf_test {
20 namespace {
21
22 class EmptyEventHandler : public EventHandler {
23 public:
RunAllTestsStart(const TestRunInfo &)24 void RunAllTestsStart(const TestRunInfo&) override {}
TestCaseStart(const TestCase &)25 void TestCaseStart(const TestCase&) override {}
TestCaseEnd(const TestCase &,const Results &)26 void TestCaseEnd(const TestCase&, const Results&) override {}
TestCaseIteration(const IterationResult &)27 void TestCaseIteration(const IterationResult&) override {}
RunAllTestsEnd()28 void RunAllTestsEnd() override {}
29 };
30
31 EmptyEventHandler handler;
32
TestFunction()33 void TestFunction() {
34 for (volatile int i = 0; i < 100000; i = i + 1) {
35 }
36 }
37
TEST(StateTest,KeepRunningTest)38 TEST(StateTest, KeepRunningTest) {
39 constexpr int test_iterations = 10;
40 State state_obj = internal::CreateState(test_iterations, handler, "");
41 int total_iterations = 0;
42 while (state_obj.KeepRunning()) {
43 ++total_iterations;
44 TestFunction();
45 }
46 EXPECT_EQ(total_iterations, test_iterations);
47 }
48
TEST(StateTest,SingleTest)49 TEST(StateTest, SingleTest) {
50 constexpr int test_iterations = 1;
51 State state_obj = internal::CreateState(test_iterations, handler, "");
52 int total_iterations = 0;
53 while (state_obj.KeepRunning()) {
54 ++total_iterations;
55 TestFunction();
56 }
57 EXPECT_EQ(total_iterations, test_iterations);
58 }
59
60 } // namespace
61 } // namespace pw::perf_test
62