2018-07-18 13:51:01 -07:00
|
|
|
/*
|
|
|
|
* Author: Noel Eck <noel.eck@intel.com>
|
|
|
|
* Copyright (c) 2018 Intel Corporation.
|
|
|
|
*
|
2019-07-30 19:41:32 -07:00
|
|
|
* This program and the accompanying materials are made available under the
|
|
|
|
* terms of the The MIT License which is available at
|
|
|
|
* https://opensource.org/licenses/MIT.
|
2018-07-18 13:51:01 -07:00
|
|
|
*
|
2019-07-30 19:41:32 -07:00
|
|
|
* SPDX-License-Identifier: MIT
|
2018-07-18 13:51:01 -07:00
|
|
|
*/
|
|
|
|
|
2018-05-01 10:56:27 -07:00
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
2018-07-18 13:51:01 -07:00
|
|
|
|
2018-03-02 11:45:15 -08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "upm_utilities.h"
|
|
|
|
#include "upm_utilities.hpp"
|
|
|
|
|
2018-05-01 10:56:27 -07:00
|
|
|
/* Average over AVG_CNT iterations */
|
|
|
|
#define AVG_CNT 5
|
|
|
|
|
|
|
|
/* Specify a delay for all tests under 1s */
|
|
|
|
#define ms_50 std::chrono::milliseconds(50)
|
|
|
|
|
|
|
|
/* Specify a +/- value for all tests under 1s. Since the delay methods in
|
|
|
|
* non-realtime operating systems can vary greatly, use a lenient value for
|
|
|
|
* testing these methods. */
|
|
|
|
#define time_range std::chrono::milliseconds(5)
|
|
|
|
|
|
|
|
/* Helper defines */
|
|
|
|
#define to_ms std::chrono::duration_cast<std::chrono::milliseconds>
|
|
|
|
#define to_us std::chrono::duration_cast<std::chrono::microseconds>
|
|
|
|
#define to_ns std::chrono::duration_cast<std::chrono::nanoseconds>
|
|
|
|
|
2018-03-02 11:45:15 -08:00
|
|
|
/* Utilities test fixture */
|
|
|
|
class utilities_unit : public ::testing::Test
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
/* One-time setup logic if needed */
|
|
|
|
utilities_unit() {}
|
|
|
|
|
|
|
|
/* One-time tear-down logic if needed */
|
|
|
|
virtual ~utilities_unit() {}
|
|
|
|
|
|
|
|
/* Per-test setup logic if needed */
|
|
|
|
virtual void SetUp() {}
|
|
|
|
|
|
|
|
/* Per-test tear-down logic if needed */
|
|
|
|
virtual void TearDown() {}
|
|
|
|
};
|
|
|
|
|
2018-05-01 10:56:27 -07:00
|
|
|
/* Sanity check on min_delay_ns */
|
|
|
|
TEST_F(utilities_unit, min_delay_LT_500us)
|
|
|
|
{
|
|
|
|
/* Determine a rough-average for the minimum delay using chrono */
|
|
|
|
std::chrono::nanoseconds min_delay_ns = std::chrono::nanoseconds::zero();
|
|
|
|
for (int i = 0; i < AVG_CNT; i++)
|
|
|
|
{
|
|
|
|
auto start = std::chrono::steady_clock::now();
|
|
|
|
std::this_thread::sleep_for(std::chrono::nanoseconds(1));
|
|
|
|
auto end = std::chrono::steady_clock::now();
|
|
|
|
min_delay_ns += to_ns(end-start);
|
|
|
|
}
|
|
|
|
min_delay_ns /= AVG_CNT;
|
|
|
|
|
2018-07-31 15:46:02 -07:00
|
|
|
ASSERT_LT(to_us(min_delay_ns).count(), 3000);
|
2018-05-01 10:56:27 -07:00
|
|
|
}
|
|
|
|
|
2018-03-02 11:45:15 -08:00
|
|
|
/* Test the second delay method */
|
|
|
|
TEST_F(utilities_unit, test_upm_delay)
|
|
|
|
{
|
2018-05-01 10:56:27 -07:00
|
|
|
upm_clock_t clock = upm_clock_init();
|
2018-03-02 11:45:15 -08:00
|
|
|
|
|
|
|
/* Test a corner case */
|
|
|
|
upm_delay(0);
|
2018-05-01 10:56:27 -07:00
|
|
|
|
|
|
|
/* +- check for 0s */
|
|
|
|
EXPECT_EQ(upm_elapsed_ms(&clock), 0);
|
|
|
|
|
|
|
|
clock = upm_clock_init();
|
2018-03-02 11:45:15 -08:00
|
|
|
upm_delay(1);
|
|
|
|
|
2018-05-01 10:56:27 -07:00
|
|
|
/* +- check near 1s */
|
|
|
|
EXPECT_NEAR(upm_elapsed_ms(&clock), 1000, time_range.count());
|
2018-03-02 11:45:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Test the millisecond delay method */
|
|
|
|
TEST_F(utilities_unit, test_upm_delay_ms)
|
|
|
|
{
|
2018-05-01 10:56:27 -07:00
|
|
|
upm_clock_t clock = upm_clock_init();
|
2018-03-02 11:45:15 -08:00
|
|
|
|
|
|
|
/* Test a corner case */
|
|
|
|
upm_delay_ms(0);
|
|
|
|
|
2018-05-01 10:56:27 -07:00
|
|
|
/* +- check for 0ms */
|
|
|
|
EXPECT_EQ(upm_elapsed_ms(&clock), 0);
|
|
|
|
|
|
|
|
clock = upm_clock_init();
|
|
|
|
upm_delay_ms(ms_50.count() * AVG_CNT);
|
|
|
|
|
|
|
|
/* +- check near 50ms */
|
|
|
|
EXPECT_NEAR(upm_elapsed_ms(&clock)/AVG_CNT, ms_50.count(), time_range.count());
|
2018-03-02 11:45:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Test the microsecond delay method */
|
|
|
|
TEST_F(utilities_unit, test_upm_delay_us)
|
|
|
|
{
|
2018-05-01 10:56:27 -07:00
|
|
|
upm_clock_t clock = upm_clock_init();
|
2018-03-02 11:45:15 -08:00
|
|
|
|
|
|
|
/* Test a corner case */
|
|
|
|
upm_delay_us(0);
|
|
|
|
|
2018-05-01 10:56:27 -07:00
|
|
|
/* +- check for 0us +/- 100us */
|
|
|
|
EXPECT_NEAR(upm_elapsed_us(&clock), 0, 100);
|
|
|
|
|
|
|
|
clock = upm_clock_init();
|
|
|
|
upm_delay_us(to_us(ms_50).count() * AVG_CNT);
|
|
|
|
|
|
|
|
/* +- check near 50ms */
|
|
|
|
EXPECT_NEAR(upm_elapsed_us(&clock)/AVG_CNT, to_us(ms_50).count(),
|
|
|
|
to_us(time_range).count());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test the nanosecond delay method */
|
|
|
|
TEST_F(utilities_unit, test_upm_delay_ns)
|
|
|
|
{
|
|
|
|
upm_clock_t clock = upm_clock_init();
|
|
|
|
|
|
|
|
/* Test a corner case */
|
|
|
|
upm_delay_ns(0);
|
|
|
|
|
|
|
|
/* +- check for 0us +/- 100us */
|
|
|
|
EXPECT_NEAR(upm_elapsed_ns(&clock), 0, 100000);
|
|
|
|
|
|
|
|
clock = upm_clock_init();
|
|
|
|
upm_delay_ns(to_ns(ms_50).count() * AVG_CNT);
|
|
|
|
|
|
|
|
/* +- check near 50ms */
|
|
|
|
EXPECT_NEAR(upm_elapsed_ns(&clock)/AVG_CNT, to_ns(ms_50).count(),
|
|
|
|
to_ns(time_range).count());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test the max us delay (default to disabled) */
|
|
|
|
TEST_F(utilities_unit, DISABLED_test_upm_delay_us_max)
|
|
|
|
{
|
|
|
|
upm_clock_t clock = upm_clock_init();
|
|
|
|
upm_delay_us(4294967295);
|
|
|
|
EXPECT_NEAR(upm_elapsed_us(&clock), 4294967295, 150);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test the Air Quality Index method */
|
|
|
|
TEST_F(utilities_unit, test_upm_ugm3_to_aqi)
|
|
|
|
{
|
|
|
|
EXPECT_EQ(upm_ugm3_to_aqi(10), 41);
|
2018-03-02 11:45:15 -08:00
|
|
|
}
|