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