BitVector32.Sections. Also added a unit test for the bug.
svn path=/trunk/mcs/; revision=111591
if (maxValue < 1)
throw new ArgumentException ("maxValue");
- int bit = HighestSetBit(maxValue) + 1;
+ int bit = HighestSetBit(maxValue);
int mask = (1 << bit) - 1;
- int offset = previous.Offset + NumberOfSetBits (previous.Mask);
+ int offset = previous.Offset + HighestSetBit (previous.Mask);
- if (offset > 32) {
+ if (offset + bit > 32) {
throw new ArgumentException ("Sections cannot exceed 32 bits in total");
}
}
// Private utilities
- private static int NumberOfSetBits (int i)
+ private static int HighestSetBit (int i)
{
int count = 0;
- for (int bit = 0; bit < 32; bit++) {
- int mask = 1 << bit;
- if ((i & mask) != 0)
- count++;
- }
+ while(i >> count != 0)
+ count++;
return count;
}
-
- private static int HighestSetBit (int i)
- {
- for (int bit = 31; bit >= 0; bit--) {
- int mask = 1 << bit;
- if ((mask & i) != 0) {
- return bit;
- }
- }
-
- return -1;
- }
}
}
+2008-08-06 Scott Peterson <lunchtimemama@gmail.com>
+
+ * BitVector32.cs: Fixed a bug which allowed for invalid sections
+ to be created with CreateSection. Also simplified HighestSetBit
+ algorithm and got rid ofNumberOfSetBits (using HighestSetBit
+ works just fine).
+
2008-07-31 Jb Evain <jbevain@novell.com>
* StringDictionary.cs: remove ComponentModel bits for NET_2_1.
Assert.AreEqual ("Section{0x1f, 0x0}", s2.ToString (), "ToString()");
}
+ [Test]
+ public void SectionCorrectSize ()
+ {
+ BitVector32.Section s1 = BitVector32.CreateSection (32767);
+ BitVector32.Section s2 = BitVector32.CreateSection (32767, s1);
+ BitVector32.Section s3 = BitVector32.CreateSection (3, s2);
+ BitVector32 v1 = new BitVector32 (0);
+ v1[s3] = 3;
+ Assert.AreEqual (v1[s3], 3);
+ }
+
+ [Test]
+ public void SectionIncorrectSize ()
+ {
+ BitVector32.Section s1 = BitVector32.CreateSection (32767);
+ BitVector32.Section s2 = BitVector32.CreateSection (32767, s1);
+ try {
+ BitVector32.Section s3 = BitVector32.CreateSection (4, s2);
+ Assert.Fail("Illegal section created");
+ } catch (ArgumentException) {}
+ }
+
[Test]
public void NegativeIndexer ()
{
+2008-08-21 Scott Peterson <lunchtimemama@gmail.com>
+
+ * BitVector32Test.cs: Added test to make sure illegal sections cannot
+ be created.
+
2008-01-07 Gert Driesen <drieseng@users.sourceforge.net>
* NameObjectCollectionBaseTest.cs: Add tests for argument checking in