xref: /aosp_15_r20/external/pigweed/pw_perf_test/state_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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 "pw_perf_test/state.h"
16 
17 #include "pw_perf_test/event_handler.h"
18 #include "pw_unit_test/framework.h"
19 
20 namespace pw::perf_test {
21 namespace {
22 
23 class EmptyEventHandler : public EventHandler {
24  public:
RunAllTestsStart(const TestRunInfo &)25   void RunAllTestsStart(const TestRunInfo&) override {}
RunAllTestsEnd()26   void RunAllTestsEnd() override {}
TestCaseStart(const TestCase &)27   void TestCaseStart(const TestCase&) override {}
TestCaseIteration(const TestIteration &)28   void TestCaseIteration(const TestIteration&) override {}
TestCaseMeasure(const TestMeasurement &)29   void TestCaseMeasure(const TestMeasurement&) override {}
TestCaseEnd(const TestCase &)30   void TestCaseEnd(const TestCase&) override {}
31 };
32 
33 EmptyEventHandler handler;
34 
TestFunction()35 void TestFunction() {
36   for (volatile int i = 0; i < 100000; i = i + 1) {
37   }
38 }
39 
TEST(StateTest,KeepRunningTest)40 TEST(StateTest, KeepRunningTest) {
41   constexpr int test_iterations = 10;
42   State state_obj = internal::CreateState(test_iterations, handler, "");
43   int total_iterations = 0;
44   while (state_obj.KeepRunning()) {
45     ++total_iterations;
46     TestFunction();
47   }
48   EXPECT_EQ(total_iterations, test_iterations);
49 }
50 
TEST(StateTest,SingleTest)51 TEST(StateTest, SingleTest) {
52   constexpr int test_iterations = 1;
53   State state_obj = internal::CreateState(test_iterations, handler, "");
54   int total_iterations = 0;
55   while (state_obj.KeepRunning()) {
56     ++total_iterations;
57     TestFunction();
58   }
59   EXPECT_EQ(total_iterations, test_iterations);
60 }
61 
62 }  // namespace
63 }  // namespace pw::perf_test
64