Example #1
0
        public void Value_HashCheck()
        {
            byte partitionBits = 1;

            int id = 1253432;

            // Get Value, Hash, and Partition Index
            Value idValue   = Value.Create(id);
            int   hash      = idValue.GetHashCode();
            int   partition = PartitionMask.IndexOfHash(hash, partitionBits);

            // Verify Matches reports consistently with IndexOfHash
            PartitionMask mask     = PartitionMask.BuildSet(partitionBits)[partition];
            string        maskName = mask.ToString();

            Assert.IsTrue(mask.Matches(hash));

            // Verify Value.Assign hash is consistent
            Value n = Value.Create(null);

            n.Assign(id);
            int hashViaAssign = n.GetHashCode();

            Assert.AreEqual(hash, hashViaAssign);

            // Get Hash of unwrapped value [debuggability]
            int wrongHash      = id.GetHashCode();
            int wrongPartition = PartitionMask.IndexOfHash(wrongHash, partitionBits);
        }
        public void PartitionMask_Basic()
        {
            PartitionMask mask = PartitionMask.All;

            // Verify 'All' is count zero, value zero
            Assert.AreEqual(0, mask.BitCount);
            Assert.AreEqual(0, mask.Value);

            // Everything matches a zero-width mask
            mask.BitCount = 0;
            Assert.IsTrue(mask.Matches(0));
            Assert.IsTrue(mask.Matches(~0));
            Assert.AreEqual("", mask.ToString());

            // 1* should match values starting with 0x8 only
            mask.BitCount = 1;
            mask.Value    = (0x1 << 31);
            Assert.IsFalse(mask.Matches(0));
            Assert.IsTrue(mask.Matches(~0));
            Assert.IsTrue(mask.Matches(unchecked ((int)0x80000000)));
            Assert.IsFalse(mask.Matches(0x7FFFFFFF));
            Assert.AreEqual("1", mask.ToString());

            // 0* should match values without the first bit set
            mask.BitCount = 1;
            mask.Value    = 0;
            Assert.IsTrue(mask.Matches(0));
            Assert.IsFalse(mask.Matches(~0));
            Assert.IsFalse(mask.Matches(unchecked ((int)0x80000000)));
            Assert.IsTrue(mask.Matches(0x7FFFFFFF));
            Assert.AreEqual("0", mask.ToString());

            // 11* should match values with the first two bits
            mask.BitCount = 2;
            mask.Value    = (0x3 << 30);
            Assert.IsFalse(mask.Matches(0));
            Assert.IsTrue(mask.Matches(~0));
            Assert.IsFalse(mask.Matches(unchecked ((int)0x80000000)));
            Assert.IsTrue(mask.Matches(unchecked ((int)0xC0000000)));
            Assert.IsTrue(mask.Matches(unchecked ((int)0xE0000000)));
            Assert.IsFalse(mask.Matches(0x7FFFFFFF));
            Assert.AreEqual("11", mask.ToString());

            // 1101* should match values with the first four bits
            mask.BitCount = 4;
            mask.Value    = (0xD << 28);
            Assert.IsFalse(mask.Matches(0));
            Assert.IsFalse(mask.Matches(~0));
            Assert.IsFalse(mask.Matches(unchecked ((int)0xC0000000)));
            Assert.IsTrue(mask.Matches(unchecked ((int)0xD0000000)));
            Assert.IsTrue(mask.Matches(unchecked ((int)0xD7777777)));
            Assert.IsFalse(mask.Matches(unchecked ((int)0xE0000000)));
            Assert.IsFalse(mask.Matches(unchecked ((int)0xF0000000)));
            Assert.IsFalse(mask.Matches(unchecked ((int)0x7FFFFFFF)));
            Assert.AreEqual("1101", mask.ToString());

            // Verify 'All' has not been altered by copy/use
            Assert.AreEqual(0, PartitionMask.All.BitCount);
            Assert.AreEqual(0, PartitionMask.All.Value);
        }