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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
// Copyright (C) 2011 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#include <dlib/any.h>
#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include "../rand.h"
#include "tester.h"
namespace
{
using namespace test;
using namespace dlib;
using namespace std;
logger dlog("test.any_function");
// ----------------------------------------------------------------------------------------
int add ( int a, int b) { return a + b; }
string cat ( string a, string b) { return a + b; }
// ----------------------------------------------------------------------------------------
void set_vals1( int& a) { a = 1; }
void set_vals2( int& a, int& b) { a = 1; b = 2; }
void set_vals3( int& a, int& b, int& c) { a = 1; b = 2; c = 3; }
void set_vals4( int& a, int& b, int& c, int& d) { a = 1; b = 2; c = 3; d = 4; }
void set_vals5( int& a, int& b, int& c, int& d, int& e) { a = 1; b = 2; c = 3; d = 4; e = 5; }
void set_vals6( int& a, int& b, int& c, int& d, int& e, int& f) { a = 1; b = 2; c = 3; d = 4; e = 5; f = 6; }
void set_vals7( int& a, int& b, int& c, int& d, int& e, int& f, int& g) { a = 1; b = 2; c = 3; d = 4; e = 5; f = 6; g = 7; }
void set_vals8( int& a, int& b, int& c, int& d, int& e, int& f, int& g, int& h)
{ a = 1; b = 2; c = 3; d = 4; e = 5; f = 6; g = 7; h = 8; }
void set_vals9( int& a, int& b, int& c, int& d, int& e, int& f, int& g, int& h, int& i)
{ a = 1; b = 2; c = 3; d = 4; e = 5; f = 6; g = 7; h = 8; i = 9;}
void set_vals10( int& a, int& b, int& c, int& d, int& e, int& f, int& g, int& h, int& i, int& j)
{ a = 1; b = 2; c = 3; d = 4; e = 5; f = 6; g = 7; h = 8; i = 9; j = 10;}
void zero_vals( int& a, int& b, int& c, int& d, int& e, int& f, int& g, int& h, int& i, int& j)
{ a = 0; b = 0; c = 0; d = 0; e = 0; f = 0; g = 0; h = 0; i = 0; j = 0;}
// ----------------------------------------------------------------------------------------
struct test
{
int operator()() const { return 4; }
};
struct test2
{
int v;
test2() : v(0) {}
test2(int val) : v(val) {}
int operator()() const { return v; }
};
// ----------------------------------------------------------------------------------------
void test_contains_4(
const any_function<int()> a
)
{
DLIB_TEST(a.is_empty() == false);
DLIB_TEST(a.is_set() == true);
DLIB_TEST(a.contains<test>() == true);
DLIB_TEST(a.contains<int(*)()>() == false);
DLIB_TEST(any_cast<test>(a)() == 4);
DLIB_TEST(a() == 4);
}
// ----------------------------------------------------------------------------------------
void run_test()
{
any_function<int()> a, b, c;
DLIB_TEST(a.is_empty());
DLIB_TEST(a.is_set()==false);
DLIB_TEST(a.contains<int(*)()>() == false);
DLIB_TEST(a.contains<test>() == false);
DLIB_TEST(a.is_empty());
a = b;
swap(a,b);
a.swap(b);
a = test();
test_contains_4(a);
bool error = false;
try
{
any_cast<int(*)()>(a);
}
catch (bad_any_cast&)
{
error = true;
}
DLIB_TEST(error);
swap(a,b);
test_contains_4(b);
DLIB_TEST(a.is_empty());
a = b;
test_contains_4(a);
c.get<test2>() = test2(10);
DLIB_TEST(c.get<test2>().v == 10);
a = c;
DLIB_TEST(a.cast_to<test2>().v == 10);
a.clear();
DLIB_TEST(a.is_empty());
error = false;
try
{
any_cast<test>(a);
}
catch (bad_any_cast&)
{
error = true;
}
DLIB_TEST(error);
}
// ----------------------------------------------------------------------------------------
void run_test2()
{
any_function<int(int,int)> f = &add;
DLIB_TEST(f(1,3) == 4);
any_function<string(string,string)> g(&cat);
DLIB_TEST(g("one", "two") == "onetwo");
}
// ----------------------------------------------------------------------------------------
void run_test3()
{
any_function<void(int&)> f1;
any_function<void(int&,int&)> f2;
any_function<void(int&,int&,int&)> f3;
any_function<void(int&,int&,int&,int&)> f4;
any_function<void(int&,int&,int&,int&,int&)> f5;
any_function<void(int&,int&,int&,int&,int&,int&)> f6;
any_function<void(int&,int&,int&,int&,int&,int&,int&)> f7;
any_function<void(int&,int&,int&,int&,int&,int&,int&,int&)> f8;
any_function<void(int&,int&,int&,int&,int&,int&,int&,int&,int&)> f9;
any_function<void(int&,int&,int&,int&,int&,int&,int&,int&,int&,int&)> f10;
f1 = set_vals1;
f2 = set_vals2;
f3 = set_vals3;
f4 = set_vals4;
f5 = set_vals5;
f6 = set_vals6;
f7 = set_vals7;
f8 = set_vals8;
f9 = set_vals9;
f10 = set_vals10;
int a,b,c,d,e,f,g,h,i,j;
zero_vals(a,b,c,d,e,f,g,h,i,j);
f1(a);
DLIB_TEST(a==1);
zero_vals(a,b,c,d,e,f,g,h,i,j);
f2(a,b);
DLIB_TEST(a==1 && b==2);
zero_vals(a,b,c,d,e,f,g,h,i,j);
f3(a,b,c);
DLIB_TEST(a==1 && b==2 && c==3);
zero_vals(a,b,c,d,e,f,g,h,i,j);
f4(a,b,c,d);
DLIB_TEST(a==1 && b==2 && c==3 && d==4);
zero_vals(a,b,c,d,e,f,g,h,i,j);
f5(a,b,c,d,e);
DLIB_TEST(a==1 && b==2 && c==3 && d==4 && e==5);
zero_vals(a,b,c,d,e,f,g,h,i,j);
f6(a,b,c,d,e,f);
DLIB_TEST(a==1 && b==2 && c==3 && d==4 && e==5 && f==6);
zero_vals(a,b,c,d,e,f,g,h,i,j);
f7(a,b,c,d,e,f,g);
DLIB_TEST(a==1 && b==2 && c==3 && d==4 && e==5 && f==6 && g==7);
zero_vals(a,b,c,d,e,f,g,h,i,j);
f8(a,b,c,d,e,f,g,h);
DLIB_TEST(a==1 && b==2 && c==3 && d==4 && e==5 && f==6 && g==7 && h==8);
zero_vals(a,b,c,d,e,f,g,h,i,j);
f9(a,b,c,d,e,f,g,h,i);
DLIB_TEST(a==1 && b==2 && c==3 && d==4 && e==5 && f==6 && g==7 && h==8 && i==9);
zero_vals(a,b,c,d,e,f,g,h,i,j);
f10(a,b,c,d,e,f,g,h,i,j);
DLIB_TEST(a==1 && b==2 && c==3 && d==4 && e==5 && f==6 && g==7 && h==8 && i==9 && j==10);
zero_vals(a,b,c,d,e,f,g,h,i,j);
}
// ----------------------------------------------------------------------------------------
class test_any_function : public tester
{
public:
test_any_function (
) :
tester ("test_any_function",
"Runs tests on the any_function component.")
{}
void perform_test (
)
{
print_spinner();
run_test();
print_spinner();
run_test2();
print_spinner();
run_test3();
}
} a;
}
|