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