2 // BitVector32Test.cs - NUnit Test Cases for System.Net.BitVector32
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 // Sebastien Pouliot <sebastien@ximian.com>
9 // (C) 2003 Martin Willemoes Hansen
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
13 using NUnit.Framework;
15 using System.Collections;
16 using System.Collections.Specialized;
18 namespace MonoTests.System.Collections.Specialized
21 public class BitVector32Test
24 public void Constructors ()
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 ());
33 BitVector32 b2 = new BitVector32 (b);
34 Assert.IsTrue (b.Equals (b2), "Equals(b2)");
35 Assert.AreEqual (b.GetHashCode (), b2.GetHashCode (), "GetHashCode==");
38 Assert.IsFalse (b.Equals (b2), "Equals(b32)");
39 Assert.IsFalse (b.GetHashCode () == b2.GetHashCode (), "GetHashCode!=");
43 public void Constructors_MaxValue ()
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)");
51 public void Constructors_MinValue ()
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)");
59 public void Indexers ()
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]);
69 Assertion.Assert ("#7", b [4]);
70 Assertion.Assert ("#8", b [8]);
71 Assertion.Assert ("#9", !b [16]);
73 Assertion.Assert ("#10", b [4]);
74 Assertion.Assert ("#11", !b [8]);
75 Assertion.Assert ("#12", !b [16]);
77 BitVector32.Section s = BitVector32.CreateSection (31);
78 s = BitVector32.CreateSection (64, s);
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]);
89 //Console.WriteLine (b1.ToString ());
93 public void CreateMask ()
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");
105 [ExpectedException (typeof (InvalidOperationException))]
106 public void CreateMask_MinValue ()
108 BitVector32.CreateMask (Int32.MinValue);
112 public void CreateSection ()
114 BitVector32.Section s = BitVector32.CreateSection (1);
115 Assertion.AssertEquals ("#1", (short) 1, s.Mask);
117 s = BitVector32.CreateSection (2);
118 Assertion.AssertEquals ("#2", (short) 3, s.Mask);
120 s = BitVector32.CreateSection (3);
121 Assertion.AssertEquals ("#3", (short) 3, s.Mask);
123 s = BitVector32.CreateSection (5);
124 Assertion.AssertEquals ("#4", (short) 7, s.Mask);
126 s = BitVector32.CreateSection (20);
127 Assertion.AssertEquals ("#4", (short) 0x1f, s.Mask);
129 s = BitVector32.CreateSection (Int16.MaxValue);
130 Assertion.AssertEquals ("#5", (short) 0x7fff, s.Mask);
132 s = BitVector32.CreateSection (Int16.MaxValue - 100);
133 Assertion.AssertEquals ("#6", (short) 0x7fff, s.Mask);
136 BitVector32.Section s2 = BitVector32.CreateSection (0);
137 Assertion.Fail ("#7");
138 } catch (ArgumentException) {}
141 BitVector32.Section s2 = BitVector32.CreateSection (-1);
142 Assertion.Fail ("#8");
143 } catch (ArgumentException) {}
146 BitVector32.Section s2 = BitVector32.CreateSection (Int16.MinValue);
147 Assertion.Fail ("#9");
148 } catch (ArgumentException) {}
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);
162 public void Section ()
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)");
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()");
177 public void NegativeIndexer ()
179 BitVector32 bv = new BitVector32 (-1);
181 Assert.IsTrue (bv [Int32.MinValue], "Int32.MinValue");
183 Assert.IsFalse (bv [Int32.MinValue], "Int32.MinValue");
188 public void TestSectionIndexer ()
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]");
198 Assertion.AssertEquals ("#12a", Int32.MaxValue, bv.Data);
201 [Test, ExpectedException (typeof (ArgumentException))]
202 public void TestCreateSection1 ()
204 BitVector32.Section section = BitVector32.CreateSection (Int16.MaxValue);
205 section = BitVector32.CreateSection (0, section);
208 [Test, ExpectedException (typeof (ArgumentException))]
209 public void TestCreateSection2 ()
211 BitVector32.Section section = BitVector32.CreateSection (Int16.MaxValue);
212 section = BitVector32.CreateSection (-1, section);
215 [Test, ExpectedException (typeof (ArgumentException))]
216 public void TestCreateSection3 ()
218 BitVector32.Section section = BitVector32.CreateSection (Int16.MaxValue);
219 section = BitVector32.CreateSection (Int16.MinValue, section);
222 private void Print (BitVector32.Section s)
224 Console.WriteLine (s.ToString () + " : "+ s.Mask + " : " + s.Offset);