2 // BitArrayTest.cs - NUnit Test Cases for the System.Collections.BitArray class
\r
4 // Author: David Menestrina (dmenest@yahoo.com)
\r
7 using NUnit.Framework;
\r
8 using System.Collections;
\r
11 namespace MonoTests.System.Collections
\r
14 public class BitArrayTest : TestCase
\r
16 private BitArray testBa;
\r
17 private bool [] testPattern;
\r
18 private BitArray op1;
\r
19 private BitArray op2;
\r
21 public static ITest Suite
\r
24 return new TestSuite(typeof(BitArrayTest));
\r
28 private void verifyPattern(BitArray ba, bool[] pattern)
\r
30 AssertEquals(ba.Length, pattern.Length);
\r
31 for (int i = 0; i < pattern.Length; i++)
\r
32 AssertEquals(ba[i], pattern[i]);
\r
35 public BitArrayTest() : base ("MonoTests.System.Collections.BitArrayTest testcase") {}
\r
36 public BitArrayTest( string name ) : base(name) { }
\r
38 protected override void SetUp()
\r
40 testPattern = new bool[70];
\r
43 for(i = 0; i < testPattern.Length/2; i++)
\r
44 testPattern[i] = ((i % 2) == 0);
\r
45 for(; i < testPattern.Length; i++)
\r
46 testPattern[i] = ((i % 2) != 0);
\r
48 testBa = new BitArray(70);
\r
49 for(i = 0; i < testBa.Length/2; i++)
\r
50 testBa[i] = ((i % 2) == 0);
\r
51 for(; i < testBa.Length; i++)
\r
52 testBa[i] = ((i % 2) != 0);
\r
54 // for TestAnd, TestOr, TestNot, TestXor
\r
55 op1 = new BitArray(new int[] { 0x33333333, 0x33333333 });
\r
56 op2 = new BitArray(new int[] { 0x66666666, 0x66666666 });
\r
59 public void TestBoolConstructor()
\r
61 BitArray ba = new BitArray(testPattern);
\r
62 verifyPattern(ba, testPattern);
\r
65 public void TestCopyConstructor()
\r
67 BitArray ba = new BitArray(testBa);
\r
69 verifyPattern(ba, testPattern);
\r
72 public void TestByteConstructor()
\r
74 byte [] byteArr = new byte[] { 0xaa, 0x55, 0xaa, 0x55, 0x80 };
\r
75 BitArray ba = new BitArray(byteArr);
\r
77 AssertEquals("Lengths not equal", ba.Length, byteArr.Length * 8);
\r
80 Assert("7 not true", ba[7]);
\r
81 Assert("6 not false", !ba[6]);
\r
82 Assert("15 not false", !ba[15]);
\r
83 Assert("14 not true", ba[14]);
\r
84 Assert("39 not true", ba[39]);
\r
85 Assert("35 not false", !ba[35]);
\r
89 public void TestIntConstructor()
\r
91 int [] intArr = new int[] { ~0x55555555, 0x55555551 };
\r
92 BitArray ba = new BitArray(intArr);
\r
94 AssertEquals(ba.Length, intArr.Length * 32);
\r
106 public void TestValConstructor()
\r
108 BitArray ba = new BitArray(64, false);
\r
110 AssertEquals(ba.Length, 64);
\r
111 foreach (bool b in ba)
\r
114 ba = new BitArray(64, true);
\r
116 AssertEquals(ba.Length, 64);
\r
117 foreach (bool b in ba)
\r
121 public void TestClone()
\r
123 BitArray ba = (BitArray)testBa.Clone();
\r
125 verifyPattern(ba, testPattern);
\r
127 // ensure that changes in ba don't get propagated to testBa
\r
132 verifyPattern(testBa, testPattern);
\r
135 public void TestSetLength()
\r
137 int origLen = testBa.Length;
\r
138 testBa.Length += 33;
\r
140 AssertEquals(origLen + 33, testBa.Length);
\r
141 for (int i = origLen; i < testBa.Length; i++)
\r
144 testBa.Length -= 33;
\r
145 verifyPattern(testBa, testPattern);
\r
148 public void TestAnd()
\r
150 BitArray result = op1.And(op2);
\r
151 AssertEquals(result.Length, op1.Length);
\r
152 for (int i = 0; i < result.Length; )
\r
154 Assert(!result[i++]);
\r
155 Assert(result[i++]);
\r
156 Assert(!result[i++]);
\r
157 Assert(!result[i++]);
\r
161 public void TestOr()
\r
163 BitArray result = op1.Or(op2);
\r
164 AssertEquals(result.Length, op1.Length);
\r
165 for (int i = 0; i < result.Length; )
\r
167 Assert(result[i++]);
\r
168 Assert(result[i++]);
\r
169 Assert(result[i++]);
\r
170 Assert(!result[i++]);
\r
174 public void TestNot()
\r
176 BitArray result = op1.Not();
\r
177 AssertEquals(result.Length, op1.Length);
\r
178 for (int i = 0; i < result.Length; )
\r
180 Assert(!result[i++]);
\r
181 Assert(!result[i++]);
\r
182 Assert(result[i++]);
\r
183 Assert(result[i++]);
\r
187 public void TestXor()
\r
189 BitArray result = op1.Xor(op2);
\r
190 AssertEquals(result.Length, op1.Length);
\r
191 for (int i = 0; i < result.Length; )
\r
193 Assert(result[i++]);
\r
194 Assert(!result[i++]);
\r
195 Assert(result[i++]);
\r
196 Assert(!result[i++]);
\r
200 public void TestSetAll()
\r
202 testBa.SetAll(false);
\r
203 foreach(bool b in testBa)
\r
205 testBa.SetAll(true);
\r
206 foreach(bool b in testBa)
\r
210 public void TestCopyToBool()
\r
213 bool[] barray = new bool[testBa.Length + 10];
\r
215 testBa.CopyTo(barray, 5);
\r
217 for (int i = 0; i < testBa.Length; i++)
\r
218 AssertEquals(testBa[i], barray[i+5]);
\r
220 catch(Exception e){
\r
221 Fail("Unexpected exception thrown: " + e.ToString());
\r
225 public void TestCopyToByte()
\r
228 testBa.Length = 34;
\r
229 byte[] barray = new byte[5 + 10];
\r
231 testBa.CopyTo(barray, 5);
\r
233 for (int i = 5; i < 9; i++)
\r
234 AssertEquals(0x55, barray[i] & 0xff);
\r
236 // FIXME: MS fails on the next line. This is because
\r
237 // we truncated testBa.Length, and MS's internal array still
\r
238 // has the old bits set. CopyTo() doesn't say specifically
\r
239 // whether the "junk" bits (bits past Length, but within Length
\r
240 // rounded up to 32) will be copied as 0, or if those bits are
\r
242 //AssertEquals(0x01, barray[9] & 0xff);
\r
244 catch(Exception e){
\r
245 Fail("Unexpected exception thrown: " + e.ToString());
\r
249 public void TestCopyToInt()
\r
252 testBa.Length = 34;
\r
253 int[] iarray = new int[2 + 10];
\r
255 testBa.CopyTo(iarray, 5);
\r
257 AssertEquals(0x55555555, iarray[5]);
\r
258 // FIXME: Same thing here as in TestCopyToByte
\r
259 //AssertEquals(0x01, iarray[6]);
\r
261 catch(Exception e){
\r
262 Fail("Unexpected exception thrown: " + e.ToString());
\r
266 public void TestEnumerator()
\r
270 IEnumerator e = testBa.GetEnumerator();
\r
272 for (int i = 0; e.MoveNext(); i++)
\r
273 AssertEquals(e.Current, testPattern[i]);
\r
275 Assert(!e.MoveNext());
\r
276 // read, to make sure reading isn't considered a write.
\r
277 bool b = testBa[0];
\r
280 for (int i = 0; e.MoveNext(); i++)
\r
281 AssertEquals(e.Current, testPattern[i]);
\r
286 testBa[0] = !testBa[0];
\r
288 Fail("IEnumerator.MoveNext() should throw when collection modified.");
\r
290 catch (InvalidOperationException)
\r
294 catch(Exception ex){
\r
295 Fail("Unexpected exception thrown: " + ex.ToString());
\r