Compare commits

...

9 Commits

17 changed files with 465 additions and 14 deletions

View File

@ -22,6 +22,7 @@
"unity.h": "c",
"unity_config.h": "c",
"unity_internals.h": "c",
"stdarg.h": "c"
"stdarg.h": "c",
"graph.h": "c"
}
}

View File

@ -1,12 +1,12 @@
# 问题和解决方案
# 问题和解决方案
## 1. 添加配置文件
在unity_internals.h文件中添加了宏定义方便使用unity_config.h文件。
## 1. 添加配置文件
在unity_internals.h文件中添加了宏定义方便使用unity_config.h文件。
```c
#define UNITY_INCLUDE_CONFIG_H
```
## 2. 多文件打印测试文件路径不合理的问题
修改`RUN_TEST`宏,在测试文件路径前添加`__FILE__`。
借助`UnitySetTestFile`函数,打印的路径信息对多文件测试也有效。
## 2. 多文件打印测试文件路径不合理的问题
修改`RUN_TEST`宏,在测试文件路径前添加`__FILE__`。
借助`UnitySetTestFile`函数,打印的路径信息对多文件测试也有效。

View File

@ -1,7 +1,7 @@
/* =========================================================================
Unity - A Test Framework for C
ThrowTheSwitch.org
Copyright (c) 2007-24 Mike Karlesky, Mark VanderVoord, & Greg Williams
Copyright (c) 2007-25 Mike Karlesky, Mark VanderVoord, & Greg Williams
SPDX-License-Identifier: MIT
========================================================================= */

View File

@ -1,7 +1,7 @@
/* =========================================================================
Unity - A Test Framework for C
ThrowTheSwitch.org
Copyright (c) 2007-24 Mike Karlesky, Mark VanderVoord, & Greg Williams
Copyright (c) 2007-25 Mike Karlesky, Mark VanderVoord, & Greg Williams
SPDX-License-Identifier: MIT
========================================================================= */
@ -11,7 +11,7 @@
#define UNITY_VERSION_MAJOR 2
#define UNITY_VERSION_MINOR 6
#define UNITY_VERSION_BUILD 0
#define UNITY_VERSION_BUILD 1
#define UNITY_VERSION ((UNITY_VERSION_MAJOR << 16) | (UNITY_VERSION_MINOR << 8) | UNITY_VERSION_BUILD)
#ifdef __cplusplus

View File

@ -1,15 +1,13 @@
/* =========================================================================
Unity - A Test Framework for C
ThrowTheSwitch.org
Copyright (c) 2007-24 Mike Karlesky, Mark VanderVoord, & Greg Williams
Copyright (c) 2007-25 Mike Karlesky, Mark VanderVoord, & Greg Williams
SPDX-License-Identifier: MIT
========================================================================= */
#ifndef UNITY_INTERNALS_H
#define UNITY_INTERNALS_H
#define UNITY_INCLUDE_CONFIG_H
#ifdef UNITY_INCLUDE_CONFIG_H
#include "unity_config.h"
#endif
@ -817,7 +815,7 @@ extern const char UnityStrErrShorthand[];
#ifndef RUN_TEST
#ifdef UNITY_SUPPORT_VARIADIC_MACROS
#define RUN_TEST(...) RUN_TEST_AT_LINE(__VA_ARGS__, __LINE__, throwaway)
#define RUN_TEST_AT_LINE(func, line, ...) UnitySetTestFile(__FILE__);UnityDefaultTestRun(func, #func, line)
#define RUN_TEST_AT_LINE(func, line, ...) UnityDefaultTestRun(func, #func, line)
#endif
#endif

62
include/graph.h Normal file
View File

@ -0,0 +1,62 @@
/**
* @file graph.h
* @author wenjf (Orig5826@163.com)
* @brief
* @version 0.1
* @date 2024-09-03
*
* @copyright Copyright (c) 2024
*
*/
#ifndef _GRAPH_H
#define _GRAPH_H
#include "common.h"
struct _graph_node {
void *obj;
uint32_t **edge;
uint8_t *visited;
};
struct _graph {
struct _graph_node *_head;
uint32_t **edges;
uint32_t _size;
uint32_t _obj_size;
uint32_t _capacity;
uint32_t _ratio;
// init
void (*init)(struct _graph *self);
bool (*from_matrix)(struct _graph *self, void *obj, uint32_t *edges, uint32_t size);
// kernel
bool (*add)(struct _graph *self, void *obj);
bool (*get)(struct _graph *self, uint32_t idx, void *obj);
bool (*remove)(struct _graph *self, uint32_t idx);
// base
uint32_t (*size)(struct _graph *self);
uint32_t (*capacity)(struct _graph *self);
bool (*empty)(struct _graph *self);
bool (*full)(struct _graph *self);
bool (*dfs)(struct _graph *self, uint32_t idx);
bool (*bfs)(struct _graph *self, uint32_t idx);
//
bool (*clear)(struct _graph *self);
void (*destory)(struct _graph *self);
void (*print)(struct _graph *self);
void (*print_obj)(void *obj);
};
typedef struct _graph* graph_t;
graph_t graph_new2(uint32_t obj_size, uint32_t capacity);
void graph_free(graph_t *graph);
#endif

View File

@ -17,5 +17,6 @@
#include "deque.h"
#include "tree.h"
#include "heap.h"
#include "graph.h"
#endif // _UNICSTL_H_

307
src/graph.c Normal file
View File

@ -0,0 +1,307 @@
/**
* @file graph.c
* @author wenjf (Orig5826@163.com)
* @brief
* @version 0.1
* @date 2024-09-03
*
* @copyright Copyright (c) 2024
*
*/
#include "graph.h"
#include "queue.h"
#include "stack.h"
static uint32_t graph_size(struct _graph* self)
{
if(self == NULL)
{
return 0;
}
return self->_size;
}
static uint32_t graph_capacity(struct _graph* self)
{
if(self == NULL)
{
return 0;
}
return self->_capacity;
}
static bool graph_clear(struct _graph *self)
{
if(self == NULL && self->_head == NULL)
{
return 0;
}
for(uint32_t i = 0; i < self->_capacity; i++)
{
for(uint32_t j = 0; j < self->_capacity; j++)
{
self->_head->edge[i][j] = 0;
}
}
self->_size = 0;
return self->_size = 0;
}
static void graph_destory(struct _graph *self)
{
if(self == NULL)
{
return;
}
self->clear(self);
if(self->_head != NULL)
{
if(self->_head->visited != NULL)
{
free(self->_head->visited);
}
if(self->_head->obj != NULL)
{
free(self->_head->obj);
}
if(self->_head->edge != NULL)
{
for(uint32_t i = 0; i < self->_capacity; i++)
{
if(self->_head->edge[i] != NULL)
{
free(self->_head->edge[i]);
}
}
free(self->_head->edge);
}
free(self->_head);
self->_head = NULL;
}
}
static void graph_print(struct _graph *self)
{
if(self == NULL || self->_head == NULL || self->print_obj == NULL)
{
return;
}
printf("\n ");
for(uint32_t i = 0; i < self->_capacity; i++)
{
self->print_obj((char *)self->_head->obj + i * self->_obj_size);
}
printf("\n");
for(uint32_t i = 0; i < self->_capacity; i++)
{
self->print_obj((char *)self->_head->obj + i * self->_obj_size);
for(uint32_t j = 0; j < self->_capacity; j++)
{
printf(" %2d ", self->_head->edge[i][j]);
}
printf("\n");
}
printf("\n");
printf("print done.\n");
}
static bool graph_from_matrix(struct _graph *self, void *obj, uint32_t *edges, uint32_t size)
{
if(self == NULL || self->_head == NULL)
{
return false;
}
if(size > self->_capacity || obj == NULL || edges == NULL)
{
return false;
}
for(uint32_t i = 0; i < size; i++)
{
memmove((char *)self->_head->obj + i * self->_obj_size, (char *)obj + i * self->_obj_size, self->_obj_size);
}
for(uint32_t i = 0; i < size; i++)
{
for(uint32_t j = 0; j < size; j++)
{
self->_head->edge[i][j] = edges[i * size + j];
}
}
self->_size = size;
return true;
}
static bool graph_bfs(struct _graph *self, uint32_t idx)
{
if(self == NULL || self->_head == NULL)
{
return false;
}
if(idx >= self->_size || idx >= self->_capacity)
{
return false;
}
for(uint32_t i = 0; i < self->_size; i++)
{
self->_head->visited[i] = 0;
}
// printf("bfs start.\n");
queue_t queue = queue_new();
queue_init(queue, sizeof(uint32_t));
queue->push(queue, &idx);
while (!queue->empty(queue))
{
queue->pop(queue, &idx);
self->_head->visited[idx] = 1;
for(uint32_t i = 0; i < self->_size; i++)
{
if(self->_head->edge[idx][i] == 1)
{
if(self->_head->visited[i] == 0)
{
queue->push(queue, &i);
}
// self->print_obj((char *)self->_head->obj + idx * self->_obj_size);
// printf("->");
// self->print_obj((char *)self->_head->obj + i * self->_obj_size);
// printf(", ");
}
}
// printf("\n");
}
queue_free(&queue);
// printf("bfs done.\n");
return true;
}
static bool graph_dfs(struct _graph *self, uint32_t idx)
{
}
static void graph_init2(struct _graph *self)
{
if(self == NULL || self->_head == NULL)
{
return;
}
for(uint32_t i = 0; i < self->_capacity; i++)
{
*((int *)self->_head->obj + i) = i;
for(uint32_t j = 0; j < self->_capacity; j++)
{
self->_head->edge[i][j] = 0;
}
}
}
graph_t graph_new2(uint32_t obj_size, uint32_t capacity)
{
if(obj_size == 0 || capacity == 0)
{
return NULL;
}
graph_t graph = malloc(sizeof(struct _graph));
if(graph == NULL)
{
goto done;
}
graph->_size = 0;
graph->_obj_size = obj_size;
graph->_capacity = capacity;
graph->_ratio = 1;
graph->init = graph_init2;
graph->destory = graph_destory;
graph->size = graph_size;
graph->capacity = graph_capacity;
graph->clear = graph_clear;
graph->from_matrix = graph_from_matrix;
graph->bfs = graph_bfs;
graph->dfs = graph_dfs;
graph->print_obj = NULL;
graph->print = graph_print;
graph->_head = (struct _graph_node *)malloc(sizeof(struct _graph_node));
if(graph->_head == NULL)
{
goto done1;
}
graph->_head->obj = (void *)malloc(graph->_obj_size);
if(graph->_head->obj == NULL)
{
goto done2;
}
graph->_head->edge = (uint32_t **)malloc(graph->_capacity * sizeof(uint32_t *));
if(graph->_head->edge == NULL)
{
goto done3;
}
uint32_t i = 0;
for(i = 0; i < graph->_capacity; i++)
{
graph->_head->edge[i] = (uint32_t *)malloc(graph->_capacity * sizeof(uint32_t));
if(graph->_head->edge[i] == NULL)
{
goto done4;
}
}
graph->_head->visited = (uint8_t *)calloc(1, graph->_capacity * sizeof(uint8_t));
if(graph->_head->visited == NULL)
{
goto done5;
}
// init graph
graph->init(graph);
return graph;
done5:
done4:
for(uint32_t j = 0; j < i; j++)
{
free(graph->_head->edge[j]);
}
free(graph->_head->edge);
done3:
free(graph->_head->obj);
done2:
free(graph->_head);
done1:
free(graph);
done:
return NULL;
}
void graph_free(graph_t *graph)
{
if(graph == NULL || *graph == NULL)
{
return;
}
(*graph)->destory(*graph);
free(*graph);
*graph = NULL;
}

View File

@ -84,6 +84,7 @@ int main(int argc, char const *argv[])
TEST_ADD(test_deque);
TEST_ADD(test_heap);
TEST_ADD(test_tree);
TEST_ADD(test_graph);
return UNITY_END();
}

View File

@ -15,6 +15,7 @@
#include <stdbool.h>
#include <stdio.h>
#define UNITY_INCLUDE_CONFIG_H
#include "unicstl.h"
#include "unity.h"
@ -47,5 +48,6 @@ void test_list(void);
void test_deque(void);
void test_tree(void);
void test_heap(void);
void test_graph(void);
#endif // _TEST_H_

View File

@ -269,6 +269,8 @@ static void test_deque_struct(void)
void test_deque(void)
{
UnitySetTestFile(__FILE__);
RUN_TEST(test_deque_num);
RUN_TEST(test_deque_struct);
}

67
test/test_graph.c Normal file
View File

@ -0,0 +1,67 @@
/**
* @file test_graph.c
* @author wenjf (Orig5826@163.com)
* @brief
* @version 0.1
* @date 2024-09-03
*
* @copyright Copyright (c) 2024
*
*/
#include "test.h"
void test_graph_new(void)
{
graph_t graph = graph_new2(sizeof(int), 10);
TEST_ASSERT_NOT_NULL(graph);
graph_free(&graph);
TEST_ASSERT_NULL(graph);
}
void test_graph_print(void)
{
graph_t graph = graph_new2(sizeof(int), 10);
TEST_ASSERT_NOT_NULL(graph);
graph->print_obj = print_num;
graph->print(graph);
graph_free(&graph);
TEST_ASSERT_NULL(graph);
}
void test_graph_from_matrix(void)
{
// const uint32_t size = 10;
#define size 5
int vertexs[size] = {0, 1, 2, 3, 4};
int matrix[size * size] = {
0, 1, 0, 0, 0,
1, 0, 1, 0, 0,
0, 1, 0, 1, 0,
0, 0, 1, 0, 1,
0, 0, 0, 1, 0,
};
graph_t graph = graph_new2(sizeof(int), 10);
TEST_ASSERT_NOT_NULL(graph);
graph->print_obj = print_num;
// graph->print(graph);
graph->from_matrix(graph, vertexs, matrix, size);
// graph->print(graph);
graph->bfs(graph, 1);
graph_free(&graph);
TEST_ASSERT_NULL(graph);
}
void test_graph(void)
{
UnitySetTestFile(__FILE__);
RUN_TEST(test_graph_new);
// RUN_TEST(test_graph_print);
// RUN_TEST(test_graph_from_matrix);
}

View File

@ -135,6 +135,8 @@ static void test_heap_struct(void)
void test_heap(void)
{
UnitySetTestFile(__FILE__);
RUN_TEST(test_heap_num);
RUN_TEST(test_heap_struct);
}

View File

@ -356,6 +356,8 @@ static void test_list_iter(void)
void test_list(void)
{
UnitySetTestFile(__FILE__);
RUN_TEST(test_list_init2);
RUN_TEST(test_list_new);
RUN_TEST(test_list_append);

View File

@ -625,6 +625,8 @@ static void test_queue2_struct(void)
void test_queue(void)
{
UnitySetTestFile(__FILE__);
RUN_TEST(test_queue_init);
RUN_TEST(test_queue_new);
RUN_TEST(test_queue_push);

View File

@ -493,6 +493,8 @@ static void test_stack2_struct(void)
void test_stack(void)
{
UnitySetTestFile(__FILE__);
RUN_TEST(test_stack_init);
RUN_TEST(test_stack_new);
RUN_TEST(test_stack_push);

View File

@ -916,6 +916,8 @@ static void test_rbtree_delete(void)
void test_tree(void)
{
UnitySetTestFile(__FILE__);
RUN_TEST(test_avltree_iter);
RUN_TEST(test_avltree_insert);
RUN_TEST(test_avltree_delete);