Files
RTOS/tests/integration/test_timing.c
T
root ca13734bf0 Add full automotive RTOS project
Add kernel (Cortex-M0/M3/M4, Tricore, S32K, RISC-V ports), drivers,
middleware (CAN stack, diagnostics, safety), applications, board
support, build/test tooling, and documentation.
2026-08-23 03:35:29 -04:00

148 lines
3.3 KiB
C

/**
* @file test_timing.c
* @brief Integration tests for timing accuracy
*/
#include "unity.h"
#include "kernel.h"
#include "task.h"
#include <string.h>
#include <math.h>
/* Timing test variables */
static volatile uint32_t periodic_count = 0;
static volatile uint32_t first_period_time = 0;
static volatile uint32_t last_period_time = 0;
static volatile uint32_t max_period_jitter = 0;
/* Periodic test task */
static void periodic_task(void* params) {
(void)params;
uint32_t current_time = kernel_get_tick_count();
if (periodic_count == 0) {
first_period_time = current_time;
} else {
uint32_t period = current_time - last_period_time;
if (period > max_period_jitter) {
max_period_jitter = period;
}
}
last_period_time = current_time;
periodic_count++;
while (1) {
kernel_delay(10);
}
}
/* Setup */
void setUp(void) {
kernel_init();
periodic_count = 0;
max_period_jitter = 0;
}
/* Teardown */
void tearDown(void) {
kernel_stop();
}
/* ============================================================================
* Test Cases
* ============================================================================ */
/**
* @brief Test periodic task timing
*/
void test_periodic_timing(void) {
TaskConfig_t config = {
.name = "periodic",
.function = periodic_task,
.parameters = NULL,
.stack_size = 1024,
.priority = 1,
.period_ticks = 10
};
TaskHandle_t task = task_create(&config);
kernel_start();
kernel_delay(1000); /* Run for 1 second */
/* Verify task executed */
TEST_ASSERT_GREATER_THAN(90, periodic_count);
TEST_ASSERT_LESS_THAN(110, periodic_count);
/* Verify timing accuracy */
TEST_ASSERT_LESS_OR_EQUAL(2, max_period_jitter); /* Max 2ms jitter */
}
/**
* @brief Test delay accuracy
*/
void test_delay_accuracy(void) {
kernel_start();
uint32_t start = kernel_get_tick_count();
kernel_delay(100);
uint32_t end = kernel_get_tick_count();
uint32_t elapsed = end - start;
TEST_ASSERT_GREATER_OR_EQUAL(99, elapsed);
TEST_ASSERT_LESS_OR_EQUAL(102, elapsed);
}
/**
* @brief Test tick count
*/
void test_tick_count(void) {
kernel_start();
uint32_t start = kernel_get_tick_count();
kernel_delay(50);
uint32_t end = kernel_get_tick_count();
TEST_ASSERT_EQUAL(50, end - start);
}
/**
* @brief Test high-frequency task
*/
void test_high_frequency_task(void) {
TaskConfig_t config = {
.name = "fast_task",
.function = periodic_task,
.parameters = NULL,
.stack_size = 1024,
.priority = 0,
.period_ticks = 1
};
TaskHandle_t task = task_create(&config);
kernel_start();
kernel_delay(100);
/* 1ms task should execute approximately 100 times */
TEST_ASSERT_GREATER_THAN(90, periodic_count);
TEST_ASSERT_LESS_THAN(110, periodic_count);
}
/* ============================================================================
* Test Runner
* ============================================================================ */
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_periodic_timing);
RUN_TEST(test_delay_accuracy);
RUN_TEST(test_tick_count);
RUN_TEST(test_high_frequency_task);
return UNITY_END();
}