/*============================================================================= Library: CppMicroServices Copyright (c) The CppMicroServices developers. See the COPYRIGHT file at the top-level directory of this distribution and at https://github.com/CppMicroServices/CppMicroServices/COPYRIGHT . Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. =============================================================================*/ #include "cppmicroservices/Any.h" #include "TestingMacros.h" #include #include using namespace cppmicroservices; template void TestUnsafeAnyCast(Any& anyObj, T val) { T* valPtr = unsafe_any_cast(&anyObj); US_TEST_CONDITION(valPtr != nullptr, "unsafe_any_cast"); US_TEST_CONDITION(*(valPtr) == val, "compare returned value from unsafe_any_cast"); } int AnyTest(int /*argc*/, char* /*argv*/ []) { US_TEST_BEGIN("AnyTest"); Any anyEmpty; US_TEST_FOR_EXCEPTION(std::logic_error, anyEmpty.ToString()) US_TEST_NO_EXCEPTION(anyEmpty.ToStringNoExcept()) Any anyBool = true; US_TEST_CONDITION(anyBool.Type() == typeid(bool), "Any[bool].Type()") US_TEST_CONDITION(any_cast(anyBool) == true, "any_cast()") US_TEST_CONDITION(anyBool.ToString() == "1", "Any[bool].ToString()") US_TEST_CONDITION(anyBool.ToJSON() == "true", "Any[bool].ToJSON()") TestUnsafeAnyCast(anyBool, true); anyBool = false; US_TEST_CONDITION(anyBool.ToString() == "0", "Any[bool].ToString()") US_TEST_CONDITION(anyBool.ToJSON() == "false", "Any[bool].ToJSON()") TestUnsafeAnyCast(anyBool, false); Any anyInt = 13; US_TEST_CONDITION(anyInt.Type() == typeid(int), "Any[int].Type()") US_TEST_CONDITION(any_cast(anyInt) == 13, "any_cast()") US_TEST_CONDITION(anyInt.ToString() == "13", "Any[int].ToString()") US_TEST_CONDITION(anyInt.ToJSON() == "13", "Any[int].ToJSON()") TestUnsafeAnyCast(anyInt, 13); Any anyChar = 'a'; US_TEST_CONDITION(anyChar.Type() == typeid(char), "Any[char].Type()") US_TEST_CONDITION(any_cast(anyChar) == 'a', "any_cast()") US_TEST_CONDITION(anyChar.ToString() == "a", "Any[char].ToString()") US_TEST_CONDITION(anyChar.ToJSON() == "a", "Any[char].ToJSON()") TestUnsafeAnyCast(anyChar, 'a'); Any anyFloat = 0.2f; US_TEST_CONDITION(anyFloat.Type() == typeid(float), "Any[float].Type()") US_TEST_CONDITION(any_cast(anyFloat) - 0.2f < std::numeric_limits::epsilon(), "any_cast()") US_TEST_CONDITION(anyFloat.ToString() == "0.2", "Any[float].ToString()") US_TEST_CONDITION(anyFloat.ToString() == "0.2", "Any[float].ToJSON()") TestUnsafeAnyCast(anyFloat, 0.2f); Any anyDouble = 0.5; US_TEST_CONDITION(anyDouble.Type() == typeid(double), "Any[double].Type()") US_TEST_CONDITION(any_cast(anyDouble) - 0.5 < std::numeric_limits::epsilon(), "any_cast()") US_TEST_CONDITION(anyDouble.ToString() == "0.5", "Any[double].ToString()") US_TEST_CONDITION(anyDouble.ToString() == "0.5", "Any[double].ToJSON()") TestUnsafeAnyCast(anyDouble, 0.5); Any anyString = std::string("hello"); US_TEST_CONDITION(anyString.Type() == typeid(std::string), "Any[std::string].Type()") US_TEST_CONDITION(any_cast(anyString) == "hello", "any_cast()") US_TEST_CONDITION(anyString.ToString() == "hello", "Any[std::string].ToString()") US_TEST_CONDITION(anyString.ToJSON() == "\"hello\"", "Any[std::string].ToJSON()") TestUnsafeAnyCast(anyString, std::string("hello")); std::vector vecInts; vecInts.push_back(1); vecInts.push_back(2); Any anyVectorOfInts = vecInts; US_TEST_CONDITION(anyVectorOfInts.Type() == typeid(std::vector), "Any[std::vector].Type()") US_TEST_CONDITION(any_cast>(anyVectorOfInts) == vecInts, "any_cast>()") US_TEST_CONDITION(anyVectorOfInts.ToString() == "[1,2]", "Any[std::vector].ToString()") US_TEST_CONDITION(anyVectorOfInts.ToJSON() == "[1,2]", "Any[std::vector].ToJSON()") std::list listInts; listInts.push_back(1); listInts.push_back(2); Any anyListOfInts = listInts; US_TEST_CONDITION(anyListOfInts.Type() == typeid(std::list), "Any[std::list].Type()") US_TEST_CONDITION(any_cast>(anyListOfInts) == listInts, "any_cast>()") US_TEST_CONDITION(anyListOfInts.ToString() == "[1,2]", "Any[std::list].ToString()") US_TEST_CONDITION(anyListOfInts.ToJSON() == "[1,2]", "Any[std::list].ToJSON()") std::set setInts; setInts.insert(1); setInts.insert(2); Any anySetOfInts = setInts; US_TEST_CONDITION(anySetOfInts.Type() == typeid(std::set), "Any[std::set].Type()") US_TEST_CONDITION(any_cast>(anySetOfInts) == setInts, "any_cast>()") US_TEST_CONDITION(anySetOfInts.ToString() == "[1,2]", "Any[std::set].ToString()") US_TEST_CONDITION(anySetOfInts.ToJSON() == "[1,2]", "Any[std::set].ToJSON()") std::vector vecAny; vecAny.push_back(1); vecAny.push_back(std::string("hello")); Any anyVectorOfAnys = vecAny; US_TEST_CONDITION(anyVectorOfAnys.Type() == typeid(std::vector), "Any[std::vector].Type()") US_TEST_CONDITION(anyVectorOfAnys.ToString() == "[1,hello]", "Any[std::vector].ToString()") US_TEST_CONDITION(anyVectorOfAnys.ToJSON() == "[1,\"hello\"]", "Any[std::vector].ToJSON()") std::list listAny; listAny.push_back(1); listAny.push_back(std::string("hello")); Any anyListOfAnys = listAny; US_TEST_CONDITION(anyListOfAnys.Type() == typeid(std::list), "Any[std::list].Type()") US_TEST_CONDITION(anyListOfAnys.ToString() == "[1,hello]", "Any[std::list].ToString()") US_TEST_CONDITION(anyListOfAnys.ToJSON() == "[1,\"hello\"]", "Any[std::list].ToJSON()") std::map map1; map1["one"] = 1; map1["two"] = 2; Any anyMap1 = map1; US_TEST_CONDITION(anyMap1.Type() == typeid(std::map), "Any[std::map].Type()") US_TEST_CONDITION((any_cast>(anyMap1) == map1), "any_cast>()") US_TEST_CONDITION(anyMap1.ToString() == "{one : 1, two : 2}", "Any[std::map].ToString()") US_TEST_CONDITION(anyMap1.ToJSON() == "{\"one\" : 1, \"two\" : 2}", "Any[std::map].ToJSON()") std::map map2; map2[1] = 0.3; map2[3] = std::string("bye"); Any anyMap2 = map2; US_TEST_CONDITION(anyMap2.Type() == typeid(std::map), "Any[std::map].Type()") US_TEST_CONDITION(anyMap2.ToString() == "{1 : 0.3, 3 : bye}", "Any[std::map].ToString()") US_TEST_CONDITION(anyMap2.ToJSON() == "{\"1\" : 0.3, \"3\" : \"bye\"}", "Any[std::map].ToJSON()") std::map map3; map3["number"] = 5; std::vector numbers; numbers.push_back(9); numbers.push_back(8); numbers.push_back(7); map3["vector"] = numbers; map3["map"] = map2; Any anyMap3 = map3; US_TEST_CONDITION(anyMap3.Type() == typeid(std::map), "Any[std::map].Type()") US_TEST_CONDITION( anyMap3.ToString() == "{map : {1 : 0.3, 3 : bye}, number : 5, vector : [9,8,7]}", "Any[std::map].ToString()") US_TEST_CONDITION(anyMap3.ToJSON() == "{\"map\" : {\"1\" : 0.3, \"3\" : \"bye\"}, \"number\" : " "5, \"vector\" : [9,8,7]}", "Any[std::map].ToJSON()") // Test BadAnyCastException exceptions const Any uncastableConstAny(0.0); Any uncastableAny(0.0); US_TEST_FOR_EXCEPTION(cppmicroservices::BadAnyCastException, any_cast(uncastableConstAny)) try { (void)any_cast(uncastableConstAny); } catch (const cppmicroservices::BadAnyCastException& ex) { US_TEST_OUTPUT(<< ex.what()) } US_TEST_FOR_EXCEPTION(cppmicroservices::BadAnyCastException, any_cast(uncastableAny)) try { (void)any_cast(uncastableAny); } catch (const cppmicroservices::BadAnyCastException& ex) { US_TEST_OUTPUT(<< ex.what()) } US_TEST_FOR_EXCEPTION(cppmicroservices::BadAnyCastException, ref_any_cast(uncastableConstAny)) try { (void)ref_any_cast(uncastableConstAny); } catch (const cppmicroservices::BadAnyCastException& ex) { US_TEST_OUTPUT(<< ex.what()) } US_TEST_FOR_EXCEPTION(cppmicroservices::BadAnyCastException, ref_any_cast(uncastableAny)) try { (void)ref_any_cast(uncastableAny); } catch (const cppmicroservices::BadAnyCastException& ex) { US_TEST_OUTPUT(<< ex.what()) } US_TEST_END() }