NameSetTests.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <Atom/RHI.Reflect/NameIdReflectionMap.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <AzCore/Serialization/ObjectStream.h>
  14. #include <AzCore/std/containers/vector.h>
  15. #include <AzCore/IO/ByteContainerStream.h>
  16. #include <AzCore/Serialization/Utils.h>
  17. namespace UnitTest
  18. {
  19. class NamedReflectionTests
  20. : public RHITestFixture
  21. {
  22. protected:
  23. void SetUp() override
  24. {
  25. RHITestFixture::SetUp();
  26. AZ::IO::FileIOBase::SetInstance(aznew AZ::IO::LocalFileIO());
  27. }
  28. void TearDown() override
  29. {
  30. delete AZ::IO::FileIOBase::GetInstance();
  31. AZ::IO::FileIOBase::SetInstance(nullptr);
  32. RHITestFixture::TearDown();
  33. }
  34. };
  35. TEST_F(NamedReflectionTests, NameIdReflectionMap_Empty)
  36. {
  37. AZ::RHI::NameIdReflectionMap<AZ::RHI::Handle<>> map;
  38. EXPECT_EQ(map.Size(), 0);
  39. }
  40. TEST_F(NamedReflectionTests, NameIdReflectionMap_Insert)
  41. {
  42. AZ::RHI::NameIdReflectionMap<AZ::RHI::Handle<>> map;
  43. // insert() also sorts the vector
  44. map.Insert(AZ::Name("name1"), AZ::RHI::Handle<>(3));
  45. map.Insert(AZ::Name("name2"), AZ::RHI::Handle<>(2));
  46. map.Insert(AZ::Name("name3"), AZ::RHI::Handle<>(1));
  47. EXPECT_EQ(map.Size(), 3);
  48. }
  49. TEST_F(NamedReflectionTests, NameIdReflectionMap_Serialize)
  50. {
  51. AZ::SerializeContext serializeContext;
  52. AZ::Name::Reflect(&serializeContext);
  53. AZ::RHI::Handle<>::Reflect(&serializeContext);
  54. AZ::RHI::NameIdReflectionMap<AZ::RHI::Handle<>>::Reflect(&serializeContext);
  55. AZ::RHI::NameIdReflectionMap<AZ::RHI::Handle<>> map;
  56. map.Insert(AZ::Name("name1"), AZ::RHI::Handle<>(3));
  57. map.Insert(AZ::Name("name2"), AZ::RHI::Handle<>(2));
  58. map.Insert(AZ::Name("name3"), AZ::RHI::Handle<>(1));
  59. // XML
  60. AZStd::vector<char> xmlBuffer;
  61. AZ::IO::ByteContainerStream<AZStd::vector<char> > xmlStream(&xmlBuffer);
  62. AZ::ObjectStream* xmlObjStream = AZ::ObjectStream::Create(&xmlStream, serializeContext, AZ::ObjectStream::ST_XML);
  63. xmlObjStream->WriteClass(&map);
  64. xmlObjStream->Finalize();
  65. const AZStd::string output(xmlBuffer.data(), xmlBuffer.size());
  66. EXPECT_NE(output.size(), 0);
  67. }
  68. TEST_F(NamedReflectionTests, NameIdReflectionMap_Deserialize)
  69. {
  70. const char* serializeDataFormat = R"(<ObjectStream version="3">
  71. <Class name = "AZ::RHI::NameIdReflectionMap&lt;AZ::RHI::Handle&lt;unsigned int, DefaultNamespaceType&gt;&gt;" type = "{4EAD7B2D-6190-5CB1-898D-5B96EB36EB46}" >
  72. <Class name = "AZStd::vector" field = "ReflectionMap" type = "{74463005-1C3D-5949-A2FB-90E795144DD6}">
  73. <Class name = "AZ::RHI::ReflectionNamePair&lt;AZ::RHI::Handle&lt;unsigned int, DefaultNamespaceType&gt;&gt;" field = "element" version = "2" type = "{A9301E84-7228-5301-9B2A-8A096DE3C712}">
  74. <Class name = "Name" field = "Name" value = "%s" type = "{3D2B920C-9EFD-40D5-AAE0-DF131C3D4931}" />
  75. <Class name = "AZ::RHI::Handle&lt;unsigned int, DefaultNamespaceType&gt;" field = "Index" version = "1" type = "{1811456D-0C3D-58C8-ACE8-FD47F4E80E25}">
  76. <Class name = "unsigned int" field = "m_index" value = "%s" type = "{43DA906B-7DEF-4CA8-9790-854106D3F983}" />
  77. </Class>
  78. </Class>
  79. <Class name = "AZ::RHI::ReflectionNamePair&lt;AZ::RHI::Handle&lt;unsigned int, DefaultNamespaceType&gt;&gt;" field = "element" version = "2" type = "{A9301E84-7228-5301-9B2A-8A096DE3C712}">
  80. <Class name = "Name" field = "Name" value = "%s" type = "{3D2B920C-9EFD-40D5-AAE0-DF131C3D4931}" />
  81. <Class name = "AZ::RHI::Handle&lt;unsigned int, DefaultNamespaceType&gt;" field = "Index" version = "1" type = "{1811456D-0C3D-58C8-ACE8-FD47F4E80E25}">
  82. <Class name = "unsigned int" field = "m_index" value = "%s" type = "{43DA906B-7DEF-4CA8-9790-854106D3F983}" />
  83. </Class>
  84. </Class>
  85. <Class name = "AZ::RHI::ReflectionNamePair&lt;AZ::RHI::Handle&lt;unsigned int, DefaultNamespaceType&gt;&gt;" field = "element" version = "2" type = "{A9301E84-7228-5301-9B2A-8A096DE3C712}">
  86. <Class name = "Name" field = "Name" value = "%s" type = "{3D2B920C-9EFD-40D5-AAE0-DF131C3D4931}" />
  87. <Class name = "AZ::RHI::Handle&lt;unsigned int, DefaultNamespaceType&gt;" field = "Index" version = "1" type = "{1811456D-0C3D-58C8-ACE8-FD47F4E80E25}">
  88. <Class name = "unsigned int" field = "m_index" value = "%s" type = "{43DA906B-7DEF-4CA8-9790-854106D3F983}" />
  89. </Class>
  90. </Class>
  91. </Class>
  92. </Class>
  93. </ObjectStream>)";
  94. // The internal storage sorts by the hash value of strings, so name2 comes before name3, which comes before name1.
  95. // So the inpuit is specifically putting them out order with how it appears sorted when inserted.
  96. AZStd::string inputData = AZStd::string::format(serializeDataFormat, "name3", "3", "name2", "2", "name1", "1");
  97. AZ::SerializeContext serializeContext;
  98. AZ::Name::Reflect(&serializeContext);
  99. AZ::RHI::Handle<>::Reflect(&serializeContext);
  100. AZ::RHI::NameIdReflectionMap<AZ::RHI::Handle<>>::Reflect(&serializeContext);
  101. AZStd::vector<AZ::u8> binaryData(inputData.begin(), inputData.end());
  102. AZ::IO::ByteContainerStream<const AZStd::vector<AZ::u8> > binaryStream(&binaryData);
  103. binaryStream.Seek(0, AZ::IO::GenericStream::ST_SEEK_BEGIN);
  104. {
  105. AZ::RHI::NameIdReflectionMap<AZ::RHI::Handle<>> map;
  106. EXPECT_TRUE(AZ::Utils::LoadObjectFromStreamInPlace(binaryStream, map, &serializeContext));
  107. EXPECT_EQ(map.Size(), 3);
  108. EXPECT_EQ(map.Find(AZ::Name("name1")).m_index, 1);
  109. EXPECT_EQ(map.Find(AZ::Name("name2")).m_index, 2);
  110. EXPECT_EQ(map.Find(AZ::Name("name3")).m_index, 3);
  111. }
  112. }
  113. }