New test.
[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   public void TestCopyToByte()
216   {
217     try {
218             testBa.Length = 34;
219             byte[] barray = new byte[5 + 10];
220             
221             testBa.CopyTo(barray, 5);
222
223             for (int i = 5; i < 9; i++)
224               AssertEquals(0x55, barray[i] & 0xff);
225
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);
233     }
234     catch(Exception e){
235         Fail("Unexpected exception thrown: " + e.ToString());
236     }
237   }
238
239   public void TestCopyToInt()
240   {
241     try {
242             testBa.Length = 34;
243             int[] iarray = new int[2 + 10];
244             
245             testBa.CopyTo(iarray, 5);
246
247             AssertEquals(0x55555555, iarray[5]);
248             // FIXME:  Same thing here as in TestCopyToByte
249             //AssertEquals(0x01, iarray[6]);
250     }
251     catch(Exception e){
252         Fail("Unexpected exception thrown: " + e.ToString());
253     }
254   }
255
256   public void TestEnumerator()
257   {
258     
259     try {
260             IEnumerator e = testBa.GetEnumerator();
261             
262             for (int i = 0; e.MoveNext(); i++)
263               AssertEquals(e.Current, testPattern[i]);
264
265             Assert(!e.MoveNext());
266             // read, to make sure reading isn't considered a write.
267             bool b = testBa[0];
268
269             e.Reset();
270             for (int i = 0; e.MoveNext(); i++)
271               AssertEquals(e.Current, testPattern[i]);
272
273             try
274             {
275               e.Reset();
276               testBa[0] = !testBa[0];
277               e.MoveNext();
278               Fail("IEnumerator.MoveNext() should throw when collection modified.");
279             }
280             catch (InvalidOperationException)
281             {
282             }
283     }
284     catch(Exception ex){
285         Fail("Unexpected exception thrown: " + ex.ToString());
286     }
287   }
288 }
289
290 }