(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / Test / System.Collections / BitArrayTest.cs
blobe122119115e022fbd00bdd67450439e6556ea85b
1 //
2 // BitArrayTest.cs - NUnit Test Cases for the System.Collections.BitArray class
3 //
4 // Author: David Menestrina (dmenest@yahoo.com)
5 //
7 using NUnit.Framework;
8 using System.Collections;
9 using System;
11 namespace MonoTests.System.Collections
14 public class BitArrayTest : TestCase
16 private BitArray testBa;
17 private bool [] testPattern;
18 private BitArray op1;
19 private BitArray op2;
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];
32 int i;
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);
69 // spot check
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);
86 // spot check
88 Assert(ba[31]);
89 Assert(!ba[30]);
90 Assert(!ba[63]);
91 Assert(ba[62]);
92 Assert(ba[32]);
93 Assert(!ba[35]);
96 public void TestValConstructor()
98 BitArray ba = new BitArray(64, false);
100 AssertEquals(ba.Length, 64);
101 foreach (bool b in ba)
102 Assert(!b);
104 ba = new BitArray(64, true);
106 AssertEquals(ba.Length, 64);
107 foreach (bool b in ba)
108 Assert(b);
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
118 ba[0] = false;
119 ba[1] = false;
120 ba[2] = false;
122 verifyPattern(testBa, testPattern);
125 public void TestSetLength()
127 int origLen = testBa.Length;
128 testBa.Length += 33;
130 AssertEquals(origLen + 33, testBa.Length);
131 for (int i = origLen; i < testBa.Length; i++)
132 testBa[i] = true;
134 testBa.Length -= 33;
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++]);
145 Assert(result[i++]);
146 Assert(!result[i++]);
147 Assert(!result[i++]);
151 public void TestOr()
153 BitArray result = op1.Or(op2);
154 AssertEquals(result.Length, op1.Length);
155 for (int i = 0; i < result.Length; )
157 Assert(result[i++]);
158 Assert(result[i++]);
159 Assert(result[i++]);
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++]);
172 Assert(result[i++]);
173 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; )
183 Assert(result[i++]);
184 Assert(!result[i++]);
185 Assert(result[i++]);
186 Assert(!result[i++]);
190 public void TestSetAll()
192 testBa.SetAll(false);
193 foreach(bool b in testBa)
194 Assert(!b);
195 testBa.SetAll(true);
196 foreach(bool b in testBa)
197 Assert(b);
200 public void TestCopyToBool()
202 try {
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]);
210 catch(Exception e){
211 Fail("Unexpected exception thrown: " + e.ToString());
215 public void TestCopyToByte()
217 try {
218 testBa.Length = 34;
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
231 // undefined.
232 //AssertEquals(0x01, barray[9] & 0xff);
234 catch(Exception e){
235 Fail("Unexpected exception thrown: " + e.ToString());
239 public void TestCopyToInt()
241 try {
242 testBa.Length = 34;
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]);
251 catch(Exception e){
252 Fail("Unexpected exception thrown: " + e.ToString());
256 public void TestEnumerator()
259 try {
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.
267 bool b = testBa[0];
269 e.Reset();
270 for (int i = 0; e.MoveNext(); i++)
271 AssertEquals(e.Current, testPattern[i]);
275 e.Reset();
276 testBa[0] = !testBa[0];
277 e.MoveNext();
278 Fail("IEnumerator.MoveNext() should throw when collection modified.");
280 catch (InvalidOperationException)
284 catch(Exception ex){
285 Fail("Unexpected exception thrown: " + ex.ToString());