1
0

DirectionRandomizerTest.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using Content.Shared;
  4. using NUnit.Framework;
  5. using Robust.Shared.Maths;
  6. using Robust.UnitTesting;
  7. namespace Content.Tests.Shared;
  8. [TestFixture]
  9. public sealed class DirectionRandomizerTest : RobustUnitTest
  10. {
  11. [Test]
  12. [TestCase(new[]
  13. {
  14. Direction.East,
  15. Direction.NorthEast,
  16. Direction.West,
  17. Direction.NorthWest,
  18. Direction.South,
  19. Direction.SouthWest,
  20. Direction.North,
  21. Direction.SouthEast,
  22. })]
  23. [TestCase(new[]
  24. {
  25. Direction.East,
  26. Direction.West,
  27. Direction.South,
  28. Direction.North,
  29. })]
  30. [TestCase(new[]
  31. {
  32. Direction.East,
  33. Direction.West,
  34. })]
  35. public void TestRandomization(Direction[] x)
  36. {
  37. var set = new HashSet<Direction>(x);
  38. var randomizer = new Span<Direction>(x);
  39. randomizer.Shuffle();
  40. foreach (var direction in randomizer)
  41. {
  42. if (set.Contains(direction))
  43. {
  44. set.Remove(direction);
  45. }
  46. else
  47. {
  48. // Asserts no double direction
  49. Assert.Fail("Post randomization the enumerator had repeated direction");
  50. }
  51. }
  52. // Because of above foreach this asserts
  53. // rand[1,2,3] - [1,2,3] == {}
  54. // i.e. randomized set minus original set is empty
  55. Assert.That(set.Count == 0, "Each element must appear once ");
  56. }
  57. }