| 19 | using namespace us; |
| 20 | |
| 21 | int usAnyTest(int /*argc*/, char* /*argv*/[]) |
| 22 | { |
| 23 | US_TEST_BEGIN("AnyTest"); |
| 24 | |
| 25 | Any empty; |
| 26 | US_TEST_CONDITION(empty.Empty(), "Default-constructed Any is empty") |
| 27 | US_TEST_CONDITION(empty.ToString() == "", "Empty Any.ToString() returns empty string") |
| 28 | US_TEST_CONDITION(empty.ToJSON() == "null", "Empty Any.ToJSON() returns JSON null literal") |
| 29 | |
| 30 | Any anyBool = true; |
| 31 | US_TEST_CONDITION(anyBool.Type() == typeid(bool), "Any[bool].Type()") |
| 32 | US_TEST_CONDITION(any_cast<bool>(anyBool) == true, "any_cast<bool>()") |
| 33 | US_TEST_CONDITION(anyBool.ToString() == "1", "Any[bool].ToString()") |
| 34 | US_TEST_CONDITION(anyBool.ToJSON() == "true", "Any[bool].ToJSON()") |
| 35 | anyBool = false; |
| 36 | US_TEST_CONDITION(anyBool.ToString() == "0", "Any[bool].ToString()") |
| 37 | US_TEST_CONDITION(anyBool.ToJSON() == "false", "Any[bool].ToJSON()") |
| 38 | |
| 39 | Any anyInt = 13; |
| 40 | US_TEST_CONDITION(anyInt.Type() == typeid(int), "Any[int].Type()") |
| 41 | US_TEST_CONDITION(any_cast<int>(anyInt) == 13, "any_cast<int>()") |
| 42 | US_TEST_CONDITION(anyInt.ToString() == "13", "Any[int].ToString()") |
| 43 | US_TEST_CONDITION(anyInt.ToJSON() == "13", "Any[int].ToJSON()") |
| 44 | |
| 45 | Any anyChar = 'a'; |
| 46 | US_TEST_CONDITION(anyChar.Type() == typeid(char), "Any[char].Type()") |
| 47 | US_TEST_CONDITION(any_cast<char>(anyChar) == 'a', "any_cast<char>()") |
| 48 | US_TEST_CONDITION(anyChar.ToString() == "a", "Any[char].ToString()") |
| 49 | US_TEST_CONDITION(anyChar.ToJSON() == "a", "Any[char].ToJSON()") |
| 50 | |
| 51 | Any anyFloat = 0.2f; |
| 52 | US_TEST_CONDITION(anyFloat.Type() == typeid(float), "Any[float].Type()") |
| 53 | US_TEST_CONDITION(any_cast<float>(anyFloat) - 0.2f < std::numeric_limits<float>::epsilon(), "any_cast<float>()") |
| 54 | US_TEST_CONDITION(anyFloat.ToString() == "0.2", "Any[float].ToString()") |
| 55 | US_TEST_CONDITION(anyFloat.ToString() == "0.2", "Any[float].ToJSON()") |
| 56 | |
| 57 | Any anyDouble = 0.5; |
| 58 | US_TEST_CONDITION(anyDouble.Type() == typeid(double), "Any[double].Type()") |
| 59 | US_TEST_CONDITION(any_cast<double>(anyDouble) - 0.5 < std::numeric_limits<double>::epsilon(), "any_cast<double>()") |
| 60 | US_TEST_CONDITION(anyDouble.ToString() == "0.5", "Any[double].ToString()") |
| 61 | US_TEST_CONDITION(anyDouble.ToString() == "0.5", "Any[double].ToJSON()") |
| 62 | |
| 63 | Any anyString = std::string("hello"); |
| 64 | US_TEST_CONDITION(anyString.Type() == typeid(std::string), "Any[std::string].Type()") |
| 65 | US_TEST_CONDITION(any_cast<std::string>(anyString) == "hello", "any_cast<std::string>()") |
| 66 | US_TEST_CONDITION(anyString.ToString() == "hello", "Any[std::string].ToString()") |
| 67 | US_TEST_CONDITION(anyString.ToJSON() == "\"hello\"", "Any[std::string].ToJSON()") |
| 68 | |
| 69 | std::vector<int> vecInts; |
| 70 | vecInts.push_back(1); |
| 71 | vecInts.push_back(2); |
| 72 | Any anyVectorOfInts = vecInts; |
| 73 | US_TEST_CONDITION(anyVectorOfInts.Type() == typeid(std::vector<int>), "Any[std::vector<int>].Type()") |
| 74 | US_TEST_CONDITION(any_cast<std::vector<int> >(anyVectorOfInts) == vecInts, "any_cast<std::vector<int>>()") |
| 75 | US_TEST_CONDITION(anyVectorOfInts.ToString() == "[1,2]", "Any[std::vector<int>].ToString()") |
| 76 | US_TEST_CONDITION(anyVectorOfInts.ToJSON() == "[1,2]", "Any[std::vector<int>].ToJSON()") |
| 77 | |
| 78 | std::list<int> listInts; |