95 lines
2.7 KiB
C
95 lines
2.7 KiB
C
#include <stdio.h>
|
|
#include <stdbool.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "graph.h"
|
|
|
|
// Helper function to print test results (Sonet Thinking Generated)
|
|
void test_result(const char *test_name, bool result) {
|
|
printf("%-40s %s\n", test_name, result ? "PASSED" : "FAILED");
|
|
}
|
|
/*void print_string(const void *s)
|
|
{
|
|
const char *string = s;
|
|
printf("[%s] ", string);
|
|
}
|
|
*/
|
|
int main(void)
|
|
{
|
|
printf("Testing graph implementation...\n\n");
|
|
int max_nodes = 10;
|
|
// Test 1: Create an empty graph
|
|
graph *g = graph_empty(max_nodes);
|
|
test_result("Create empty graph", g != NULL);
|
|
|
|
// Test 2: Check if graph is empty
|
|
test_result("Graph is empty", graph_is_empty(g));
|
|
|
|
// Test 3: Insert nodes
|
|
char *node1 = "AAA";
|
|
char *node2 = "BBB";
|
|
char *node3 = "CCC";
|
|
|
|
g = graph_insert_node(g, node1);
|
|
test_result("Insert node A", !graph_is_empty(g));
|
|
|
|
g = graph_insert_node(g, node2);
|
|
g = graph_insert_node(g, node3);
|
|
|
|
// Test 4: Find nodes
|
|
node *n1 = graph_find_node(g, node1);
|
|
test_result("Find node A", n1 != NULL);
|
|
|
|
node *n2 = graph_find_node(g, node2);
|
|
test_result("Find node B", n2 != NULL);
|
|
|
|
node *n3 = graph_find_node(g, node3);
|
|
test_result("Find node C", n3 != NULL);
|
|
|
|
node *n4 = graph_find_node(g, "D");
|
|
test_result("Find non-existent node D", n4 == NULL);
|
|
|
|
// Test 5: Insert edges
|
|
g = graph_insert_edge(g, n1, n2);
|
|
g = graph_insert_edge(g, n1, n3);
|
|
g = graph_insert_edge(g, n2, n1);
|
|
g = graph_insert_edge(g, n2, n2);
|
|
g = graph_insert_edge(g, n1, n1);
|
|
|
|
test_result("Graph has edges", graph_has_edges(g));
|
|
|
|
// Test 6: Node seen status
|
|
test_result("Node A not seen initially", !graph_node_is_seen(g, n1));
|
|
|
|
g = graph_node_set_seen(g, n1, true);
|
|
test_result("Node A is seen after setting", graph_node_is_seen(g, n1));
|
|
|
|
g = graph_node_set_seen(g, n2, true);
|
|
test_result("Node B is seen after setting", graph_node_is_seen(g, n2));
|
|
|
|
g = graph_reset_seen(g);
|
|
test_result("Node A not seen after reset", !graph_node_is_seen(g, n1));
|
|
test_result("Node B not seen after reset", !graph_node_is_seen(g, n2));
|
|
|
|
// Test 7: Neighbors (if implemented)
|
|
/*
|
|
dlist *neighbours = graph_neighbours(g, n1);
|
|
if (neighbours != NULL) {
|
|
printf("Neighbors of A: ");
|
|
// Print neighbors
|
|
//dlist_print(neighbours, print_string);
|
|
printf("\n\n");
|
|
dlist_kill(neighbours);
|
|
}*/
|
|
|
|
// Test 8: Print the graph
|
|
graph_print(g);
|
|
|
|
// Test 9: Clean up (if implemented)
|
|
graph_kill(g);
|
|
printf("Graph destroyed\n");
|
|
|
|
|
|
printf("\nTests completed.\n");
|
|
return 0;
|
|
} |