ArrayHelperTest.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using NUnit.Framework;
  2. namespace Unity.RenderStreaming.RuntimeTest
  3. {
  4. class ArrayHelpersTest
  5. {
  6. [Test]
  7. public void LengthSafe()
  8. {
  9. var array = new int[] { 1, 2, 3 };
  10. Assert.That(ArrayHelpers.LengthSafe(array), Is.EqualTo(3));
  11. array = null;
  12. Assert.That(ArrayHelpers.LengthSafe(array), Is.EqualTo(0));
  13. }
  14. [Test]
  15. public void Append()
  16. {
  17. var array = new int[] { 1, 2, 3 };
  18. ArrayHelpers.Append(ref array, 4);
  19. Assert.That(array, Is.EqualTo(new int[] { 1, 2, 3, 4 }));
  20. array = null;
  21. ArrayHelpers.Append(ref array, 1);
  22. Assert.That(array, Is.EqualTo(new int[] { 1 }));
  23. }
  24. [Test]
  25. public void AppendArray()
  26. {
  27. var array = new int[] { 1, 2, 3 };
  28. var array2 = new int[] { 4, 5 };
  29. ArrayHelpers.Append(ref array, array2);
  30. Assert.That(array, Is.EqualTo(new int[] { 1, 2, 3, 4, 5 }));
  31. array = null;
  32. ArrayHelpers.Append(ref array, array2);
  33. Assert.That(array, Is.EqualTo(new int[] { 4, 5 }));
  34. }
  35. [Test]
  36. public void IndexOf()
  37. {
  38. var array = new int[] { 1, 2, 3 };
  39. Assert.That(ArrayHelpers.IndexOf(array, 2), Is.EqualTo(1));
  40. Assert.That(ArrayHelpers.IndexOf(array, 4), Is.EqualTo(-1));
  41. array = null;
  42. Assert.That(ArrayHelpers.IndexOf(array, 2), Is.EqualTo(-1));
  43. }
  44. [Test]
  45. public void Erase()
  46. {
  47. var array = new int[] { 1, 2, 3 };
  48. Assert.That(ArrayHelpers.Erase(ref array, 2), Is.True);
  49. Assert.That(array, Is.EqualTo(new int[] { 1, 3 }));
  50. Assert.That(ArrayHelpers.Erase(ref array, 2), Is.False);
  51. Assert.That(array, Is.EqualTo(new int[] { 1, 3 }));
  52. array = null;
  53. Assert.That(ArrayHelpers.Erase(ref array, 2), Is.False);
  54. Assert.That(array, Is.Null);
  55. }
  56. [Test]
  57. public void EraseAt()
  58. {
  59. var array = new int[] { 1, 2, 3 };
  60. ArrayHelpers.EraseAt(ref array, 1);
  61. Assert.That(array, Is.EqualTo(new int[] { 1, 3 }));
  62. array = new int[] { 1 };
  63. ArrayHelpers.EraseAt(ref array, 0);
  64. Assert.That(array, Is.Null);
  65. }
  66. [Test]
  67. public void PutAtIfNotSet()
  68. {
  69. var array = new int[] { 1, 2, 3 };
  70. ArrayHelpers.PutAtIfNotSet(ref array, 3, () => { return 4; });
  71. Assert.That(array, Is.EqualTo(new int[] { 1, 2, 3, 4 }));
  72. }
  73. }
  74. }