예제 #1
0
        public void CreateNetPacketFromBufferTest()
        {
            var       shortValue  = _randomizer.Short();
            var       floatValue  = _randomizer.Float();
            const int contentSize = sizeof(short) + sizeof(float);
            var       data        = BitConverter.GetBytes(contentSize)
                                    .Concat(BitConverter.GetBytes(shortValue))
                                    .Concat(BitConverter.GetBytes((float)floatValue))
                                    .ToArray();

            var packet            = new LitePacket(data);
            var packetContentSize = packet.Read <int>();
            var shortValuePacket  = packet.Read <short>();
            var floatValuePacket  = packet.Read <float>();

            Assert.Equal(LitePacketMode.Read, packet.Mode);
            Assert.NotNull(packet.Buffer);
            Assert.Equal(data, packet.Buffer);
            Assert.Equal(contentSize, BitConverter.ToInt32(packet.Buffer, 0));
            Assert.Equal(contentSize, packetContentSize);

            Assert.Equal(shortValue, BitConverter.ToInt16(packet.Buffer, sizeof(int)));
            Assert.Equal(shortValue, shortValuePacket);

            Assert.Equal(floatValue, BitConverter.ToSingle(packet.Buffer, sizeof(int) + sizeof(short)));
            Assert.Equal(floatValue, floatValuePacket);

            packet.Dispose();
        }