RewindableContainerTests.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <Multiplayer/IMultiplayer.h>
  9. #include <Multiplayer/NetworkTime/RewindableArray.h>
  10. #include <Multiplayer/NetworkTime/RewindableFixedVector.h>
  11. #include <Multiplayer/NetworkTime/RewindableObject.h>
  12. #include <Source/NetworkTime/NetworkTime.h>
  13. #include <AzCore/Console/LoggerSystemComponent.h>
  14. #include <AzCore/Time/TimeSystem.h>
  15. #include <AzCore/UnitTest/TestTypes.h>
  16. namespace UnitTest
  17. {
  18. class RewindableContainerTests
  19. : public LeakDetectionFixture
  20. {
  21. public:
  22. Multiplayer::NetworkTime m_networkTime;
  23. AZ::LoggerSystemComponent m_loggerComponent;
  24. AZ::TimeSystem m_timeSystem;
  25. };
  26. static constexpr uint32_t RewindableContainerSize = 7;
  27. TEST_F(RewindableContainerTests, BasicVectorTest)
  28. {
  29. Multiplayer::RewindableFixedVector<uint32_t, RewindableContainerSize> test(0, 0);
  30. // Test push_back
  31. for (uint32_t idx = 0; idx < RewindableContainerSize; ++idx)
  32. {
  33. test.push_back(idx);
  34. EXPECT_EQ(idx, test[idx]);
  35. Multiplayer::GetNetworkTime()->IncrementHostFrameId();
  36. }
  37. // Test rewind for all pushed values and overall size
  38. for (uint32_t idx = 0; idx < RewindableContainerSize; ++idx)
  39. {
  40. Multiplayer::ScopedAlterTime time(static_cast<Multiplayer::HostFrameId>(idx), AZ::Time::ZeroTimeMs, 1.f, AzNetworking::InvalidConnectionId);
  41. EXPECT_EQ(idx + 1, test.size());
  42. EXPECT_EQ(idx, test.back());
  43. }
  44. // Test pop_back
  45. test.pop_back();
  46. EXPECT_EQ(RewindableContainerSize - 1, test.size());
  47. Multiplayer::GetNetworkTime()->IncrementHostFrameId();
  48. // Test iterator
  49. uint32_t iterCount = 0;
  50. auto iter = test.begin();
  51. while (iter != test.end())
  52. {
  53. ++iterCount;
  54. ++iter;
  55. }
  56. EXPECT_EQ(RewindableContainerSize - 1, iterCount);
  57. // Test clear and empty
  58. test.clear();
  59. EXPECT_EQ(0, test.size());
  60. Multiplayer::GetNetworkTime()->IncrementHostFrameId();
  61. EXPECT_TRUE(test.empty());
  62. // Test rewind for pop_back and clear
  63. Multiplayer::ScopedAlterTime pop_time(static_cast<Multiplayer::HostFrameId>(RewindableContainerSize), AZ::Time::ZeroTimeMs, 1.f, AzNetworking::InvalidConnectionId);
  64. EXPECT_EQ(RewindableContainerSize - 1, test.size());
  65. Multiplayer::ScopedAlterTime clear_time(static_cast<Multiplayer::HostFrameId>(RewindableContainerSize + 1), AZ::Time::ZeroTimeMs, 1.f, AzNetworking::InvalidConnectionId);
  66. EXPECT_EQ(0, test.size());
  67. // Test copy_values and resize_no_construct
  68. test.resize_no_construct(RewindableContainerSize);
  69. test.copy_values(&test[RewindableContainerSize-1], 1);
  70. EXPECT_EQ(1, test.size());
  71. test.resize_no_construct(RewindableContainerSize);
  72. EXPECT_EQ(test[0], test[RewindableContainerSize - 1]);
  73. }
  74. TEST_F(RewindableContainerTests, BasicArrayTest)
  75. {
  76. Multiplayer::RewindableArray<uint32_t, RewindableContainerSize> test;
  77. test.fill(0);
  78. Multiplayer::GetNetworkTime()->IncrementHostFrameId();
  79. // Test push_back
  80. for (uint32_t idx = 0; idx < RewindableContainerSize; ++idx)
  81. {
  82. test[idx] = idx;
  83. EXPECT_EQ(idx, test[idx].Get());
  84. Multiplayer::GetNetworkTime()->IncrementHostFrameId();
  85. }
  86. // Test rewind for all values and overall size
  87. for (uint32_t idx = 1; idx <= RewindableContainerSize; ++idx)
  88. {
  89. Multiplayer::ScopedAlterTime time(static_cast<Multiplayer::HostFrameId>(idx), AZ::Time::ZeroTimeMs, 1.f, AzNetworking::InvalidConnectionId);
  90. for (uint32_t testIdx = 0; testIdx < RewindableContainerSize; ++testIdx)
  91. {
  92. if (testIdx < idx)
  93. {
  94. EXPECT_EQ(testIdx, test[testIdx].Get());
  95. }
  96. else
  97. {
  98. EXPECT_EQ(0, test[testIdx].Get());
  99. }
  100. }
  101. }
  102. }
  103. }