File: disjoint_subsets.cpp

package info (click to toggle)
mldemos 0.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,224 kB
  • ctags: 46,525
  • sloc: cpp: 306,887; ansic: 167,718; ml: 126; sh: 109; makefile: 2
file content (106 lines) | stat: -rw-r--r-- 2,630 bytes parent folder | download | duplicates (6)
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
// 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");

    void test_disjoint_subset()
    {
        print_spinner();
        disjoint_subsets s;

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

        s.set_size(5);
        DLIB_TEST(s.size() == 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);


        unsigned long id = s.merge_sets(1,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) == 4);

        id = s.merge_sets(s.find_set(1),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) == id);

        unsigned long id2 = s.merge_sets(0,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);

        id = s.merge_sets(s.find_set(1),s.find_set(0));
        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.size() == 5);
        s.set_size(1);
        DLIB_TEST(s.size() == 1);
        DLIB_TEST(s.find_set(0) == 0);
        s.set_size(2);
        DLIB_TEST(s.size() == 2);
        DLIB_TEST(s.find_set(0) == 0);
        DLIB_TEST(s.find_set(1) == 1);
        id = s.merge_sets(0,1);
        DLIB_TEST(s.size() == 2);
        DLIB_TEST(id == s.find_set(0));
        DLIB_TEST(id == s.find_set(1));
        DLIB_TEST(s.size() == 2);
        s.clear();
        DLIB_TEST(s.size() == 0);

    }


    class tester_disjoint_subsets : public tester
    {
    public:
        tester_disjoint_subsets (
        ) :
            tester ("test_disjoint_subsets",
                    "Runs tests on the disjoint_subsets component.")
        {}

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


}