McppBinderTests.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 <AzTest/AzTest.h>
  9. #include <AzCore/UnitTest/TestTypes.h>
  10. #include "Common/ShaderBuilderTestFixture.h"
  11. #include <CommonFiles/Preprocessor.h>
  12. namespace UnitTest
  13. {
  14. using namespace AZ;
  15. // The main purpose of this class is to test ShaderBuilder::McppBinder::Fprintf_StaticHinge()
  16. // Which has three common scenarios to validate.
  17. // 1- The formatted string is expected to yield less bytes than McppBinder::DefaultFprintfBufferSize.
  18. // 2- The formatted string is expected to yield exactly McppBinder::DefaultFprintfBufferSize number of bytes.
  19. // 3- The formatted string is expectedc to yield more bytes than McppBinder::DefaultFprintfBufferSize.
  20. class McppBinderTests : public ShaderBuilderTestFixture
  21. {
  22. public:
  23. // Fills @buffer with 'a' to 'z' for up to @bufferSize number of bytes.
  24. // This function will null('\0') char terminate @buffer.
  25. void FillBufferWithAlphabet(char* buffer, int bufferSize)
  26. {
  27. for (int bufferPos = 0, rollback = 0; bufferPos < (bufferSize - 1); ++bufferPos)
  28. {
  29. const char value = 'a' + static_cast<char>(rollback++);
  30. buffer[bufferPos] = value;
  31. if (value == 'z')
  32. {
  33. rollback = 0;
  34. }
  35. }
  36. buffer[bufferSize - 1] = '\0';
  37. }
  38. // Pushes the null terminated string, @inputString, into McppBinder capture stream
  39. // using McppBinder::Fprintf_StaticHinge().
  40. // Returns the content of the McppBinder capture stream as a string.
  41. AZStd::string PrintStringThroughStaticHinge(const char* inputString)
  42. {
  43. ShaderBuilder::PreprocessorData preprocessorData;
  44. ShaderBuilder::McppBinder mcppBinder(preprocessorData, false);
  45. ShaderBuilder::McppBinder::Fprintf_StaticHinge(MCPP_OUTDEST::MCPP_OUT, "%s", inputString);
  46. // convert from std::ostringstring to AZStd::string
  47. return AZStd::string(mcppBinder.m_outStream.str().c_str());
  48. }
  49. }; // class McppBinderTests
  50. TEST_F(McppBinderTests, ShouldPrintLessBytesThanDefaultSize)
  51. {
  52. constexpr int bufferSize = (ShaderBuilder::McppBinder::DefaultFprintfBufferSize / 2) + 1;
  53. EXPECT_TRUE(bufferSize > 0);
  54. char buffer[bufferSize] = "";
  55. FillBufferWithAlphabet(buffer, bufferSize);
  56. auto printedString = PrintStringThroughStaticHinge(buffer);
  57. EXPECT_EQ(AZStd::string(buffer), printedString);
  58. }
  59. TEST_F(McppBinderTests, ShouldPrintSameBytesAsDefaultSize)
  60. {
  61. constexpr int bufferSize = ShaderBuilder::McppBinder::DefaultFprintfBufferSize + 1;
  62. EXPECT_TRUE(bufferSize > 0);
  63. char buffer[bufferSize] = "";
  64. FillBufferWithAlphabet(buffer, bufferSize);
  65. auto printedString = PrintStringThroughStaticHinge(buffer);
  66. EXPECT_EQ(AZStd::string(buffer), printedString);
  67. }
  68. TEST_F(McppBinderTests, ShouldPrintMoreBytesThanDefaultSize)
  69. {
  70. constexpr int bufferSize = (ShaderBuilder::McppBinder::DefaultFprintfBufferSize * 2) + 1;
  71. EXPECT_TRUE(bufferSize > 0);
  72. char buffer[bufferSize] = "";
  73. FillBufferWithAlphabet(buffer, bufferSize);
  74. auto printedString = PrintStringThroughStaticHinge(buffer);
  75. EXPECT_EQ(AZStd::string(buffer), printedString);
  76. }
  77. } //namespace UnitTest
  78. //AZ_UNIT_TEST_HOOK(DEFAULT_UNIT_TEST_ENV);