diff --git a/CMakeLists.txt b/CMakeLists.txt index 825ca74fa..502c69bff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 3.2) project(llbc) # Set project c++ standard. -set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD 11) # Set project cmake options. set(CMAKE_VERBOSE_MAKEFILE on) diff --git a/iOSBuild/testsuite.xcodeproj/project.pbxproj b/iOSBuild/testsuite.xcodeproj/project.pbxproj index aa9e50b56..8a0586c9f 100644 --- a/iOSBuild/testsuite.xcodeproj/project.pbxproj +++ b/iOSBuild/testsuite.xcodeproj/project.pbxproj @@ -70,7 +70,7 @@ D87A14F61DA3559100DB1F62 /* TestCase_Core_Utils_Misc.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D87A14AD1DA3559100DB1F62 /* TestCase_Core_Utils_Misc.cpp */; }; D87A14F71DA3559100DB1F62 /* TestCase_Core_Utils_Network.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D87A14AF1DA3559100DB1F62 /* TestCase_Core_Utils_Network.cpp */; }; D87A14F81DA3559100DB1F62 /* TestCase_Core_Utils_Text.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D87A14B11DA3559100DB1F62 /* TestCase_Core_Utils_Text.cpp */; }; - D87A14F91DA3559100DB1F62 /* TestCase_Core_VariantTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D87A14B41DA3559100DB1F62 /* TestCase_Core_VariantTest.cpp */; }; + D87A14F91DA3559100DB1F62 /* TestCase_Core_Variant.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D87A14B41DA3559100DB1F62 /* TestCase_Core_Variant.cpp */; }; D87A14FA1DA3559100DB1F62 /* TestCase_ObjBase_Array.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D87A14B71DA3559100DB1F62 /* TestCase_ObjBase_Array.cpp */; }; D87A14FB1DA3559100DB1F62 /* TestCase_ObjBase_Dictionary.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D87A14B91DA3559100DB1F62 /* TestCase_ObjBase_Dictionary.cpp */; }; D87A14FC1DA3559100DB1F62 /* TestCase_ObjBase_Object.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D87A14BB1DA3559100DB1F62 /* TestCase_ObjBase_Object.cpp */; }; @@ -199,8 +199,8 @@ D87A14B01DA3559100DB1F62 /* TestCase_Core_Utils_Network.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestCase_Core_Utils_Network.h; sourceTree = ""; }; D87A14B11DA3559100DB1F62 /* TestCase_Core_Utils_Text.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TestCase_Core_Utils_Text.cpp; sourceTree = ""; }; D87A14B21DA3559100DB1F62 /* TestCase_Core_Utils_Text.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestCase_Core_Utils_Text.h; sourceTree = ""; }; - D87A14B41DA3559100DB1F62 /* TestCase_Core_VariantTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TestCase_Core_VariantTest.cpp; sourceTree = ""; }; - D87A14B51DA3559100DB1F62 /* TestCase_Core_VariantTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestCase_Core_VariantTest.h; sourceTree = ""; }; + D87A14B41DA3559100DB1F62 /* TestCase_Core_Variant.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TestCase_Core_Variant.cpp; sourceTree = ""; }; + D87A14B51DA3559100DB1F62 /* TestCase_Core_Variant.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestCase_Core_Variant.h; sourceTree = ""; }; D87A14B71DA3559100DB1F62 /* TestCase_ObjBase_Array.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TestCase_ObjBase_Array.cpp; sourceTree = ""; }; D87A14B81DA3559100DB1F62 /* TestCase_ObjBase_Array.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestCase_ObjBase_Array.h; sourceTree = ""; }; D87A14B91DA3559100DB1F62 /* TestCase_ObjBase_Dictionary.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TestCase_ObjBase_Dictionary.cpp; sourceTree = ""; }; @@ -509,8 +509,8 @@ D87A14B31DA3559100DB1F62 /* variant */ = { isa = PBXGroup; children = ( - D87A14B41DA3559100DB1F62 /* TestCase_Core_VariantTest.cpp */, - D87A14B51DA3559100DB1F62 /* TestCase_Core_VariantTest.h */, + D87A14B41DA3559100DB1F62 /* TestCase_Core_Variant.cpp */, + D87A14B51DA3559100DB1F62 /* TestCase_Core_Variant.h */, ); path = variant; sourceTree = ""; @@ -662,7 +662,7 @@ D87A14DD1DA3559100DB1F62 /* TestCase_Core_Helper_JVHelper.cpp in Sources */, D87A14D91DA3559100DB1F62 /* TestCase_Core_Config_Property.cpp in Sources */, D87A14261DA3541D00DB1F62 /* AppDelegate.mm in Sources */, - D87A14F91DA3559100DB1F62 /* TestCase_Core_VariantTest.cpp in Sources */, + D87A14F91DA3559100DB1F62 /* TestCase_Core_Variant.cpp in Sources */, D87A14C21DA3559100DB1F62 /* TestCase_Comm_Facade.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/llbc/include/llbc/common/OSHeader.h b/llbc/include/llbc/common/OSHeader.h index 4d6b4b14f..e37b152fd 100644 --- a/llbc/include/llbc/common/OSHeader.h +++ b/llbc/include/llbc/common/OSHeader.h @@ -107,7 +107,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/llbc/include/llbc/core/variant/Variant.h b/llbc/include/llbc/core/variant/Variant.h index b9fe52443..522536a0b 100644 --- a/llbc/include/llbc/core/variant/Variant.h +++ b/llbc/include/llbc/core/variant/Variant.h @@ -214,6 +214,7 @@ class LLBC_EXPORT LLBC_Variant explicit LLBC_Variant(const char *str); explicit LLBC_Variant(const std::string &str); explicit LLBC_Variant(const LLBC_String &str); + explicit LLBC_Variant(const LLBC_CString &str); template explicit LLBC_Variant(const std::pair<_T1, _T2> &pa); explicit LLBC_Variant(const Seq &seq); @@ -463,7 +464,9 @@ class LLBC_EXPORT LLBC_Variant template ::value, int>::type = 0> LLBC_Variant& operator=(const _T &en); + LLBC_Variant &operator=(const std::string &str); LLBC_Variant &operator=(const LLBC_String &str); + LLBC_Variant &operator=(const LLBC_CString &str); template LLBC_Variant &operator=(const std::pair<_T1, _T2> &pa); LLBC_Variant &operator=(const Seq &seq); diff --git a/llbc/include/llbc/core/variant/VariantInl.h b/llbc/include/llbc/core/variant/VariantInl.h index 41f5fb077..6c434cce3 100644 --- a/llbc/include/llbc/core/variant/VariantInl.h +++ b/llbc/include/llbc/core/variant/VariantInl.h @@ -137,6 +137,13 @@ inline LLBC_Variant::LLBC_Variant(const LLBC_String &str) _holder.data.obj.str = new LLBC_String(str.data(), str.size()); } +inline LLBC_Variant::LLBC_Variant(const LLBC_CString &str) +{ + _holder.type = LLBC_VariantType::STR_DFT; + if (!str.empty()) + _holder.data.obj.str = new LLBC_String(str.data(), str.size()); +} + template LLBC_Variant::LLBC_Variant(const std::pair<_T1, _T2> &pa) { diff --git a/llbc/src/core/variant/Variant.cpp b/llbc/src/core/variant/Variant.cpp index 4407d0da9..1cdd4512c 100644 --- a/llbc/src/core/variant/Variant.cpp +++ b/llbc/src/core/variant/Variant.cpp @@ -758,7 +758,7 @@ LLBC_Variant &LLBC_Variant::operator=(ulong ul) return *this; } -LLBC_Variant & LLBC_Variant::operator=(const char * const &str) +LLBC_Variant &LLBC_Variant::operator=(const char * const &str) { if (!IsStr()) { @@ -823,6 +823,25 @@ LLBC_Variant &LLBC_Variant::operator=(const double &d) return *this; } +LLBC_Variant &LLBC_Variant::operator=(const std::string &str) +{ + BecomeStr(); + if (str.empty()) + { + if (_holder.data.obj.str) + _holder.data.obj.str->clear(); + } + else + { + if (_holder.data.obj.str) + *_holder.data.obj.str = str; + else + _holder.data.obj.str = new LLBC_String(str); + } + + return *this; +} + LLBC_Variant &LLBC_Variant::operator=(const LLBC_String &str) { BecomeStr(); @@ -842,6 +861,25 @@ LLBC_Variant &LLBC_Variant::operator=(const LLBC_String &str) return *this; } +LLBC_Variant &LLBC_Variant::operator=(const LLBC_CString &str) +{ + BecomeStr(); + if (str.empty()) + { + if (_holder.data.obj.str) + _holder.data.obj.str->clear(); + } + else + { + if (_holder.data.obj.str) + *_holder.data.obj.str = str; + else + _holder.data.obj.str = new LLBC_String(str); + } + + return *this; +} + LLBC_Variant &LLBC_Variant::operator=(const Seq &seq) { BecomeSeq(); diff --git a/testsuite/TestSuite.h b/testsuite/TestSuite.h index 4326d1997..3c65917fe 100644 --- a/testsuite/TestSuite.h +++ b/testsuite/TestSuite.h @@ -49,7 +49,7 @@ #include "core/helper/TestCase_Core_Helper_StlHelper.h" #include "core/file/TestCase_Core_File_File.h" #include "core/file/TestCase_Core_File_Directory.h" -#include "core/variant/TestCase_Core_VariantTest.h" +#include "core/variant/TestCase_Core_Variant.h" #include "core/config/TestCase_Core_Config_Ini.h" #include "core/config/TestCase_Core_Config_Property.h" #include "core/time/TestCase_Core_Time_Time.h" diff --git a/testsuite/TestSuiteDef.h b/testsuite/TestSuiteDef.h index c26e65290..417df3a54 100644 --- a/testsuite/TestSuiteDef.h +++ b/testsuite/TestSuiteDef.h @@ -28,7 +28,7 @@ __DEFINE_TEST_CASE(TestCase_Core_Utils_Network) __DEFINE_TEST_CASE(TestCase_Core_Helper_StlHelper) __DEFINE_TEST_CASE(TestCase_Core_File_File) __DEFINE_TEST_CASE(TestCase_Core_File_Directory) -__DEFINE_TEST_CASE(TestCase_Core_VariantTest) +__DEFINE_TEST_CASE(TestCase_Core_Variant) __DEFINE_TEST_CASE(TestCase_Core_Config_Ini) __DEFINE_TEST_CASE(TestCase_Core_Config_Property) __DEFINE_TEST_CASE(TestCase_Core_Time_Time) diff --git a/testsuite/core/variant/TestCase_Core_VariantTest.cpp b/testsuite/core/variant/TestCase_Core_Variant.cpp similarity index 94% rename from testsuite/core/variant/TestCase_Core_VariantTest.cpp rename to testsuite/core/variant/TestCase_Core_Variant.cpp index 0dd269baf..0ed0bfe84 100644 --- a/testsuite/core/variant/TestCase_Core_VariantTest.cpp +++ b/testsuite/core/variant/TestCase_Core_Variant.cpp @@ -1,676 +1,711 @@ -// The MIT License (MIT) - -// Copyright (c) 2013 lailongwei -// -// Permission is hereby granted, free of charge, to any person obtaining a copy of -// this software and associated documentation files (the "Software"), to deal in -// the Software without restriction, including without limitation the rights to -// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -// the Software, and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR -// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER -// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - - -#include "core/variant/TestCase_Core_VariantTest.h" -#include "TestCase_Core_VariantTest.h" - -int TestCase_Core_VariantTest::Run(int argc, char *argv[]) -{ - std::cout << "LLBC_Variant test:" << std::endl; - - BasicTest(); - std::cout << std::endl; - - EnumTest(); - std::cout << std::endl; - - CompareTest(); - std::cout << std::endl; - - CompositeKeyTest(); - std::cout << std::endl; - - ArithmeticTest(); - std::cout << std::endl; - - PairTest(); - std::cout << std::endl; - - SeqTest(); - std::cout << std::endl; - - DictTest(); - std::cout << std::endl; - - SerializeTest(); - std::cout << std::endl; - - HashTest(); - std::cout << std::endl; - - ConvertToUnurderedStlContainerTest(); - - std::cout <<"Press any key to continue ... ..." <() << std::endl; - - std::cout << "Implicit [const ]char * cast test:" << std::endl; - strVal = "hello world"; - std::cout << "- After strVal = \"hello world\":" << strVal << std::endl; - std::cout << "- strVal ? strVal : \"hello\": " << (strVal ? strVal : "hello") << std::endl; - - char mutableHeyStr[4] = { 'H', 'e' , 'y', '\0' }; - strVal = mutableHeyStr; - std::cout << " - After strVal = mutableHeyStr(Hey):" << strVal << std::endl; - std::cout << "- strVal ? strVal : mutableHeyStr(Hey): " << (strVal ? strVal : mutableHeyStr) << std::endl; - - LLBC_Variant constructFromCStr("hello world"); - LLBC_Variant constructFromMStr(mutableHeyStr); - std::cout << "Construct from mutable/immutable test:" << std::endl; - std::cout << "- construct from cstr:\"hello world\":" << constructFromCStr < return: " << tse2.AsEnum() << std::endl; - - TraditionalStyleEnum tse3 = tse1; - std::cout << " - operator return: " << tse3 << std::endl; - - LLBC_Variant tse4; - tse4 = TraditionalStyleEnum::C; - std::cout << " - operator=(Enum): " << tse4 << std::endl; - - std::cout << " - static_cast(nil): " << static_cast(LLBC_Variant()) << std::endl; - } - - { - std::cout << "- Class style enum:" << std::endl; - - LLBC_Variant cse1(ClassStyleEnum::X); - std::cout << " - default construct: " << cse1<< std::endl; - - LLBC_Variant cse2(-2); - std::cout << " - AsEnum<> return: " - << static_cast(cse2.AsEnum()) << std::endl; - - ClassStyleEnum cse3 = cse1; - std::cout << " - operator return: " - << static_cast(cse3) << std::endl; - - LLBC_Variant cse4; - cse4 = ClassStyleEnum::Z; - std::cout << " - operator(Enum): " << cse4 << std::endl; - - std::cout << " - static_cast(nil): " - << static_cast(static_cast(LLBC_Variant())) << std::endl; - } -} - -void TestCase_Core_VariantTest::CompareTest() -{ - std::cout << "Compare test: " << std::endl; - - LLBC_Variant nil; - LLBC_Variant boolTrue(true); - LLBC_Variant boolFalse(false); - std::cout << "nil < nil?: " << (nil < nil) << ", nil == nil?: " << (nil == nil) << std::endl; - std::cout << "nil < boolTrue?: " << (nil < boolTrue) << ", nil == boolTrue?: " << (nil == boolTrue) << std::endl; - std::cout << "nil < boolFalse?: " << (nil < boolFalse) << ", nil == boolFalse?: " << (nil == boolFalse) << std::endl; - std::cout << "boolTrue < boolTrue?: " << (boolTrue < boolTrue) << ", boolTrue == boolTrue?: " << (boolTrue == boolTrue) << std::endl; - std::cout << "boolFalse < boolFalse?: " << (boolFalse < boolFalse) << ", boolFalse == boolFalse?: " << (boolFalse == boolFalse) << std::endl; - std::cout << "boolTrue < boolFalse?: " << (boolTrue < boolFalse) << ", boolTrue == boolFalse?: " << (boolTrue == boolFalse) << std::endl; - std::cout << "boolFalse < boolTrue?: " << (boolFalse < boolTrue) << ", boolFalse == boolTrue?: " << (boolFalse == boolTrue) << std::endl; - - LLBC_Variant int0(0); - LLBC_Variant int1(1); - LLBC_Variant int10010(10010); - LLBC_Variant int10086(10086); - std::cout << "nil < int10010?: " << (nil < int10010) << ", nil == int10010?: " << (nil == int10010) << std::endl; - std::cout << "int0 < boolFalse?: " << (int0 < boolFalse) << ", int0 == boolFalse?: " << (int0 == boolFalse) << std::endl; - std::cout << "int1 < boolTrue?: " << (int1 < boolTrue) << ", int1 == boolTrue?: " << (int1 == boolTrue) << std::endl; - std::cout << "int10010 < int10086?: " << (int10010 < int10086) << ", int10010 == int10086?: " << (int10010 == int10086) << std::endl; - std::cout << "int10086 < int10010?: " << (int10086 < int10010) << ", int10086 == int10010?: " << (int10086 == int10010) << std::endl; - std::cout << "int10086 < int10086?: " << (int10086 < int10086) << ", int10086 == int10086?: " << (int10086 == int10086) << std::endl; - - LLBC_Variant dbl10010(10010.0); - LLBC_Variant dbl10086(10086.0); - std::cout << "nil < dbl10010?: " << (nil < dbl10010) << ", nil == dbl10010?:" << (nil == dbl10010) << std::endl; - std::cout << "int0 < dbl10010?: " << (int0 < dbl10010) << ", int0 == dbl10010?: " << (int0 == dbl10010) << std::endl; - std::cout << "int10010 < dbl10010?: " << (int10010 < dbl10010) << ", int10010 == dbl10010?: " << (int10010 == dbl10010) << std::endl; - std::cout << "dbl10010 < int10010?: " << (dbl10010 < int10010) << ", dbl10010 == int10010?: " << (dbl10010 == int10010) << std::endl; - std::cout << "dbl10010 < dbl10086?: " << (dbl10010 < dbl10086) << ", dbl10010 == dbl10086?: " << (dbl10010 == dbl10086) << std::endl; - std::cout << "dbl10086 < dbl10086?: " << (dbl10086 < dbl10086) << ", dbl10086 == dbl10086?: " << (dbl10086 == dbl10086) << std::endl; - - LLBC_Variant str1_FromStringLiteral("Hello world(string literal)"); - LLBC_Variant str1_FromStlString(std::string("Hello world(stl string)")); - LLBC_Variant str1_FromLLBCString(LLBC_String("Hello world(llbc string)")); - LLBC_Variant str2_FromStringLiteral("Hello world(string literal)"); - std::cout << "str1_FromStringLiteral: " << str1_FromStringLiteral << std::endl; - std::cout << "str2_FromStringLiteral: " << str2_FromStringLiteral << std::endl; - std::cout << "str1_FromStlString: " << str1_FromStlString << std::endl; - std::cout << "str1_FromLLBCString: " << str1_FromLLBCString << std::endl; - std::cout << "str1_FromStringLiteral == str1_FromStlString?: " << (str1_FromStringLiteral == str1_FromStlString) << std::endl; - std::cout << "str1_FromStringLiteral == str2_FromStringLiteral?: " << (str1_FromStringLiteral == str2_FromStringLiteral) << std::endl; - std::cout << "str1_FromStringLiteral == dbl10086?: " << (str1_FromStringLiteral == dbl10086) << std::endl; - - LLBC_Variant seq1; - LLBC_Variant seq2; - LLBC_Variant seq2Clone; - seq1.SeqPushBack(LLBC_Variant::nil); - seq2.SeqPushBack(1); - seq2Clone = seq2; - std::cout << "nil < seq1?: " << (nil < seq1) << ", nil == seq1?: " << (nil == seq1) << std::endl; - std::cout << "boolTrue < seq1?: " << (boolTrue < seq1) << ", boolTrue == seq1?: " << (boolTrue == seq1) << std::endl; - std::cout << "int10086 < seq1?: " << (int10086 < seq1) << ", int10086 == seq1?: " << (int10086 == seq1) << std::endl; - std::cout << "dbl10086 < seq1?: " << (dbl10086 < seq1) << ", dbl10086 == seq1?: " << (dbl10086 == seq1) << std::endl; - std::cout << "seq1 < seq1?: " << (seq1 < seq1) << ", seq1 == seq1?: " << (seq1 == seq1) << std::endl; - std::cout << "seq1 < seq2?: " << (seq1 < seq2) << ", seq1 == seq2?: " << (seq1 == seq2) << std::endl; - std::cout << "seq2 < seq2Clone?: " << (seq2 < seq2Clone) << ", seq2 == seq2Clone?: " << (seq2 == seq2Clone) << std::endl; - - LLBC_Variant dict1; - LLBC_Variant dict2; - dict1["Key1"] = "Value1"; - dict2["Key1"] = "Value2"; - std::cout << "nil < ?: dict1?: " << (nil < dict1) << ", nil == dict1?: " << (nil == dict1) << std::endl; - std::cout << "boolTrue < dict1?: " << (boolTrue < dict1) << ", boolTrue == dict1?: " << (boolTrue == dict1) << std::endl; - std::cout << "int10086 < dict1?: " << (int10086 < dict1) << ", int10086 == dict1?: " << (int10086 == dict1) << std::endl; - std::cout << "dbl10086 < dict1?: " << (dbl10086 < dict1) << ", dbl10086 == dict1?: " << (dbl10086 == dict1) << std::endl; - std::cout << "seq1 < dict1?: " << (seq1 < dict1) << ", seq1 == dict1?: " << (seq1 == dict1) << std::endl; - std::cout << "dict1 < dict1?: " << (dict1 < dict1) << ", dict1 == dict1?: " << (dict1 == dict1) << std::endl; - std::cout << "dict1 < dict2?: " << (dict1 < dict2) << ", dict1 == dict2?: " << (dict1 == dict2) << std::endl; -} - -void TestCase_Core_VariantTest::CompositeKeyTest() -{ - std::cout <<"Composite key test:" <, std::map > pa; - for (int i = 0; i < 10; ++i) - { - pa.first.push_back(i); - pa.second.emplace(i, i * 100); - } - LLBC_Variant var2; - var2 = pa; - std::cout << "Copy from pair:" << var2 << std::endl; - - pa.first.clear(); - pa.second.clear(); - pa = var2; - std::cout << "Recover from Variant, pair.first:" << LLBC_Variant(pa.first) - << ", pair.second:" << LLBC_Variant(pa.second) << std::endl; -} - -void TestCase_Core_VariantTest::SeqTest() -{ - std::cout << "Sequence test" << std::endl; - - // Construct default sequence. - LLBC_Variant subSeq; - subSeq["Hello"] = 3; - subSeq[LLBC_Variant::nil] = "The nil key value"; - - LLBC_Variant seq1; - seq1.SeqPushBack(3); - seq1.SeqPushBack(false); - seq1.SeqPushBack("Hello World"); - seq1.SeqPushBack(subSeq); - std::cout << "seq1 data:" << seq1 << std::endl; - - // Assignment test. - LLBC_Variant seq2 = seq1; - std::cout << "seq2 = seq1, seq2: " << seq2 << std::endl; - - // Arithmetic test. - LLBC_Variant left; - left.SeqPushBack(1); - left.SeqPushBack(2); - left.SeqPushBack(2); - left.SeqPushBack(3); - - LLBC_Variant right; - right.SeqPushBack(2); - right.SeqPushBack(3); - right.SeqPushBack(3); - right.SeqPushBack(4); - std::cout << "Perform sequence arithmetic test, left:" << left << ", right:" << right << std::endl; - std::cout << "left + right: " << left + right << std::endl; - std::cout << "left - right: " << left - right << std::endl; - std::cout << "left * right: " << left * right << std::endl; - std::cout << "left / right: " << left / right << std::endl; - - // Sequence + - * / non sequence test. - LLBC_Variant seq10; - seq10.SeqPushBack(10); - seq10.SeqPushBack("Hello world"); - seq10.SeqPushBack(false); - std::cout << "Perform sequence +-*/ non seuqnce test:" << std::endl; - std::cout << "seq10: " << seq10 << std::endl; - std::cout << "seq10 + 10086: " << seq10 + 10086 << std::endl; - std::cout << "seq10 + nil: " << seq10 + LLBC_Variant::nil << std::endl; - std::cout << "seq10 + hello world: " << seq10 + "hello world" << std::endl; - std::cout << "seq10 - false: " << seq10 - false << std::endl; - - // Sequence batch push back test. - std::cout <<"Sequence batch push back test:" <> deserRaw; - std::cout << "Deserialized from stream: [" << deserRaw << "]" << std::endl; - - // Serialize & Deserialize string type. - LLBC_Variant str("Hello World!"); - stream.SetReadPos(0); - stream.SetWritePos(0); - stream << str; - std::cout <<"String obj[" <> deserStr; - std::cout << "Deserialized from stream: [" << deserStr << "]" << std::endl; - - // Serialize & Deserialize seq type. - LLBC_Variant seq; - seq.SeqPushBack(1); - seq.SeqPushBack(false); - seq.SeqPushBack("Hello world"); - stream.SetReadPos(0); - stream.SetWritePos(0); - stream << seq; - std::cout << "Seq obj[" << seq << "] serialized size: " << stream.GetWritePos() << std::endl; - - LLBC_Variant deserSeq; - stream >> deserSeq; - std::cout << "Deserialized from stream:[" << deserSeq << "]" << std::endl; - - // Serialize & Deserialize dict type. - LLBC_Variant dict; - dict[1] = "Hello World!"; - dict[2] = "Hey Judy!"; - stream.SetReadPos(0); - stream.SetWritePos(0); - stream << dict; - std::cout << "Dict obj[" << dict << "] serialized size: " << stream.GetWritePos() << std::endl; - - LLBC_Variant deserDict; - stream >> deserDict; - std::cout << "Deserialized from stream: [" << deserDict << "]" << std::endl; -} - -void TestCase_Core_VariantTest::HashTest() -{ - std::cout <<"Hash test" <()(v) < us = v; - std::cout <<"Variant: " < +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +#include "core/variant/TestCase_Core_Variant.h" +#include "TestCase_Core_Variant.h" + +int TestCase_Core_Variant::Run(int argc, char *argv[]) +{ + std::cout << "LLBC_Variant test:" << std::endl; + + BasicTest(); + std::cout << std::endl; + + StringTest(); + std::cout << std::endl; + + EnumTest(); + std::cout << std::endl; + + CompareTest(); + std::cout << std::endl; + + CompositeKeyTest(); + std::cout << std::endl; + + ArithmeticTest(); + std::cout << std::endl; + + PairTest(); + std::cout << std::endl; + + SeqTest(); + std::cout << std::endl; + + DictTest(); + std::cout << std::endl; + + SerializeTest(); + std::cout << std::endl; + + HashTest(); + std::cout << std::endl; + + ConvertToUnurderedStlContainerTest(); + + std::cout <<"Press any key to continue ... ..." <() << std::endl; + + std::cout << "Implicit [const ]char * cast test:" << std::endl; + strVal = "hello world"; + std::cout << "- After strVal = \"hello world\":" << strVal << std::endl; + std::cout << "- strVal ? strVal : \"hello\": " << (strVal ? strVal : "hello") << std::endl; + + char mutableHeyStr[4] = { 'H', 'e' , 'y', '\0' }; + strVal = mutableHeyStr; + std::cout << " - After strVal = mutableHeyStr(Hey):" << strVal << std::endl; + std::cout << "- strVal ? strVal : mutableHeyStr(Hey): " << (strVal ? strVal : mutableHeyStr) << std::endl; + + LLBC_Variant constructFromCStr("hello world"); + LLBC_Variant constructFromMStr(mutableHeyStr); + std::cout << "Construct from mutable/immutable test:" << std::endl; + std::cout << "- construct from cstr:\"hello world\":" << constructFromCStr < return: " << tse2.AsEnum() << std::endl; + + TraditionalStyleEnum tse3 = tse1; + std::cout << " - operator return: " << tse3 << std::endl; + + LLBC_Variant tse4; + tse4 = TraditionalStyleEnum::C; + std::cout << " - operator=(Enum): " << tse4 << std::endl; + + std::cout << " - static_cast(nil): " << static_cast(LLBC_Variant()) << std::endl; + } + + { + std::cout << "- Class style enum:" << std::endl; + + LLBC_Variant cse1(ClassStyleEnum::X); + std::cout << " - default construct: " << cse1<< std::endl; + + LLBC_Variant cse2(-2); + std::cout << " - AsEnum<> return: " + << static_cast(cse2.AsEnum()) << std::endl; + + ClassStyleEnum cse3 = cse1; + std::cout << " - operator return: " + << static_cast(cse3) << std::endl; + + LLBC_Variant cse4; + cse4 = ClassStyleEnum::Z; + std::cout << " - operator(Enum): " << cse4 << std::endl; + + std::cout << " - static_cast(nil): " + << static_cast(static_cast(LLBC_Variant())) << std::endl; + } +} + +void TestCase_Core_Variant::CompareTest() +{ + std::cout << "Compare test: " << std::endl; + + LLBC_Variant nil; + LLBC_Variant boolTrue(true); + LLBC_Variant boolFalse(false); + std::cout << "nil < nil?: " << (nil < nil) << ", nil == nil?: " << (nil == nil) << std::endl; + std::cout << "nil < boolTrue?: " << (nil < boolTrue) << ", nil == boolTrue?: " << (nil == boolTrue) << std::endl; + std::cout << "nil < boolFalse?: " << (nil < boolFalse) << ", nil == boolFalse?: " << (nil == boolFalse) << std::endl; + std::cout << "boolTrue < boolTrue?: " << (boolTrue < boolTrue) << ", boolTrue == boolTrue?: " << (boolTrue == boolTrue) << std::endl; + std::cout << "boolFalse < boolFalse?: " << (boolFalse < boolFalse) << ", boolFalse == boolFalse?: " << (boolFalse == boolFalse) << std::endl; + std::cout << "boolTrue < boolFalse?: " << (boolTrue < boolFalse) << ", boolTrue == boolFalse?: " << (boolTrue == boolFalse) << std::endl; + std::cout << "boolFalse < boolTrue?: " << (boolFalse < boolTrue) << ", boolFalse == boolTrue?: " << (boolFalse == boolTrue) << std::endl; + + LLBC_Variant int0(0); + LLBC_Variant int1(1); + LLBC_Variant int10010(10010); + LLBC_Variant int10086(10086); + std::cout << "nil < int10010?: " << (nil < int10010) << ", nil == int10010?: " << (nil == int10010) << std::endl; + std::cout << "int0 < boolFalse?: " << (int0 < boolFalse) << ", int0 == boolFalse?: " << (int0 == boolFalse) << std::endl; + std::cout << "int1 < boolTrue?: " << (int1 < boolTrue) << ", int1 == boolTrue?: " << (int1 == boolTrue) << std::endl; + std::cout << "int10010 < int10086?: " << (int10010 < int10086) << ", int10010 == int10086?: " << (int10010 == int10086) << std::endl; + std::cout << "int10086 < int10010?: " << (int10086 < int10010) << ", int10086 == int10010?: " << (int10086 == int10010) << std::endl; + std::cout << "int10086 < int10086?: " << (int10086 < int10086) << ", int10086 == int10086?: " << (int10086 == int10086) << std::endl; + + LLBC_Variant dbl10010(10010.0); + LLBC_Variant dbl10086(10086.0); + std::cout << "nil < dbl10010?: " << (nil < dbl10010) << ", nil == dbl10010?:" << (nil == dbl10010) << std::endl; + std::cout << "int0 < dbl10010?: " << (int0 < dbl10010) << ", int0 == dbl10010?: " << (int0 == dbl10010) << std::endl; + std::cout << "int10010 < dbl10010?: " << (int10010 < dbl10010) << ", int10010 == dbl10010?: " << (int10010 == dbl10010) << std::endl; + std::cout << "dbl10010 < int10010?: " << (dbl10010 < int10010) << ", dbl10010 == int10010?: " << (dbl10010 == int10010) << std::endl; + std::cout << "dbl10010 < dbl10086?: " << (dbl10010 < dbl10086) << ", dbl10010 == dbl10086?: " << (dbl10010 == dbl10086) << std::endl; + std::cout << "dbl10086 < dbl10086?: " << (dbl10086 < dbl10086) << ", dbl10086 == dbl10086?: " << (dbl10086 == dbl10086) << std::endl; + + LLBC_Variant str1_FromStringLiteral("Hello world(string literal)"); + LLBC_Variant str1_FromStlString(std::string("Hello world(stl string)")); + LLBC_Variant str1_FromLLBCString(LLBC_String("Hello world(llbc string)")); + LLBC_Variant str2_FromStringLiteral("Hello world(string literal)"); + std::cout << "str1_FromStringLiteral: " << str1_FromStringLiteral << std::endl; + std::cout << "str2_FromStringLiteral: " << str2_FromStringLiteral << std::endl; + std::cout << "str1_FromStlString: " << str1_FromStlString << std::endl; + std::cout << "str1_FromLLBCString: " << str1_FromLLBCString << std::endl; + std::cout << "str1_FromStringLiteral == str1_FromStlString?: " << (str1_FromStringLiteral == str1_FromStlString) << std::endl; + std::cout << "str1_FromStringLiteral == str2_FromStringLiteral?: " << (str1_FromStringLiteral == str2_FromStringLiteral) << std::endl; + std::cout << "str1_FromStringLiteral == dbl10086?: " << (str1_FromStringLiteral == dbl10086) << std::endl; + + LLBC_Variant seq1; + LLBC_Variant seq2; + LLBC_Variant seq2Clone; + seq1.SeqPushBack(LLBC_Variant::nil); + seq2.SeqPushBack(1); + seq2Clone = seq2; + std::cout << "nil < seq1?: " << (nil < seq1) << ", nil == seq1?: " << (nil == seq1) << std::endl; + std::cout << "boolTrue < seq1?: " << (boolTrue < seq1) << ", boolTrue == seq1?: " << (boolTrue == seq1) << std::endl; + std::cout << "int10086 < seq1?: " << (int10086 < seq1) << ", int10086 == seq1?: " << (int10086 == seq1) << std::endl; + std::cout << "dbl10086 < seq1?: " << (dbl10086 < seq1) << ", dbl10086 == seq1?: " << (dbl10086 == seq1) << std::endl; + std::cout << "seq1 < seq1?: " << (seq1 < seq1) << ", seq1 == seq1?: " << (seq1 == seq1) << std::endl; + std::cout << "seq1 < seq2?: " << (seq1 < seq2) << ", seq1 == seq2?: " << (seq1 == seq2) << std::endl; + std::cout << "seq2 < seq2Clone?: " << (seq2 < seq2Clone) << ", seq2 == seq2Clone?: " << (seq2 == seq2Clone) << std::endl; + + LLBC_Variant dict1; + LLBC_Variant dict2; + dict1["Key1"] = "Value1"; + dict2["Key1"] = "Value2"; + std::cout << "nil < ?: dict1?: " << (nil < dict1) << ", nil == dict1?: " << (nil == dict1) << std::endl; + std::cout << "boolTrue < dict1?: " << (boolTrue < dict1) << ", boolTrue == dict1?: " << (boolTrue == dict1) << std::endl; + std::cout << "int10086 < dict1?: " << (int10086 < dict1) << ", int10086 == dict1?: " << (int10086 == dict1) << std::endl; + std::cout << "dbl10086 < dict1?: " << (dbl10086 < dict1) << ", dbl10086 == dict1?: " << (dbl10086 == dict1) << std::endl; + std::cout << "seq1 < dict1?: " << (seq1 < dict1) << ", seq1 == dict1?: " << (seq1 == dict1) << std::endl; + std::cout << "dict1 < dict1?: " << (dict1 < dict1) << ", dict1 == dict1?: " << (dict1 == dict1) << std::endl; + std::cout << "dict1 < dict2?: " << (dict1 < dict2) << ", dict1 == dict2?: " << (dict1 == dict2) << std::endl; +} + +void TestCase_Core_Variant::CompositeKeyTest() +{ + std::cout <<"Composite key test:" <, std::map > pa; + for (int i = 0; i < 10; ++i) + { + pa.first.push_back(i); + pa.second.emplace(i, i * 100); + } + LLBC_Variant var2; + var2 = pa; + std::cout << "Copy from pair:" << var2 << std::endl; + + pa.first.clear(); + pa.second.clear(); + pa = var2; + std::cout << "Recover from Variant, pair.first:" << LLBC_Variant(pa.first) + << ", pair.second:" << LLBC_Variant(pa.second) << std::endl; +} + +void TestCase_Core_Variant::SeqTest() +{ + std::cout << "Sequence test" << std::endl; + + // Construct default sequence. + LLBC_Variant subSeq; + subSeq["Hello"] = 3; + subSeq[LLBC_Variant::nil] = "The nil key value"; + + LLBC_Variant seq1; + seq1.SeqPushBack(3); + seq1.SeqPushBack(false); + seq1.SeqPushBack("Hello World"); + seq1.SeqPushBack(subSeq); + std::cout << "seq1 data:" << seq1 << std::endl; + + // Assignment test. + LLBC_Variant seq2 = seq1; + std::cout << "seq2 = seq1, seq2: " << seq2 << std::endl; + + // Arithmetic test. + LLBC_Variant left; + left.SeqPushBack(1); + left.SeqPushBack(2); + left.SeqPushBack(2); + left.SeqPushBack(3); + + LLBC_Variant right; + right.SeqPushBack(2); + right.SeqPushBack(3); + right.SeqPushBack(3); + right.SeqPushBack(4); + std::cout << "Perform sequence arithmetic test, left:" << left << ", right:" << right << std::endl; + std::cout << "left + right: " << left + right << std::endl; + std::cout << "left - right: " << left - right << std::endl; + std::cout << "left * right: " << left * right << std::endl; + std::cout << "left / right: " << left / right << std::endl; + + // Sequence + - * / non sequence test. + LLBC_Variant seq10; + seq10.SeqPushBack(10); + seq10.SeqPushBack("Hello world"); + seq10.SeqPushBack(false); + std::cout << "Perform sequence +-*/ non seuqnce test:" << std::endl; + std::cout << "seq10: " << seq10 << std::endl; + std::cout << "seq10 + 10086: " << seq10 + 10086 << std::endl; + std::cout << "seq10 + nil: " << seq10 + LLBC_Variant::nil << std::endl; + std::cout << "seq10 + hello world: " << seq10 + "hello world" << std::endl; + std::cout << "seq10 - false: " << seq10 - false << std::endl; + + // Sequence batch push back test. + std::cout <<"Sequence batch push back test:" <> deserRaw; + std::cout << "Deserialized from stream: [" << deserRaw << "]" << std::endl; + + // Serialize & Deserialize string type. + LLBC_Variant str("Hello World!"); + stream.SetReadPos(0); + stream.SetWritePos(0); + stream << str; + std::cout <<"String obj[" <> deserStr; + std::cout << "Deserialized from stream: [" << deserStr << "]" << std::endl; + + // Serialize & Deserialize seq type. + LLBC_Variant seq; + seq.SeqPushBack(1); + seq.SeqPushBack(false); + seq.SeqPushBack("Hello world"); + stream.SetReadPos(0); + stream.SetWritePos(0); + stream << seq; + std::cout << "Seq obj[" << seq << "] serialized size: " << stream.GetWritePos() << std::endl; + + LLBC_Variant deserSeq; + stream >> deserSeq; + std::cout << "Deserialized from stream:[" << deserSeq << "]" << std::endl; + + // Serialize & Deserialize dict type. + LLBC_Variant dict; + dict[1] = "Hello World!"; + dict[2] = "Hey Judy!"; + stream.SetReadPos(0); + stream.SetWritePos(0); + stream << dict; + std::cout << "Dict obj[" << dict << "] serialized size: " << stream.GetWritePos() << std::endl; + + LLBC_Variant deserDict; + stream >> deserDict; + std::cout << "Deserialized from stream: [" << deserDict << "]" << std::endl; +} + +void TestCase_Core_Variant::HashTest() +{ + std::cout <<"Hash test" <()(v) < us = v; + std::cout <<"Variant: " <