BitArrayTest.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // BitArrayTest.cs - NUnit Test Cases for the System.Collections.BitArray class
  3. //
  4. // Author: David Menestrina ([email protected])
  5. //
  6. using NUnit.Framework;
  7. using System.Collections;
  8. using System;
  9. namespace MonoTests.System.Collections
  10. {
  11. public class BitArrayTest : TestCase
  12. {
  13. private BitArray testBa;
  14. private bool [] testPattern;
  15. private BitArray op1;
  16. private BitArray op2;
  17. private void verifyPattern(BitArray ba, bool[] pattern)
  18. {
  19. AssertEquals(ba.Length, pattern.Length);
  20. for (int i = 0; i < pattern.Length; i++)
  21. AssertEquals(ba[i], pattern[i]);
  22. }
  23. protected override void SetUp()
  24. {
  25. testPattern = new bool[70];
  26. int i;
  27. for(i = 0; i < testPattern.Length/2; i++)
  28. testPattern[i] = ((i % 2) == 0);
  29. for(; i < testPattern.Length; i++)
  30. testPattern[i] = ((i % 2) != 0);
  31. testBa = new BitArray(70);
  32. for(i = 0; i < testBa.Length/2; i++)
  33. testBa[i] = ((i % 2) == 0);
  34. for(; i < testBa.Length; i++)
  35. testBa[i] = ((i % 2) != 0);
  36. // for TestAnd, TestOr, TestNot, TestXor
  37. op1 = new BitArray(new int[] { 0x33333333, 0x33333333 });
  38. op2 = new BitArray(new int[] { 0x66666666, 0x66666666 });
  39. }
  40. public void TestBoolConstructor()
  41. {
  42. BitArray ba = new BitArray(testPattern);
  43. verifyPattern(ba, testPattern);
  44. }
  45. public void TestCopyConstructor()
  46. {
  47. BitArray ba = new BitArray(testBa);
  48. verifyPattern(ba, testPattern);
  49. }
  50. public void TestByteConstructor()
  51. {
  52. byte [] byteArr = new byte[] { 0xaa, 0x55, 0xaa, 0x55, 0x80 };
  53. BitArray ba = new BitArray(byteArr);
  54. AssertEquals("Lengths not equal", ba.Length, byteArr.Length * 8);
  55. // spot check
  56. Assert("7 not true", ba[7]);
  57. Assert("6 not false", !ba[6]);
  58. Assert("15 not false", !ba[15]);
  59. Assert("14 not true", ba[14]);
  60. Assert("39 not true", ba[39]);
  61. Assert("35 not false", !ba[35]);
  62. }
  63. public void TestIntConstructor()
  64. {
  65. int [] intArr = new int[] { ~0x55555555, 0x55555551 };
  66. BitArray ba = new BitArray(intArr);
  67. AssertEquals(ba.Length, intArr.Length * 32);
  68. // spot check
  69. Assert(ba[31]);
  70. Assert(!ba[30]);
  71. Assert(!ba[63]);
  72. Assert(ba[62]);
  73. Assert(ba[32]);
  74. Assert(!ba[35]);
  75. }
  76. public void TestValConstructor()
  77. {
  78. BitArray ba = new BitArray(64, false);
  79. AssertEquals(ba.Length, 64);
  80. foreach (bool b in ba)
  81. Assert(!b);
  82. ba = new BitArray(64, true);
  83. AssertEquals(ba.Length, 64);
  84. foreach (bool b in ba)
  85. Assert(b);
  86. }
  87. public void TestClone()
  88. {
  89. BitArray ba = (BitArray)testBa.Clone();
  90. verifyPattern(ba, testPattern);
  91. // ensure that changes in ba don't get propagated to testBa
  92. ba[0] = false;
  93. ba[1] = false;
  94. ba[2] = false;
  95. verifyPattern(testBa, testPattern);
  96. }
  97. public void TestSetLength()
  98. {
  99. int origLen = testBa.Length;
  100. testBa.Length += 33;
  101. AssertEquals(origLen + 33, testBa.Length);
  102. for (int i = origLen; i < testBa.Length; i++)
  103. testBa[i] = true;
  104. testBa.Length -= 33;
  105. verifyPattern(testBa, testPattern);
  106. }
  107. public void TestAnd()
  108. {
  109. BitArray result = op1.And(op2);
  110. AssertEquals(result.Length, op1.Length);
  111. for (int i = 0; i < result.Length; )
  112. {
  113. Assert(!result[i++]);
  114. Assert(result[i++]);
  115. Assert(!result[i++]);
  116. Assert(!result[i++]);
  117. }
  118. }
  119. public void TestOr()
  120. {
  121. BitArray result = op1.Or(op2);
  122. AssertEquals(result.Length, op1.Length);
  123. for (int i = 0; i < result.Length; )
  124. {
  125. Assert(result[i++]);
  126. Assert(result[i++]);
  127. Assert(result[i++]);
  128. Assert(!result[i++]);
  129. }
  130. }
  131. public void TestNot()
  132. {
  133. BitArray result = op1.Not();
  134. AssertEquals(result.Length, op1.Length);
  135. for (int i = 0; i < result.Length; )
  136. {
  137. Assert(!result[i++]);
  138. Assert(!result[i++]);
  139. Assert(result[i++]);
  140. Assert(result[i++]);
  141. }
  142. }
  143. public void TestXor()
  144. {
  145. BitArray result = op1.Xor(op2);
  146. AssertEquals(result.Length, op1.Length);
  147. for (int i = 0; i < result.Length; )
  148. {
  149. Assert(result[i++]);
  150. Assert(!result[i++]);
  151. Assert(result[i++]);
  152. Assert(!result[i++]);
  153. }
  154. }
  155. public void TestSetAll()
  156. {
  157. testBa.SetAll(false);
  158. foreach(bool b in testBa)
  159. Assert(!b);
  160. testBa.SetAll(true);
  161. foreach(bool b in testBa)
  162. Assert(b);
  163. }
  164. public void TestCopyToBool()
  165. {
  166. try {
  167. bool[] barray = new bool[testBa.Length + 10];
  168. testBa.CopyTo(barray, 5);
  169. for (int i = 0; i < testBa.Length; i++)
  170. AssertEquals(testBa[i], barray[i+5]);
  171. }
  172. catch(Exception e){
  173. Fail("Unexpected exception thrown: " + e.ToString());
  174. }
  175. }
  176. public void TestCopyToByte()
  177. {
  178. try {
  179. testBa.Length = 34;
  180. byte[] barray = new byte[5 + 10];
  181. testBa.CopyTo(barray, 5);
  182. for (int i = 5; i < 9; i++)
  183. AssertEquals(0x55, barray[i] & 0xff);
  184. // FIXME: MS fails on the next line. This is because
  185. // we truncated testBa.Length, and MS's internal array still
  186. // has the old bits set. CopyTo() doesn't say specifically
  187. // whether the "junk" bits (bits past Length, but within Length
  188. // rounded up to 32) will be copied as 0, or if those bits are
  189. // undefined.
  190. //AssertEquals(0x01, barray[9] & 0xff);
  191. }
  192. catch(Exception e){
  193. Fail("Unexpected exception thrown: " + e.ToString());
  194. }
  195. }
  196. public void TestCopyToInt()
  197. {
  198. try {
  199. testBa.Length = 34;
  200. int[] iarray = new int[2 + 10];
  201. testBa.CopyTo(iarray, 5);
  202. AssertEquals(0x55555555, iarray[5]);
  203. // FIXME: Same thing here as in TestCopyToByte
  204. //AssertEquals(0x01, iarray[6]);
  205. }
  206. catch(Exception e){
  207. Fail("Unexpected exception thrown: " + e.ToString());
  208. }
  209. }
  210. public void TestEnumerator()
  211. {
  212. try {
  213. IEnumerator e = testBa.GetEnumerator();
  214. for (int i = 0; e.MoveNext(); i++)
  215. AssertEquals(e.Current, testPattern[i]);
  216. Assert(!e.MoveNext());
  217. // read, to make sure reading isn't considered a write.
  218. bool b = testBa[0];
  219. e.Reset();
  220. for (int i = 0; e.MoveNext(); i++)
  221. AssertEquals(e.Current, testPattern[i]);
  222. try
  223. {
  224. e.Reset();
  225. testBa[0] = !testBa[0];
  226. e.MoveNext();
  227. Fail("IEnumerator.MoveNext() should throw when collection modified.");
  228. }
  229. catch (InvalidOperationException)
  230. {
  231. }
  232. }
  233. catch(Exception ex){
  234. Fail("Unexpected exception thrown: " + ex.ToString());
  235. }
  236. }
  237. }
  238. }