File: tuple.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 (184 lines) | stat: -rw-r--r-- 4,050 bytes parent folder | download | duplicates (4)
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
// Copyright (C) 2007  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/tuple.h>

#include "tester.h"

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

    logger dlog("test.tuple");

    struct nil 
    {
        template <typename T>
        void operator() (
            const T& 
        ) const
        {
        }
    };


    struct inc 
    {
        template <typename T>
        void operator() (
            T& a
        ) const
        {
            a += 1;
        }
    };


    template <typename T>
    void check_const (
        const T& t
    )
    {
        t.template get<0>();

        typedef typename T::template get_type<0>::type type0;
        t.template get<type0>();
        t.template index<type0>();
    }

    template <typename T>
    void check_nonconst (
        T& t
    )
    {
        t.template get<0>();

        typedef typename T::template get_type<0>::type type0;
        t.template get<type0>();
        t.template index<type0>();
    }

    void tuple_test (
    )
    /*!
        ensures
            - runs tests on tuple functions for compliance with the specs 
    !*/
    {        

        print_spinner();

        tuple<> a;
        tuple<int> b;
        tuple<int, float> c;


        a.get<1>();
        a.get<2>();
        a.get<3>();
        a.get<4>();
        a.get<5>();

        check_nonconst(b);
        check_nonconst(c);
        check_const(b);
        check_const(c);

        COMPILE_TIME_ASSERT((is_same_type<tuple<>::get_type<0>::type, null_type>::value));
        COMPILE_TIME_ASSERT((is_same_type<tuple<int>::get_type<0>::type, int>::value));
        COMPILE_TIME_ASSERT((is_same_type<tuple<int,float>::get_type<0>::type, int>::value));
        COMPILE_TIME_ASSERT((is_same_type<tuple<int,float>::get_type<1>::type, float>::value));
        COMPILE_TIME_ASSERT((is_same_type<tuple<int,float>::get_type<2>::type, null_type>::value));

        b.get<0>() = 8;
        DLIB_TEST(b.get<int>() == 8);
        DLIB_TEST(b.index<int>() == 0);

        c.get<0>() = 9;
        DLIB_TEST(c.get<int>() == 9);
        DLIB_TEST(c.index<int>() == 0);
        c.get<1>() = 3.0;
        DLIB_TEST(c.get<float>() == 3.0);
        DLIB_TEST(c.index<float>() == 1);



        {
            typedef tuple<int, short, long> T;
            T a, b;
            a.get<0>() = 1;
            a.get<1>() = 3;
            a.get<2>() = 2;

            b = a;

            inc i;
            nil n;
            a.for_each(inc());
            a.for_each(i);
            const_cast<const T&>(a).for_each(nil());
            const_cast<const T&>(a).for_each(n);

            DLIB_TEST(a.get<0>() == b.get<0>()+2);
            DLIB_TEST(a.get<1>() == b.get<1>()+2);
            DLIB_TEST(a.get<2>() == b.get<2>()+2);

            ostringstream sout;

            serialize(a,sout);
            istringstream sin(sout.str());
            deserialize(b,sin);

            DLIB_TEST(a.get<0>() == b.get<0>());
            DLIB_TEST(a.get<1>() == b.get<1>());
            DLIB_TEST(a.get<2>() == b.get<2>());

            a.for_index(i,0);
            a.for_index(inc(),1);
            const_cast<const T&>(a).for_index(n,2);
            const_cast<const T&>(a).for_index(nil(),0);

            DLIB_TEST(a.get<0>() == b.get<0>()+1);
            DLIB_TEST(a.get<1>() == b.get<1>()+1);
            DLIB_TEST(a.get<2>() == b.get<2>()+0);

            swap(a,b);

            DLIB_TEST(b.get<0>() == a.get<0>()+1);
            DLIB_TEST(b.get<1>() == a.get<1>()+1);
            DLIB_TEST(b.get<2>() == a.get<2>()+0);
        }


    }




    class tuple_tester : public tester
    {
    public:
        tuple_tester (
        ) :
            tester ("test_tuple",
                    "Runs tests on the tuple object")
        {}

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

}