Exemple #1
0
        public virtual void  TestFloatToByte()
        {
            System.Random rand = NewRandom();
            // up iterations for more exhaustive test after changing something
            for (int i = 0; i < 100000; i++)
            {
                float f = BitConverter.ToSingle(BitConverter.GetBytes(rand.Next()), 0);
                if (f != f)
                {
                    continue; // skip NaN
                }
                sbyte b1 = Orig_floatToByte(f);
                sbyte b2 = SmallFloat.FloatToByte(f, 3, 15);
                sbyte b3 = SmallFloat.FloatToByte315(f);
                Assert.AreEqual(b1, b2);
                Assert.AreEqual(b2, b3);

                sbyte b4 = SmallFloat.FloatToByte(f, 5, 2);
                sbyte b5 = SmallFloat.FloatToByte52(f);
                Assert.AreEqual(b4, b5);
            }
        }
Exemple #2
0
        public virtual void TestFloatToByte()
        {
            Assert.AreEqual(0, Orig_floatToByte_v13(5.8123817E-10f));      // verify the old bug (see LUCENE-2937)
            Assert.AreEqual(1, Orig_floatToByte(5.8123817E-10f));          // verify it's fixed in this test code
            Assert.AreEqual(1, SmallFloat.FloatToByte315(5.8123817E-10f)); // verify it's fixed

            // test some constants
            Assert.AreEqual(0, SmallFloat.FloatToByte315(0));
            //Java's Float.MIN_VALUE equals C#'s float.Epsilon
            Assert.AreEqual(1, SmallFloat.FloatToByte315(float.Epsilon));           // underflow rounds up to smallest positive
            Assert.AreEqual(255, SmallFloat.FloatToByte315(float.MaxValue) & 0xff); // overflow rounds down to largest positive
            Assert.AreEqual(255, SmallFloat.FloatToByte315(float.PositiveInfinity) & 0xff);

            // all negatives map to 0
            Assert.AreEqual(0, SmallFloat.FloatToByte315(-float.Epsilon));
            Assert.AreEqual(0, SmallFloat.FloatToByte315(-float.MaxValue));
            Assert.AreEqual(0, SmallFloat.FloatToByte315(float.NegativeInfinity));

            // up iterations for more exhaustive test after changing something
            int num = AtLeast(100000);

            for (int i = 0; i < num; i++)
            {
                float f = Number.IntBitsToFloat(Random().Next());
                if (float.IsNaN(f)) // skip NaN
                {
                    continue;
                }
                sbyte b1 = Orig_floatToByte(f);
                sbyte b2 = SmallFloat.FloatToByte(f, 3, 15);
                sbyte b3 = SmallFloat.FloatToByte315(f);
                Assert.AreEqual(b1, b2);
                Assert.AreEqual(b2, b3);

                sbyte b4 = SmallFloat.FloatToByte(f, 5, 2);
                sbyte b5 = SmallFloat.FloatToByte52(f);
                Assert.AreEqual(b4, b5);
            }
        }