Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BitArrayTest.cs « System.Collections « Test « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7d29eb449113a871297d20cbd1258726b59e2e95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//
// BitArrayTest.cs - NUnit Test Cases for the System.Collections.BitArray class
// 
// Author: David Menestrina (dmenest@yahoo.com)
//

using NUnit.Framework;
using System.Collections;
using System;

namespace MonoTests.System.Collections
{

public class BitArrayTest : TestCase 
{
  private BitArray testBa;
  private bool [] testPattern;
  private BitArray op1;
  private BitArray op2;

  public static ITest Suite
  {
    get {
      return new TestSuite(typeof(BitArrayTest));
    }
  }

  private void verifyPattern(BitArray ba, bool[] pattern)
  {
    AssertEquals(ba.Length, pattern.Length);
    for (int i = 0; i < pattern.Length; i++)
      AssertEquals(ba[i], pattern[i]);
  }

  public BitArrayTest() : base ("MonoTests.System.Collections.BitArrayTest testcase") {}
  public BitArrayTest( string name ) : base(name) { }	
  
  protected override void SetUp()
  {
    testPattern = new bool[70];

    int i;
    for(i = 0; i < testPattern.Length/2; i++)
      testPattern[i] = ((i % 2) == 0);
    for(; i < testPattern.Length; i++)
      testPattern[i] = ((i % 2) != 0);

    testBa = new BitArray(70);
    for(i = 0; i < testBa.Length/2; i++)
      testBa[i] = ((i % 2) == 0);
    for(; i < testBa.Length; i++)
      testBa[i] = ((i % 2) != 0);

    // for TestAnd, TestOr, TestNot, TestXor
    op1 = new BitArray(new int[] { 0x33333333, 0x33333333 });
    op2 = new BitArray(new int[] { 0x66666666, 0x66666666 });
  }
  
  public void TestBoolConstructor()
  {
    BitArray ba = new BitArray(testPattern);
    verifyPattern(ba, testPattern);
  }

  public void TestCopyConstructor() 
  {
    BitArray ba = new BitArray(testBa);

    verifyPattern(ba, testPattern);
  }

  public void TestByteConstructor()
  {
    byte [] byteArr = new byte[] { 0xaa, 0x55, 0xaa, 0x55, 0x80 };
    BitArray ba = new BitArray(byteArr);
    
    AssertEquals("Lengths not equal", ba.Length, byteArr.Length * 8);
    
    // spot check
    Assert("7 not true", ba[7]);
    Assert("6 not false", !ba[6]);
    Assert("15 not false", !ba[15]);
    Assert("14 not true", ba[14]);
    Assert("39 not true", ba[39]);
    Assert("35 not false", !ba[35]);

  }

  public void TestIntConstructor()
  {
    int [] intArr = new int[] { ~0x55555555, 0x55555551 };
    BitArray ba = new BitArray(intArr);
    
    AssertEquals(ba.Length, intArr.Length * 32);
    
    // spot check
    
    Assert(ba[31]);
    Assert(!ba[30]);
    Assert(!ba[63]);
    Assert(ba[62]);
    Assert(ba[32]);
    Assert(!ba[35]);
  }

  public void TestValConstructor()
  {
    BitArray ba = new BitArray(64, false);

    AssertEquals(ba.Length, 64);
    foreach (bool b in ba)
      Assert(!b);

    ba = new BitArray(64, true);

    AssertEquals(ba.Length, 64);
    foreach (bool b in ba)
      Assert(b);
  }

  public void TestClone()
  {
    BitArray ba = (BitArray)testBa.Clone();

    verifyPattern(ba, testPattern);

    // ensure that changes in ba don't get propagated to testBa
    ba[0] = false;
    ba[1] = false;
    ba[2] = false;
    
    verifyPattern(testBa, testPattern);
  }
  
  public void TestSetLength()
  {
    int origLen = testBa.Length;
    testBa.Length += 33;

    AssertEquals(origLen + 33, testBa.Length);
    for (int i = origLen; i < testBa.Length; i++)
      testBa[i] = true;

    testBa.Length -= 33;
    verifyPattern(testBa, testPattern);
  }

  public void TestAnd()
  {
    BitArray result = op1.And(op2);
    AssertEquals(result.Length, op1.Length);
    for (int i = 0; i < result.Length; )
    {
      Assert(!result[i++]);
      Assert(result[i++]);
      Assert(!result[i++]);
      Assert(!result[i++]);
    }
  }

  public void TestOr()
  {
    BitArray result = op1.Or(op2);
    AssertEquals(result.Length, op1.Length);
    for (int i = 0; i < result.Length; )
    {
      Assert(result[i++]);
      Assert(result[i++]);
      Assert(result[i++]);
      Assert(!result[i++]);
    }
  }

  public void TestNot()
  {
    BitArray result = op1.Not();
    AssertEquals(result.Length, op1.Length);
    for (int i = 0; i < result.Length; )
    {
      Assert(!result[i++]);
      Assert(!result[i++]);
      Assert(result[i++]);
      Assert(result[i++]);
    }
  }

  public void TestXor()
  {
    BitArray result = op1.Xor(op2);
    AssertEquals(result.Length, op1.Length);
    for (int i = 0; i < result.Length; )
    {
      Assert(result[i++]);
      Assert(!result[i++]);
      Assert(result[i++]);
      Assert(!result[i++]);
    }
  }

  public void TestSetAll()
  {
    testBa.SetAll(false);
    foreach(bool b in testBa)
      Assert(!b);
    testBa.SetAll(true);
    foreach(bool b in testBa)
      Assert(b);
  }

  public void TestCopyToBool()
  {
    try {
	    bool[] barray = new bool[testBa.Length + 10];
	    
	    testBa.CopyTo(barray, 5);

	    for (int i = 0; i < testBa.Length; i++)
	      AssertEquals(testBa[i], barray[i+5]);
    }
    catch(Exception e){
	Fail("Unexpected exception thrown: " + e.ToString());
    }
  }

  public void TestCopyToByte()
  {
    try {
	    testBa.Length = 34;
	    byte[] barray = new byte[5 + 10];
	    
	    testBa.CopyTo(barray, 5);

	    for (int i = 5; i < 9; i++)
	      AssertEquals(0x55, barray[i] & 0xff);

	    // FIXME: MS fails on the next line.  This is because
	    // we truncated testBa.Length, and MS's internal array still
	    // has the old bits set.  CopyTo() doesn't say specifically
	    // whether the "junk" bits (bits past Length, but within Length
	    // rounded up to 32) will be copied as 0, or if those bits are
	    // undefined.
	    //AssertEquals(0x01, barray[9] & 0xff);
    }
    catch(Exception e){
	Fail("Unexpected exception thrown: " + e.ToString());
    }
  }

  public void TestCopyToInt()
  {
    try {
	    testBa.Length = 34;
	    int[] iarray = new int[2 + 10];
	    
	    testBa.CopyTo(iarray, 5);

	    AssertEquals(0x55555555, iarray[5]);
	    // FIXME:  Same thing here as in TestCopyToByte
	    //AssertEquals(0x01, iarray[6]);
    }
    catch(Exception e){
	Fail("Unexpected exception thrown: " + e.ToString());
    }
  }

  public void TestEnumerator()
  {
    
    try {
	    IEnumerator e = testBa.GetEnumerator();
	    
	    for (int i = 0; e.MoveNext(); i++)
	      AssertEquals(e.Current, testPattern[i]);

	    Assert(!e.MoveNext());
	    // read, to make sure reading isn't considered a write.
	    bool b = testBa[0];

	    e.Reset();
	    for (int i = 0; e.MoveNext(); i++)
	      AssertEquals(e.Current, testPattern[i]);

	    try
	    {
	      e.Reset();
	      testBa[0] = !testBa[0];
	      e.MoveNext();
	      Fail("IEnumerator.MoveNext() should throw when collection modified.");
	    }
	    catch (InvalidOperationException)
	    {
	    }
    }
    catch(Exception ex){
	Fail("Unexpected exception thrown: " + ex.ToString());
    }
  }
}

}