2
0

CrcTests.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <AzCore/Math/Crc.h>
  9. #include <AzCore/std/containers/array.h>
  10. #include <AzCore/UnitTest/TestTypes.h>
  11. #if defined(HAVE_BENCHMARK)
  12. #include "CrcTestsCompileTimeLiterals.h"
  13. namespace Benchmark
  14. {
  15. inline namespace Crc32Internal
  16. {
  17. //! The number of different Crc32 Values to calculate at compile
  18. //! This can be upped to check the Benchmarks
  19. constexpr size_t NumConstevalCrc32CompileValues = 1;
  20. using TestCrc32Array = AZStd::array<AZ::Crc32, NumConstevalCrc32CompileValues>;
  21. constexpr TestCrc32Array CreateCrc32FromLiteral(const AZStd::string_view (&testValues) [TestStringLiteralSize])
  22. {
  23. TestCrc32Array resultArray{};
  24. for (size_t crcIndex = 0; crcIndex < NumConstevalCrc32CompileValues; ++crcIndex)
  25. {
  26. resultArray[crcIndex] = AZ::Crc32(testValues[crcIndex % TestStringLiteralSize]);
  27. }
  28. return resultArray;
  29. }
  30. // Generates an array of Crc32 values based on the generated array of test string literals
  31. // The time it takes this function to compile is the amount of time spent creating Crc32 values
  32. // at compile time
  33. constexpr TestCrc32Array GenerateTestCrc32Values()
  34. {
  35. return CreateCrc32FromLiteral(TestStringLiterals);
  36. }
  37. }
  38. class Crc32BenchmarkEnvironment
  39. : public AZ::Test::BenchmarkEnvironmentBase
  40. {};
  41. static Crc32BenchmarkEnvironment& s_crcBenchmarkEnv = AZ::Test::RegisterBenchmarkEnvironment<Crc32BenchmarkEnvironment>();
  42. static void MeasureCrc32ConstevalTime(::benchmark::State& state)
  43. {
  44. // Runtime performance is not actually being measured by this test.
  45. // This function only exist to calculate AZ::Crc32 values at compile time
  46. for ([[maybe_unused]] auto _ : state)
  47. {
  48. [[maybe_unused]] constexpr auto resultArray = Crc32Internal::GenerateTestCrc32Values();
  49. }
  50. }
  51. BENCHMARK(MeasureCrc32ConstevalTime);
  52. }
  53. #endif
  54. namespace UnitTest
  55. {
  56. class Crc32Fixture
  57. : public UnitTest::LeakDetectionFixture
  58. {
  59. };
  60. TEST_F(Crc32Fixture, Constructor_IsConstexpr)
  61. {
  62. static_assert(AZ::Crc32() == AZ::Crc32{ 0U }, "Default constructed Crc32 should 0");
  63. static_assert(AZ::Crc32(0x6dc044c5) == AZ::Crc32(0x6dc044c5), R"(Crc32 should match the calculation on the string "group")");
  64. static_assert(AZ::Crc32("EditorData") == AZ::Crc32(0xf44f1a1d), R"(Crc32 should match the calculation on the string "editordata")");
  65. // Crc value for lowercase string of "group"
  66. static_assert(AZ::Crc32(AZStd::string_view{ "EditorData+RuntimeData", 10 }) == AZ::Crc32(0xf44f1a1d), R"(Crc32 should match the calculation on the string "editordata")");
  67. static_assert(AZ::Crc32("Editor", 6, true) == AZ::Crc32(0xccf1f1ba), R"(Crc32 should match the calculation on the string "editor")");
  68. static_assert(AZ::Crc32("Editor", 6, false) == AZ::Crc32(0xcb5df48c), R"(Crc32 should match the calculation on the string "Editor")");
  69. constexpr uint8_t binaryData[] = { 'B', 'i', 'n', 0x3 };
  70. static_assert(AZ::Crc32(binaryData, 4, false) == AZ::Crc32(0x3528a896), R"(Crc32 should match the calculation on the binary blob "Bin\x03")");
  71. constexpr AZStd::byte byteData[]{ AZStd::byte('B'), AZStd::byte('i'), AZStd::byte('n'), AZStd::byte(0x3) };
  72. static_assert(AZ::Crc32(AZStd::span(byteData)) == AZ::Crc32(0x3528a896));
  73. }
  74. TEST_F(Crc32Fixture, OperatorUint32t_IsConstexpr)
  75. {
  76. static_assert(static_cast<AZ::u32>(AZ::Crc32("EditorData")) == 0xf44f1a1d, R"(Crc32 should match the calculation on the string "editordata")");
  77. }
  78. TEST_F(Crc32Fixture, Add_IsConstexpr)
  79. {
  80. constexpr auto TestAdd = []() constexpr -> AZ::Crc32
  81. {
  82. AZ::Crc32 hello("Hello");
  83. hello.Add(" World");
  84. return hello;
  85. };
  86. constexpr AZ::Crc32 addResult = TestAdd();
  87. static_assert(addResult == AZ::Crc32(0x0d4a1185), R"(Crc32 Add function result is unexpected)");
  88. EXPECT_EQ(AZ::Crc32(0x0d4a1185), addResult);
  89. }
  90. TEST_F(Crc32Fixture, CrcConstevalMacro_IsConstexpr)
  91. {
  92. AZ::Crc32 constEvalLiteralValue = AZ_CRC_CE("Hello");
  93. static_assert(AZ_CRC_CE("Hello") == AZ::Crc32(0x3610a686));
  94. EXPECT_EQ(AZ::Crc32(0x3610a686), constEvalLiteralValue);
  95. AZ::Crc32 constEvalIntValue = AZ_CRC_CE(0x4727dc92);
  96. static_assert(AZ_CRC_CE(0x4727dc92) == AZ::Crc32(0x4727dc92));
  97. EXPECT_EQ(AZ::Crc32(0x4727dc92), constEvalIntValue);
  98. }
  99. }