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); } } }
public virtual void TestLongSpecialValues() { long[] vals = new long[] { System.Int64.MinValue, System.Int64.MinValue + 1, System.Int64.MinValue + 2, -5003400000000L, -4000L, -3000L, -2000L, -1000L, -1L, 0L, 1L, 10L, 300L, 50006789999999999L, System.Int64.MaxValue - 2, System.Int64.MaxValue - 1, System.Int64.MaxValue }; System.String[] prefixVals = new System.String[vals.Length]; for (int i = 0; i < vals.Length; i++) { prefixVals[i] = NumericUtils.LongToPrefixCoded(vals[i]); // check forward and back conversion Assert.AreEqual(vals[i], NumericUtils.PrefixCodedToLong(prefixVals[i]), "forward and back conversion should generate same long"); // test if decoding values as int fails correctly Assert.Throws <FormatException>(() => NumericUtils.PrefixCodedToInt(prefixVals[i]), "decoding a prefix coded long value as int 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 < 64; j++) { long prefixVal = NumericUtils.PrefixCodedToLong(NumericUtils.LongToPrefixCoded(vals[i], j)); long mask = (1L << j) - 1L; Assert.AreEqual(vals[i] & mask, vals[i] - prefixVal, "difference between prefix val and original value for " + vals[i] + " with shift=" + j); } } }
public virtual double ParseDouble(System.String val) { int shift = val[0] - NumericUtils.SHIFT_START_LONG; if (shift > 0 && shift <= 63) { throw new FieldCacheImpl.StopFillCacheException(); } return(NumericUtils.SortableLongToDouble(NumericUtils.PrefixCodedToLong(val))); }
public virtual void TestLongSpecialValues() { long[] vals = new long[] { long.MinValue, long.MinValue + 1, long.MinValue + 2, -5003400000000L, -4000L, -3000L, -2000L, -1000L, -1L, 0L, 1L, 10L, 300L, 50006789999999999L, long.MaxValue - 2, long.MaxValue - 1, long.MaxValue }; BytesRef[] prefixVals = new BytesRef[vals.Length]; for (int i = 0; i < vals.Length; i++) { prefixVals[i] = new BytesRef(NumericUtils.BUF_SIZE_LONG); NumericUtils.LongToPrefixCodedBytes(vals[i], 0, prefixVals[i]); // check forward and back conversion Assert.AreEqual(vals[i], NumericUtils.PrefixCodedToLong(prefixVals[i]), "forward and back conversion should generate same long"); // test if decoding values as int fails correctly try { NumericUtils.PrefixCodedToInt(prefixVals[i]); Assert.Fail("decoding a prefix coded long value as int should fail"); } catch (FormatException e) { // 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_LONG); for (int i = 0; i < vals.Length; i++) { for (int j = 0; j < 64; j++) { NumericUtils.LongToPrefixCodedBytes(vals[i], j, @ref); long prefixVal = NumericUtils.PrefixCodedToLong(@ref); long mask = (1L << j) - 1L; Assert.AreEqual(vals[i] & mask, vals[i] - prefixVal, "difference between prefix val and original value for " + vals[i] + " with shift=" + j); } } }
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_INT); NumericUtils.IntToPrefixCodedBytes(vals[i], 0, prefixVals[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 try { NumericUtils.PrefixCodedToLong(prefixVals[i]); Assert.Fail("decoding a prefix coded int value as long should fail"); } catch (FormatException e) { // 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_LONG); for (int i = 0; i < vals.Length; i++) { for (int j = 0; j < 32; j++) { NumericUtils.IntToPrefixCodedBytes(vals[i], j, @ref); int prefixVal = NumericUtils.PrefixCodedToInt(@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); } } }
public virtual void TestLongConversionAndOrdering() { // generate a series of encoded longs, each numerical one bigger than the one before System.String last = null; for (long l = -100000L; l < 100000L; l++) { System.String act = NumericUtils.LongToPrefixCoded(l); 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(l, NumericUtils.PrefixCodedToLong(act), "forward and back conversion should generate same long"); // next step last = act; } }
public virtual void TestLongConversionAndOrdering() { // generate a series of encoded longs, each numerical one bigger than the one before BytesRef last = null, act = new BytesRef(NumericUtils.BUF_SIZE_LONG); for (long l = -100000L; l < 100000L; l++) { NumericUtils.LongToPrefixCodedBytes(l, 0, act); if (last != null) { // test if smaller Assert.IsTrue(last.CompareTo(act) < 0, "actual bigger than last (BytesRef)"); //Assert.IsTrue(last.Utf8ToString().CompareTo(act.Utf8ToString()) < 0, "actual bigger than last (as String)"); } // test is back and forward conversion works Assert.AreEqual(l, NumericUtils.PrefixCodedToLong(act), "forward and back conversion should generate same long"); // next step last = act; act = new BytesRef(NumericUtils.BUF_SIZE_LONG); } }