summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/test/disjoint_subsets_sized.cpp
blob: 57ced68e624a495433053e59046ce2adfa9b2ec1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (C) 2011  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.


#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <dlib/disjoint_subsets.h>

#include "tester.h"

namespace
{

    using namespace test;
    using namespace dlib;
    using namespace std;

    logger dlog("test.disjoint_subsets_sized");

    void test_disjoint_subsets_sized()
    {
        print_spinner();
        disjoint_subsets_sized s;

        DLIB_TEST(s.size() == 0);
        DLIB_TEST(s.get_number_of_sets() == 0);

        s.set_size(5);
        DLIB_TEST(s.size() == 5);
        DLIB_TEST(s.get_number_of_sets() == 5);

        DLIB_TEST(s.find_set(0) == 0);
        DLIB_TEST(s.find_set(1) == 1);
        DLIB_TEST(s.find_set(2) == 2);
        DLIB_TEST(s.find_set(3) == 3);
        DLIB_TEST(s.find_set(4) == 4);

        DLIB_TEST(s.get_size_of_set(0) == 1);
        DLIB_TEST(s.get_size_of_set(1) == 1);
        DLIB_TEST(s.get_size_of_set(2) == 1);
        DLIB_TEST(s.get_size_of_set(3) == 1);
        DLIB_TEST(s.get_size_of_set(4) == 1);

        unsigned long id = s.merge_sets(1,3);
        DLIB_TEST(s.get_number_of_sets() == 4);
        DLIB_TEST(s.find_set(0) == 0);
        DLIB_TEST(s.find_set(1) == id);
        DLIB_TEST(s.find_set(2) == 2);
        DLIB_TEST(s.find_set(3) == id);
        DLIB_TEST(s.find_set(4) == 4);
        DLIB_TEST(s.get_size_of_set(0) == 1);
        DLIB_TEST(s.get_size_of_set(s.find_set(1)) == 2);
        DLIB_TEST(s.get_size_of_set(2) == 1);
        DLIB_TEST(s.get_size_of_set(s.find_set(3)) == 2);
        DLIB_TEST(s.get_size_of_set(4) == 1);

        id = s.merge_sets(s.find_set(1),4);
        DLIB_TEST(s.get_number_of_sets() == 3);
        DLIB_TEST(s.find_set(0) == 0);
        DLIB_TEST(s.find_set(1) == id);
        DLIB_TEST(s.find_set(2) == 2);
        DLIB_TEST(s.find_set(3) == id);
        DLIB_TEST(s.find_set(4) == id);
        DLIB_TEST(s.get_size_of_set(0) == 1);
        DLIB_TEST(s.get_size_of_set(s.find_set(1)) == 3);
        DLIB_TEST(s.get_size_of_set(2) == 1);
        DLIB_TEST(s.get_size_of_set(s.find_set(3)) == 3);
        DLIB_TEST(s.get_size_of_set(s.find_set(4)) == 3);

        unsigned long id2 = s.merge_sets(0,2);
        DLIB_TEST(s.get_number_of_sets() == 2);
        DLIB_TEST(s.find_set(0) == id2);
        DLIB_TEST(s.find_set(1) == id);
        DLIB_TEST(s.find_set(2) == id2);
        DLIB_TEST(s.find_set(3) == id);
        DLIB_TEST(s.find_set(4) == id);
        DLIB_TEST(s.get_size_of_set(s.find_set(0)) == 2);
        DLIB_TEST(s.get_size_of_set(s.find_set(1)) == 3);
        DLIB_TEST(s.get_size_of_set(s.find_set(2)) == 2);
        DLIB_TEST(s.get_size_of_set(s.find_set(3)) == 3);
        DLIB_TEST(s.get_size_of_set(s.find_set(4)) == 3);

        id = s.merge_sets(s.find_set(1),s.find_set(0));
        DLIB_TEST(s.get_number_of_sets() == 1);
        DLIB_TEST(s.find_set(0) == id);
        DLIB_TEST(s.find_set(1) == id);
        DLIB_TEST(s.find_set(2) == id);
        DLIB_TEST(s.find_set(3) == id);
        DLIB_TEST(s.find_set(4) == id);
        DLIB_TEST(s.get_size_of_set(s.find_set(0)) == 5);
        DLIB_TEST(s.get_size_of_set(s.find_set(1)) == 5);
        DLIB_TEST(s.get_size_of_set(s.find_set(2)) == 5);
        DLIB_TEST(s.get_size_of_set(s.find_set(3)) == 5);
        DLIB_TEST(s.get_size_of_set(s.find_set(4)) == 5);

        DLIB_TEST(s.size() == 5);
        s.set_size(1);
        DLIB_TEST(s.size() == 1);
        DLIB_TEST(s.get_number_of_sets() == 1);
        DLIB_TEST(s.find_set(0) == 0);
        DLIB_TEST(s.get_size_of_set(0) == 1);
        s.set_size(2);
        DLIB_TEST(s.size() == 2);
        DLIB_TEST(s.get_number_of_sets() == 2);
        DLIB_TEST(s.find_set(0) == 0);
        DLIB_TEST(s.find_set(1) == 1);
        DLIB_TEST(s.get_size_of_set(0) == 1);
        DLIB_TEST(s.get_size_of_set(1) == 1);
        id = s.merge_sets(0,1);
        DLIB_TEST(s.size() == 2);
        DLIB_TEST(s.get_number_of_sets() == 1);
        DLIB_TEST(id == s.find_set(0));
        DLIB_TEST(id == s.find_set(1));
        DLIB_TEST(s.get_size_of_set(s.find_set(0)) == 2);
        DLIB_TEST(s.get_size_of_set(s.find_set(1)) == 2);
        DLIB_TEST(s.size() == 2);
        s.clear();
        DLIB_TEST(s.size() == 0);
        DLIB_TEST(s.get_number_of_sets() == 0);

    }


    class tester_disjoint_subsets_sized : public tester
    {
    public:
        tester_disjoint_subsets_sized (
        ) :
            tester ("test_disjoint_subsets_sized",
                    "Runs tests on the disjoint_subsets_sized component.")
        {}

        void perform_test (
        )
        {
            test_disjoint_subsets_sized();
        }
    } a;


}