2008-08-31 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / corlib / Test / System.Collections / BitArrayTest.cs
1 //
2 // BitArrayTest.cs - NUnit Test Cases for the System.Collections.BitArray class
3 // 
4 // Author: David Menestrina (dmenest@yahoo.com)
5 //
6
7 using NUnit.Framework;
8 using System.Collections;
9 using System;
10
11 namespace MonoTests.System.Collections
12 {
13
14 public class BitArrayTest : TestCase 
15 {
16   private BitArray testBa;
17   private bool [] testPattern;
18   private BitArray op1;
19   private BitArray op2;
20
21   private void verifyPattern(BitArray ba, bool[] pattern)
22   {
23     AssertEquals(ba.Length, pattern.Length);
24     for (int i = 0; i < pattern.Length; i++)
25       AssertEquals(ba[i], pattern[i]);
26   }
27
28   protected override void SetUp()
29   {
30     testPattern = new bool[70];
31
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);
37
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);
43
44     // for TestAnd, TestOr, TestNot, TestXor
45     op1 = new BitArray(new int[] { 0x33333333, 0x33333333 });
46     op2 = new BitArray(new int[] { 0x66666666, 0x66666666 });
47   }
48   
49   public void TestBoolConstructor()
50   {
51     BitArray ba = new BitArray(testPattern);
52     verifyPattern(ba, testPattern);
53   }
54
55   public void TestCopyConstructor() 
56   {
57     BitArray ba = new BitArray(testBa);
58
59     verifyPattern(ba, testPattern);
60   }
61
62   public void TestByteConstructor()
63   {
64     byte [] byteArr = new byte[] { 0xaa, 0x55, 0xaa, 0x55, 0x80 };
65     BitArray ba = new BitArray(byteArr);
66     
67     AssertEquals("Lengths not equal", ba.Length, byteArr.Length * 8);
68     
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]);
76
77   }
78
79   public void TestIntConstructor()
80   {
81     int [] intArr = new int[] { ~0x55555555, 0x55555551 };
82     BitArray ba = new BitArray(intArr);
83     
84     AssertEquals(ba.Length, intArr.Length * 32);
85     
86     // spot check
87     
88     Assert(ba[31]);
89     Assert(!ba[30]);
90     Assert(!ba[63]);
91     Assert(ba[62]);
92     Assert(ba[32]);
93     Assert(!ba[35]);
94   }
95
96   public void TestValConstructor()
97   {
98     BitArray ba = new BitArray(64, false);
99
100     AssertEquals(ba.Length, 64);
101     foreach (bool b in ba)
102       Assert(!b);
103
104     ba = new BitArray(64, true);
105
106     AssertEquals(ba.Length, 64);
107     foreach (bool b in ba)
108       Assert(b);
109   }
110
111   public void TestClone()
112   {
113     BitArray ba = (BitArray)testBa.Clone();
114
115     verifyPattern(ba, testPattern);
116
117     // ensure that changes in ba don't get propagated to testBa
118     ba[0] = false;
119     ba[1] = false;
120     ba[2] = false;
121     
122     verifyPattern(testBa, testPattern);
123   }
124   
125   public void TestSetLength()
126   {
127     int origLen = testBa.Length;
128     testBa.Length += 33;
129
130     AssertEquals(origLen + 33, testBa.Length);
131     for (int i = origLen; i < testBa.Length; i++)
132       testBa[i] = true;
133
134     testBa.Length -= 33;
135     verifyPattern(testBa, testPattern);
136   }
137
138   public void TestAnd()
139   {
140     BitArray result = op1.And(op2);
141     AssertEquals(result.Length, op1.Length);
142     for (int i = 0; i < result.Length; )
143     {
144       Assert(!result[i++]);
145       Assert(result[i++]);
146       Assert(!result[i++]);
147       Assert(!result[i++]);
148     }
149   }
150
151   public void TestOr()
152   {
153     BitArray result = op1.Or(op2);
154     AssertEquals(result.Length, op1.Length);
155     for (int i = 0; i < result.Length; )
156     {
157       Assert(result[i++]);
158       Assert(result[i++]);
159       Assert(result[i++]);
160       Assert(!result[i++]);
161     }
162   }
163
164   public void TestNot()
165   {
166     BitArray result = op1.Not();
167     AssertEquals(result.Length, op1.Length);
168     for (int i = 0; i < result.Length; )
169     {
170       Assert(!result[i++]);
171       Assert(!result[i++]);
172       Assert(result[i++]);
173       Assert(result[i++]);
174     }
175   }
176
177   public void TestXor()
178   {
179     BitArray result = op1.Xor(op2);
180     AssertEquals(result.Length, op1.Length);
181     for (int i = 0; i < result.Length; )
182     {
183       Assert(result[i++]);
184       Assert(!result[i++]);
185       Assert(result[i++]);
186       Assert(!result[i++]);
187     }
188   }
189
190   public void TestSetAll()
191   {
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);
198   }
199
200   public void TestCopyToBool()
201   {
202     try {
203             bool[] barray = new bool[testBa.Length + 10];
204             
205             testBa.CopyTo(barray, 5);
206
207             for (int i = 0; i < testBa.Length; i++)
208               AssertEquals(testBa[i], barray[i+5]);
209     }
210     catch(Exception e){
211         Fail("Unexpected exception thrown: " + e.ToString());
212     }
213   }
214
215   [Test]
216   public void CopyToEmptyEmpty () {
217           BitArray bitarray = new BitArray(0);
218
219           int[] intarray = new int[0];
220
221           bitarray.CopyTo(intarray, 0);
222   }
223
224   public void TestCopyToByte()
225   {
226     try {
227             testBa.Length = 34;
228             byte[] barray = new byte[5 + 10];
229             
230             testBa.CopyTo(barray, 5);
231
232             for (int i = 5; i < 9; i++)
233               AssertEquals(0x55, barray[i] & 0xff);
234
235             // FIXME: MS fails on the next line.  This is because
236             // we truncated testBa.Length, and MS's internal array still
237             // has the old bits set.  CopyTo() doesn't say specifically
238             // whether the "junk" bits (bits past Length, but within Length
239             // rounded up to 32) will be copied as 0, or if those bits are
240             // undefined.
241             //AssertEquals(0x01, barray[9] & 0xff);
242     }
243     catch(Exception e){
244         Fail("Unexpected exception thrown: " + e.ToString());
245     }
246   }
247
248   public void TestCopyToInt()
249   {
250     try {
251             testBa.Length = 34;
252             int[] iarray = new int[2 + 10];
253             
254             testBa.CopyTo(iarray, 5);
255
256             AssertEquals(0x55555555, iarray[5]);
257             // FIXME:  Same thing here as in TestCopyToByte
258             //AssertEquals(0x01, iarray[6]);
259     }
260     catch(Exception e){
261         Fail("Unexpected exception thrown: " + e.ToString());
262     }
263   }
264
265   public void TestEnumerator()
266   {
267     
268     try {
269             IEnumerator e = testBa.GetEnumerator();
270             
271             for (int i = 0; e.MoveNext(); i++)
272               AssertEquals(e.Current, testPattern[i]);
273
274             Assert(!e.MoveNext());
275             // read, to make sure reading isn't considered a write.
276             bool b = testBa[0];
277
278             e.Reset();
279             for (int i = 0; e.MoveNext(); i++)
280               AssertEquals(e.Current, testPattern[i]);
281
282             try
283             {
284               e.Reset();
285               testBa[0] = !testBa[0];
286               e.MoveNext();
287               Fail("IEnumerator.MoveNext() should throw when collection modified.");
288             }
289             catch (InvalidOperationException)
290             {
291             }
292     }
293     catch(Exception ex){
294         Fail("Unexpected exception thrown: " + ex.ToString());
295     }
296   }
297 }
298
299 }