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
: TestCase
16 private BitArray testBa
;
17 private bool [] testPattern
;
21 private void verifyPattern(BitArray ba
, bool[] pattern
)
23 AssertEquals(ba
.Length
, pattern
.Length
);
24 for (int i
= 0; i
< pattern
.Length
; i
++)
25 AssertEquals(ba
[i
], pattern
[i
]);
28 protected override void SetUp()
30 testPattern
= new bool[70];
33 for(i
= 0; i
< testPattern
.Length
/2; i
++)
34 testPattern
[i
] = ((i
% 2) == 0);
35 for(; i
< testPattern
.Length
; i
++)
36 testPattern
[i
] = ((i
% 2) != 0);
38 testBa
= new BitArray(70);
39 for(i
= 0; i
< testBa
.Length
/2; i
++)
40 testBa
[i
] = ((i
% 2) == 0);
41 for(; i
< testBa
.Length
; i
++)
42 testBa
[i
] = ((i
% 2) != 0);
44 // for TestAnd, TestOr, TestNot, TestXor
45 op1
= new BitArray(new int[] { 0x33333333, 0x33333333 }
);
46 op2
= new BitArray(new int[] { 0x66666666, 0x66666666 }
);
49 public void TestBoolConstructor()
51 BitArray ba
= new BitArray(testPattern
);
52 verifyPattern(ba
, testPattern
);
55 public void TestCopyConstructor()
57 BitArray ba
= new BitArray(testBa
);
59 verifyPattern(ba
, testPattern
);
62 public void TestByteConstructor()
64 byte [] byteArr
= new byte[] { 0xaa, 0x55, 0xaa, 0x55, 0x80 }
;
65 BitArray ba
= new BitArray(byteArr
);
67 AssertEquals("Lengths not equal", ba
.Length
, byteArr
.Length
* 8);
70 Assert("7 not true", ba
[7]);
71 Assert("6 not false", !ba
[6]);
72 Assert("15 not false", !ba
[15]);
73 Assert("14 not true", ba
[14]);
74 Assert("39 not true", ba
[39]);
75 Assert("35 not false", !ba
[35]);
79 public void TestIntConstructor()
81 int [] intArr
= new int[] { ~0x55555555, 0x55555551 }
;
82 BitArray ba
= new BitArray(intArr
);
84 AssertEquals(ba
.Length
, intArr
.Length
* 32);
96 public void TestValConstructor()
98 BitArray ba
= new BitArray(64, false);
100 AssertEquals(ba
.Length
, 64);
101 foreach (bool b
in ba
)
104 ba
= new BitArray(64, true);
106 AssertEquals(ba
.Length
, 64);
107 foreach (bool b
in ba
)
111 public void TestClone()
113 BitArray ba
= (BitArray
)testBa
.Clone();
115 verifyPattern(ba
, testPattern
);
117 // ensure that changes in ba don't get propagated to testBa
122 verifyPattern(testBa
, testPattern
);
125 public void TestSetLength()
127 int origLen
= testBa
.Length
;
130 AssertEquals(origLen
+ 33, testBa
.Length
);
131 for (int i
= origLen
; i
< testBa
.Length
; i
++)
135 verifyPattern(testBa
, testPattern
);
138 public void TestAnd()
140 BitArray result
= op1
.And(op2
);
141 AssertEquals(result
.Length
, op1
.Length
);
142 for (int i
= 0; i
< result
.Length
; )
144 Assert(!result
[i
++]);
146 Assert(!result
[i
++]);
147 Assert(!result
[i
++]);
153 BitArray result
= op1
.Or(op2
);
154 AssertEquals(result
.Length
, op1
.Length
);
155 for (int i
= 0; i
< result
.Length
; )
160 Assert(!result
[i
++]);
164 public void TestNot()
166 BitArray result
= op1
.Not();
167 AssertEquals(result
.Length
, op1
.Length
);
168 for (int i
= 0; i
< result
.Length
; )
170 Assert(!result
[i
++]);
171 Assert(!result
[i
++]);
177 public void TestXor()
179 BitArray result
= op1
.Xor(op2
);
180 AssertEquals(result
.Length
, op1
.Length
);
181 for (int i
= 0; i
< result
.Length
; )
184 Assert(!result
[i
++]);
186 Assert(!result
[i
++]);
190 public void TestSetAll()
192 testBa
.SetAll(false);
193 foreach(bool b
in testBa
)
196 foreach(bool b
in testBa
)
200 public void TestCopyToBool()
203 bool[] barray
= new bool[testBa
.Length
+ 10];
205 testBa
.CopyTo(barray
, 5);
207 for (int i
= 0; i
< testBa
.Length
; i
++)
208 AssertEquals(testBa
[i
], barray
[i
+5]);
211 Fail("Unexpected exception thrown: " + e
.ToString());
215 public void TestCopyToByte()
219 byte[] barray
= new byte[5 + 10];
221 testBa
.CopyTo(barray
, 5);
223 for (int i
= 5; i
< 9; i
++)
224 AssertEquals(0x55, barray
[i
] & 0xff);
226 // FIXME: MS fails on the next line. This is because
227 // we truncated testBa.Length, and MS's internal array still
228 // has the old bits set. CopyTo() doesn't say specifically
229 // whether the "junk" bits (bits past Length, but within Length
230 // rounded up to 32) will be copied as 0, or if those bits are
232 //AssertEquals(0x01, barray[9] & 0xff);
235 Fail("Unexpected exception thrown: " + e
.ToString());
239 public void TestCopyToInt()
243 int[] iarray
= new int[2 + 10];
245 testBa
.CopyTo(iarray
, 5);
247 AssertEquals(0x55555555, iarray
[5]);
248 // FIXME: Same thing here as in TestCopyToByte
249 //AssertEquals(0x01, iarray[6]);
252 Fail("Unexpected exception thrown: " + e
.ToString());
256 public void TestEnumerator()
260 IEnumerator e
= testBa
.GetEnumerator();
262 for (int i
= 0; e
.MoveNext(); i
++)
263 AssertEquals(e
.Current
, testPattern
[i
]);
265 Assert(!e
.MoveNext());
266 // read, to make sure reading isn't considered a write.
270 for (int i
= 0; e
.MoveNext(); i
++)
271 AssertEquals(e
.Current
, testPattern
[i
]);
276 testBa
[0] = !testBa
[0];
278 Fail("IEnumerator.MoveNext() should throw when collection modified.");
280 catch (InvalidOperationException
)
285 Fail("Unexpected exception thrown: " + ex
.ToString());