test/unionfind_test.cc
changeset 184 716b220697a0
parent 105 e4948ef6a4ca
child 209 765619b7cbb2
equal deleted inserted replaced
1:6f41e45927f5 2:4de5820fe583
    13  * This software is provided "AS IS" with no warranty of any kind,
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    15  * purpose.
    16  *
    16  *
    17  */
    17  */
    18 
       
    19 #include <iostream>
       
    20 
    18 
    21 #include <lemon/list_graph.h>
    19 #include <lemon/list_graph.h>
    22 #include <lemon/maps.h>
    20 #include <lemon/maps.h>
    23 #include <lemon/unionfind.h>
    21 #include <lemon/unionfind.h>
    24 #include "test_tools.h"
    22 #include "test_tools.h"
    37   for(int i=0;i<20;i++) n.push_back(g.addNode());
    35   for(int i=0;i<20;i++) n.push_back(g.addNode());
    38 
    36 
    39   U.insert(n[1]);
    37   U.insert(n[1]);
    40   U.insert(n[2]);
    38   U.insert(n[2]);
    41 
    39 
    42   check(U.join(n[1],n[2]) != -1,"Test failed.");
    40   check(U.join(n[1],n[2]) != -1, "Something is wrong with UnionFindEnum");
    43 
    41 
    44   U.insert(n[3]);
    42   U.insert(n[3]);
    45   U.insert(n[4]);
    43   U.insert(n[4]);
    46   U.insert(n[5]);
    44   U.insert(n[5]);
    47   U.insert(n[6]);
    45   U.insert(n[6]);
    48   U.insert(n[7]);
    46   U.insert(n[7]);
    49 
    47 
    50 
    48 
    51   check(U.join(n[1],n[4]) != -1,"Test failed.");
    49   check(U.join(n[1],n[4]) != -1, "Something is wrong with UnionFindEnum");
    52   check(U.join(n[2],n[4]) == -1,"Test failed.");
    50   check(U.join(n[2],n[4]) == -1, "Something is wrong with UnionFindEnum");
    53   check(U.join(n[3],n[5]) != -1,"Test failed.");
    51   check(U.join(n[3],n[5]) != -1, "Something is wrong with UnionFindEnum");
    54 
    52 
    55 
    53 
    56   U.insert(n[8],U.find(n[5]));
    54   U.insert(n[8],U.find(n[5]));
    57 
    55 
    58 
    56 
    59   check(U.size(U.find(n[4])) == 3,"Test failed.");
    57   check(U.size(U.find(n[4])) == 3, "Something is wrong with UnionFindEnum");
    60   check(U.size(U.find(n[5])) == 3,"Test failed.");
    58   check(U.size(U.find(n[5])) == 3, "Something is wrong with UnionFindEnum");
    61   check(U.size(U.find(n[6])) == 1,"Test failed.");
    59   check(U.size(U.find(n[6])) == 1, "Something is wrong with UnionFindEnum");
    62   check(U.size(U.find(n[2])) == 3,"Test failed.");
    60   check(U.size(U.find(n[2])) == 3, "Something is wrong with UnionFindEnum");
    63 
    61 
    64 
    62 
    65   U.insert(n[9]);
    63   U.insert(n[9]);
    66   U.insert(n[10],U.find(n[9]));
    64   U.insert(n[10],U.find(n[9]));
    67 
    65 
    68 
    66 
    69   check(U.join(n[8],n[10]) != -1,"Test failed.");
    67   check(U.join(n[8],n[10])  != -1, "Something is wrong with UnionFindEnum");
    70 
    68 
    71 
    69 
    72   check(U.size(U.find(n[4])) == 3,"Test failed.");
    70   check(U.size(U.find(n[4])) == 3, "Something is wrong with UnionFindEnum");
    73   check(U.size(U.find(n[9])) == 5,"Test failed.");
    71   check(U.size(U.find(n[9])) == 5, "Something is wrong with UnionFindEnum");
    74 
    72 
    75   check(U.size(U.find(n[8])) == 5,"Test failed.");
    73   check(U.size(U.find(n[8])) == 5, "Something is wrong with UnionFindEnum");
    76 
    74 
    77   U.erase(n[9]);
    75   U.erase(n[9]);
    78   U.erase(n[1]);
    76   U.erase(n[1]);
    79 
    77 
    80   check(U.size(U.find(n[10])) == 4,"Test failed.");
    78   check(U.size(U.find(n[10])) == 4, "Something is wrong with UnionFindEnum");
    81   check(U.size(U.find(n[2])) == 2,"Test failed.");
    79   check(U.size(U.find(n[2]))  == 2, "Something is wrong with UnionFindEnum");
    82 
    80 
    83   U.erase(n[6]);
    81   U.erase(n[6]);
    84   U.split(U.find(n[8]));
    82   U.split(U.find(n[8]));
    85 
    83 
    86 
    84 
    87   check(U.size(U.find(n[4])) == 2,"Test failed.");
    85   check(U.size(U.find(n[4])) == 2, "Something is wrong with UnionFindEnum");
    88   check(U.size(U.find(n[3])) == 1,"Test failed.");
    86   check(U.size(U.find(n[3])) == 1, "Something is wrong with UnionFindEnum");
    89   check(U.size(U.find(n[2])) == 2,"Test failed.");
    87   check(U.size(U.find(n[2])) == 2, "Something is wrong with UnionFindEnum");
    90 
    88 
    91 
    89 
    92   check(U.join(n[3],n[4]) != -1,"Test failed.");
    90   check(U.join(n[3],n[4]) != -1, "Something is wrong with UnionFindEnum");
    93   check(U.join(n[2],n[4]) == -1,"Test failed.");
    91   check(U.join(n[2],n[4]) == -1, "Something is wrong with UnionFindEnum");
    94 
    92 
    95 
    93 
    96   check(U.size(U.find(n[4])) == 3,"Test failed.");
    94   check(U.size(U.find(n[4])) == 3, "Something is wrong with UnionFindEnum");
    97   check(U.size(U.find(n[3])) == 3,"Test failed.");
    95   check(U.size(U.find(n[3])) == 3, "Something is wrong with UnionFindEnum");
    98   check(U.size(U.find(n[2])) == 3,"Test failed.");
    96   check(U.size(U.find(n[2])) == 3, "Something is wrong with UnionFindEnum");
    99 
    97 
   100   U.eraseClass(U.find(n[4]));
    98   U.eraseClass(U.find(n[4]));
   101   U.eraseClass(U.find(n[7]));
    99   U.eraseClass(U.find(n[7]));
   102 
   100 
   103   return 0;
   101   return 0;