2017-09-05 13:54:21 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
* Copyright (c) 2017 ARM Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2018-03-29 20:37:11 +00:00
|
|
|
#if !DEVICE_USTICKER
|
|
|
|
#error [NOT_SUPPORTED] usticker not supported for this target.
|
|
|
|
#endif
|
|
|
|
|
2017-09-05 13:54:21 +00:00
|
|
|
#include "mbed.h"
|
|
|
|
#include "greentea-client/test_env.h"
|
|
|
|
#include "unity.h"
|
|
|
|
#include "utest.h"
|
|
|
|
#include "drivers/TimerEvent.h"
|
|
|
|
#include "hal/ticker_api.h"
|
|
|
|
#include "rtos.h"
|
|
|
|
|
|
|
|
using namespace utest::v1;
|
|
|
|
|
2018-03-06 09:05:35 +00:00
|
|
|
#if !DEVICE_USTICKER
|
2018-07-27 09:17:07 +00:00
|
|
|
#error [NOT_SUPPORTED] test not supported
|
2018-03-06 09:05:35 +00:00
|
|
|
#endif
|
|
|
|
|
2017-09-05 13:54:21 +00:00
|
|
|
#define TEST_DELAY_US 50000ULL
|
2018-05-21 07:15:35 +00:00
|
|
|
#define DELTA 2
|
2017-09-05 13:54:21 +00:00
|
|
|
|
|
|
|
class TestTimerEvent: public TimerEvent {
|
|
|
|
private:
|
|
|
|
Semaphore sem;
|
2018-07-27 09:17:07 +00:00
|
|
|
virtual void handler()
|
|
|
|
{
|
2017-09-05 13:54:21 +00:00
|
|
|
sem.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
TestTimerEvent() :
|
2018-07-27 09:17:07 +00:00
|
|
|
TimerEvent(), sem(0, 1)
|
|
|
|
{
|
2018-05-17 11:21:14 +00:00
|
|
|
|
|
|
|
sleep_manager_lock_deep_sleep();
|
2017-09-05 13:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TestTimerEvent(const ticker_data_t *data) :
|
2018-07-27 09:17:07 +00:00
|
|
|
TimerEvent(data), sem(0, 1)
|
|
|
|
{
|
2017-09-05 13:54:21 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 09:17:07 +00:00
|
|
|
virtual ~TestTimerEvent()
|
|
|
|
{
|
2018-05-17 11:21:14 +00:00
|
|
|
sleep_manager_unlock_deep_sleep();
|
2017-09-05 13:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make these methods publicly accessible
|
|
|
|
using TimerEvent::insert;
|
|
|
|
using TimerEvent::insert_absolute;
|
|
|
|
using TimerEvent::remove;
|
|
|
|
|
2018-07-27 09:17:07 +00:00
|
|
|
int32_t sem_wait(uint32_t millisec)
|
|
|
|
{
|
2017-09-05 13:54:21 +00:00
|
|
|
return sem.wait(millisec);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TestTimerEventRelative: public TestTimerEvent {
|
|
|
|
public:
|
|
|
|
static const int32_t SEM_SLOTS_AFTER_PAST_TS_INSERTED = 0;
|
|
|
|
TestTimerEventRelative() :
|
2018-07-27 09:17:07 +00:00
|
|
|
TestTimerEvent()
|
|
|
|
{
|
2017-09-05 13:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TestTimerEventRelative(const ticker_data_t *data) :
|
2018-07-27 09:17:07 +00:00
|
|
|
TestTimerEvent(data)
|
|
|
|
{
|
2017-09-05 13:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set relative timestamp of internal event to present_time + ts
|
2018-07-27 09:17:07 +00:00
|
|
|
void set_future_timestamp(timestamp_t ts)
|
|
|
|
{
|
2017-09-05 13:54:21 +00:00
|
|
|
insert(::ticker_read(_ticker_data) + ts);
|
|
|
|
}
|
|
|
|
|
2018-07-27 09:17:07 +00:00
|
|
|
void set_past_timestamp(void)
|
|
|
|
{
|
2017-09-05 13:54:21 +00:00
|
|
|
insert(::ticker_read(_ticker_data) - 1UL);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TestTimerEventAbsolute: public TestTimerEvent {
|
|
|
|
public:
|
|
|
|
static const int32_t SEM_SLOTS_AFTER_PAST_TS_INSERTED = 1;
|
|
|
|
TestTimerEventAbsolute() :
|
2018-07-27 09:17:07 +00:00
|
|
|
TestTimerEvent()
|
|
|
|
{
|
2017-09-05 13:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TestTimerEventAbsolute(const ticker_data_t *data) :
|
2018-07-27 09:17:07 +00:00
|
|
|
TestTimerEvent(data)
|
|
|
|
{
|
2017-09-05 13:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set absolute timestamp of internal event to present_time + ts
|
2018-07-27 09:17:07 +00:00
|
|
|
void set_future_timestamp(us_timestamp_t ts)
|
|
|
|
{
|
2017-09-05 13:54:21 +00:00
|
|
|
insert_absolute(::ticker_read_us(_ticker_data) + ts);
|
|
|
|
}
|
|
|
|
|
2018-07-27 09:17:07 +00:00
|
|
|
void set_past_timestamp(void)
|
|
|
|
{
|
2017-09-05 13:54:21 +00:00
|
|
|
insert_absolute(::ticker_read_us(_ticker_data) - 1ULL);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Template for tests: insert, insert_absolute
|
|
|
|
*
|
|
|
|
* Test insert
|
|
|
|
* Given an instance of @a TimerEvent subclass
|
|
|
|
* When a tiestamp is set with @a insert()
|
|
|
|
* and given time elapses
|
|
|
|
* Then an event handler is called
|
|
|
|
*
|
|
|
|
* Test insert_absolute
|
|
|
|
* Given an instance of @a TimerEvent subclass
|
|
|
|
* When a tiestamp is set with @a insert_absolute()
|
|
|
|
* and given time elapses
|
|
|
|
* Then an event handler is called
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2018-07-27 09:17:07 +00:00
|
|
|
void test_insert(void)
|
|
|
|
{
|
2017-09-05 13:54:21 +00:00
|
|
|
T tte;
|
|
|
|
|
|
|
|
tte.set_future_timestamp(TEST_DELAY_US);
|
|
|
|
int32_t sem_slots = tte.sem_wait(0);
|
|
|
|
TEST_ASSERT_EQUAL(0, sem_slots);
|
|
|
|
|
2018-05-21 07:15:35 +00:00
|
|
|
sem_slots = tte.sem_wait(TEST_DELAY_US / 1000 + DELTA);
|
2017-09-05 13:54:21 +00:00
|
|
|
TEST_ASSERT_EQUAL(1, sem_slots);
|
|
|
|
|
|
|
|
tte.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Template for tests: remove
|
|
|
|
*
|
|
|
|
* Test remove after insert
|
|
|
|
* Given an instance of @a TimerEvent subclass
|
|
|
|
* When a tiestamp is set with @a insert()
|
|
|
|
* and timestamp is removed before being reached
|
|
|
|
* Then the event handler is never called
|
|
|
|
*
|
|
|
|
* Test remove after insert_absolute
|
|
|
|
* Given an instance of @a TimerEvent subclass
|
|
|
|
* When a tiestamp is set with @a insert_absolute()
|
|
|
|
* and timestamp is removed before being reached
|
|
|
|
* Then the event handler is never called
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2018-07-27 09:17:07 +00:00
|
|
|
void test_remove(void)
|
|
|
|
{
|
2017-09-05 13:54:21 +00:00
|
|
|
T tte;
|
|
|
|
|
|
|
|
tte.set_future_timestamp(TEST_DELAY_US * 2);
|
|
|
|
int32_t sem_slots = tte.sem_wait(TEST_DELAY_US / 1000);
|
|
|
|
TEST_ASSERT_EQUAL(0, sem_slots);
|
|
|
|
tte.remove();
|
|
|
|
|
2018-05-21 07:15:35 +00:00
|
|
|
sem_slots = tte.sem_wait(TEST_DELAY_US * 2 / 1000 + DELTA);
|
2017-09-05 13:54:21 +00:00
|
|
|
TEST_ASSERT_EQUAL(0, sem_slots);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Test insert_absolute zero
|
|
|
|
* Given an instance of @a TimerEvent subclass
|
|
|
|
* When a timestamp of 0 us is set with @a insert_absolute()
|
|
|
|
* Then an event handler is called instantly
|
|
|
|
*/
|
2018-07-27 09:17:07 +00:00
|
|
|
void test_insert_zero(void)
|
|
|
|
{
|
2017-09-05 13:54:21 +00:00
|
|
|
TestTimerEvent tte;
|
|
|
|
|
|
|
|
tte.insert_absolute(0ULL);
|
|
|
|
int32_t sem_slots = tte.sem_wait(0);
|
|
|
|
TEST_ASSERT_EQUAL(1, sem_slots);
|
|
|
|
|
|
|
|
tte.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Template for tests: insert, insert_absolute past timestamp
|
|
|
|
*
|
|
|
|
* Test insert timestamp from the past
|
|
|
|
* Given an instance of @a TimerEvent subclass
|
|
|
|
* When a timestamp of X us is set with @a insert()
|
|
|
|
* and X is less than present_time
|
|
|
|
* Then an event handler is **not** called instantly
|
|
|
|
* (the event is scheduled after the ticker's overflow)
|
|
|
|
*
|
|
|
|
* Test insert_absolute timestamp from the past
|
|
|
|
* Given an instance of @a TimerEvent subclass
|
|
|
|
* When a timestamp of X us is set with @a insert_absolute()
|
|
|
|
* and X is less than present_time
|
|
|
|
* Then an event handler is called instantly
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2018-07-27 09:17:07 +00:00
|
|
|
void test_insert_past(void)
|
|
|
|
{
|
2017-09-05 13:54:21 +00:00
|
|
|
T tte;
|
|
|
|
|
|
|
|
tte.set_past_timestamp();
|
|
|
|
int32_t sem_slots = tte.sem_wait(0);
|
|
|
|
TEST_ASSERT_EQUAL(tte.SEM_SLOTS_AFTER_PAST_TS_INSERTED, sem_slots);
|
|
|
|
|
|
|
|
tte.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
utest::v1::status_t test_setup(const size_t number_of_cases)
|
2018-07-27 09:17:07 +00:00
|
|
|
{
|
2017-09-05 13:54:21 +00:00
|
|
|
GREENTEA_SETUP(5, "default_auto");
|
|
|
|
return verbose_test_setup_handler(number_of_cases);
|
|
|
|
}
|
|
|
|
|
|
|
|
Case cases[] = {
|
|
|
|
Case("Test insert", test_insert<TestTimerEventRelative>),
|
|
|
|
Case("Test insert_absolute", test_insert<TestTimerEventAbsolute>),
|
|
|
|
|
|
|
|
Case("Test remove after insert", test_remove<TestTimerEventRelative>),
|
|
|
|
Case("Test remove after insert_absolute", test_remove<TestTimerEventAbsolute>),
|
|
|
|
|
|
|
|
Case("Test insert_absolute zero", test_insert_zero),
|
|
|
|
|
|
|
|
Case("Test insert timestamp from the past", test_insert_past<TestTimerEventRelative>),
|
|
|
|
Case("Test insert_absolute timestamp from the past", test_insert_past<TestTimerEventAbsolute>),
|
|
|
|
};
|
|
|
|
|
|
|
|
Specification specification(test_setup, cases);
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
return !Harness::run(specification);
|
|
|
|
}
|