File: FunctorTest.h

package info (click to toggle)
libloki 0.1.7-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,608 kB
  • sloc: cpp: 30,475; ansic: 1,974; makefile: 365; php: 316; perl: 108
file content (214 lines) | stat: -rw-r--r-- 6,296 bytes parent folder | download | duplicates (5)
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
207
208
209
210
211
212
213
214
///////////////////////////////////////////////////////////////////////////////
// Unit Test for Loki
//
// Copyright Terje Sletteb and Pavel Vozenilek 2002.
// Copyright Peter Kmmel, 2006

// Permission to use, copy, modify, and distribute this software for any
// purpose is hereby granted without fee, provided that this copyright and
// permissions notice appear in all copies and derivatives.
//
// This software is provided "as is" without express or implied warranty.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef FUNCTORTEST_H
#define FUNCTORTEST_H

// $Id: FunctorTest.h 760 2006-10-17 20:36:13Z syntheticpp $


#include <loki/Functor.h>

///////////////////////////////////////////////////////////////////////////////
// FunctorTest
///////////////////////////////////////////////////////////////////////////////

void free_function(bool &result)
{
    result=true;
}

class FunctorTest : public Test
{
public:
    FunctorTest() : Test("Functor.h")
    {}

    virtual void execute(TestResult &result)
    {
        printName(result);

        using namespace Loki;

        bool r;

        TestFunctor testFunctor;
        TestClass testClass;


#ifndef LOKI_DISABLE_TYPELIST_MACROS

        Functor<void,LOKI_TYPELIST_1(bool &)> function(testFunction);
        Functor<void,LOKI_TYPELIST_1(bool &)> function2(testFunction);
        Functor<void,LOKI_TYPELIST_1(bool &)> functor(testFunctor);
        Functor<void,LOKI_TYPELIST_1(bool &)> functor2(testFunctor);
        Functor<void,LOKI_TYPELIST_1(bool &)> classFunctor(&testClass,&TestClass::member);
        Functor<void,LOKI_TYPELIST_1(bool &)> classFunctor2(&testClass,&TestClass::member);
        Functor<void,LOKI_TYPELIST_1(bool &)> functorCopy(function);
        Functor<void,LOKI_TYPELIST_1(bool &)> functorCopy2(function);

        Functor<void,NullType> bindFunctor(BindFirst(function,testResult));
        Functor<void,NullType> bindFunctor2(BindFirst(function,testResult));

        Functor<void> chainFunctor(Chain(bindFunctor,bindFunctor));
        Functor<void> chainFunctor2(Chain(bindFunctor,bindFunctor));

        Functor<void,LOKI_TYPELIST_1(bool &)> member_func(&testClass,&TestClass::member);
        Functor<void,LOKI_TYPELIST_1(bool &)> free_func(&free_function);
        Functor<void,LOKI_TYPELIST_1(bool &)> NULL_func;
        Functor<void,LOKI_TYPELIST_1(bool &)> NULL_func0;
#else

        Functor<void,Seq<bool &> > function(testFunction);
        Functor<void,Seq<bool &> > function2(testFunction);
        Functor<void,Seq<bool &> > functor(testFunctor);
        Functor<void,Seq<bool &> > functor2(testFunctor);
        Functor<void,Seq<bool &> > classFunctor(&testClass,&TestClass::member);
        Functor<void,Seq<bool &> > classFunctor2(&testClass,&TestClass::member);
        Functor<void,Seq<bool &> > functorCopy(function);
        Functor<void,Seq<bool &> > functorCopy2(function);

        //TODO:
        // BindFirst and Chainer

        Functor<void,Seq<bool &> > member_func(&testClass,&TestClass::member);
        Functor<void,Seq<bool &> > free_func(&free_function);
        Functor<void,Seq<bool &> > NULL_func;
        Functor<void,Seq<bool &> > NULL_func0;
#endif

        testResult=false;
        function(testResult);
        bool functionResult=testResult;

        testResult=false;
        functor(testResult);
        bool functorResult=testResult;

        testResult=false;
        classFunctor(testResult);
        bool classFunctorResult=testResult;

        testResult=false;
        functorCopy(testResult);
        bool functorCopyResult=testResult;

#ifdef LOKI_FUNCTORS_ARE_COMPARABLE

        bool functionCompare = function==function2;
        bool functorCompare = functor!=functor2;  // is this a bug?
        bool classFunctorCompare = classFunctor==classFunctor2;
        bool functorCopyCompare = functorCopy==functorCopy2;

        bool free_mem = free_func!=member_func;
        bool mem_free = member_func!=free_func;

        bool null0 = NULL_func == NULL_func0;
        bool null1 = NULL_func != free_func;
        bool null2 = NULL_func != member_func;
        bool null3 = free_func != NULL_func;
        bool null4 = member_func != NULL_func;


#ifndef LOKI_DISABLE_TYPELIST_MACROS

        bool bindFunctorCompare = bindFunctor==bindFunctor2;
        bool chainFunctorCompare = chainFunctor==chainFunctor2;
#endif

        bool compare =  functionCompare &&
                        functorCompare &&
                        classFunctorCompare &&
                        functorCopyCompare &&
                        mem_free &&
                        free_mem &&
                        null0 &&
                        null1 &&
                        null2 &&
                        null3 &&
                        null4
#ifndef LOKI_DISABLE_TYPELIST_MACROS
                        && bindFunctorCompare
                        && chainFunctorCompare;
#else
                        ;
#endif

#else

        bool compare=true;
#endif //LOKI_FUNCTORS_ARE_COMPARABLE


#ifndef LOKI_DISABLE_TYPELIST_MACROS

        testResult=false;
        bindFunctor();
        bool bindFunctorResult=testResult;

        testResult=false;
        chainFunctor();
        bool chainFunctorResult=testResult;

        r=functionResult && functorResult && classFunctorResult && functorCopyResult && bindFunctorResult &&
          chainFunctorResult && compare;
#else
        //TODO!
        r=functionResult && functorResult && classFunctorResult && functorCopyResult && compare;
#endif

        testAssert("Functor",r,result);

        std::cout << '\n';
    }

private:
    static bool testResult;

    static void testFunction(bool &result)
    {
        result=true;
    }

    class TestFunctor
    {
    public:
        void operator()(bool &result)
        {
            result=true;
        }
        bool operator==(const TestFunctor& rhs) const
        {
            const TestFunctor* p = &rhs;
            return this==p;
        }
    };

    class TestClass
    {
    public:
        void member(bool &result)
        {
            result=true;
        }
    };
}
functorTest;

bool FunctorTest::testResult;

#ifndef SMALLOBJ_CPP
# define SMALLOBJ_CPP
# include "../../src/SmallObj.cpp"
#endif
#endif