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( string name ) : base(name) { }
\r
37 protected override void SetUp()
\r
39 testPattern = new bool[70];
\r
42 for(i = 0; i < testPattern.Length/2; i++)
\r
43 testPattern[i] = ((i % 2) == 0);
\r
44 for(; i < testPattern.Length; i++)
\r
45 testPattern[i] = ((i % 2) != 0);
\r
47 testBa = new BitArray(70);
\r
48 for(i = 0; i < testBa.Length/2; i++)
\r
49 testBa[i] = ((i % 2) == 0);
\r
50 for(; i < testBa.Length; i++)
\r
51 testBa[i] = ((i % 2) != 0);
\r
53 // for TestAnd, TestOr, TestNot, TestXor
\r
54 op1 = new BitArray(new int[] { 0x33333333, 0x33333333 });
\r
55 op2 = new BitArray(new int[] { 0x66666666, 0x66666666 });
\r
58 public void TestBoolConstructor()
\r
60 BitArray ba = new BitArray(testPattern);
\r
61 verifyPattern(ba, testPattern);
\r
64 public void TestCopyConstructor()
\r
66 BitArray ba = new BitArray(testBa);
\r
68 verifyPattern(ba, testPattern);
\r
71 public void TestByteConstructor()
\r
73 byte [] byteArr = new byte[] { 0xaa, 0x55, 0xaa, 0x55, 0x80 };
\r
74 BitArray ba = new BitArray(byteArr);
\r
76 AssertEquals("Lengths not equal", ba.Length, byteArr.Length * 8);
\r
79 Assert("7 not true", ba[7]);
\r
80 Assert("6 not false", !ba[6]);
\r
81 Assert("15 not false", !ba[15]);
\r
82 Assert("14 not true", ba[14]);
\r
83 Assert("39 not true", ba[39]);
\r
84 Assert("35 not false", !ba[35]);
\r
88 public void TestIntConstructor()
\r
90 int [] intArr = new int[] { ~0x55555555, 0x55555551 };
\r
91 BitArray ba = new BitArray(intArr);
\r
93 AssertEquals(ba.Length, intArr.Length * 32);
\r
105 public void TestValConstructor()
\r
107 BitArray ba = new BitArray(64, false);
\r
109 AssertEquals(ba.Length, 64);
\r
110 foreach (bool b in ba)
\r
113 ba = new BitArray(64, true);
\r
115 AssertEquals(ba.Length, 64);
\r
116 foreach (bool b in ba)
\r
120 public void TestClone()
\r
122 BitArray ba = (BitArray)testBa.Clone();
\r
124 verifyPattern(ba, testPattern);
\r
126 // ensure that changes in ba don't get propagated to testBa
\r
131 verifyPattern(testBa, testPattern);
\r
134 public void TestSetLength()
\r
136 int origLen = testBa.Length;
\r
137 testBa.Length += 33;
\r
139 AssertEquals(origLen + 33, testBa.Length);
\r
140 for (int i = origLen; i < testBa.Length; i++)
\r
143 testBa.Length -= 33;
\r
144 verifyPattern(testBa, testPattern);
\r
147 public void TestAnd()
\r
149 BitArray result = op1.And(op2);
\r
150 AssertEquals(result.Length, op1.Length);
\r
151 for (int i = 0; i < result.Length; )
\r
153 Assert(!result[i++]);
\r
154 Assert(result[i++]);
\r
155 Assert(!result[i++]);
\r
156 Assert(!result[i++]);
\r
160 public void TestOr()
\r
162 BitArray result = op1.Or(op2);
\r
163 AssertEquals(result.Length, op1.Length);
\r
164 for (int i = 0; i < result.Length; )
\r
166 Assert(result[i++]);
\r
167 Assert(result[i++]);
\r
168 Assert(result[i++]);
\r
169 Assert(!result[i++]);
\r
173 public void TestNot()
\r
175 BitArray result = op1.Not();
\r
176 AssertEquals(result.Length, op1.Length);
\r
177 for (int i = 0; i < result.Length; )
\r
179 Assert(!result[i++]);
\r
180 Assert(!result[i++]);
\r
181 Assert(result[i++]);
\r
182 Assert(result[i++]);
\r
186 public void TestXor()
\r
188 BitArray result = op1.Xor(op2);
\r
189 AssertEquals(result.Length, op1.Length);
\r
190 for (int i = 0; i < result.Length; )
\r
192 Assert(result[i++]);
\r
193 Assert(!result[i++]);
\r
194 Assert(result[i++]);
\r
195 Assert(!result[i++]);
\r
199 public void TestSetAll()
\r
201 testBa.SetAll(false);
\r
202 foreach(bool b in testBa)
\r
204 testBa.SetAll(true);
\r
205 foreach(bool b in testBa)
\r
209 public void TestCopyToBool()
\r
212 bool[] barray = new bool[testBa.Length + 10];
\r
214 testBa.CopyTo(barray, 5);
\r
216 for (int i = 0; i < testBa.Length; i++)
\r
217 AssertEquals(testBa[i], barray[i+5]);
\r
219 catch(Exception e){
\r
220 Fail("Unexpected exception thrown: " + e.ToString());
\r
224 public void TestCopyToByte()
\r
227 testBa.Length = 34;
\r
228 byte[] barray = new byte[5 + 10];
\r
230 testBa.CopyTo(barray, 5);
\r
232 for (int i = 5; i < 9; i++)
\r
233 AssertEquals(0x55, barray[i] & 0xff);
\r
235 // FIXME: MS fails on the next line. This is because
\r
236 // we truncated testBa.Length, and MS's internal array still
\r
237 // has the old bits set. CopyTo() doesn't say specifically
\r
238 // whether the "junk" bits (bits past Length, but within Length
\r
239 // rounded up to 32) will be copied as 0, or if those bits are
\r
241 //AssertEquals(0x01, barray[9] & 0xff);
\r
243 catch(Exception e){
\r
244 Fail("Unexpected exception thrown: " + e.ToString());
\r
248 public void TestCopyToInt()
\r
251 testBa.Length = 34;
\r
252 int[] iarray = new int[2 + 10];
\r
254 testBa.CopyTo(iarray, 5);
\r
256 AssertEquals(0x55555555, iarray[5]);
\r
257 // FIXME: Same thing here as in TestCopyToByte
\r
258 //AssertEquals(0x01, iarray[6]);
\r
260 catch(Exception e){
\r
261 Fail("Unexpected exception thrown: " + e.ToString());
\r
265 public void TestEnumerator()
\r
269 IEnumerator e = testBa.GetEnumerator();
\r
271 for (int i = 0; e.MoveNext(); i++)
\r
272 AssertEquals(e.Current, testPattern[i]);
\r
274 Assert(!e.MoveNext());
\r
275 // read, to make sure reading isn't considered a write.
\r
276 bool b = testBa[0];
\r
279 for (int i = 0; e.MoveNext(); i++)
\r
280 AssertEquals(e.Current, testPattern[i]);
\r
285 testBa[0] = !testBa[0];
\r
287 Fail("IEnumerator.MoveNext() should throw when collection modified.");
\r
289 catch (InvalidOperationException)
\r
293 catch(Exception ex){
\r
294 Fail("Unexpected exception thrown: " + ex.ToString());
\r