/** * @file test_stress.c * @brief Stress tests for system */ #include "unity.h" #include "kernel.h" #include "task.h" #include "semaphore.h" #include "queue.h" #include /* Stress test constants */ #define STRESS_TEST_DURATION 5000 /* 5 seconds */ #define MAX_STRESS_TASKS 20 #define STRESS_QUEUE_SIZE 100 /* Stress test variables */ static TaskHandle_t stress_tasks[MAX_STRESS_TASKS]; static Semaphore_t stress_semaphores[MAX_STRESS_TASKS]; static Queue_t stress_queue; static volatile uint32_t task_counts[MAX_STRESS_TASKS]; static volatile uint32_t total_executions = 0; static volatile bool stress_test_passed = true; /* Stress task function */ static void stress_task(void* params) { uint32_t task_id = (uint32_t)params; while (1) { /* Increment counters */ task_counts[task_id]++; total_executions++; /* Semaphore operations */ semaphore_take(&stress_semaphores[task_id], 10); semaphore_give(&stress_semaphores[task_id]); /* Queue operations */ uint32_t data = task_id; queue_send(&stress_queue, &data, 0); queue_receive(&stress_queue, &data, 0); /* Random delay */ kernel_delay((task_id % 5) + 1); } } /* Setup */ void setUp(void) { kernel_init(); total_executions = 0; stress_test_passed = true; /* Initialize stress test resources */ queue_create(&stress_queue, malloc(STRESS_QUEUE_SIZE * sizeof(uint32_t)), sizeof(uint32_t), STRESS_QUEUE_SIZE); for (int i = 0; i < MAX_STRESS_TASKS; i++) { semaphore_create(&stress_semaphores[i], SEMAPHORE_BINARY, 1, 1); task_counts[i] = 0; } } /* Teardown */ void tearDown(void) { kernel_stop(); } /* ============================================================================ * Test Cases * ============================================================================ */ /** * @brief Test with maximum tasks */ void test_max_tasks_stress(void) { /* Create maximum number of tasks */ for (int i = 0; i < MAX_STRESS_TASKS; i++) { TaskConfig_t config = { .name = "stress_task", .function = stress_task, .parameters = (void*)(uint32_t)i, .stack_size = 1024, .priority = i % MAX_PRIORITY_LEVELS, .period_ticks = 0 }; stress_tasks[i] = task_create(&config); TEST_ASSERT_NOT_NULL(stress_tasks[i]); } /* Run stress test */ kernel_start(); kernel_delay(STRESS_TEST_DURATION); /* Verify all tasks executed */ for (int i = 0; i < MAX_STRESS_TASKS; i++) { TEST_ASSERT_GREATER_THAN(0, task_counts[i]); } TEST_ASSERT_GREATER_THAN(1000, total_executions); } /** * @brief Test context switching stress */ void test_context_switch_stress(void) { /* Create tasks that cause frequent context switches */ for (int i = 0; i < 5; i++) { TaskConfig_t config = { .name = "ctx_switch", .function = stress_task, .parameters = (void*)(uint32_t)i, .stack_size = 512, .priority = i, .period_ticks = 0 }; stress_tasks[i] = task_create(&config); } kernel_start(); kernel_delay(1000); SchedulerStatistics_t stats; scheduler_get_statistics(&stats); TEST_ASSERT_GREATER_THAN(100, stats.context_switches); } /** * @brief Test memory stress */ void test_memory_stress(void) { /* Allocate and free memory repeatedly */ void* pointers[100]; for (int iteration = 0; iteration < 100; iteration++) { for (int i = 0; i < 100; i++) { pointers[i] = malloc(128); TEST_ASSERT_NOT_NULL(pointers[i]); } for (int i = 0; i < 100; i++) { free(pointers[i]); } } TEST_ASSERT_TRUE(true); } /** * @brief Test interrupt stress */ void test_interrupt_stress(void) { /* Enable maximum interrupts */ kernel_start(); /* Generate interrupt load */ for (int i = 0; i < 1000; i++) { /* Trigger software interrupts */ __asm volatile("SVC #0"); } kernel_delay(100); TEST_ASSERT_TRUE(stress_test_passed); } /* ============================================================================ * Test Runner * ============================================================================ */ int main(void) { UNITY_BEGIN(); RUN_TEST(test_max_tasks_stress); RUN_TEST(test_context_switch_stress); RUN_TEST(test_memory_stress); RUN_TEST(test_interrupt_stress); return UNITY_END(); }