From e59cf7b09e7388d369e8d2bf73501cde79c28708 Mon Sep 17 00:00:00 2001 From: Toni Uhlig Date: Thu, 8 Apr 2021 16:43:58 +0200 Subject: Squashed 'EASTL/' content from commit fad5471 git-subtree-dir: EASTL git-subtree-split: fad54717f8e4ebb13b20095da7efd07a53af0f10 --- test/source/TestString.cpp | 142 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 test/source/TestString.cpp (limited to 'test/source/TestString.cpp') diff --git a/test/source/TestString.cpp b/test/source/TestString.cpp new file mode 100644 index 0000000..1bd06e7 --- /dev/null +++ b/test/source/TestString.cpp @@ -0,0 +1,142 @@ +///////////////////////////////////////////////////////////////////////////// +// Copyright (c) Electronic Arts Inc. All rights reserved. +///////////////////////////////////////////////////////////////////////////// + +#include "EASTLTest.h" +#include +#include +#include +#include +#include +#include + +using namespace eastl; + +// Verify char8_t support is present if the test build requested it. +#if defined(EASTL_EXPECT_CHAR8T_SUPPORT) && !EA_CHAR8_UNIQUE +static_assert(false, "Building with char8_t tests enabled, but EA_CHAR8_UNIQUE evaluates to false."); +#endif + + +// inject string literal string conversion macros into the unit tests +#define TEST_STRING_NAME TestBasicString +#define LITERAL(x) x +#include "TestString.inl" + +#define TEST_STRING_NAME TestBasicStringW +#define LITERAL(x) EA_WCHAR(x) +#include "TestString.inl" + +#define TEST_STRING_NAME TestBasicString8 +#define LITERAL(x) EA_CHAR8(x) +#include "TestString.inl" + +#define TEST_STRING_NAME TestBasicString16 +#define LITERAL(x) EA_CHAR16(x) +#include "TestString.inl" + +#define TEST_STRING_NAME TestBasicString32 +#define LITERAL(x) EA_CHAR32(x) +#include "TestString.inl" + +int TestString() +{ + int nErrorCount = 0; + + nErrorCount += TestBasicString>(); + nErrorCount += TestBasicString(); + + nErrorCount += TestBasicStringW>(); + nErrorCount += TestBasicStringW(); + +#if defined(EA_CHAR8_UNIQUE) && EA_CHAR8_UNIQUE + nErrorCount += TestBasicString8>(); + nErrorCount += TestBasicString8(); +#endif + + nErrorCount += TestBasicString16>(); + nErrorCount += TestBasicString16(); + +#if defined(EA_CHAR32_NATIVE) && EA_CHAR32_NATIVE + nErrorCount += TestBasicString32>(); + nErrorCount += TestBasicString32(); +#endif + + // Check for memory leaks by using the 'CountingAllocator' to ensure no active allocation after tests have completed. + CountingAllocator::resetCount(); + nErrorCount += TestBasicString>(); + VERIFY(CountingAllocator::getActiveAllocationCount() == 0); + + nErrorCount += TestBasicStringW>(); + VERIFY(CountingAllocator::getActiveAllocationCount() == 0); + +#if defined(EA_CHAR8_UNIQUE) && EA_CHAR8_UNIQUE + nErrorCount += TestBasicString8>(); + VERIFY(CountingAllocator::getActiveAllocationCount() == 0); +#endif + + nErrorCount += TestBasicString16>(); + VERIFY(CountingAllocator::getActiveAllocationCount() == 0); + +#if defined(EA_CHAR32_NATIVE) && EA_CHAR32_NATIVE + nErrorCount += TestBasicString32>(); + VERIFY(CountingAllocator::getActiveAllocationCount() == 0); +#endif + + // to_string + { + VERIFY(eastl::to_string(42) == "42"); + VERIFY(eastl::to_string(42l) == "42"); + VERIFY(eastl::to_string(42ll) == "42"); + VERIFY(eastl::to_string(42u) == "42"); + VERIFY(eastl::to_string(42ul) == "42"); + VERIFY(eastl::to_string(42ull) == "42"); + VERIFY(eastl::to_string(42.f) == "42.000000"); + VERIFY(eastl::to_string(42.0) == "42.000000"); + #if !defined(EA_COMPILER_GNUC) && !defined(EA_PLATFORM_MINGW) + // todo: long double sprintf functionality is unrealiable on unix-gcc, requires further debugging. + VERIFY(eastl::to_string(42.0l) == "42.000000"); + #endif + } + + // to_wstring + { + VERIFY(eastl::to_wstring(42) == L"42"); + VERIFY(eastl::to_wstring(42l) == L"42"); + VERIFY(eastl::to_wstring(42ll) == L"42"); + VERIFY(eastl::to_wstring(42u) == L"42"); + VERIFY(eastl::to_wstring(42ul) == L"42"); + VERIFY(eastl::to_wstring(42ull) == L"42"); + VERIFY(eastl::to_wstring(42.f) == L"42.000000"); + VERIFY(eastl::to_wstring(42.0) == L"42.000000"); + #if !defined(EA_COMPILER_GNUC) && !defined(EA_PLATFORM_MINGW) + // todo: long double sprintf functionality is unrealiable on unix-gcc, requires further debugging. + VERIFY(eastl::to_wstring(42.0l) == L"42.000000"); + #endif + } + + #if EASTL_USER_LITERALS_ENABLED + { + VERIFY("cplusplus"s == "cplusplus"); + VERIFY(L"cplusplus"s == L"cplusplus"); + VERIFY(u"cplusplus"s == u"cplusplus"); + VERIFY(U"cplusplus"s == U"cplusplus"); + VERIFY(u8"cplusplus"s == u8"cplusplus"); + } + #endif + + + { + // CustomAllocator has no data members which reduces the size of an eastl::basic_string via the empty base class optimization. + using EboString = eastl::basic_string; + + // this must match the eastl::basic_string heap memory layout struct which is a pointer and 2 eastl_size_t. + const int expectedSize = sizeof(EboString::pointer) + (2 * sizeof(EboString::size_type)); + + static_assert(sizeof(EboString) == expectedSize, "unexpected layout size of basic_string"); + } + + return nErrorCount; +} + + -- cgit v1.2.3