From 22715022742488eb2418745aff9c0bae3dad3c96 Mon Sep 17 00:00:00 2001 From: wjf-hs Date: Thu, 29 Aug 2024 10:50:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A7=E6=A6=82=E7=9F=A5=E9=81=93=E8=AF=A5?= =?UTF-8?q?=E6=80=8E=E4=B9=88=E5=86=99=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rdparty/unicstl-unity/src/unity_config.h | 3 +- test/test.c | 7 +- test/test_queue.c | 88 ++++++++++++++++++++++- 3 files changed, 91 insertions(+), 7 deletions(-) diff --git a/3rdparty/unicstl-unity/src/unity_config.h b/3rdparty/unicstl-unity/src/unity_config.h index 70df6a4..6d5e8c8 100644 --- a/3rdparty/unicstl-unity/src/unity_config.h +++ b/3rdparty/unicstl-unity/src/unity_config.h @@ -2,7 +2,8 @@ #ifndef _UNITY_CONFIG_H_ #define _UNITY_CONFIG_H_ -#define UNITY_OUTPUT_COLOR 1 +// #define UNITY_OUTPUT_COLOR 1 #define UNITY_USE_FLUSH_STDOUT 1 +#define UNITY_INCLUDE_PRINT_FORMATTED 1 // support TEST_PRINTF #endif diff --git a/test/test.c b/test/test.c index bd6d9e2..b8e0bf4 100644 --- a/test/test.c +++ b/test/test.c @@ -60,12 +60,12 @@ void print_str(void* obj) // -------------------------------------------------- void setUp(void) { - // printf("\n----- ²âÊÔ¿ªÊ¼ -----\n"); + // before each test } void tearDown(void) { - // printf("\n----- ²âÊÔ½áÊø -----\n"); + // after each test } int main(int argc, char const *argv[]) @@ -76,6 +76,5 @@ int main(int argc, char const *argv[]) test_stack(); test_queue(); - UNITY_END(); - return 0; + return UNITY_END(); } diff --git a/test/test_queue.c b/test/test_queue.c index 9956ce3..1d5f9ae 100644 --- a/test/test_queue.c +++ b/test/test_queue.c @@ -10,20 +10,104 @@ */ #include "test.h" -static void test_queue_destory(void) +static void test_queue_init(void) { queue_t queue = NULL; + + // queue_init queue = queue_new(); TEST_ASSERT_NOT_NULL(queue); TEST_ASSERT_TRUE(queue_init(queue, sizeof(int))); + // TEST_ASSERT_NULL(queue); + + queue_free(&queue); TEST_ASSERT_NULL(queue); + // queue_init2 + queue = queue_new(); + TEST_ASSERT_TRUE(queue_init2(queue, sizeof(int), 10)); + TEST_ASSERT_NOT_NULL(queue->front); + queue_free(&queue); TEST_ASSERT_NULL(queue); } +static void test_queue_clear(void) +{ + queue_t queue = NULL; + queue = queue_new(); + TEST_ASSERT_TRUE(queue_init(queue, sizeof(int))); + // TEST_ASSERT_NULL(queue); + + queue_free(&queue); + TEST_ASSERT_NULL(queue); +} + + +static void test_queue_init_num(void) +{ + uint32_t i = 0; + int data[] = { 1,2,3,4,5,6,7,8,9,10 }; + int temp = 0; + uint32_t len = sizeof(data) / sizeof(data[0]); + + struct _queue queue; + TEST_ASSERT_TRUE(queue_init(&queue, sizeof(int))); + queue.print_obj = print_num; + + TEST_ASSERT_TRUE(queue.clear(&queue)); + + TEST_ASSERT_FALSE(queue.front(&queue, &temp)); + TEST_ASSERT_FALSE(queue.back(&queue, &temp)); + + for (i = 0; i < len; i++) + { + TEST_ASSERT_TRUE(queue.push(&queue, &data[i])); + TEST_ASSERT_EQUAL_INT(i + 1, queue.size(&queue)); + + TEST_ASSERT_TRUE(queue.front(&queue, &temp)); + TEST_ASSERT_EQUAL_INT(data[0], temp); + + TEST_ASSERT_TRUE(queue.back(&queue, &temp)); + TEST_ASSERT_EQUAL_INT(data[i], temp); + } + + TEST_ASSERT_TRUE(queue.clear(&queue)); + TEST_ASSERT_TRUE(queue.empty(&queue)); //? + for (i = 0; i < len; i++) + { + TEST_ASSERT_TRUE(queue.push(&queue, &data[i])); + } + + for (i = 0; i < len; i++) + { + TEST_ASSERT_TRUE(queue.front(&queue, &temp)); + TEST_ASSERT_EQUAL_INT(data[i], temp); + + TEST_ASSERT_TRUE(queue.back(&queue, &temp)); + TEST_ASSERT_EQUAL_INT(data[len - 1], temp); + + TEST_ASSERT_TRUE(queue.pop(&queue, &temp)); + + if(!queue.empty(&queue)) + { + TEST_ASSERT_TRUE(queue.front(&queue, &temp)); + TEST_ASSERT_EQUAL_INT(data[i + 1], temp); + + TEST_ASSERT_TRUE(queue.back(&queue, &temp)); + TEST_ASSERT_EQUAL_INT(data[len - 1], temp); + } + } + TEST_ASSERT_TRUE(queue.empty(&queue)); + TEST_ASSERT_FALSE(queue.pop(&queue, &temp)); + queue.destory(&queue); +} + + void test_queue(void) { - RUN_TEST(test_queue_destory); + // TEST_MESSAGE("----- test_queue -----"); + RUN_TEST(test_queue_init); + RUN_TEST(test_queue_init_num); }