xref: /aosp_15_r20/external/pigweed/pw_chrono_rp2040/clock_properties_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2023 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 "pico/critical_section.h"
16 #include "pw_chrono/system_clock.h"
17 #include "pw_unit_test/framework.h"
18 
19 namespace pw::chrono::rp2040 {
20 namespace {
21 
22 static_assert(pw::chrono::SystemClock::is_free_running == true);
23 
TEST(ClockProperties,IsFreeRunning)24 TEST(ClockProperties, IsFreeRunning) {
25   // Enter a critical section.
26   critical_section state;
27   critical_section_init(&state);
28   critical_section_enter_blocking(&state);
29 
30   // Check initial clock value.
31   auto start = pw::chrono::SystemClock::now();
32   auto end = start;
33 
34   // Poll pw::chrono::SystemClock::now() until a change is detected. If no
35   // change is detected after kMaxAttempts, give up.
36   constexpr int kMaxAttempts = 100000;
37   for (int i = 0; i < kMaxAttempts; ++i) {
38     end = pw::chrono::SystemClock::now();
39     if (end != start) {
40       break;
41     }
42   }
43 
44   // Exit critical section.
45   critical_section_exit(&state);
46 
47   EXPECT_GT(end, start);
48 }
49 
50 }  // namespace
51 }  // namespace pw::chrono::rp2040
52