BitArrayTests.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using NUnit.Framework;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine.Rendering;
  5. namespace UnityEngine.Rendering.Tests
  6. {
  7. class BitArrayTests
  8. {
  9. BitArray8[] ba8;
  10. BitArray16[] ba16;
  11. BitArray32[] ba32;
  12. BitArray64[] ba64;
  13. BitArray128[] ba128;
  14. BitArray256[] ba256;
  15. static readonly uint[] aIndexes = new uint[] { 300, 200, 198, 100, 98, 60, 58, 30, 28, 10, 8, 4, 2, 0, 0 }; //double 0 entry to test double entry
  16. static readonly uint[] bIndexes = new uint[] { 300, 200, 199, 100, 99, 60, 59, 30, 29, 10, 9, 8, 5, 1, 0 };
  17. static readonly uint[] getSetTestedIndexes = new uint[] { 201, 200, 101, 100, 61, 60, 31, 30, 11, 10, 1, 0 }; // on a, odd value are false, even true
  18. const string aHumanized = "00000000.00000000.00000000.00000000.00000000.00000000.00000001.01000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00010100.00000000.00000000.00000000.00000000.00010100.00000000.00000000.00000000.01010000.00000000.00000101.00010101";
  19. const string bHumanized = "00000000.00000000.00000000.00000000.00000000.00000000.00000001.10000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00011000.00000000.00000000.00000000.00000000.00011000.00000000.00000000.00000000.01100000.00000000.00000111.00100011";
  20. const string aAndBHumanized = "00000000.00000000.00000000.00000000.00000000.00000000.00000001.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00010000.00000000.00000000.00000000.00000000.00010000.00000000.00000000.00000000.01000000.00000000.00000101.00000001";
  21. const string aOrBHumanized = "00000000.00000000.00000000.00000000.00000000.00000000.00000001.11000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00011100.00000000.00000000.00000000.00000000.00011100.00000000.00000000.00000000.01110000.00000000.00000111.00110111";
  22. const string notAHumanized = "11111111.11111111.11111111.11111111.11111111.11111111.11111110.10111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11101011.11111111.11111111.11111111.11111111.11101011.11111111.11111111.11111111.10101111.11111111.11111010.11101010";
  23. const string zeroHumanized = "00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000";
  24. const string maxHumanized = "11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111";
  25. [SetUp]
  26. public void SetUpBitArray()
  27. {
  28. ba8 = new[] { new BitArray8(), new BitArray8(aIndexes), new BitArray8(bIndexes), new BitArray8(byte.MaxValue) };
  29. ba16 = new[] { new BitArray16(), new BitArray16(aIndexes), new BitArray16(bIndexes), new BitArray16(ushort.MaxValue) };
  30. ba32 = new[] { new BitArray32(), new BitArray32(aIndexes), new BitArray32(bIndexes), new BitArray32(uint.MaxValue) };
  31. ba64 = new[] { new BitArray64(), new BitArray64(aIndexes), new BitArray64(bIndexes), new BitArray64(ulong.MaxValue) };
  32. ba128 = new[] { new BitArray128(), new BitArray128(aIndexes), new BitArray128(bIndexes), new BitArray128(ulong.MaxValue, ulong.MaxValue) };
  33. ba256 = new[] { new BitArray256(), new BitArray256(aIndexes), new BitArray256(bIndexes), new BitArray256(ulong.MaxValue, ulong.MaxValue, ulong.MaxValue, ulong.MaxValue) };
  34. }
  35. //[TearDown]
  36. //nothing to do as they are non static struct
  37. string GetLastHumanizedBits(string a, uint bitNumber) => a.Substring(a.Length - ((int)bitNumber + ((int)bitNumber - 1) / 8)); //handle '.' separators
  38. void TestBitArrayMethods<T>(T[] ba, uint capacity)
  39. where T : IBitArray
  40. {
  41. Assert.AreEqual(capacity, ba[0].capacity);
  42. Assert.AreEqual(true, ba[0].allFalse);
  43. Assert.AreEqual(false, ba[0].allTrue);
  44. var trimmedZeroHumanized = GetLastHumanizedBits(zeroHumanized, capacity);
  45. var humanized = ba[0].humanizedData;
  46. Assert.AreEqual(trimmedZeroHumanized, humanized);
  47. Assert.AreEqual(capacity, ba[1].capacity);
  48. Assert.AreEqual(false, ba[1].allFalse);
  49. Assert.AreEqual(false, ba[1].allTrue);
  50. var trimmedAHumanized = GetLastHumanizedBits(aHumanized, capacity);
  51. humanized = ba[1].humanizedData;
  52. Assert.AreEqual(trimmedAHumanized, humanized);
  53. Assert.AreEqual(capacity, ba[2].capacity);
  54. Assert.AreEqual(false, ba[2].allFalse);
  55. Assert.AreEqual(false, ba[2].allTrue);
  56. var trimmedBHumanized = GetLastHumanizedBits(bHumanized, capacity);
  57. humanized = ba[2].humanizedData;
  58. Assert.AreEqual(trimmedBHumanized, humanized);
  59. Assert.AreEqual(capacity, ba[3].capacity);
  60. Assert.AreEqual(false, ba[3].allFalse);
  61. Assert.AreEqual(true, ba[3].allTrue);
  62. var trimmedMaxHumnized = GetLastHumanizedBits(maxHumanized, capacity);
  63. humanized = ba[3].humanizedData;
  64. Assert.AreEqual(trimmedMaxHumnized, humanized);
  65. }
  66. void TestBitArrayOperator<T>(T[] ba)
  67. where T : IBitArray
  68. {
  69. //ensure we keep value type when refactoring
  70. var ba_4 = ba[1];
  71. Assert.AreEqual(ba_4, ba[1]);
  72. Assert.AreNotSame(ba_4, ba[1]);
  73. ba_4 = ba[2];
  74. Assert.AreNotEqual(ba_4, ba[1]);
  75. //test and
  76. var bAndA = ba[2].BitAnd(ba[1]);
  77. var aAndB = ba[1].BitAnd(ba[2]);
  78. Assert.AreEqual(bAndA, aAndB);
  79. Assert.AreEqual(bAndA.humanizedData, GetLastHumanizedBits(aAndBHumanized, ba[0].capacity));
  80. //test or
  81. var bOrA = ba[2].BitOr(ba[1]);
  82. var aOrB = ba[1].BitOr(ba[2]);
  83. Assert.AreEqual(bOrA, aOrB);
  84. Assert.AreEqual(bOrA.humanizedData, GetLastHumanizedBits(aOrBHumanized, ba[0].capacity));
  85. //test not
  86. var notA = ba[1].BitNot();
  87. Assert.AreEqual(notA.humanizedData, GetLastHumanizedBits(notAHumanized, ba[0].capacity));
  88. //test indexer
  89. foreach(uint index in getSetTestedIndexes.Where(i => i < ba[0].capacity))
  90. {
  91. //test get
  92. Assert.AreEqual(ba[1][index], (index & 1) == 0); //on a, odd value are false and even true
  93. //test set
  94. var bai = ba[1];
  95. bai[index] = ba[1][index];
  96. Assert.AreEqual(ba[1], bai);
  97. bai[index] = !ba[1][index];
  98. Assert.AreNotEqual(ba[1], bai);
  99. Assert.AreEqual(bai[index], !ba[1][index]);
  100. }
  101. }
  102. [Test]
  103. public void TestBitArray8()
  104. {
  105. TestBitArrayMethods(ba8, 8u);
  106. TestBitArrayOperator(ba8);
  107. }
  108. [Test]
  109. public void TestBitArray16()
  110. {
  111. TestBitArrayMethods(ba16, 16u);
  112. TestBitArrayOperator(ba16);
  113. }
  114. [Test]
  115. public void TestBitArray32()
  116. {
  117. TestBitArrayMethods(ba32, 32u);
  118. TestBitArrayOperator(ba32);
  119. }
  120. [Test]
  121. public void TestBitArray64()
  122. {
  123. TestBitArrayMethods(ba64, 64u);
  124. TestBitArrayOperator(ba64);
  125. }
  126. [Test]
  127. public void TestBitArray128()
  128. {
  129. TestBitArrayMethods(ba128, 128u);
  130. TestBitArrayOperator(ba128);
  131. }
  132. [Test]
  133. public void TestBitArray256()
  134. {
  135. TestBitArrayMethods(ba256, 256u);
  136. TestBitArrayOperator(ba256);
  137. }
  138. }
  139. }