Exemple #1
0
        public virtual void TestIntSpecialValues()
        {
            int[]      vals       = new int[] { int.MinValue, int.MinValue + 1, int.MinValue + 2, -64765767, -4000, -3000, -2000, -1000, -1, 0, 1, 10, 300, 765878989, int.MaxValue - 2, int.MaxValue - 1, int.MaxValue };
            BytesRef[] prefixVals = new BytesRef[vals.Length];

            for (int i = 0; i < vals.Length; i++)
            {
                prefixVals[i] = new BytesRef(NumericUtils.BUF_SIZE_INT32);
                NumericUtils.Int32ToPrefixCodedBytes(vals[i], 0, prefixVals[i]);

                // check forward and back conversion
                Assert.AreEqual(vals[i], NumericUtils.PrefixCodedToInt32(prefixVals[i]), "forward and back conversion should generate same int");

                // test if decoding values as long fails correctly
                try
                {
                    NumericUtils.PrefixCodedToInt64(prefixVals[i]);
                    Assert.Fail("decoding a prefix coded int value as long should fail");
                }
#pragma warning disable 168
                catch (FormatException e)
#pragma warning restore 168
                {
                    // worked
                }
            }

            // check sort order (prefixVals should be ascending)
            for (int i = 1; i < prefixVals.Length; i++)
            {
                Assert.IsTrue(prefixVals[i - 1].CompareTo(prefixVals[i]) < 0, "check sort order");
            }

            // check the prefix encoding, lower precision should have the difference to original value equal to the lower removed bits
            BytesRef @ref = new BytesRef(NumericUtils.BUF_SIZE_INT64);
            for (int i = 0; i < vals.Length; i++)
            {
                for (int j = 0; j < 32; j++)
                {
                    NumericUtils.Int32ToPrefixCodedBytes(vals[i], j, @ref);
                    int prefixVal = NumericUtils.PrefixCodedToInt32(@ref);
                    int mask      = (1 << j) - 1;
                    Assert.AreEqual(vals[i] & mask, vals[i] - prefixVal, "difference between prefix val and original value for " + vals[i] + " with shift=" + j);
                }
            }
        }
Exemple #2
0
        public virtual void TestIntConversionAndOrdering()
        {
            // generate a series of encoded ints, each numerical one bigger than the one before
            BytesRef last = null, act = new BytesRef(NumericUtils.BUF_SIZE_INT32);

            for (int i = -100000; i < 100000; i++)
            {
                NumericUtils.Int32ToPrefixCodedBytes(i, 0, act);
                if (last != null)
                {
                    // test if smaller
                    Assert.IsTrue(last.CompareTo(act) < 0, "actual bigger than last (BytesRef)");
                    Assert.IsTrue(last.Utf8ToString().CompareToOrdinal(act.Utf8ToString()) < 0, "actual bigger than last (as String)");
                }
                // test is back and forward conversion works
                Assert.AreEqual(i, NumericUtils.PrefixCodedToInt32(act), "forward and back conversion should generate same int");
                // next step
                last = act;
                act  = new BytesRef(NumericUtils.BUF_SIZE_INT32);
            }
        }