2 // BitArrayTest.cs - NUnit Test Cases for the System.Collections.BitArray class
4 // Author: David Menestrina (dmenest@yahoo.com)
8 using System.Collections;
11 namespace MonoTests.System.Collections
14 public class BitArrayTest
16 private BitArray testBa;
17 private bool [] testPattern;
21 private void verifyPattern(BitArray ba, bool[] pattern)
23 Assert.AreEqual (ba.Length, pattern.Length);
24 for (int i = 0; i < pattern.Length; i++)
25 Assert.AreEqual (ba[i], pattern[i]);
31 testPattern = new bool[70];
34 for(i = 0; i < testPattern.Length/2; i++)
35 testPattern[i] = ((i % 2) == 0);
36 for(; i < testPattern.Length; i++)
37 testPattern[i] = ((i % 2) != 0);
39 testBa = new BitArray(70);
40 for(i = 0; i < testBa.Length/2; i++)
41 testBa[i] = ((i % 2) == 0);
42 for(; i < testBa.Length; i++)
43 testBa[i] = ((i % 2) != 0);
45 // for TestAnd, TestOr, TestNot, TestXor
46 op1 = new BitArray(new int[] { 0x33333333, 0x33333333 });
47 op2 = new BitArray(new int[] { 0x66666666, 0x66666666 });
51 public void TestBoolConstructor()
53 BitArray ba = new BitArray(testPattern);
54 verifyPattern(ba, testPattern);
58 public void TestCopyConstructor()
60 BitArray ba = new BitArray(testBa);
62 verifyPattern(ba, testPattern);
66 public void TestByteConstructor()
68 byte [] byteArr = new byte[] { 0xaa, 0x55, 0xaa, 0x55, 0x80 };
69 BitArray ba = new BitArray(byteArr);
71 Assert.AreEqual (ba.Length, byteArr.Length * 8, "Lengths not equal");
74 Assert.IsTrue (ba[7], "7 not true");
75 Assert.IsTrue (!ba[6], "6 not false");
76 Assert.IsTrue (!ba[15], "15 not false");
77 Assert.IsTrue (ba[14], "14 not true");
78 Assert.IsTrue (ba[39], "39 not true");
79 Assert.IsTrue (!ba[35], "35 not false");
84 public void TestIntConstructor()
86 int [] intArr = new int[] { ~0x55555555, 0x55555551 };
87 BitArray ba = new BitArray(intArr);
89 Assert.AreEqual (ba.Length, intArr.Length * 32);
93 Assert.IsTrue (ba[31]);
94 Assert.IsTrue (!ba[30]);
95 Assert.IsTrue (!ba[63]);
96 Assert.IsTrue (ba[62]);
97 Assert.IsTrue (ba[32]);
98 Assert.IsTrue (!ba[35]);
102 public void TestValConstructor()
104 BitArray ba = new BitArray(64, false);
106 Assert.AreEqual (ba.Length, 64);
107 foreach (bool b in ba)
110 ba = new BitArray(64, true);
112 Assert.AreEqual (ba.Length, 64);
113 foreach (bool b in ba)
118 public void TestClone()
120 BitArray ba = (BitArray)testBa.Clone();
122 verifyPattern(ba, testPattern);
124 // ensure that changes in ba don't get propagated to testBa
129 verifyPattern(testBa, testPattern);
133 public void TestSetLength()
135 int origLen = testBa.Length;
138 Assert.AreEqual (origLen + 33, testBa.Length);
139 for (int i = origLen; i < testBa.Length; i++)
143 verifyPattern(testBa, testPattern);
147 public void TestAnd()
149 BitArray result = op1.And(op2);
150 Assert.AreEqual (result.Length, op1.Length);
151 for (int i = 0; i < result.Length; )
153 Assert.IsTrue (!result[i++]);
154 Assert.IsTrue (result[i++]);
155 Assert.IsTrue (!result[i++]);
156 Assert.IsTrue (!result[i++]);
163 BitArray result = op1.Or(op2);
164 Assert.AreEqual (result.Length, op1.Length);
165 for (int i = 0; i < result.Length; )
167 Assert.IsTrue (result[i++]);
168 Assert.IsTrue (result[i++]);
169 Assert.IsTrue (result[i++]);
170 Assert.IsTrue (!result[i++]);
175 public void TestNot()
177 BitArray result = op1.Not();
178 Assert.AreEqual (result.Length, op1.Length);
179 for (int i = 0; i < result.Length; )
181 Assert.IsTrue (!result[i++]);
182 Assert.IsTrue (!result[i++]);
183 Assert.IsTrue (result[i++]);
184 Assert.IsTrue (result[i++]);
189 public void TestXor()
191 BitArray result = op1.Xor(op2);
192 Assert.AreEqual (result.Length, op1.Length);
193 for (int i = 0; i < result.Length; )
195 Assert.IsTrue (result[i++]);
196 Assert.IsTrue (!result[i++]);
197 Assert.IsTrue (result[i++]);
198 Assert.IsTrue (!result[i++]);
203 public void TestSetAll()
205 testBa.SetAll(false);
206 foreach(bool b in testBa)
209 foreach(bool b in testBa)
214 public void TestCopyToBool()
217 bool[] barray = new bool[testBa.Length + 10];
219 testBa.CopyTo(barray, 5);
221 for (int i = 0; i < testBa.Length; i++)
222 Assert.AreEqual (testBa[i], barray[i+5]);
225 Assert.Fail ("Unexpected exception thrown: " + e.ToString());
230 public void CopyToEmptyEmpty () {
231 BitArray bitarray = new BitArray(0);
233 int[] intarray = new int[0];
235 bitarray.CopyTo(intarray, 0);
239 public void TestCopyToByte()
243 byte[] barray = new byte[5 + 10];
245 testBa.CopyTo(barray, 5);
247 for (int i = 5; i < 9; i++)
248 Assert.AreEqual (0x55, barray[i] & 0xff);
250 // FIXME: MS fails on the next line. This is because
251 // we truncated testBa.Length, and MS's internal array still
252 // has the old bits set. CopyTo() doesn't say specifically
253 // whether the "junk" bits (bits past Length, but within Length
254 // rounded up to 32) will be copied as 0, or if those bits are
256 //Assert.AreEqual (0x01, barray[9] & 0xff);
259 Assert.Fail ("Unexpected exception thrown: " + e.ToString());
264 public void TestCopyToInt()
268 int[] iarray = new int[2 + 10];
270 testBa.CopyTo(iarray, 5);
272 Assert.AreEqual (0x55555555, iarray[5]);
273 // FIXME: Same thing here as in TestCopyToByte
274 //Assert.AreEqual (0x01, iarray[6]);
277 Assert.Fail ("Unexpected exception thrown: " + e.ToString());
282 public void TestEnumerator()
286 IEnumerator e = testBa.GetEnumerator();
288 for (int i = 0; e.MoveNext(); i++)
289 Assert.AreEqual (e.Current, testPattern[i]);
291 Assert.IsTrue (!e.MoveNext());
292 // read, to make sure reading isn't considered a write.
296 for (int i = 0; e.MoveNext(); i++)
297 Assert.AreEqual (e.Current, testPattern[i]);
302 testBa[0] = !testBa[0];
304 Assert.Fail ("IEnumerator.MoveNext() should throw when collection modified.");
306 catch (InvalidOperationException)
311 Assert.Fail ("Unexpected exception thrown: " + ex.ToString());