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