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
|
// Copyright (C) 2010 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");
// ----------------------------------------------------------------------------------------
void test_contains_4(
const any a
)
{
DLIB_TEST(a.is_empty() == false);
DLIB_TEST(a.contains<int>() == true);
DLIB_TEST(a.contains<double>() == false);
DLIB_TEST(any_cast<int>(a) == 4);
}
// ----------------------------------------------------------------------------------------
void run_test()
{
any a, b, c;
DLIB_TEST(a.is_empty());
DLIB_TEST(a.contains<int>() == false);
DLIB_TEST(a.contains<string>() == false);
DLIB_TEST(a.is_empty());
a = b;
swap(a,b);
a.swap(b);
a = 4;
DLIB_TEST(a.is_empty() == false);
DLIB_TEST(a.contains<int>() == true);
DLIB_TEST(a.contains<double>() == false);
DLIB_TEST(any_cast<int>(a) == 4);
test_contains_4(a);
DLIB_TEST(a.is_empty() == false);
DLIB_TEST(a.contains<int>() == true);
DLIB_TEST(a.contains<double>() == false);
DLIB_TEST(any_cast<int>(a) == 4);
bool error = false;
try
{
any_cast<double>(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<string>() = "test string";
DLIB_TEST(c.get<string>() == "test string");
a = c;
DLIB_TEST(a.cast_to<string>() == "test string");
a.clear();
DLIB_TEST(a.is_empty());
error = false;
try
{
any_cast<string>(a);
}
catch (bad_any_cast&)
{
error = true;
}
DLIB_TEST(error);
a = 1;
b = 2;
int* a_ptr = &a.get<int>();
int* b_ptr = &b.get<int>();
swap(a,b);
DLIB_TEST(a_ptr == &b.get<int>());
DLIB_TEST(b_ptr == &a.get<int>());
}
// ----------------------------------------------------------------------------------------
class any_tester : public tester
{
public:
any_tester (
) :
tester ("test_any",
"Runs tests on the any component.")
{}
void perform_test (
)
{
run_test();
}
} a;
}
|