Example #1
0
    public void BigEndianIO_TEST()
    {
        for (int i = 0; i <= ushort.MaxValue; ++i)
        {
            ushort value = (ushort)i;
            var    ms    = new MemoryStream();
            BigEndianWriter.WriteUShort(ms, value);
            ushort read1 = BigEndianReader.ReadUShort(ms.ToArray(), 0);
            Assert.AreEqual(read1, value);

            ulong read2 = BigEndianReader.ReadNBytesUnsignedInteger(ms.ToArray(), 2, 0);
            Assert.IsTrue(read2 <= ushort.MaxValue);
            Assert.AreEqual((ushort)read2, value);
        }

        Xorshift random = new Xorshift(Seed);

        for (int i = 0; i < 100000; ++i)
        {
            uint value = random.Generate();
            var  ms    = new MemoryStream();
            BigEndianWriter.WriteUInt(ms, value);
            uint read1 = BigEndianReader.ReadUInt(ms.ToArray(), 0);
            Assert.AreEqual(read1, value);

            ulong read2 = BigEndianReader.ReadNBytesUnsignedInteger(ms.ToArray(), 4, 0);
            Assert.IsTrue(read2 <= uint.MaxValue);
            Assert.AreEqual((uint)read2, value);
        }

        for (int i = 0; i < 100000; ++i)
        {
            ulong value = (ulong)random.Generate() | ((ulong)random.Generate() << 32);
            var   ms    = new MemoryStream();
            BigEndianWriter.WriteULong(ms, value);
            ulong read1 = BigEndianReader.ReadULong(ms.ToArray(), 0);
            Assert.AreEqual(read1, value);

            ulong read2 = BigEndianReader.ReadNBytesUnsignedInteger(ms.ToArray(), 8, 0);
            Assert.AreEqual(read2, value);
        }
    }