Initial commit

This commit is contained in:
alexpete
2021-03-05 11:26:34 -08:00
commit a10351f38d
27091 changed files with 5521199 additions and 0 deletions
@@ -0,0 +1,135 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "MathTestHelpers.h"
#include <AzCore/Math/Matrix3x3.h>
#include <AzCore/Math/Matrix4x4.h>
#include <AzCore/Math/Quaternion.h>
#include <AzCore/Math/Sphere.h>
#include <AzCore/Math/ToString.h>
#include <AzCore/Math/Vector2.h>
#include <AzCore/Math/Vector3.h>
#include <AzCore/Math/Vector4.h>
#include <AzCore/Math/Transform.h>
#include <AzCore/Math/Color.h>
// make gtest/gmock aware of these types so when a failure occurs we get more useful output
namespace AZ
{
std::ostream& operator<<(std::ostream& os, const Vector2& vec)
{
return os
<< "(X: " << vec.GetX()
<< ", Y: " << vec.GetY()
<< ")";
}
std::ostream& operator<<(std::ostream& os, const Vector3& vec)
{
return os
<< "(X: " << vec.GetX()
<< ", Y: " << vec.GetY()
<< ", Z: " << vec.GetZ()
<< ")";
}
std::ostream& operator<<(std::ostream& os, const Vector4& vec)
{
return os
<< "(X: " << vec.GetX()
<< ", Y: " << vec.GetY()
<< ", Z: " << vec.GetZ()
<< ", W: " << vec.GetW()
<< ")";
}
std::ostream& operator<<(std::ostream& os, const Quaternion& quat)
{
return os
<< "(X: " << quat.GetX()
<< ", Y: " << quat.GetY()
<< ", Z: " << quat.GetZ()
<< ", W: " << quat.GetW()
<< ")";
}
std::ostream& operator<<(std::ostream& os, const Transform& transform)
{
return os
<< "translation: " << transform.GetTranslation()
<< " rotation: " << transform.GetRotation()
<< " scale: " << transform.GetScale();
}
std::ostream& operator<<(std::ostream& os, const Color& color)
{
return os
<< "red: " << color.GetR()
<< " green: " << color.GetG()
<< " blue: " << color.GetB()
<< " alpha: " << color.GetA();
}
std::ostream& operator<< (std::ostream& os, const Sphere& sphere)
{
os << "center: " << sphere.GetCenter() << " radius: " << sphere.GetRadius();
return os;
}
// pretty-printer for matrices in test output
template<typename Mat>
std::ostream& PrintMatrix(std::ostream& os, const Mat& mat)
{
auto printElement = [&os, &mat](int64_t row, int64_t col) -> std::ostream&
{
const std::streamsize width = 10;
os << std::setw(width) << std::fixed << mat.GetElement(row, col);
return os;
};
os << "\n";
for (int64_t row = 0; row < Mat::RowCount - 1; ++row)
{
for (int64_t col = 0; col < Mat::ColCount; ++col)
{
printElement(row, col) << ", ";
}
os << "\n";
}
for (int64_t col = 0; col < Mat::ColCount - 1; ++col)
{
printElement(Mat::RowCount - 1, col) << ", ";
}
printElement(Mat::RowCount - 1, Mat::ColCount - 1);
return os;
}
std::ostream& operator<<(std::ostream& os, const Matrix3x3& mat)
{
return PrintMatrix(os, mat);
}
std::ostream& operator<<(std::ostream& os, const Matrix3x4& mat)
{
return PrintMatrix(os, mat);
}
std::ostream& operator<<(std::ostream& os, const Matrix4x4& mat)
{
return PrintMatrix(os, mat);
}
} // namespace AZ
@@ -0,0 +1,113 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include <AzTest/AzTest.h>
#include <ostream>
namespace AZ
{
class Matrix3x3;
class Matrix3x4;
class Matrix4x4;
class Quaternion;
class Sphere;
class Vector2;
class Vector3;
class Vector4;
class Transform;
class Color;
std::ostream& operator<<(std::ostream& os, const Vector2& vec);
std::ostream& operator<<(std::ostream& os, const Vector3& vec);
std::ostream& operator<<(std::ostream& os, const Vector4& vec);
std::ostream& operator<<(std::ostream& os, const Quaternion& quat);
std::ostream& operator<<(std::ostream& os, const Transform& transform);
std::ostream& operator<<(std::ostream& os, const Color& transform);
std::ostream& operator<<(std::ostream& os, const Sphere& sphere);
std::ostream& operator<<(std::ostream& os, const Matrix3x3& mat);
std::ostream& operator<<(std::ostream& os, const Matrix3x4& mat);
std::ostream& operator<<(std::ostream& os, const Matrix4x4& mat);
} // namespace AZ
namespace UnitTest
{
// is-close matcher to make tests easier to read and failures more useful
// (more information is included in the output)
MATCHER_P(IsClose, expected, "")
{
AZ_UNUSED(result_listener);
if (arg.IsClose(expected))
{
return true;
}
return false;
}
// is-close matcher with tolerance to make tests easier to read and failures more useful
// (more information is included in the output)
MATCHER_P2(IsCloseTolerance, expected, tolerance, "")
{
AZ_UNUSED(result_listener);
if (arg.IsClose(expected, tolerance))
{
return true;
}
return false;
}
// is-close matcher for use with Pointwise container comparisons
MATCHER(ContainerIsClose, "")
{
AZ_UNUSED(result_listener);
const auto& [expected, actual] = arg;
if (expected.IsClose(actual))
{
return true;
}
return false;
}
// is-close matcher with tolerance for use with Pointwise container comparisons
MATCHER_P(ContainerIsCloseTolerance, tolerance, "")
{
AZ_UNUSED(result_listener);
const auto& [expected, actual] = arg;
if (expected.IsClose(actual, tolerance))
{
return true;
}
return false;
}
// IsFinite matcher to make it easier to validate Vector2, Vector3, Vector4 and Quaternion.
// For example:
// AZ::Quaternion rotation;
// EXPECT_THAT(rotation, IsFinite());
//
// AZStd::vector<AZ::Vector3> positions;
// EXPECT_THAT(positions, ::testing::Each(IsFinite()));
MATCHER(IsFinite, "")
{
AZ_UNUSED(result_listener);
return arg.IsFinite();
}
} // namespace UnitTest
@@ -0,0 +1,168 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AZTestShared/Utils/Utils.h>
#include "AzCore/Component/Entity.h"
#include "AzCore/Asset/AssetManager.h"
#include "AzCore/Slice/SliceComponent.h"
namespace UnitTest
{
AZStd::string GetTestFolderPath()
{
return AZ_TRAIT_TEST_ROOT_FOLDER;
}
void MakePathFromTestFolder(char* buffer, int bufferLen, const char* fileName)
{
azsnprintf(buffer, bufferLen, "%s%s", GetTestFolderPath().c_str(), fileName);
}
ErrorHandler::ErrorHandler(const char* errorPattern)
: m_errorCount(0)
, m_warningCount(0)
, m_errorPattern(errorPattern)
{
AZ::Debug::TraceMessageBus::Handler::BusConnect();
}
ErrorHandler::~ErrorHandler()
{
AZ::Debug::TraceMessageBus::Handler::BusDisconnect();
}
int ErrorHandler::GetErrorCount() const
{
return m_errorCount;
}
int ErrorHandler::GetWarningCount() const
{
return m_warningCount;
}
bool ErrorHandler::SuppressExpectedErrors([[maybe_unused]] const char* window, const char* message)
{
return AZStd::string(message).find(m_errorPattern) != AZStd::string::npos;
}
bool ErrorHandler::OnPreError(
const char* window, [[maybe_unused]] const char* fileName, [[maybe_unused]] int line,
[[maybe_unused]] const char* func, const char* message)
{
m_errorCount++;
return SuppressExpectedErrors(window, message);
}
bool ErrorHandler::OnPreWarning(
const char* window, [[maybe_unused]] const char* fileName, [[maybe_unused]] int line,
[[maybe_unused]] const char* func, const char* message)
{
m_warningCount++;
return SuppressExpectedErrors(window, message);
}
bool ErrorHandler::OnPrintf(const char* window, const char* message)
{
return SuppressExpectedErrors(window, message);
}
}
namespace AZ
{
namespace Test
{
AZ::Data::Asset<AZ::SliceAsset> CreateSliceFromComponent(AZ::Component* assetComponent, AZ::Data::AssetId sliceAssetId)
{
auto* sliceEntity = aznew AZ::Entity;
auto* contentEntity = aznew AZ::Entity;
if (assetComponent)
{
contentEntity->AddComponent(assetComponent);
}
auto sliceAsset = Data::AssetManager::Instance().CreateAsset<AZ::SliceAsset>(sliceAssetId, AZ::Data::AssetLoadBehavior::Default);
AZ::SliceComponent* component = sliceEntity->CreateComponent<AZ::SliceComponent>();
component->SetIsDynamic(true);
sliceAsset.Get()->SetData(sliceEntity, component);
component->AddEntity(contentEntity);
return sliceAsset;
}
AssertAbsorber::AssertAbsorber()
{
AZ::Debug::TraceMessageBus::Handler::BusConnect();
}
AssertAbsorber::~AssertAbsorber()
{
AZ::Debug::TraceMessageBus::Handler::BusDisconnect();
}
bool AssertAbsorber::OnPreAssert(const char* fileName, int line, const char* func, const char* message)
{
AZ_UNUSED(fileName);
AZ_UNUSED(line);
AZ_UNUSED(func);
AZ_UNUSED(message);
++m_assertCount;
return true;
}
bool AssertAbsorber::OnAssert(const char* message)
{
AZ_UNUSED(message);
return true;
}
bool AssertAbsorber::OnPreError(const char* window, const char* fileName, int line, const char* func, const char* message)
{
AZ_UNUSED(window);
AZ_UNUSED(fileName);
AZ_UNUSED(line);
AZ_UNUSED(func);
AZ_UNUSED(message);
++m_errorCount;
return true;
}
bool AssertAbsorber::OnError(const char* window, const char* message)
{
AZ_UNUSED(window);
AZ_UNUSED(message);
return true;
}
bool AssertAbsorber::OnWarning(const char* window, const char* message)
{
AZ_UNUSED(window);
AZ_UNUSED(message);
return true;
}
bool AssertAbsorber::OnPreWarning(const char* window, const char* fileName, int line, const char* func, const char* message)
{
AZ_UNUSED(window);
AZ_UNUSED(fileName);
AZ_UNUSED(line);
AZ_UNUSED(func);
AZ_UNUSED(message);
++m_warningCount;
return true;
}
}// namespace Test
}// namespace AZ
@@ -0,0 +1,81 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include <AzCore/std/string/string.h>
#include <AzCore/Debug/TraceMessageBus.h>
#include "AzCore/Slice/SliceAsset.h"
namespace UnitTest
{
//! Returns a test folder path
AZStd::string GetTestFolderPath();
void MakePathFromTestFolder(char* buffer, int bufferLen, const char* fileName);
//! Handler for the trace message bus to suppress errors / warnings that are expected due to testing particular
//! code branches, so as to avoid filling the test output with traces.
class ErrorHandler
: public AZ::Debug::TraceMessageBus::Handler
{
public:
explicit ErrorHandler(const char* errorPattern);
~ErrorHandler();
int GetErrorCount() const;
int GetWarningCount() const;
bool SuppressExpectedErrors(const char* window, const char* message);
// AZ::Debug::TraceMessageBus
bool OnPreError(
const char* window, const char* fileName, int line, const char* func, const char* message) override;
bool OnPreWarning(
const char* window, const char* fileName, int line, const char* func, const char* message) override;
bool OnPrintf(const char* window, const char* message) override;
private:
AZStd::string m_errorPattern;
int m_errorCount;
int m_warningCount;
};
}
namespace AZ
{
class Component;
namespace Test
{
//! Creates an entity, assigns the given component to it and then creates a slice asset containing the entity
AZ::Data::Asset<AZ::SliceAsset> CreateSliceFromComponent(AZ::Component* assetComponent, AZ::Data::AssetId sliceAssetId);
// the Assert Absorber can be used to absorb asserts, errors and warnings during unit tests.
class AssertAbsorber
: public AZ::Debug::TraceMessageBus::Handler
{
public:
AssertAbsorber();
~AssertAbsorber();
bool OnPreAssert(const char* fileName, int line, const char* func, const char* message) override;
bool OnAssert(const char* message) override;
bool OnPreError(const char* window, const char* fileName, int line, const char* func, const char* message) override;
bool OnError(const char* window, const char* message) override;
bool OnWarning(const char* window, const char* message) override;
bool OnPreWarning(const char* window, const char* fileName, int line, const char* func, const char* message) override;
AZ::u32 m_errorCount{ 0 };
AZ::u32 m_warningCount{ 0 };
AZ::u32 m_assertCount{ 0 };
};
} //namespace Test
}// namespace AZ