Files
o3de/Code/Framework/AzCore/Tests/UUIDTests.cpp
T
lumberyard-employee-dm 090aa8f053 Removed ununeeded includes from EBus EBus.h and Policies.h (#4256)
* Removed ununeeded includes from EBus EBus.h and Policies.h

Updated the locations which needed those includes

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Adding missing include for <memory> to AWsClientAuthBus.h

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Remove the while true loop in the EBusQueuePolicy Execute() function

The while true loop in Execute was for allowing additional functions to be queued in the middle of execution of current list of functions.
That functionality was dangerous, because if a queued function added itself during execution unconditionally, then it would result in an infinite loop

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated the AssetManager::DispatchEvents function to pump the AssetBus event queue until empty

Queued Events on the AssetBus is able to queue additional events on that Bus during execution of those events.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Changed the AssetManager::DispatchEvents function to only execute the
AssetBus queued events once

Changed the AssetJobsFloodTest.AssetWithNoLoadReference_LoadDependencies_BehaviorObeyed test to dispatch events until the OnAssetContainerReady callback is signaled.

This happens after every asset load to make sure that the
expiring AssetContainer instances are removed from `AssetManager::m_ownedAssetContainer` container before retrying to load the same asset.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Added a MaxTimeoutSeconds constant for the maximum amount of the time to run a single DispatchEvents loop

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>
2021-09-29 18:13:37 -05:00

251 lines
9.2 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <AzCore/UnitTest/TestTypes.h>
#include <AzCore/Math/Uuid.h>
#include <AzCore/std/containers/unordered_set.h>
using namespace AZ;
namespace UnitTest
{
class UuidTests
: public UnitTest::AllocatorsTestFixture
{
static const int numUuid = 2000;
Uuid* m_array;
public:
void SetUp() override
{
AllocatorsTestFixture::SetUp();
m_array = (Uuid*)azmalloc(sizeof(Uuid) * numUuid, AZStd::alignment_of<Uuid>::value);
}
void TearDown() override
{
azfree(m_array);
AllocatorsTestFixture::TearDown();
}
void run()
{
Uuid defId;
defId.data[0] = 0xb5;
defId.data[1] = 0x70;
defId.data[2] = 0x0f;
defId.data[3] = 0x2e;
defId.data[4] = 0x66;
defId.data[5] = 0x1b;
defId.data[6] = 0x4a;
defId.data[7] = 0xc0;
defId.data[8] = 0x93;
defId.data[9] = 0x35;
defId.data[10] = 0x81;
defId.data[11] = 0x7c;
defId.data[12] = 0xb4;
defId.data[13] = 0xc0;
defId.data[14] = 0x9c;
defId.data[15] = 0xcb;
// null
Uuid id = Uuid::CreateNull();
AZ_TEST_ASSERT(id.IsNull());
const char idStr1[] = "{B5700F2E-661B-4AC0-9335-817CB4C09CCB}";
const char idStr2[] = "{B5700F2E661B4AC09335817CB4C09CCB}";
const char idStr3[] = "B5700F2E-661B-4AC0-9335-817CB4C09CCB";
const char idStr4[] = "B5700F2E661B4AC09335817CB4C09CCB";
// create from string
id = Uuid::CreateString(idStr1);
AZ_TEST_ASSERT(id == defId);
id = Uuid::CreateString(idStr2);
AZ_TEST_ASSERT(id == defId);
id = Uuid::CreateString(idStr3);
AZ_TEST_ASSERT(id == defId);
id = Uuid::CreateString(idStr4);
AZ_TEST_ASSERT(id == defId);
// variant
AZ_TEST_ASSERT(id.GetVariant() == Uuid::VAR_RFC_4122);
// version
AZ_TEST_ASSERT(id.GetVersion() == Uuid::VER_RANDOM);
// tostring
char buffer[39];
id = Uuid::CreateString(idStr1);
AZ_TEST_ASSERT(id.ToString(buffer, 39, true, true) == 39);
AZ_TEST_ASSERT(strcmp(buffer, idStr1) == 0);
AZ_TEST_ASSERT(id.ToString(buffer, 35, true, false) == 35);
AZ_TEST_ASSERT(strcmp(buffer, idStr2) == 0);
AZ_TEST_ASSERT(id.ToString(buffer, 37, false, true) == 37);
AZ_TEST_ASSERT(strcmp(buffer, idStr3) == 0);
AZ_TEST_ASSERT(id.ToString(buffer, 33, false, false) == 33);
AZ_TEST_ASSERT(strcmp(buffer, idStr4) == 0);
AZ_TEST_ASSERT(id.ToString<AZStd::string>() == AZStd::string(idStr1));
AZ_TEST_ASSERT(id.ToString<AZStd::string>(true, false) == AZStd::string(idStr2));
AZ_TEST_ASSERT(id.ToString<AZStd::string>(false, true) == AZStd::string(idStr3));
AZ_TEST_ASSERT(id.ToString<AZStd::string>(false, false) == AZStd::string(idStr4));
AZStd::string str1;
id.ToString(str1);
AZ_TEST_ASSERT(str1 == AZStd::string(idStr1));
id.ToString(str1, true, false);
AZ_TEST_ASSERT(str1 == AZStd::string(idStr2));
id.ToString(str1, false, true);
AZ_TEST_ASSERT(str1 == AZStd::string(idStr3));
id.ToString(str1, false, false);
AZ_TEST_ASSERT(str1 == AZStd::string(idStr4));
// operators
Uuid idBigger("C5700F2E661B4ac09335817CB4C09CCB");
AZ_TEST_ASSERT(id < idBigger);
AZ_TEST_ASSERT(id != idBigger);
AZ_TEST_ASSERT(idBigger > id);
// hash
AZStd::hash<AZ::Uuid> hash;
size_t hashVal = hash(id);
AZ_TEST_ASSERT(hashVal != 0);
// test the hashing and equal function in a unordered container
typedef AZStd::unordered_set<AZ::Uuid> UuidSetType;
UuidSetType uuidSet;
uuidSet.insert(id);
AZ_TEST_ASSERT(uuidSet.find(id) != uuidSet.end());
// check uniqueness (very quick and basic)
for (int i = 0; i < numUuid; ++i)
{
m_array[i] = Uuid::Create();
}
for (int i = 0; i < numUuid; ++i)
{
Uuid uniqueToTest = Uuid::Create();
for (int j = 0; j < numUuid; ++j)
{
AZ_TEST_ASSERT(m_array[j] != uniqueToTest);
}
}
// test the name function
Uuid uuidName = Uuid::CreateName("BlaBla");
// check variant
AZ_TEST_ASSERT(uuidName.GetVariant() == Uuid::VAR_RFC_4122);
// check version
AZ_TEST_ASSERT(uuidName.GetVersion() == Uuid::VER_NAME_SHA1);
// check id
AZ_TEST_ASSERT(uuidName == Uuid::CreateName("BlaBla"));
}
};
TEST_F(UuidTests, Test)
{
run();
}
TEST_F(UuidTests, GreaterThanOrEqualTo_LeftGreaterThanRight_ReturnsTrue)
{
const char leftStr[] = "{F418022E-DAFE-4450-BCB9-4B7727070178}";
const char rightStr[] = "{B5700F2E-661B-4AC0-9335-817CB4C09CCB}";
Uuid left = Uuid::CreateString(leftStr);
Uuid right = Uuid::CreateString(rightStr);
EXPECT_TRUE(left >= right);
}
TEST_F(UuidTests, GreaterThanOrEqualTo_LeftLessThanRight_ReturnsFalse)
{
const char leftStr[] = "{A418022E-DAFE-4450-BCB9-4B7727070178}";
const char rightStr[] = "{B5700F2E-661B-4AC0-9335-817CB4C09CCB}";
Uuid left = Uuid::CreateString(leftStr);
Uuid right = Uuid::CreateString(rightStr);
EXPECT_FALSE(left >= right);
}
TEST_F(UuidTests, GreaterThanOrEqualTo_LeftEqualsRight_ReturnsTrue)
{
const char uuidStr[] = "{F418022E-DAFE-4450-BCB9-4B7727070178}";
Uuid left = Uuid::CreateString(uuidStr);
Uuid right = Uuid::CreateString(uuidStr);
EXPECT_TRUE(left >= right);
}
TEST_F(UuidTests, LessThanOrEqualTo_LeftGreaterThanRight_ReturnsFalse)
{
const char leftStr[] = "{F418022E-DAFE-4450-BCB9-4B7727070178}";
const char rightStr[] = "{B5700F2E-661B-4AC0-9335-817CB4C09CCB}";
Uuid left = Uuid::CreateString(leftStr);
Uuid right = Uuid::CreateString(rightStr);
EXPECT_FALSE(left <= right);
}
TEST_F(UuidTests, LessThanOrEqualTo_LeftLessThanRight_ReturnsTrue)
{
const char leftStr[] = "{A418022E-DAFE-4450-BCB9-4B7727070178}";
const char rightStr[] = "{B5700F2E-661B-4AC0-9335-817CB4C09CCB}";
Uuid left = Uuid::CreateString(leftStr);
Uuid right = Uuid::CreateString(rightStr);
EXPECT_TRUE(left <= right);
}
TEST_F(UuidTests, LessThanOrEqualTo_LeftEqualsRight_ReturnsTrue)
{
const char uuidStr[] = "{F418022E-DAFE-4450-BCB9-4B7727070178}";
Uuid left = Uuid::CreateString(uuidStr);
Uuid right = Uuid::CreateString(uuidStr);
EXPECT_TRUE(left <= right);
}
TEST_F(UuidTests, CreateStringPermissive_HexAndSpacesGiven_Success)
{
const char uuidStr[] = "{34D44249-E599-4B30-811F-4215C2DEA269}";
Uuid left = Uuid::CreateString(uuidStr);
const char permissiveStr[] = "{ 0x34D44249 - 0xE5994B30 - 0x811F4215 - 0xC2DEA269 }";
Uuid right = Uuid::CreateStringPermissive(permissiveStr);
EXPECT_EQ(left, right);
const char permissiveStr2[] = "{ 0x34D44249-0xE5994B30 0x811F4215 - C2DEA269 }";
right = Uuid::CreateStringPermissive(permissiveStr2);
EXPECT_EQ(left, right);
const char permissiveStr3[] = "34D44249-0xE5994B30 0x811F4215 - C2DEA269 }";
right = Uuid::CreateStringPermissive(permissiveStr3);
EXPECT_EQ(left, right);
const char permissiveStr4[] = "{ x34D44249 - xE5994B30 - x811F4215 xC2DEA269 }";
right = Uuid::CreateStringPermissive(permissiveStr4);
EXPECT_EQ(left, right);
const char permissiveStr5[] = "{ 0X34D44249 - 0XE5994B30 - 0X811F4215 0XC2DEA269 }";
right = Uuid::CreateStringPermissive(permissiveStr5);
EXPECT_EQ(left, right);
}
TEST_F(UuidTests, CreateStringPermissive_InvalidHexAndSpacesGiven_Fails)
{
const char uuidStr[] = "{8FDDE7B1 - C332 - 4EBA - BD85 - 2898E7440E4C}";
Uuid left = Uuid::CreateStringPermissive(uuidStr);
const char permissiveStr1[] = "{ 8FDDE7B1 - 0 xC332 - 4EBA - BD85 - 2898E7440E4C}";
Uuid right = Uuid::CreateStringPermissive(permissiveStr1);
EXPECT_NE(left, right);
}
TEST_F(UuidTests, CreateStringPermissive_InvalidCharacterGiven_Fails)
{
Uuid left = Uuid::CreateNull();
// The below check should just give an empty uuid due to the g
const char permissiveStr1[] = "{CCF8AB1E- gA04A-43D1-AD8A-70725BC3392E}";
Uuid right = Uuid::CreateStringPermissive(permissiveStr1);
EXPECT_EQ(left, right);
}
}