// Copyright (C) 2010 Davis E. King (davis@dlib.net) // License: Boost Software License See LICENSE.txt for the full license. #include #include #include #include #include #include #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() == true); DLIB_TEST(a.contains() == false); DLIB_TEST(any_cast(a) == 4); } // ---------------------------------------------------------------------------------------- void run_test() { any a, b, c; DLIB_TEST(a.is_empty()); DLIB_TEST(a.contains() == false); DLIB_TEST(a.contains() == 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() == true); DLIB_TEST(a.contains() == false); DLIB_TEST(any_cast(a) == 4); test_contains_4(a); DLIB_TEST(a.is_empty() == false); DLIB_TEST(a.contains() == true); DLIB_TEST(a.contains() == false); DLIB_TEST(any_cast(a) == 4); bool error = false; try { any_cast(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() = "test string"; DLIB_TEST(c.get() == "test string"); a = c; DLIB_TEST(a.cast_to() == "test string"); a.clear(); DLIB_TEST(a.is_empty()); error = false; try { any_cast(a); } catch (bad_any_cast&) { error = true; } DLIB_TEST(error); a = 1; b = 2; int* a_ptr = &a.get(); int* b_ptr = &b.get(); swap(a,b); DLIB_TEST(a_ptr == &b.get()); DLIB_TEST(b_ptr == &a.get()); } // ---------------------------------------------------------------------------------------- class any_tester : public tester { public: any_tester ( ) : tester ("test_any", "Runs tests on the any component.") {} void perform_test ( ) { run_test(); } } a; }