图new和free测试通过

This commit is contained in:
建峰 2024-09-03 10:15:28 +08:00
parent 2d1c498c91
commit 14ae536d71
7 changed files with 99 additions and 7 deletions

View File

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

View File

@ -15,7 +15,7 @@
struct _graph_node { struct _graph_node {
void *obj; void *obj;
struct _graph_node *next; uint32_t **edge;
}; };
struct _graph { struct _graph {

View File

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

View File

@ -34,7 +34,14 @@ static bool graph_clear(struct _graph *self)
{ {
return 0; return 0;
} }
self->_head->obj = NULL; 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; return self->_size = 0;
} }
@ -47,11 +54,43 @@ static void graph_destory(struct _graph *self)
self->clear(self); self->clear(self);
if(self->_head != NULL) if(self->_head != NULL)
{ {
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); free(self->_head);
self->_head = NULL; self->_head = NULL;
} }
} }
static void graph_init2(struct _graph *self)
{
if(self == NULL || self->_head == NULL)
{
return;
}
for(uint32_t i = 0; i < self->_capacity; 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) graph_t graph_new2(uint32_t obj_size, uint32_t capacity)
{ {
if(obj_size == 0 || capacity == 0) if(obj_size == 0 || capacity == 0)
@ -70,28 +109,53 @@ graph_t graph_new2(uint32_t obj_size, uint32_t capacity)
graph->_capacity = capacity; graph->_capacity = capacity;
graph->_ratio = 1; graph->_ratio = 1;
graph->init = graph_init2;
graph->destory = graph_destory;
graph->size = graph_size; graph->size = graph_size;
graph->capacity = graph_capacity; graph->capacity = graph_capacity;
graph->clear = graph_clear;
graph->_head = (struct _graph_node *)malloc(sizeof(struct _graph_node)); graph->_head = (struct _graph_node *)malloc(sizeof(struct _graph_node));
if(graph->_head == NULL) if(graph->_head == NULL)
{ {
goto done1; goto done1;
} }
graph->_head->next = NULL;
graph->_head->obj = (void *)malloc(graph->_obj_size * graph->_capacity * graph->_capacity); graph->_head->obj = (void *)malloc(graph->_obj_size);
if(graph->_head->obj == NULL) if(graph->_head->obj == NULL)
{ {
goto done2; 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;
}
}
// init graph // init graph
graph->init(graph); graph->init(graph);
return graph; return graph;
done4:
// free(graph->_head->obj) for(uint32_t j = 0; j < i; j++)
{
free(graph->_head->edge[j]);
}
free(graph->_head->edge);
done3:
free(graph->_head->obj);
done2: done2:
free(graph->_head); free(graph->_head);
done1: done1:

View File

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

View File

@ -47,5 +47,6 @@ void test_list(void);
void test_deque(void); void test_deque(void);
void test_tree(void); void test_tree(void);
void test_heap(void); void test_heap(void);
void test_graph(void);
#endif // _TEST_H_ #endif // _TEST_H_

24
test/test_graph.c Normal file
View File

@ -0,0 +1,24 @@
/**
* @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 g = graph_new2(sizeof(int), 10);
TEST_ASSERT(g != NULL);
graph_free(&g);
TEST_ASSERT(g == NULL);
}
void test_graph(void)
{
RUN_TEST(test_graph_new);
}