UtilsTests.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "RHITestFixture.h"
  9. #include <AzFramework/IO/LocalFileIO.h>
  10. #include <Atom/RHI.Edit/Utils.h>
  11. #include <AzFramework/Application/Application.h>
  12. namespace UnitTest
  13. {
  14. class UtilsTests
  15. : public RHITestFixture
  16. {
  17. protected:
  18. UtilsTests()
  19. : m_application{ AZStd::make_unique<AzFramework::Application>() }
  20. {
  21. // Add the Atom_RHI as an active gem for the Atom_Utils test in order
  22. // to have the @gemroot:Atom_RHI@ alias set
  23. if (auto settingsRegistry = AZ::SettingsRegistry::Get(); settingsRegistry != nullptr)
  24. {
  25. AZ::Test::AddActiveGem("Atom_RHI", *settingsRegistry, AZ::IO::FileIOBase::GetInstance());
  26. }
  27. }
  28. ~UtilsTests() override
  29. {
  30. m_application.reset();
  31. }
  32. static constexpr const char TestDataFolder[] = "@gemroot:Atom_RHI@/Code/Tests/UtilsTestsData/";
  33. AZStd::unique_ptr<AzFramework::Application> m_application;
  34. };
  35. TEST_F(UtilsTests, LoadFileString)
  36. {
  37. AZStd::string testFilePath = TestDataFolder + AZStd::string("HelloWorld.txt");
  38. AZ::Outcome<AZStd::string, AZStd::string> outcome = AZ::RHI::LoadFileString(testFilePath.c_str());
  39. EXPECT_TRUE(outcome.IsSuccess());
  40. auto& str = outcome.GetValue();
  41. auto itr = AZStd::remove(str.begin(), str.end(), '\r');
  42. if (itr != str.end())
  43. {
  44. str.erase(itr);
  45. }
  46. EXPECT_EQ(AZStd::string("Hello World!\n"), str);
  47. }
  48. TEST_F(UtilsTests, LoadFileBytes)
  49. {
  50. AZStd::string testFilePath = TestDataFolder + AZStd::string("HelloWorld.txt");
  51. AZ::Outcome<AZStd::vector<uint8_t>, AZStd::string> outcome = AZ::RHI::LoadFileBytes(testFilePath.c_str());
  52. EXPECT_TRUE(outcome.IsSuccess());
  53. AZStd::string expectedText = "Hello World!\n";
  54. auto& str = outcome.GetValue();
  55. auto itr = AZStd::remove(str.begin(), str.end(), '\r');
  56. if (itr != str.end())
  57. {
  58. str.erase(itr);
  59. }
  60. EXPECT_EQ(AZStd::vector<uint8_t>(expectedText.begin(), expectedText.end()), str);
  61. }
  62. TEST_F(UtilsTests, LoadFileString_Error_DoesNotExist)
  63. {
  64. AZ_TEST_START_TRACE_SUPPRESSION;
  65. auto outcome = AZ::RHI::LoadFileString("FileDoesNotExist");
  66. AZ_TEST_STOP_TRACE_SUPPRESSION_NO_COUNT;
  67. EXPECT_FALSE(outcome.IsSuccess());
  68. EXPECT_TRUE(outcome.GetError().find("Could not open file") != AZStd::string::npos);
  69. EXPECT_TRUE(outcome.GetError().find("FileDoesNotExist") != AZStd::string::npos);
  70. }
  71. TEST_F(UtilsTests, LoadFileBytes_Error_DoesNotExist)
  72. {
  73. AZ_TEST_START_TRACE_SUPPRESSION;
  74. auto outcome = AZ::RHI::LoadFileBytes("FileDoesNotExist");
  75. AZ_TEST_STOP_TRACE_SUPPRESSION_NO_COUNT;
  76. EXPECT_FALSE(outcome.IsSuccess());
  77. EXPECT_TRUE(outcome.GetError().find("Could not open file") != AZStd::string::npos);
  78. EXPECT_TRUE(outcome.GetError().find("FileDoesNotExist") != AZStd::string::npos);
  79. }
  80. TEST_F(UtilsTests, RegexCount_DXIL)
  81. {
  82. AZStd::string testFilePath = TestDataFolder + AZStd::string("DummyTransformColor.MainPS.dx12.dxil.txt");
  83. auto ojectCode = AZ::RHI::LoadFileString(testFilePath.c_str());
  84. EXPECT_TRUE(ojectCode.IsSuccess());
  85. size_t dynamicBranchCount = AZ::RHI::RegexCount(ojectCode.GetValue(), "^ *(br|indirectbr|switch) ");
  86. EXPECT_EQ(10, dynamicBranchCount);
  87. }
  88. TEST_F(UtilsTests, RegexCount_SPIRV)
  89. {
  90. AZStd::string testFilePath = TestDataFolder + AZStd::string("DummyTransformColor.MainPS.vulkan.spirv.txt");
  91. auto ojectCode = AZ::RHI::LoadFileString(testFilePath.c_str());
  92. EXPECT_TRUE(ojectCode.IsSuccess());
  93. size_t dynamicBranchCount = AZ::RHI::RegexCount(ojectCode.GetValue(), "^ *(OpBranch|OpBranchConditional|OpSwitch) ");
  94. EXPECT_EQ(23, dynamicBranchCount);
  95. }
  96. }