public void TestEncodeDecodeInt64() { const int factor = 10; var values = new long[64 * factor]; for (var i = 1; i < 64; i++) { var value = (long)Math.Pow(2, i); values[i * factor] = value - 2; values[i * factor + 1] = value - 1; values[i * factor + 2] = value; values[i * factor + 3] = value + 1; values[i * factor + 4] = value + 2; values[i * factor + 5] = -values[i * factor]; values[i * factor + 6] = -values[i * factor + 1]; values[i * factor + 7] = -values[i * factor + 2]; values[i * factor + 8] = -values[i * factor + 3]; values[i * factor + 9] = -values[i * factor + 4]; } var buffer = stackalloc byte[9]; for (var i = 0; i < values.Length; i++) { var p = buffer; Codec.EncodeInt64(values[i], ref p); p = buffer; var value = Codec.DecodeInt64(ref p); Assert.AreEqual(values[i], value, "At idx: " + i); } }
public int Encode(ref byte *src, int lSrc, ref byte *dst, int lDst) { var data = (Int32Entry *)src; var count = lSrc / sizeof(Int32Entry) * sizeof(Int32Entry); var minTicks = long.MaxValue; var maxNegValue = int.MinValue; var minPosValue = int.MaxValue; for (var i = 0; i < count; i++) { minTicks = Math.Min(minTicks, (data + i)->ticks); if ((data + i)->value < 0) { maxNegValue = Math.Max(maxNegValue, (data + i)->value); } else { minPosValue = Math.Min(minPosValue, (data + i)->value); } } maxNegValue = -maxNegValue; var start = dst; // Encode header Codec.EncodeInt64(minTicks, ref dst); Codec.EncodeInt32(maxNegValue, ref dst); Codec.EncodeInt32(minPosValue, ref dst); for (var i = 0; i < count; i++) { Codec.EncodeInt64((data + i)->ticks - minTicks, ref dst); if ((data + i)->value < 0) { Codec.EncodeInt32(true, -(data + i)->value - maxNegValue, ref dst); } else { Codec.EncodeInt32(false, (data + i)->value - minPosValue, ref dst); } } return((int)(dst - start)); }
public void TestEncodeDecodeFlaggedInt64() { var values = new long[64 * 4]; for (var i = 1; i < 64; i++) { var value = (ulong)Math.Pow(2, i); values[i * 4] = (long)value - 1; values[i * 4 + 1] = (long)value; values[i * 4 + 2] = -values[i * 4]; values[i * 4 + 3] = -values[i * 4 + 1]; } var buffer = stackalloc byte[5]; for (var i = 0; i < values.Length; i++) { var p = buffer; Codec.EncodeInt64(true, values[i], ref p); p = buffer; var value = Codec.DecodeInt64(ref p, out var flag); Assert.AreEqual(values[i], value, "At idx: " + i); Assert.IsTrue(flag); } for (var i = 0; i < values.Length; i++) { var p = buffer; Codec.EncodeInt64(false, values[i], ref p); p = buffer; var value = Codec.DecodeInt64(ref p, out var flag); Assert.AreEqual(values[i], value, "At idx: " + i); Assert.IsFalse(flag); } }