File: static_set.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 (206 lines) | stat: -rw-r--r-- 5,254 bytes parent folder | download | duplicates (12)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright (C) 2005  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/queue.h>
#include <dlib/static_set.h>
#include <dlib/set.h>
#include "tester.h"

namespace  
{

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

    logger dlog("test.static_set");

    template <
        typename set
        >
    void static_set_kernel_test (
    )
    /*!
        requires
            - set is an implementation of static_set/static_set_kernel_abstract.h and
              is instantiated to hold ints
        ensures
            - runs tests on set for compliance with the specs 
    !*/
    {        

        print_spinner();

        srand(static_cast<unsigned int>(time(0)));

        typedef queue<int>::kernel_2a_c queue_of_int;
        typedef dlib::set<int>::kernel_1a_c set_of_int;

        queue_of_int q, qb, qc;
        set_of_int ds;

        set S;
        S.load(ds);

        for (int k = 1; k < 1000; ++k)
        {
            q.clear();
            qb.clear();
            qc.clear();
            unsigned long num = k; 
            for (unsigned long i = 0; i < num; ++i)
            {
                int a = ::rand()&0xFF;
                int b = a;
                int c = a;
                q.enqueue(a);
                qb.enqueue(b);
                qc.enqueue(c);
            }



            set s;

            DLIB_TEST(s.size() == 0);
            DLIB_TEST(s.at_start());
            DLIB_TEST(s.current_element_valid() == false);
            DLIB_TEST(s.move_next() == false);
            DLIB_TEST(s.current_element_valid() == false);
            DLIB_TEST(s.at_start() == false);

            s.load(q);
            DLIB_TEST(s.at_start());
            set se;
            se.load(q);

            DLIB_TEST(se.size() == 0);
            DLIB_TEST(se.at_start() == true);
            DLIB_TEST(se.current_element_valid() == false);     
            DLIB_TEST(se.move_next() == false);
            DLIB_TEST(se.at_start() == false);
            DLIB_TEST(se.current_element_valid() == false);


            DLIB_TEST(s.size() == qb.size());
            DLIB_TEST(s.at_start() == true);
            DLIB_TEST(s.current_element_valid() == false);     
            DLIB_TEST(s.move_next() == true);
            DLIB_TEST(s.at_start() == false);
            DLIB_TEST(s.current_element_valid() == true);
            s.reset();
            se.reset();

            swap(se,s);

            DLIB_TEST(s.size() == 0);
            DLIB_TEST(s.at_start() == true);
            DLIB_TEST(s.current_element_valid() == false);     
            DLIB_TEST(s.move_next() == false);
            DLIB_TEST(s.at_start() == false);
            DLIB_TEST(s.current_element_valid() == false);

            DLIB_TEST(se.size() == qb.size());
            DLIB_TEST(se.at_start() == true);
            DLIB_TEST(se.current_element_valid() == false);     
            DLIB_TEST(se.move_next() == true);
            DLIB_TEST(se.at_start() == false);
            DLIB_TEST(se.current_element_valid() == true);
            s.reset();
            se.reset();

            swap(se,s);



            int last = 0;
            while (s.move_next())
            {
                DLIB_TEST(last <= s.element());
                last = s.element();
            }



            while (qb.move_next())
            {
                int a;
                qb.dequeue(a);
                DLIB_TEST(s.is_member(a));
                DLIB_TEST(!se.is_member(a));

                // make sure is_member() doesn't hang
                for (int l = 0; l < 100; ++l)
                {
                    int a = ::rand();
                    s.is_member(a);
                }
            }

            swap(s,se);

            // serialize the state of se, then clear se, then
            // load the state back into se.
            ostringstream sout;
            serialize(se,sout);
            DLIB_TEST(se.at_start() == true);
            istringstream sin(sout.str());
            se.clear();
            deserialize(se,sin);
            DLIB_TEST(se.at_start() == true);


            last = 0;
            while (se.move_next())
            {
                DLIB_TEST(last <= se.element());
                last = se.element();
            }


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

            while (qc.move_next())
            {
                int a;
                qc.dequeue(a);
                DLIB_TEST(se.is_member(a));
                DLIB_TEST(!s.is_member(a));
            }


        }
    }





    class static_set_tester : public tester
    {
    public:
        static_set_tester (
        ) :
            tester ("test_static_set",
                    "Runs tests on the static_set component.")
        {}

        void perform_test (
        )
        {
            dlog << LINFO << "testing kernel_1a";
            static_set_kernel_test<static_set<int>::kernel_1a>  ();
            dlog << LINFO << "testing kernel_1a_c";
            static_set_kernel_test<static_set<int>::kernel_1a_c>();
        }
    } a;

}