基于unity单元测试框架的statck测试代码

This commit is contained in:
建峰 2024-08-27 22:50:45 +08:00
parent 8cd562d923
commit c6ec1b1574
7 changed files with 160 additions and 5 deletions

View File

@ -19,6 +19,9 @@
"cstdlib": "c", "cstdlib": "c",
"string.h": "c", "string.h": "c",
"demo.h": "c", "demo.h": "c",
"unity.h": "c" "unity.h": "c",
"unity_config.h": "c",
"unity_internals.h": "c",
"stdarg.h": "c"
} }
} }

View File

@ -0,0 +1,8 @@
#ifndef _UNITY_CONFIG_H_
#define _UNITY_CONFIG_H_
#define UNITY_OUTPUT_COLOR 1
#define UNITY_USE_FLUSH_STDOUT 1
#endif

View File

@ -8,6 +8,8 @@
#ifndef UNITY_INTERNALS_H #ifndef UNITY_INTERNALS_H
#define UNITY_INTERNALS_H #define UNITY_INTERNALS_H
#define UNITY_INCLUDE_CONFIG_H 1
#ifdef UNITY_INCLUDE_CONFIG_H #ifdef UNITY_INCLUDE_CONFIG_H
#include "unity_config.h" #include "unity_config.h"
#endif #endif

2
mk.bat
View File

@ -6,3 +6,5 @@ cmake -B build -G "MinGW Makefiles"
make -C build make -C build
make -C build install make -C build install
start /b ./build/release/bin/test.exe

View File

@ -10,8 +10,68 @@
*/ */
#include "test.h" #include "test.h"
void print_num(void* obj)
{
printf("(%2d ) ", *(int*)obj);
}
int compare_num(void *obj, void *obj2)
{
int num1 = *(int*)obj;
int num2 = *(int*)obj2;
if(num1 == num2)
{
return 0;
}
return num1 > num2 ? 1 : -1;
}
void print_struct(void* obj)
{
struct _student* student = (struct _student*)obj;
printf("(%4d:%-8s) ", student->id, student->name);
}
int compare_struct(void *obj, void *obj2)
{
struct _student num1 = *(struct _student*)obj;
struct _student num2 = *(struct _student*)obj2;
if(num1.id == num2.id)
{
return 0;
}
return num1.id > num2.id ? 1 : -1;
}
void print_char(void* obj)
{
printf("(%2c ) ", *(char*)obj);
}
void print_str(void* obj)
{
printf("(%s ) ", (char*)obj);
}
// --------------------------------------------------
// 测试用例
// --------------------------------------------------
void setUp(void)
{
// printf("\n----- 测试开始 -----\n");
}
void tearDown(void)
{
// printf("\n----- 测试结束 -----\n");
}
int main(int argc, char const *argv[]) int main(int argc, char const *argv[])
{ {
printf("\n---------- 单元测试 ----------\n");
return 0; UNITY_BEGIN();
RUN_TEST(test_stack);
return UNITY_END();
} }

View File

@ -18,4 +18,34 @@
#include "unicstl.h" #include "unicstl.h"
#include "unity.h" #include "unity.h"
#endif /**
* @brief demo objects
*
*/
struct _student
{
char name[16];
int id;
};
void print_num(void* obj);
int compare_num(void *obj, void *obj2);
void print_struct(void* obj);
int compare_struct(void *obj, void *obj2);
void print_char(void* obj);
void print_str(void* obj);
/**
* @brief test function
*
*/
void test_list(void);
void test_stack(void);
void test_deque(void);
void test_queue(void);
void test_tree(void);
void test_heap(void);
#endif // _TEST_H_

50
test/test_stack.c Normal file
View File

@ -0,0 +1,50 @@
/**
* @file test_stack.c
* @author wenjf (Orig5826@163.com)
* @brief
* @version 0.1
* @date 2024-08-27
*
* @copyright Copyright (c) 2024
*
*/
#include "test.h"
static void test_stack_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 _stack s;
TEST_ASSERT_TRUE(stack_init(&s, sizeof(int)));
s.print_obj = print_num;
TEST_ASSERT_FALSE(s.peek(&s, &temp));
for (i = 0; i < len; i++)
{
s.push(&s, &data[i]);
s.peek(&s, &temp);
}
for (i = 0; i < len + 1; i++)
{
if (true == s.pop(&s, &temp))
{
if(false != s.peek(&s, &temp))
{
TEST_ASSERT_EQUAL(data[len - 2 - i], temp);
}
}
}
TEST_ASSERT_TRUE(s.empty(&s));
s.destory(&s);
}
void test_stack(void)
{
RUN_TEST(test_stack_num);
}