MultiplayerComponentTests.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include <CommonNetworkEntitySetup.h>
  8. #include <MockInterfaces.h>
  9. #include <AzCore/UnitTest/TestTypes.h>
  10. #include <AzCore/UnitTest/UnitTest.h>
  11. #include <AzNetworking/Serialization/StringifySerializer.h>
  12. #include <AzTest/AzTest.h>
  13. #include <Multiplayer/Components/MultiplayerComponent.h>
  14. namespace Multiplayer
  15. {
  16. using MultiplayerComponentTests = NetworkEntityTests;
  17. TEST_F(MultiplayerComponentTests, SerializeNetworkPropertyHelperArrayCreatesUniqueEntriesForEachValue)
  18. {
  19. constexpr size_t NumTestEntries = 5;
  20. AzNetworking::StringifySerializer serializer;
  21. AzNetworking::FixedSizeVectorBitset<NumTestEntries> bitset;
  22. AZStd::array<int32_t, NumTestEntries> testValues = { 5, 10, 15, 20, 25 };
  23. NetComponentId componentId = aznumeric_cast<NetComponentId>(0);
  24. PropertyIndex propertyIndex = aznumeric_cast<PropertyIndex>(0);
  25. MultiplayerStats stats;
  26. // Mark all the values as changed so that they get serialized.
  27. bitset.AddBits(NumTestEntries);
  28. for (uint32_t index = 0; index < NumTestEntries; index++)
  29. {
  30. bitset.SetBit(index, true);
  31. }
  32. AzNetworking::FixedSizeBitsetView bitsetView(bitset, 0, NumTestEntries);
  33. SerializeNetworkPropertyHelperArray(serializer, bitsetView, testValues, componentId, propertyIndex, stats);
  34. // Each entry in the array should have been serialized to a unique key/value pair.
  35. auto valueMap = serializer.GetValueMap();
  36. EXPECT_EQ(valueMap.size(), NumTestEntries);
  37. }
  38. TEST_F(MultiplayerComponentTests, SerializeNetworkPropertyHelperVectorCreatesUniqueEntriesForEachValue)
  39. {
  40. constexpr size_t NumTestEntries = 5;
  41. constexpr size_t NumTestEntriesPlusSize = NumTestEntries + 1;
  42. AzNetworking::StringifySerializer serializer;
  43. // We need an extra bit for tracking the currently-used size of the fixed_vector.
  44. AzNetworking::FixedSizeVectorBitset<NumTestEntriesPlusSize> bitset;
  45. AZStd::fixed_vector<int32_t, NumTestEntries> testValues = { 5, 10, 15, 20, 25 };
  46. NetComponentId componentId = aznumeric_cast<NetComponentId>(0);
  47. PropertyIndex propertyIndex = aznumeric_cast<PropertyIndex>(0);
  48. MultiplayerStats stats;
  49. // Mark all the values as changed (including "size") so that they get serialized.
  50. bitset.AddBits(NumTestEntriesPlusSize);
  51. for (uint32_t index = 0; index < NumTestEntriesPlusSize; index++)
  52. {
  53. bitset.SetBit(index, true);
  54. }
  55. AzNetworking::FixedSizeBitsetView bitsetView(bitset, 0, NumTestEntriesPlusSize);
  56. SerializeNetworkPropertyHelperVector(serializer, bitsetView, testValues, componentId, propertyIndex, stats);
  57. // Each entry in the fixed_vector should have been serialized to a unique key/value pair, along with an extra entry for "Size".
  58. auto valueMap = serializer.GetValueMap();
  59. EXPECT_EQ(valueMap.size(), NumTestEntriesPlusSize);
  60. }
  61. } // namespace Multiplayer