Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BitFieldTests.cs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using NUnit.Framework;
  3. using Unity.Collections;
  4. using Unity.Collections.Tests;
  5. internal class BitFieldTests : CollectionsTestFixture
  6. {
  7. [Test]
  8. public void BitField32_Get_Set()
  9. {
  10. var test = new BitField32();
  11. uint bits;
  12. bits = test.GetBits(0, 32);
  13. Assert.AreEqual(0x0, bits);
  14. test.SetBits(0, true);
  15. bits = test.GetBits(0, 32);
  16. Assert.AreEqual(0x1, bits);
  17. test.SetBits(0, true, 32);
  18. bits = test.GetBits(0, 32);
  19. Assert.AreEqual(0xffffffff, bits);
  20. Assert.IsTrue(test.TestAll(0, 32));
  21. test.SetBits(0, false, 32);
  22. bits = test.GetBits(0, 32);
  23. Assert.AreEqual(0x0, bits);
  24. test.SetBits(15, true, 7);
  25. Assert.IsTrue(test.TestAll(15, 7));
  26. test.SetBits(3, true, 3);
  27. Assert.IsTrue(test.TestAll(3, 3));
  28. bits = test.GetBits(0, 32);
  29. Assert.AreEqual(0x3f8038, bits);
  30. bits = test.GetBits(0, 15);
  31. Assert.AreEqual(0x38, bits);
  32. Assert.IsFalse(test.TestNone(0, 32));
  33. Assert.IsFalse(test.TestAll(0, 32));
  34. Assert.IsTrue(test.TestAny(0, 32));
  35. }
  36. [Test]
  37. public void BitField32_Count_Leading_Trailing()
  38. {
  39. var test = new BitField32();
  40. Assert.AreEqual(0, test.CountBits());
  41. Assert.AreEqual(32, test.CountLeadingZeros());
  42. Assert.AreEqual(32, test.CountTrailingZeros());
  43. test.SetBits(31, true);
  44. Assert.AreEqual(1, test.CountBits());
  45. Assert.AreEqual(0, test.CountLeadingZeros());
  46. Assert.AreEqual(31, test.CountTrailingZeros());
  47. test.SetBits(0, true);
  48. Assert.AreEqual(2, test.CountBits());
  49. Assert.AreEqual(0, test.CountLeadingZeros());
  50. Assert.AreEqual(0, test.CountTrailingZeros());
  51. test.SetBits(31, false);
  52. Assert.AreEqual(1, test.CountBits());
  53. Assert.AreEqual(31, test.CountLeadingZeros());
  54. Assert.AreEqual(0, test.CountTrailingZeros());
  55. }
  56. [Test]
  57. [TestRequiresDotsDebugOrCollectionChecks]
  58. public void BitField32_Throws()
  59. {
  60. var test = new BitField32();
  61. for (byte i = 0; i < 32; ++i)
  62. {
  63. Assert.DoesNotThrow(() => { test.GetBits(i, (byte)(32 - i)); });
  64. }
  65. Assert.Throws<ArgumentException>(() => { test.GetBits(0, 33); });
  66. Assert.Throws<ArgumentException>(() => { test.GetBits(1, 32); });
  67. }
  68. [Test]
  69. public void BitField64_Get_Set()
  70. {
  71. var test = new BitField64();
  72. ulong bits;
  73. bits = test.GetBits(0, 64);
  74. Assert.AreEqual(0x0, bits);
  75. test.SetBits(0, true);
  76. bits = test.GetBits(0, 64);
  77. Assert.AreEqual(0x1, bits);
  78. test.SetBits(0, true, 64);
  79. bits = test.GetBits(0, 64);
  80. Assert.AreEqual(0xfffffffffffffffful, bits);
  81. Assert.IsTrue(test.TestAll(0, 64));
  82. test.SetBits(0, false, 64);
  83. bits = test.GetBits(0, 64);
  84. Assert.AreEqual(0x0ul, bits);
  85. test.SetBits(15, true, 7);
  86. Assert.IsTrue(test.TestAll(15, 7));
  87. test.SetBits(3, true, 3);
  88. Assert.IsTrue(test.TestAll(3, 3));
  89. bits = test.GetBits(0, 32);
  90. Assert.AreEqual(0x3f8038, bits);
  91. bits = test.GetBits(0, 15);
  92. Assert.AreEqual(0x38, bits);
  93. Assert.IsFalse(test.TestNone(0, 64));
  94. Assert.IsFalse(test.TestAll(0, 64));
  95. Assert.IsTrue(test.TestAny(0, 64));
  96. }
  97. [Test]
  98. [TestRequiresDotsDebugOrCollectionChecks]
  99. public void BitField64_Throws()
  100. {
  101. var test = new BitField64();
  102. for (byte i = 0; i < 64; ++i)
  103. {
  104. Assert.DoesNotThrow(() => { test.GetBits(i, (byte)(64 - i)); });
  105. }
  106. Assert.Throws<ArgumentException>(() => { test.GetBits(0, 65); });
  107. Assert.Throws<ArgumentException>(() => { test.GetBits(1, 64); });
  108. }
  109. [Test]
  110. public void BitField64_Count_Leading_Trailing()
  111. {
  112. var test = new BitField64();
  113. Assert.AreEqual(0, test.CountBits());
  114. Assert.AreEqual(64, test.CountLeadingZeros());
  115. Assert.AreEqual(64, test.CountTrailingZeros());
  116. test.SetBits(63, true);
  117. Assert.AreEqual(1, test.CountBits());
  118. Assert.AreEqual(0, test.CountLeadingZeros());
  119. Assert.AreEqual(63, test.CountTrailingZeros());
  120. test.SetBits(0, true);
  121. Assert.AreEqual(2, test.CountBits());
  122. Assert.AreEqual(0, test.CountLeadingZeros());
  123. Assert.AreEqual(0, test.CountTrailingZeros());
  124. test.SetBits(63, false);
  125. Assert.AreEqual(1, test.CountBits());
  126. Assert.AreEqual(63, test.CountLeadingZeros());
  127. Assert.AreEqual(0, test.CountTrailingZeros());
  128. }
  129. }