* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System / Test / System.Collections.Specialized / BitVector32Test.cs
1 //
2 // BitVector32Test.cs - NUnit Test Cases for System.Net.BitVector32
3 //
4 // Authors:
5 //   Lawrence Pit (loz@cable.a2000.nl)
6 //   Martin Willemoes Hansen (mwh@sysrq.dk)
7 //   Sebastien Pouliot  <sebastien@ximian.com>
8 //
9 // (C) 2003 Martin Willemoes Hansen
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 //
12
13 using NUnit.Framework;
14 using System;
15 using System.Collections;
16 using System.Collections.Specialized;
17
18 namespace MonoTests.System.Collections.Specialized
19 {
20         [TestFixture]
21         public class BitVector32Test
22         {
23                 [Test]
24                 public void Constructors ()
25                 {
26                         BitVector32 b = new BitVector32 (31);
27                         Assert.AreEqual (31, b.Data, "Data");
28                         Assert.IsTrue (b.Equals (b), "Equals(self)");
29                         Assert.IsTrue (b[31], "31");
30                         Assert.IsFalse (b[32], "32");
31                         Assert.AreEqual (b.ToString (), "BitVector32{00000000000000000000000000011111}", b.ToString ());
32
33                         BitVector32 b2 = new BitVector32 (b);
34                         Assert.IsTrue (b.Equals (b2), "Equals(b2)");
35                         Assert.AreEqual (b.GetHashCode (), b2.GetHashCode (), "GetHashCode==");
36
37                         b2[32] = true;
38                         Assert.IsFalse (b.Equals (b2), "Equals(b32)");
39                         Assert.IsFalse (b.GetHashCode () == b2.GetHashCode (), "GetHashCode!=");
40                 }
41
42                 [Test]
43                 public void Constructors_MaxValue ()
44                 {
45                         BitVector32 b = new BitVector32 (Int32.MaxValue);
46                         Assert.AreEqual (Int32.MaxValue, b.Data, "Data");
47                         Assert.AreEqual ("BitVector32{01111111111111111111111111111111}", BitVector32.ToString (b), "ToString(BitVector)");
48                 }
49
50                 [Test]
51                 public void Constructors_MinValue ()
52                 {
53                         BitVector32 b = new BitVector32 (Int32.MinValue);
54                         Assert.AreEqual (Int32.MinValue, b.Data, "Data");
55                         Assert.AreEqual ("BitVector32{10000000000000000000000000000000}", BitVector32.ToString (b), "ToString(BitVector)");
56                 }
57                 
58                 [Test]
59                 public void Indexers ()
60                 {
61                         BitVector32 b = new BitVector32 (7);
62                         Assertion.Assert ("#1", b [0]);
63                         Assertion.Assert ("#2", b [1]);
64                         Assertion.Assert ("#3", b [2]);
65                         Assertion.Assert ("#4", b [4]);
66                         Assertion.Assert ("#5", !b [8]);
67                         Assertion.Assert ("#6", !b [16]);
68                         b [8] = true;
69                         Assertion.Assert ("#7", b [4]);
70                         Assertion.Assert ("#8", b [8]);
71                         Assertion.Assert ("#9", !b [16]);
72                         b [8] = false;
73                         Assertion.Assert ("#10", b [4]);
74                         Assertion.Assert ("#11", !b [8]);
75                         Assertion.Assert ("#12", !b [16]);
76
77                         BitVector32.Section s = BitVector32.CreateSection (31);
78                         s = BitVector32.CreateSection (64, s);
79                         // Print (s);
80                         
81                         // b = new BitVector32 (0x777777);
82                         BitVector32 b1 = new BitVector32 (0xffff77);
83                         BitVector32 b2 = new BitVector32 (b1 [s]);
84                         //Console.WriteLine (b1.ToString ());
85                         //Console.WriteLine (b2.ToString ());
86                         Assertion.AssertEquals ("#14", 123, b1 [s]);
87                         
88                         // b1 [s] = 15;
89                         //Console.WriteLine (b1.ToString ());
90                 }
91
92                 [Test]
93                 public void CreateMask ()
94                 {
95                         Assert.AreEqual (1, BitVector32.CreateMask (), "#1");
96                         Assert.AreEqual (1, BitVector32.CreateMask (0), "#2");
97                         Assert.AreEqual (2, BitVector32.CreateMask (1), "#3");
98                         Assert.AreEqual (32, BitVector32.CreateMask (16), "#4");
99                         Assert.AreEqual (-2, BitVector32.CreateMask (Int32.MaxValue), "#5");
100                         Assert.AreEqual (-4, BitVector32.CreateMask (-2), "#6");
101                         Assert.AreEqual (2, BitVector32.CreateMask (Int32.MinValue + 1), "#7");
102                 }
103
104                 [Test]
105                 [ExpectedException (typeof (InvalidOperationException))]
106                 public void CreateMask_MinValue ()
107                 {
108                         BitVector32.CreateMask (Int32.MinValue);
109                 }
110                 
111                 [Test]
112                 public void CreateSection ()
113                 {
114                         BitVector32.Section s = BitVector32.CreateSection (1);
115                         Assertion.AssertEquals ("#1", (short) 1, s.Mask);
116
117                         s = BitVector32.CreateSection (2);
118                         Assertion.AssertEquals ("#2", (short) 3, s.Mask);
119
120                         s = BitVector32.CreateSection (3);
121                         Assertion.AssertEquals ("#3", (short) 3, s.Mask);
122
123                         s = BitVector32.CreateSection (5);
124                         Assertion.AssertEquals ("#4", (short) 7, s.Mask);
125                         
126                         s = BitVector32.CreateSection (20);
127                         Assertion.AssertEquals ("#4", (short) 0x1f, s.Mask);
128
129                         s = BitVector32.CreateSection (Int16.MaxValue);
130                         Assertion.AssertEquals ("#5", (short) 0x7fff, s.Mask);
131
132                         s = BitVector32.CreateSection (Int16.MaxValue - 100);
133                         Assertion.AssertEquals ("#6", (short) 0x7fff, s.Mask);
134
135                         try {
136                                 BitVector32.Section s2 = BitVector32.CreateSection (0);
137                                 Assertion.Fail ("#7");
138                         } catch (ArgumentException) {}
139
140                         try {
141                                 BitVector32.Section s2 = BitVector32.CreateSection (-1);
142                                 Assertion.Fail ("#8");
143                         } catch (ArgumentException) {}
144
145                         try {
146                                 BitVector32.Section s2 = BitVector32.CreateSection (Int16.MinValue);
147                                 Assertion.Fail ("#9");
148                         } catch (ArgumentException) {}
149                         
150                         s = BitVector32.CreateSection (20);
151                         Assertion.AssertEquals ("#10a", (short) 0x1f, s.Mask);
152                         Assertion.AssertEquals ("#10b", (short) 0x00, s.Offset);                        
153                         s = BitVector32.CreateSection (120, s);
154                         Assertion.AssertEquals ("#10c", (short) 0x7f, s.Mask);
155                         Assertion.AssertEquals ("#10d", (short) 0x05, s.Offset);                                        
156                         s = BitVector32.CreateSection (1000, s);
157                         Assertion.AssertEquals ("#10e", (short) 0x3ff, s.Mask);
158                         Assertion.AssertEquals ("#10f", (short) 0x0c, s.Offset);                        
159                 }
160
161                 [Test]
162                 public void Section ()
163                 {
164                         BitVector32.Section s1 = BitVector32.CreateSection (20);
165                         Assert.AreEqual (31, s1.Mask, "1.Mask");
166                         Assert.AreEqual (0, s1.Offset, "1.Offset");
167                         Assert.AreEqual ("Section{0x1f, 0x0}", BitVector32.Section.ToString (s1), "ToString(Section)");
168
169                         BitVector32.Section s2 = BitVector32.CreateSection (20);
170                         Assert.IsTrue (s1.Equals (s2), "s1==s2");
171                         Assert.IsTrue (s2.Equals ((object)s1), "s2==s1");
172                         Assert.AreEqual (s1.GetHashCode (), s2.GetHashCode (), "GetHashCode");
173                         Assert.AreEqual ("Section{0x1f, 0x0}", s2.ToString (), "ToString()");
174                 }
175
176                 [Test]
177                 public void NegativeIndexer ()
178                 {
179                         BitVector32 bv = new BitVector32 (-1);
180 #if NET_2_0
181                         Assert.IsTrue (bv [Int32.MinValue], "Int32.MinValue");
182 #else
183                         Assert.IsFalse (bv [Int32.MinValue], "Int32.MinValue");
184 #endif
185                 }
186
187                 [Test]
188                 public void TestSectionIndexer ()
189                 {
190                         BitVector32 bv = new BitVector32 (-1);
191                         BitVector32.Section sect = BitVector32.CreateSection (1);
192                         sect = BitVector32.CreateSection (Int16.MaxValue, sect);
193                         sect = BitVector32.CreateSection (Int16.MaxValue, sect);
194                         sect = BitVector32.CreateSection (1, sect);
195                         Assert.AreEqual (1, bv[sect], "bv[sect]");
196                         bv [sect] = 0; 
197
198                         Assertion.AssertEquals ("#12a", Int32.MaxValue, bv.Data);
199                 }
200
201                 [Test, ExpectedException (typeof (ArgumentException))]
202                 public void TestCreateSection1 ()
203                 {
204                         BitVector32.Section section = BitVector32.CreateSection (Int16.MaxValue);
205                         section = BitVector32.CreateSection (0, section);
206                 }
207
208                 [Test, ExpectedException (typeof (ArgumentException))]
209                 public void TestCreateSection2 ()
210                 {
211                         BitVector32.Section section = BitVector32.CreateSection (Int16.MaxValue);
212                         section = BitVector32.CreateSection (-1, section);
213                 }
214
215                 [Test, ExpectedException (typeof (ArgumentException))]
216                 public void TestCreateSection3 ()
217                 {
218                         BitVector32.Section section = BitVector32.CreateSection (Int16.MaxValue);
219                         section = BitVector32.CreateSection (Int16.MinValue, section);
220                 }
221
222                 private void Print (BitVector32.Section s)
223                 {
224                         Console.WriteLine (s.ToString () + " : "+ s.Mask + " : " + s.Offset);
225                 }
226         }        
227 }