]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/test-network.c
network: Add functions to break network into subnets
[people/ms/libloc.git] / src / test-network.c
index b6776b48be914f6ca75051f63905d93462bd3161..af1b2e609bcaa833d8dbe5d91c4e323c412510f6 100644 (file)
@@ -118,6 +118,19 @@ int main(int argc, char** argv) {
        size_t nodes = loc_network_tree_count_nodes(tree);
        printf("The tree has %zu nodes\n", nodes);
 
+       // Check equals function
+       err = loc_network_eq(network1, network1);
+       if (!err) {
+               fprintf(stderr, "Network is not equal with itself\n");
+               exit(EXIT_FAILURE);
+       }
+
+       err = loc_network_eq(network1, network2);
+       if (err) {
+               fprintf(stderr, "Networks equal unexpectedly\n");
+               exit(EXIT_FAILURE);
+       }
+
        // Check subnet function
        err = loc_network_is_subnet_of(network1, network2);
        if (err != 0) {
@@ -131,6 +144,54 @@ int main(int argc, char** argv) {
                exit(EXIT_FAILURE);
        }
 
+       // Make list of subnets
+       struct loc_network_list* subnets = loc_network_subnets(network1);
+       if (!subnets) {
+               fprintf(stderr, "Could not find subnets of network\n");
+               exit(EXIT_FAILURE);
+       }
+
+       loc_network_list_dump(subnets);
+
+       while (!loc_network_list_empty(subnets)) {
+               struct loc_network* subnet = loc_network_list_pop(subnets);
+               if (!subnet) {
+                       fprintf(stderr, "Received an empty subnet\n");
+                       exit(EXIT_FAILURE);
+               }
+
+               char* s = loc_network_str(subnet);
+               printf("Received subnet %s\n", s);
+               free(s);
+
+               if (!loc_network_is_subnet_of(subnet, network1)) {
+                       fprintf(stderr, "Not a subnet\n");
+                       exit(EXIT_FAILURE);
+               }
+
+               loc_network_unref(subnet);
+       }
+
+       loc_network_list_unref(subnets);
+
+       struct loc_network_list* excluded = loc_network_exclude(network1, network2);
+       if (!excluded) {
+               fprintf(stderr, "Could not create excluded list\n");
+               exit(EXIT_FAILURE);
+       }
+
+       loc_network_list_dump(excluded);
+
+       // Reverse it
+       loc_network_list_reverse(excluded);
+       loc_network_list_dump(excluded);
+
+       // Sort them and dump them again
+       loc_network_list_sort(excluded);
+       loc_network_list_dump(excluded);
+
+       loc_network_list_unref(excluded);
+
        // Create a database
        struct loc_writer* writer;
        err = loc_writer_new(ctx, &writer, NULL, NULL);