Added a test for the any object.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403944
This commit is contained in:
Davis King 2010-12-04 17:18:21 +00:00
parent cfc0f1be2b
commit 7488d6f57e
3 changed files with 131 additions and 0 deletions

View File

@ -15,6 +15,7 @@ endif(COMMAND cmake_policy)
set (tests set (tests
example.cpp example.cpp
example_args.cpp example_args.cpp
any.cpp
array2d.cpp array2d.cpp
array.cpp array.cpp
base64.cpp base64.cpp

129
dlib/test/any.cpp Normal file
View File

@ -0,0 +1,129 @@
// 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 = true;
try
{
any_cast<double>(a);
}
catch (bad_any_cast&)
{
error = false;
}
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 = true;
try
{
any_cast<string>(a);
}
catch (bad_any_cast&)
{
error = false;
}
DLIB_TEST(error);
}
// ----------------------------------------------------------------------------------------
class any_tester : public tester
{
public:
any_tester (
) :
tester ("test_any",
"Runs tests on the any component.")
{}
void perform_test (
)
{
void run_test();
}
} a;
}

View File

@ -25,6 +25,7 @@ SRC += ../all/source.cpp
SRC += example.cpp SRC += example.cpp
SRC += example_args.cpp SRC += example_args.cpp
SRC += any.cpp
SRC += array2d.cpp SRC += array2d.cpp
SRC += array.cpp SRC += array.cpp
SRC += base64.cpp SRC += base64.cpp