大概知道该怎么写单元测试了

This commit is contained in:
建峰 2024-08-29 10:50:54 +08:00
parent 18a14d6d03
commit 2271502274
3 changed files with 91 additions and 7 deletions

View File

@ -2,7 +2,8 @@
#ifndef _UNITY_CONFIG_H_ #ifndef _UNITY_CONFIG_H_
#define _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_USE_FLUSH_STDOUT 1
#define UNITY_INCLUDE_PRINT_FORMATTED 1 // support TEST_PRINTF
#endif #endif

View File

@ -60,12 +60,12 @@ void print_str(void* obj)
// -------------------------------------------------- // --------------------------------------------------
void setUp(void) void setUp(void)
{ {
// printf("\n----- ²âÊÔ¿ªÊ¼ -----\n"); // before each test
} }
void tearDown(void) void tearDown(void)
{ {
// printf("\n----- ²âÊÔ½áÊø -----\n"); // after each test
} }
int main(int argc, char const *argv[]) int main(int argc, char const *argv[])
@ -76,6 +76,5 @@ int main(int argc, char const *argv[])
test_stack(); test_stack();
test_queue(); test_queue();
UNITY_END(); return UNITY_END();
return 0;
} }

View File

@ -10,20 +10,104 @@
*/ */
#include "test.h" #include "test.h"
static void test_queue_destory(void) static void test_queue_init(void)
{ {
queue_t queue = NULL; queue_t queue = NULL;
// queue_init
queue = queue_new(); queue = queue_new();
TEST_ASSERT_NOT_NULL(queue); TEST_ASSERT_NOT_NULL(queue);
TEST_ASSERT_TRUE(queue_init(queue, sizeof(int))); TEST_ASSERT_TRUE(queue_init(queue, sizeof(int)));
// TEST_ASSERT_NULL(queue);
queue_free(&queue);
TEST_ASSERT_NULL(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); queue_free(&queue);
TEST_ASSERT_NULL(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) void test_queue(void)
{ {
RUN_TEST(test_queue_destory); // TEST_MESSAGE("----- test_queue -----");
RUN_TEST(test_queue_init);
RUN_TEST(test_queue_init_num);
} }