Skip to main content

iceoryx2_pal_testing/
lib.rs

1// Copyright (c) 2023 Contributors to the Eclipse Foundation
2//
3// See the NOTICE file(s) distributed with this work for additional
4// information regarding copyright ownership.
5//
6// This program and the accompanying materials are made available under the
7// terms of the Apache Software License 2.0 which is available at
8// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9// which is available at https://opensource.org/licenses/MIT.
10//
11// SPDX-License-Identifier: Apache-2.0 OR MIT
12
13//! Low-level testing utilities and helpers.
14//!
15//! These utilities may only leverage the platform abstraction
16//! [`iceoryx2_pal_posix`] in their implementation. The `std` module may be
17//! used in `std` builds, however a `no_std` equivalent must be included for
18//! all usages.
19
20#![cfg_attr(not(feature = "std"), no_std)]
21#![warn(clippy::alloc_instead_of_core)]
22#![warn(clippy::std_instead_of_alloc)]
23#![warn(clippy::std_instead_of_core)]
24
25extern crate alloc;
26
27#[macro_use]
28pub mod assert;
29pub mod lifetime_tracker;
30pub mod memory;
31pub mod watchdog;
32
33#[macro_export(local_inner_macros)]
34macro_rules! test_requires {
35    { $condition:expr } => {
36        if !$condition { return; }
37    }
38}
39
40#[macro_export(local_inner_macros)]
41macro_rules! test_fail {
42    ($($e:expr),*) => {
43        core::panic!(
44            "test failed: {} {} {}",
45            assert_that![color_start],
46            alloc::format!($($e),*),
47            assert_that![color_end]
48        )
49    };
50}
51
52pub const AT_LEAST_TIMING_VARIANCE: f32 = iceoryx2_pal_configuration::AT_LEAST_TIMING_VARIANCE;
53
54#[doc(hidden)]
55pub fn is_terminal() -> bool {
56    #[cfg(feature = "std")]
57    {
58        use std::io::IsTerminal;
59        std::io::stderr().is_terminal()
60    }
61    #[cfg(all(not(feature = "std"), any(target_os = "linux", target_os = "nto",)))]
62    {
63        true
64    }
65    #[cfg(all(
66        not(feature = "std"),
67        not(any(target_os = "linux", target_os = "nto",))
68    ))]
69    {
70        false
71    }
72}
73
74#[doc(hidden)]
75pub fn spin_until<F, G>(mut condition: F, _guard: G)
76where
77    F: FnMut() -> bool,
78{
79    loop {
80        if condition() {
81            break;
82        }
83
84        #[cfg(feature = "std")]
85        {
86            std::thread::yield_now();
87            std::thread::sleep(core::time::Duration::from_millis(10));
88            std::thread::yield_now();
89        }
90
91        #[cfg(not(feature = "std"))]
92        core::hint::spin_loop();
93    }
94}