public void Test7BitEncoding() { byte[] arr = new byte[1024]; // test variable lengths var numbers = new ulong[7]; numbers = new ulong[] { 1, 25, 137, 255, 256, 10000, 1000000 }; ulong state = RandomSeed.GetUInt64(); for (int runs = 0; runs < 250000; runs++) { var work = arr.AsSpan(); for (int i = 0; i < numbers.Length; i++) { work.WriteVariableUInt64(numbers[i]); } int resultLength = arr.Length - work.Length; ReadOnlySpan <byte> res = arr.AsSpan(0, resultLength); for (int i = 0; i < numbers.Length; i++) { var nr = res.ReadVariableUInt64(); Assert.AreEqual(numbers[i], nr); } // re-randomize numbers[0] = (ulong)PRNG.Next(ref state, 0, 6); numbers[1] = (ulong)PRNG.Next(ref state, 7, 300); numbers[2] = (ulong)PRNG.Next(ref state, 300, 5000); numbers[3] = (ulong)PRNG.Next(ref state, 5000, 50000); numbers[4] = (ulong)PRNG.Next(ref state, 50000, 500000); numbers[5] = (ulong)PRNG.NextUInt64(ref state); numbers[6] = (ulong)PRNG.NextUInt64(ref state); } // signed var signed = new long[7]; for (int runs = 0; runs < 250000; runs++) { // re-randomize signed[0] = (long)PRNG.Next(ref state, -5, 5); signed[1] = (long)PRNG.Next(ref state, -100, 100); signed[2] = (long)PRNG.Next(ref state, -300, 300); signed[3] = (long)PRNG.Next(ref state, -5000, 5000); signed[4] = (long)PRNG.Next(ref state, -70000, 70000); signed[5] = (long)PRNG.NextUInt64(ref state); signed[6] = (long)PRNG.NextUInt64(ref state); var work = arr.AsSpan(); for (int i = 0; i < signed.Length; i++) { work.WriteVariableInt64(signed[i]); } int resultLength = arr.Length - work.Length; ReadOnlySpan <byte> res = arr.AsSpan(0, resultLength); for (int i = 0; i < signed.Length; i++) { var nr = res.ReadVariableInt64(); Assert.AreEqual(signed[i], nr); } } }
public void TestDataWriter2() { ulong ss = RandomSeed.GetUInt64(); byte[] small = new byte[64]; using (var wrt = new DataWriter(1)) { for (int runs = 0; runs < 10000; runs++) { wrt.Clear(); var originalSeed = PRNG.NextUInt64(ref ss); var seed = originalSeed; { int numOps = PRNG.Next(ref seed, 1, 15); for (int o = 0; o < numOps; o++) { switch (PRNG.Next(ref seed, 0, 6)) { case 0: wrt.WriteBool(PRNG.NextBool(ref seed)); break; case 1: wrt.WriteSingle((float)PRNG.Next(ref seed, 1, 100)); break; case 2: wrt.WriteVariableInt32(PRNG.Next(ref seed, -50, 50)); break; case 3: wrt.WriteString(PRNG.NextUInt32(ref seed).ToString()); break; case 4: wrt.WriteUInt16((ushort)PRNG.Next(ref seed, 0, ushort.MaxValue)); break; case 5: var sss = small.AsSpan(0, PRNG.Next(ref seed, 1, 25)); wrt.WriteBytes(sss); break; } } } // read var rdr = wrt.ReadOnlySpan; seed = originalSeed; { int numOps = PRNG.Next(ref seed, 1, 15); for (int o = 0; o < numOps; o++) { switch (PRNG.Next(ref seed, 0, 6)) { case 0: { var facit = PRNG.NextBool(ref seed); Assert.AreEqual(facit, rdr.ReadBool()); } break; case 1: { var facit = (float)PRNG.Next(ref seed, 1, 100); Assert.AreEqual(facit, rdr.ReadSingle()); } break; case 2: { var facit = PRNG.Next(ref seed, -50, 50); Assert.AreEqual(facit, rdr.ReadVariableInt32()); } break; case 3: { var facit = PRNG.NextUInt32(ref seed).ToString(); Assert.AreEqual(facit, rdr.ReadString()); } break; case 4: { var facit = (ushort)PRNG.Next(ref seed, 0, ushort.MaxValue); Assert.AreEqual(facit, rdr.ReadUInt16()); } break; case 5: { var sss = small.AsSpan(0, PRNG.Next(ref seed, 1, 25)); var aaa = rdr.ReadBytes(sss.Length); Assert.IsTrue(sss.SequenceEqual(aaa)); } break; } } } } } }