10 #include <EABase/eabase.h>
11 #include <EATest/EATest.h>
13 EA_DISABLE_ALL_VC_WARNINGS()
18 #if EASTL_EXCEPTIONS_ENABLED
22 EA_RESTORE_ALL_VC_WARNINGS();
33 int TestCppCXTypeTraits();
37 int TestFixedFunction();
43 int TestFixedString();
44 int TestFixedTupleVector();
45 int TestFixedVector();
49 int TestIntrusiveHash();
50 int TestIntrusiveList();
51 int TestIntrusiveSDList();
52 int TestIntrusiveSList();
60 int TestNumericLimits();
66 int TestSegmentedVector();
72 int TestStringHashMap();
76 int TestTupleVector();
83 int TestAtomicBasic();
90 #pragma warning(disable: 4324)
95 #pragma warning(default: 4056)
96 #pragma warning(default: 4061)
97 #pragma warning(default: 4062)
98 #pragma warning(default: 4191)
99 #pragma warning(default: 4217)
101 #pragma warning(default: 4254)
102 #pragma warning(default: 4255)
103 #pragma warning(default: 4263)
104 #pragma warning(default: 4264)
105 #pragma warning(default: 4287)
106 #pragma warning(default: 4289)
107 #pragma warning(default: 4296)
108 #pragma warning(default: 4302)
109 #pragma warning(default: 4339)
110 #pragma warning(default: 4347)
112 #pragma warning(default: 4529)
113 #pragma warning(default: 4545)
114 #pragma warning(default: 4546)
115 #pragma warning(default: 4547)
117 #pragma warning(default: 4549)
118 #pragma warning(default: 4536)
119 #pragma warning(default: 4555)
120 #pragma warning(default: 4557)
122 #pragma warning(default: 4623)
125 #pragma warning(default: 4628)
126 #pragma warning(default: 4640)
127 #pragma warning(default: 4668)
128 #pragma warning(default: 4682)
129 #pragma warning(default: 4686)
132 #pragma warning(default: 4793)
134 #pragma warning(default: 4905)
135 #pragma warning(default: 4906)
136 #pragma warning(default: 4917)
137 #pragma warning(default: 4928)
138 #pragma warning(default: 4931)
139 #pragma warning(default: 4946)
149 #include <EASTL/iterator.h>
150 #include <EASTL/algorithm.h>
164 kEASTL_TestLevelLow = 1,
165 kEASTL_TestLevelHigh = 10
168 extern int gEASTL_TestLevel;
180 int EASTLTest_CheckMemory_Imp(
const char* pFile,
int nLine);
181 #define EASTLTest_CheckMemory() EASTLTest_CheckMemory_Imp(__FILE__, __LINE__)
187 #if defined(_STLPORT_VERSION)
188 #define EASTLTEST_STD_STL_VER_STLPORT
189 #elif defined(_RWSTD_VER_STR) || defined(_RWSTD_NAMESPACE_END)
190 #define EASTLTEST_STD_STL_VER_APACHE
191 #elif defined(_CPPLIB_VER)
192 #define EASTLTEST_STD_STL_VER_DINKUMWARE
193 #elif defined(__GNUC__) && defined(_CXXCONFIG)
194 #define EASTLTEST_STD_STL_VER_GCC
196 #define EASTLTEST_STD_STL_VER_UNKNOWN
214 StdSTLType GetStdSTLType();
228 const char* GetStdSTLName();
233 extern int gEASTLTest_AllocationCount;
234 extern int gEASTLTest_TotalAllocationCount;
239 #define EASTLTest_Printf EA::UnitTest::Report
240 #define VERIFY EATEST_VERIFY
271 mnSeed = UINT64_C(0xfefefefefefefefe);
273 const uint64_t nResult64A = ((mnSeed * UINT64_C(6364136223846793005)) + UINT64_C(1442695040888963407));
274 const uint64_t nResult64B = ((nResult64A * UINT64_C(6364136223846793005)) + UINT64_C(1442695040888963407));
276 mnSeed = (nResult64A >> 32) ^ nResult64B;
278 return (eastl_size_t)mnSeed;
281 eastl_size_t operator()()
284 eastl_size_t operator()(eastl_size_t n)
285 {
return RandLimit(n); }
287 eastl_size_t RandLimit(eastl_size_t nLimit)
293 return (
Rand() % nLimit);
296 eastl_ssize_t RandRange(eastl_ssize_t nBegin, eastl_ssize_t nEnd)
297 {
return nBegin + (eastl_ssize_t)RandLimit((eastl_size_t)(nEnd - nBegin)); }
311 template <
typename Integer>
318 {
return (Integer)mRand.Rand(); }
320 Integer operator()(eastl_size_t n)
321 {
return (Integer)mRand.RandLimit(n); }
336 const uint32_t kMagicValue = 0x01f1cbe8;
352 uint32_t mMagicValue;
353 static int64_t sTOCount;
354 static int64_t sTOCtorCount;
355 static int64_t sTODtorCount;
356 static int64_t sTODefaultCtorCount;
357 static int64_t sTOArgCtorCount;
358 static int64_t sTOCopyCtorCount;
359 static int64_t sTOMoveCtorCount;
360 static int64_t sTOCopyAssignCount;
361 static int64_t sTOMoveAssignCount;
362 static int sMagicErrorCount;
364 explicit TestObject(
int x = 0,
bool bThrowOnCopy =
false)
365 : mX(x), mbThrowOnCopy(bThrowOnCopy), mMagicValue(kMagicValue)
369 ++sTODefaultCtorCount;
374 TestObject(
int x0,
int x1,
int x2,
bool bThrowOnCopy =
false)
375 : mX(x0 + x1 + x2), mbThrowOnCopy(bThrowOnCopy), mMagicValue(kMagicValue)
384 : mX(testObject.mX), mbThrowOnCopy(testObject.mbThrowOnCopy), mMagicValue(testObject.mMagicValue)
392 #if EASTL_EXCEPTIONS_ENABLED
393 throw "Disallowed TestObject copy";
402 : mX(testObject.mX), mbThrowOnCopy(testObject.mbThrowOnCopy), mMagicValue(testObject.mMagicValue)
411 #if EASTL_EXCEPTIONS_ENABLED
412 throw "Disallowed TestObject copy";
419 ++sTOCopyAssignCount;
421 if(&testObject !=
this)
425 mMagicValue = testObject.mMagicValue;
426 mbThrowOnCopy = testObject.mbThrowOnCopy;
429 #if EASTL_EXCEPTIONS_ENABLED
430 throw "Disallowed TestObject copy";
439 ++sTOMoveAssignCount;
441 if(&testObject !=
this)
443 eastl::swap(mX, testObject.mX);
445 eastl::swap(mMagicValue, testObject.mMagicValue);
446 eastl::swap(mbThrowOnCopy, testObject.mbThrowOnCopy);
450 #if EASTL_EXCEPTIONS_ENABLED
451 throw "Disallowed TestObject copy";
460 if(mMagicValue != kMagicValue)
472 sTODefaultCtorCount = 0;
474 sTOCopyCtorCount = 0;
475 sTOMoveCtorCount = 0;
476 sTOCopyAssignCount = 0;
477 sTOMoveAssignCount = 0;
478 sMagicErrorCount = 0;
481 static bool IsClear()
483 return (sTOCount == 0) && (sTODtorCount == sTOCtorCount) && (sMagicErrorCount == 0);
492 {
return t1.mX == t2.mX; }
495 {
return t1.mX < t2.mX; }
507 {
return static_cast<size_t>(a.mX); }
518 template <
typename T>
struct use_mX {
int operator()(
const T& t)
const {
return t.mX; } };
527 template <
size_t kSize>
571 #if defined(EA_PROCESSOR_ARM)
572 #define kEASTLTestAlign16 8
574 #define kEASTLTestAlign16 16
578 EA_PREFIX_ALIGN(kEASTLTestAlign16)
581 explicit Align16(
int x = 0) : mX(x) {}
583 } EA_POSTFIX_ALIGN(kEASTLTestAlign16);
586 {
return (a.mX == b.mX); }
589 {
return (a.mX < b.mX); }
596 #if defined(EA_PROCESSOR_ARM)
597 #define kEASTLTestAlign32 8
598 #elif defined(__GNUC__) && (((__GNUC__ * 100) + __GNUC_MINOR__) < 400)
599 #define kEASTLTestAlign32 16
601 #define kEASTLTestAlign32 32
604 EA_PREFIX_ALIGN(kEASTLTestAlign32)
607 explicit Align32(
int x = 0) : mX(x) {}
609 } EA_POSTFIX_ALIGN(kEASTLTestAlign32);
612 {
return (a.mX == b.mX); }
615 {
return (a.mX < b.mX); }
624 #if defined(EA_PROCESSOR_ARM)
625 #define kEASTLTestAlign64 8
626 #elif defined(__GNUC__) && (((__GNUC__ * 100) + __GNUC_MINOR__) < 400)
627 #define kEASTLTestAlign64 16
629 #define kEASTLTestAlign64 64
632 EA_PREFIX_ALIGN(kEASTLTestAlign64)
635 explicit Align64(
int x = 0) : mX(x) {}
637 } EA_POSTFIX_ALIGN(kEASTLTestAlign64);
640 {
return (a.mX == b.mX); }
643 {
return (a.mX < b.mX); }
650 size_t operator()(
const Align64& a)
const
652 return static_cast<size_t>(a.mX);
665 template <
typename T>
668 const T& operator()(
const T& x)
const
690 template <
typename T>
698 void reset(
int x = 0)
716 template <
typename T>
724 void reset(
int x = 0)
727 void operator()(T& t)
742 template <
typename T1,
typename T2,
typename ExtractValue1,
typename ExtractValue2>
743 int CompareContainers(
const T1& t1,
const T2& t2,
const char* ppName,
749 VERIFY(t1.empty() == t2.empty());
752 const size_t nSize1 = t1.size();
753 const size_t nSize2 = t2.size();
755 VERIFY(nSize1 == nSize2);
757 EASTLTest_Printf(
"%s: Container size difference: %u, %u\n", ppName, (
unsigned)nSize1, (
unsigned)nSize2);
763 typename T1::const_iterator it1 = t1.begin();
764 typename T2::const_iterator it2 = t2.begin();
766 for(
unsigned j = 0; it1 != t1.end(); ++it1, ++it2, ++j)
768 const typename T1::value_type& v1 = *it1;
769 const typename T2::value_type& v2 = *it2;
771 VERIFY(ev1(v1) == ev2(v2));
772 if(!(ev1(v1) == ev2(v2)))
774 EASTLTest_Printf(
"%s: Container iterator difference at index %d\n", ppName, j);
779 VERIFY(it1 == t1.end());
780 VERIFY(it2 == t2.end());
802 template <
typename InputIterator,
typename StackValue>
803 bool VerifySequence(InputIterator first, InputIterator last, StackValue ,
const char* pName, ...)
805 typedef typename eastl::iterator_traits<InputIterator>::value_type value_type;
809 bool bReturnValue =
true;
813 va_start(args, pName);
815 for( ; first != last; ++first, ++argIndex, ++seqIndex)
817 next = va_arg(args, StackValue);
819 if((next == StackValue(-1)) || !(value_type(next) == *first))
822 EASTLTest_Printf(
"[%s] Mismatch at index %d\n", pName, argIndex);
824 EASTLTest_Printf(
"Mismatch at index %d\n", argIndex);
825 bReturnValue =
false;
829 for(; first != last; ++first)
834 next = va_arg(args, StackValue);
836 if(!(next == StackValue(-1)))
840 next = va_arg(args, StackValue);
841 }
while(!(next == StackValue(-1)));
844 EASTLTest_Printf(
"[%s] Too many elements: expected %d, found %d\n", pName, argIndex, seqIndex);
846 EASTLTest_Printf(
"Too many elements: expected %d, found %d\n", argIndex, seqIndex);
847 bReturnValue =
false;
872 template <
typename InputIterator,
typename ExtractInt>
873 void PrintSequence(InputIterator first, InputIterator last, ExtractInt extractInt,
int nMaxCount,
const char* pName, ...)
876 EASTLTest_Printf(
"[%s]", pName);
878 for(
int i = 0; (i < nMaxCount) && (first != last); ++i, ++first)
880 EASTLTest_Printf(
"%d ", (
int)extractInt(*first));
883 EASTLTest_Printf(
"\n");
918 template <
typename Iterator,
typename IteratorCategory>
926 typedef Iterator iterator_type;
927 typedef IteratorCategory iterator_category;
928 typedef typename eastl::iterator_traits<Iterator>::value_type value_type;
929 typedef typename eastl::iterator_traits<Iterator>::difference_type difference_type;
930 typedef typename eastl::iterator_traits<Iterator>::reference reference;
931 typedef typename eastl::iterator_traits<Iterator>::pointer pointer;
940 : mIterator(x.mIterator) { }
943 { mIterator = i;
return *
this; }
946 { mIterator = x.mIterator;
return *
this; }
948 reference operator*()
const
949 {
return *mIterator; }
951 pointer operator->()
const
952 {
return mIterator; }
955 { ++mIterator;
return *
this; }
961 { --mIterator;
return *
this; }
966 reference operator[](
const difference_type& n)
const
967 {
return mIterator[n]; }
969 this_type& operator+=(
const difference_type& n)
970 { mIterator += n;
return *
this; }
972 this_type operator+(
const difference_type& n)
const
975 this_type& operator-=(
const difference_type& n)
976 { mIterator -= n;
return *
this; }
978 this_type operator-(
const difference_type& n)
const
981 const iterator_type& base()
const
982 {
return mIterator; }
986 template<
typename Iterator1,
typename IteratorCategory1,
typename Iterator2,
typename IteratorCategory2>
989 {
return a.base() == b.base(); }
991 template<
typename Iterator1,
typename IteratorCategory1,
typename Iterator2,
typename IteratorCategory2>
994 {
return !(a == b); }
996 template<
typename Iterator1,
typename IteratorCategory1,
typename Iterator2,
typename IteratorCategory2>
999 {
return a.base() < b.base(); }
1001 template<
typename Iterator1,
typename IteratorCategory1,
typename Iterator2,
typename IteratorCategory2>
1004 {
return !(b < a); }
1006 template<
typename Iterator1,
typename IteratorCategory1,
typename Iterator2,
typename IteratorCategory2>
1011 template<
typename Iterator1,
typename IteratorCategory1,
typename Iterator2,
typename IteratorCategory2>
1014 {
return !(a < b); }
1016 template<
typename Iterator1,
typename IteratorCategory1,
typename Iterator2,
typename IteratorCategory2>
1021 template<
typename Iterator1,
typename IteratorCategory1>
1031 template <
typename Iterator>
1033 to_input_iterator(
const Iterator& i)
1036 template <
typename Iterator>
1038 to_forward_iterator(
const Iterator& i)
1041 template <
typename Iterator>
1043 to_bidirectional_iterator(
const Iterator& i)
1046 template <
typename Iterator>
1048 to_random_access_iterator(
const Iterator& i)
1070 : mAllocCount(0), mFreeCount(0), mAllocVolume(0) {}
1073 : mAllocCount(x.mAllocCount), mFreeCount(x.mFreeCount), mAllocVolume(x.mAllocVolume) {}
1079 mAllocCount = x.mAllocCount;
1080 mFreeCount = x.mFreeCount;
1081 mAllocVolume = x.mAllocVolume;
1085 void* allocate(
size_t n,
int = 0);
1086 void* allocate(
size_t n,
size_t,
size_t,
int = 0);
1087 void deallocate(
void* p,
size_t n);
1089 const char* get_name()
const {
return "MallocAllocator"; }
1090 void set_name(
const char*) {}
1092 static void reset_all()
1096 mAllocVolumeAll = 0;
1097 mpLastAllocation = NULL;
1103 size_t mAllocVolume;
1105 static int mAllocCountAll;
1106 static int mFreeCountAll;
1107 static size_t mAllocVolumeAll;
1108 static void* mpLastAllocation;
1132 void* allocate(
size_t n,
int flags = 0);
1133 void* allocate(
size_t n,
size_t,
size_t,
int flags = 0);
1134 void deallocate(
void* p,
size_t n);
1136 const char* get_name()
const {
return "CustomAllocator"; }
1137 void set_name(
const char*) {}
1153 EASTL_ALLOCATOR_EXPLICIT
UnequalAllocator(
const char* pName = EASTL_NAME_VAL(EASTL_ALLOCATOR_DEFAULT_NAME))
1154 : mAllocator(pName) {}
1160 mAllocator = x.mAllocator;
1164 void* allocate(
size_t n,
int flags = 0) {
return mAllocator.allocate(n, flags); }
1165 void* allocate(
size_t n,
size_t alignment,
size_t offset,
int flags = 0) {
return mAllocator.allocate(n, alignment, offset, flags); }
1166 void deallocate(
void* p,
size_t n) {
return mAllocator.deallocate(p, n); }
1168 const char* get_name()
const {
return mAllocator.get_name(); }
1169 void set_name(
const char* pName) { mAllocator.set_name(pName); }
1189 EASTL_ALLOCATOR_EXPLICIT
CountingAllocator(
const char* pName = EASTL_NAME_VAL(EASTL_ALLOCATOR_DEFAULT_NAME))
1211 base_type::operator=(x);
1216 virtual void* allocate(
size_t n,
int flags = 0)
1220 totalAllocatedMemory += n;
1221 activeAllocatedMemory += n;
1222 return base_type::allocate(n, flags);
1225 virtual void* allocate(
size_t n,
size_t alignment,
size_t offset,
int flags = 0)
1229 totalAllocatedMemory += n;
1230 activeAllocatedMemory += n;
1231 return base_type::allocate(n, alignment, offset, flags);
1234 void deallocate(
void* p,
size_t n)
1237 totalDeallocCount--;
1238 activeAllocatedMemory -= n;
1239 return base_type::deallocate(p, n);
1242 const char* get_name()
const {
return base_type::get_name(); }
1243 void set_name(
const char* pName) { base_type::set_name(pName); }
1245 static auto getTotalAllocationCount() {
return totalAllocCount; }
1246 static auto getTotalAllocationSize() {
return totalAllocatedMemory; }
1247 static auto getActiveAllocationSize() {
return activeAllocatedMemory; }
1248 static auto getActiveAllocationCount() {
return activeAllocCount; }
1249 static auto neverUsed() {
return totalAllocCount == 0; }
1251 static void resetCount()
1253 activeAllocCount = 0;
1254 totalAllocCount = 0;
1255 totalDeallocCount = 0;
1257 defaultCtorCount = 0;
1260 totalAllocatedMemory = 0;
1261 activeAllocatedMemory = 0;
1264 static uint64_t activeAllocCount;
1265 static uint64_t totalAllocCount;
1266 static uint64_t totalDeallocCount;
1267 static uint64_t totalCtorCount;
1268 static uint64_t defaultCtorCount;
1269 static uint64_t copyCtorCount;
1270 static uint64_t assignOpCount;
1271 static uint64_t totalAllocatedMemory;
1272 static uint64_t activeAllocatedMemory;
1307 InstanceAllocator(
const char* = NULL, uint8_t instanceId = 0) : mInstanceId(instanceId) {}
1314 mInstanceId = x.mInstanceId;
1318 void* allocate(
size_t n,
int = 0)
1321 static_cast<uint8_t*
>(malloc(n + (kMultiplier * (mInstanceId + 1))));
1325 EA_ANALYSIS_ASSUME(p8 != NULL);
1327 return p8 + (kMultiplier * (mInstanceId + 1));
1330 void* allocate(
size_t n,
size_t,
size_t,
int = 0)
1333 static_cast<uint8_t*
>(malloc(n + (kMultiplier * (mInstanceId + 1))));
1337 EA_ANALYSIS_ASSUME(p8 != NULL);
1339 return p8 + (kMultiplier * (mInstanceId + 1));
1342 void deallocate(
void* p,
size_t )
1344 uint8_t* p8 =
static_cast<uint8_t*
>(p) - (kMultiplier * (mInstanceId + 1));
1345 EASTL_ASSERT(*p8 == mInstanceId);
1347 if (*p8 == mInstanceId)
1354 const char* get_name()
1356 sprintf(mName,
"InstanceAllocator %u", mInstanceId);
1360 void set_name(
const char*) {}
1362 static void reset_all() { mMismatchCount = 0; }
1365 uint8_t mInstanceId;
1368 static int mMismatchCount;
1385 template <
bool initialShouldThrow = true>
1389 ThrowingAllocator(
const char* = EASTL_NAME_VAL(
"ThrowingAllocator")) : mbShouldThrow(initialShouldThrow) {}
1395 mbShouldThrow = x.mbShouldThrow;
1399 void* allocate(
size_t n,
int = 0)
1401 #if EASTL_EXCEPTIONS_ENABLED
1403 throw std::bad_alloc();
1408 void* allocate(
size_t n,
size_t,
size_t,
int = 0)
1410 #if EASTL_EXCEPTIONS_ENABLED
1412 throw std::bad_alloc();
1417 void deallocate(
void* p,
size_t) { free(p); }
1419 const char* get_name()
const {
return "ThrowingAllocator"; }
1420 void set_name(
const char*) {}
1422 void set_should_throw(
bool shouldThrow) { mbShouldThrow = shouldThrow; }
1423 bool get_should_throw()
const {
return mbShouldThrow; }
1429 template <
bool initialShouldThrow>
1435 template <
bool initialShouldThrow>
1447 bool operator()(
const char* pCStr,
const eastl::string& str)
const {
return str.comparei(pCStr) == 0; }
1448 bool operator()(
const eastl::string& str,
const char* pCStr)
const {
return str.comparei(pCStr) == 0; }
1458 static uint64_t STOMP_MAGIC_V1 = 0x0101DEC1A551F1ED;
1459 static uint64_t STOMP_MAGIC_V2 = 0x12345C1A551F1ED5;
1468 void* allocate(
size_t n,
int = 0) {
return mMallocAllocator.allocate(n); }
1469 void* allocate(
size_t n,
size_t,
size_t,
int = 0) {
return mMallocAllocator.allocate(n); }
1470 void deallocate(
void* p,
size_t n) { mMallocAllocator.deallocate(p, n); }
1472 const char* get_name()
const {
return "FatAllocator"; }
1473 void set_name(
const char*) {}
1475 void Validate()
const
1477 EASTL_ASSERT(mSentinal1 == STOMP_MAGIC_V1);
1478 EASTL_ASSERT(mSentinal2 == STOMP_MAGIC_V2);
1481 uint64_t mSentinal1 = STOMP_MAGIC_V1;
1483 uint64_t mSentinal2 = STOMP_MAGIC_V2;
1491 return (a.mMallocAllocator == b.mMallocAllocator);
1499 return (a.mMallocAllocator != b.mMallocAllocator);
1504 inline int ReturnVal(
int param) {
return param; }
1505 inline int ReturnZero() {
return 0; }
1506 inline int ReturnOne() {
return 1; }
1522 T get() {
return mV; }
1541 bool operator==(
const MoveOnlyType& o)
const {
return mVal == o.mVal; }
Definition: EASTLTest.h:541
Definition: EASTLTest.h:1185
Definition: EASTLTest.h:1125
Definition: EASTLTest.h:262
Definition: EASTLTest.h:1300
Definition: EASTLTest.h:1067
Definition: EASTLTest.h:1387
Definition: EASTLTest.h:1151
Definition: EASTLTest.h:920
Definition: allocator.h:52
EA Standard Template Library.
Definition: algorithm.h:288
void fill(ForwardIterator first, ForwardIterator last, const T &value)
Definition: fill_help.h:75
Definition: EASTLTest.h:580
Definition: EASTLTest.h:606
Definition: EASTLTest.h:634
Definition: EASTLTest.h:1571
Definition: EASTLTest.h:692
Definition: EASTLTest.h:1548
Definition: EASTLTest.h:1529
Definition: EASTLTest.h:313
Definition: EASTLTest.h:718
Definition: EASTLTest.h:529
Definition: EASTLTest.h:1462
Definition: EASTLTest.h:556
Definition: EASTLTest.h:348
Definition: EASTLTest.h:1446
Definition: EASTLTest.h:1512
Definition: functional.h:1015
Definition: EASTLTest.h:667
Definition: EASTLTest.h:518