IntToPrefixCoded() public static method

Returns prefix coded bits after reducing the precision by shift bits. this is method is used by NumericTokenStream. After encoding, {@code bytes.offset} will always be 0.
public static IntToPrefixCoded ( int val, int shift, BytesRef bytes ) : void
val int the numeric value
shift int how many bits to strip from the right
bytes BytesRef will contain the encoded value
return void
Example #1
0
        public virtual void  TestIntSpecialValues()
        {
            int[]           vals       = new int[] { System.Int32.MinValue, System.Int32.MinValue + 1, System.Int32.MinValue + 2, -64765767, -4000, -3000, -2000, -1000, -1, 0, 1, 10, 300, 765878989, System.Int32.MaxValue - 2, System.Int32.MaxValue - 1, System.Int32.MaxValue };
            System.String[] prefixVals = new System.String[vals.Length];

            for (int i = 0; i < vals.Length; i++)
            {
                prefixVals[i] = NumericUtils.IntToPrefixCoded(vals[i]);

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

                // test if decoding values as long fails correctly
                Assert.Throws <FormatException>(() => NumericUtils.PrefixCodedToLong(prefixVals[i]),
                                                "decoding a prefix coded int value as long should fail");
            }

            // check sort order (prefixVals should be ascending)
            for (int i = 1; i < prefixVals.Length; i++)
            {
                Assert.IsTrue(String.CompareOrdinal(prefixVals[i - 1], 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
            for (int i = 0; i < vals.Length; i++)
            {
                for (int j = 0; j < 32; j++)
                {
                    int prefixVal = NumericUtils.PrefixCodedToInt(NumericUtils.IntToPrefixCoded(vals[i], j));
                    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);
                }
            }
        }
Example #2
0
 public virtual void  TestIntConversionAndOrdering()
 {
     // generate a series of encoded ints, each numerical one bigger than the one before
     System.String last = null;
     for (int i = -100000; i < 100000; i++)
     {
         System.String act = NumericUtils.IntToPrefixCoded(i);
         if (last != null)
         {
             // test if smaller
             Assert.IsTrue(String.CompareOrdinal(last, act) < 0, "actual bigger than last");
         }
         // test is back and forward conversion works
         Assert.AreEqual(i, NumericUtils.PrefixCodedToInt(act), "forward and back conversion should generate same int");
         // next step
         last = act;
     }
 }