Compare commits

..

No commits in common. "f148c26764d5b999949ebdedcf44c1dfe72a578f" and "14ae536d71b8f70a84c8eb362829a6aa7664a56a" have entirely different histories.

3 changed files with 4 additions and 104 deletions

View File

@ -29,7 +29,6 @@ struct _graph {
// 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);
@ -46,9 +45,6 @@ struct _graph {
//
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;

View File

@ -75,57 +75,6 @@ static void graph_destory(struct _graph *self)
}
}
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");
}
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];
}
}
return true;
}
static void graph_init2(struct _graph *self)
{
if(self == NULL || self->_head == NULL)
@ -135,7 +84,6 @@ static void graph_init2(struct _graph *self)
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;
@ -168,11 +116,6 @@ graph_t graph_new2(uint32_t obj_size, uint32_t capacity)
graph->capacity = graph_capacity;
graph->clear = graph_clear;
graph->from_matrix = graph_from_matrix;
graph->print_obj = NULL;
graph->print = graph_print;
graph->_head = (struct _graph_node *)malloc(sizeof(struct _graph_node));
if(graph->_head == NULL)
{

View File

@ -12,52 +12,13 @@
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] = {1,2,3,4,5};
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_free(&graph);
TEST_ASSERT_NULL(graph);
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);
// RUN_TEST(test_graph_print);
RUN_TEST(test_graph_from_matrix);
}