Ejemplo n.º 1
0
        public void ReadInteger()
        {
            MemoryStream stream = new MemoryStream();
            OutputChannel output = new OutputChannel(new BinaryWriter(stream));
            output.Write(123);
            stream.Seek(0, SeekOrigin.Begin);

            InputChannel channel = new InputChannel(new BinaryReader(stream));

            Assert.AreEqual(123, channel.Read());
        }
Ejemplo n.º 2
0
        public void ReadCharacter()
        {
            MemoryStream stream = new MemoryStream();
            OutputChannel output = new OutputChannel(new BinaryWriter(stream));
            char ch = 'a';
            output.Write(ch);
            stream.Seek(0, SeekOrigin.Begin);

            InputChannel channel = new InputChannel(new BinaryReader(stream));

            Assert.AreEqual(ch, channel.Read());
        }
Ejemplo n.º 3
0
        public void ReadByte()
        {
            MemoryStream stream = new MemoryStream();
            OutputChannel output = new OutputChannel(new BinaryWriter(stream));
            byte bt = 64;
            output.Write(bt);
            stream.Seek(0, SeekOrigin.Begin);

            InputChannel channel = new InputChannel(new BinaryReader(stream));

            Assert.AreEqual(bt, channel.Read());
        }
Ejemplo n.º 4
0
        public void ReadDecimal()
        {
            MemoryStream stream = new MemoryStream();
            OutputChannel output = new OutputChannel(new BinaryWriter(stream));
            decimal dc = 12.34m;
            output.Write(dc);
            stream.Seek(0, SeekOrigin.Begin);

            InputChannel channel = new InputChannel(new BinaryReader(stream));

            Assert.AreEqual(dc, channel.Read());
        }
Ejemplo n.º 5
0
        public void WriteDouble()
        {
            MemoryStream stream = new MemoryStream();
            OutputChannel channel = new OutputChannel(new BinaryWriter(stream));

            channel.Write(123.45);

            stream.Seek(0, SeekOrigin.Begin);

            BinaryReader reader = new BinaryReader(stream);

            Assert.AreEqual((byte)Types.Double, reader.ReadByte());
            Assert.AreEqual(123.45, reader.ReadDouble());
            Assert.AreEqual(-1, reader.PeekChar());

            reader.Close();
        }
Ejemplo n.º 6
0
        public void WriteDecimal()
        {
            MemoryStream stream = new MemoryStream();
            OutputChannel channel = new OutputChannel(new BinaryWriter(stream));

            decimal dc = 12.34m;

            channel.Write(dc);

            stream.Seek(0, SeekOrigin.Begin);

            BinaryReader reader = new BinaryReader(stream);

            Assert.AreEqual((byte)Types.Decimal, reader.ReadByte());
            Assert.AreEqual(dc, reader.ReadDecimal());
            Assert.AreEqual(-1, reader.PeekChar());

            reader.Close();
        }
Ejemplo n.º 7
0
        public void WriteChar()
        {
            MemoryStream stream = new MemoryStream();
            OutputChannel channel = new OutputChannel(new BinaryWriter(stream));

            char ch = 'a';

            channel.Write(ch);

            stream.Seek(0, SeekOrigin.Begin);

            BinaryReader reader = new BinaryReader(stream);

            Assert.AreEqual((byte)Types.Character, reader.ReadByte());
            Assert.AreEqual(ch, reader.ReadChar());
            Assert.AreEqual(-1, reader.PeekChar());

            reader.Close();
        }
Ejemplo n.º 8
0
        public void WriteAndReadTwoPersonObjects()
        {
            var person = new Person()
            {
                Id = 1,
                FirstName = "John",
                LastName = "Smith"
            };

            var person2 = new Person()
            {
                Id = 2,
                FirstName = "Adam",
                LastName = "Pearson"
            };

            MemoryStream stream = new MemoryStream();
            OutputChannel output = new OutputChannel(new BinaryWriter(stream));
            output.Write(person);
            output.Write(person2);

            stream.Seek(0, SeekOrigin.Begin);

            InputChannel channel = new InputChannel(new BinaryReader(stream));

            var result = channel.Read();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(Person));

            var newperson = (Person)result;

            Assert.AreEqual(person.Id, newperson.Id);
            Assert.AreEqual(person.FirstName, newperson.FirstName);
            Assert.AreEqual(person.LastName, newperson.LastName);

            result = channel.Read();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(Person));

            var newperson2 = (Person)result;

            Assert.AreEqual(person2.Id, newperson2.Id);
            Assert.AreEqual(person2.FirstName, newperson2.FirstName);
            Assert.AreEqual(person2.LastName, newperson2.LastName);
        }
Ejemplo n.º 9
0
        public void ReadShortInteger()
        {
            MemoryStream stream = new MemoryStream();
            OutputChannel output = new OutputChannel(new BinaryWriter(stream));
            short sh = short.MaxValue;
            output.Write(sh);
            stream.Seek(0, SeekOrigin.Begin);

            InputChannel channel = new InputChannel(new BinaryReader(stream));

            Assert.AreEqual(sh, channel.Read());
        }
Ejemplo n.º 10
0
        public void ReadNull()
        {
            MemoryStream stream = new MemoryStream();
            OutputChannel output = new OutputChannel(new BinaryWriter(stream));
            output.Write(null);
            stream.Seek(0, SeekOrigin.Begin);

            InputChannel channel = new InputChannel(new BinaryReader(stream));

            Assert.IsNull(channel.Read());
        }
Ejemplo n.º 11
0
        public void ReadLongInteger()
        {
            MemoryStream stream = new MemoryStream();
            OutputChannel output = new OutputChannel(new BinaryWriter(stream));
            long ln = long.MaxValue;
            output.Write(ln);
            stream.Seek(0, SeekOrigin.Begin);

            InputChannel channel = new InputChannel(new BinaryReader(stream));

            Assert.AreEqual(ln, channel.Read());
        }
Ejemplo n.º 12
0
        public void WriteShortInteger()
        {
            MemoryStream stream = new MemoryStream();
            OutputChannel channel = new OutputChannel(new BinaryWriter(stream));

            short sh = 123;

            channel.Write(sh);

            stream.Seek(0, SeekOrigin.Begin);

            BinaryReader reader = new BinaryReader(stream);

            Assert.AreEqual((byte)Types.Short, reader.ReadByte());
            Assert.AreEqual(sh, reader.ReadInt16());
            Assert.AreEqual(-1, reader.PeekChar());

            reader.Close();
        }
Ejemplo n.º 13
0
        public void SerializePersonObject()
        {
            var serializer = new TypeSerializer(typeof(Person));
            var person = new Person()
            {
                Id = 1,
                FirstName = "John",
                LastName = "Smith"
            };

            MemoryStream stream = new MemoryStream();
            OutputChannel output = new OutputChannel(new BinaryWriter(stream));
            serializer.SerializeObject(person, output);
            stream.Seek(0, SeekOrigin.Begin);

            InputChannel channel = new InputChannel(new BinaryReader(stream));

            Assert.AreEqual(1, channel.Read());
            Assert.AreEqual("John", channel.Read());
            Assert.AreEqual("Smith", channel.Read());
        }
Ejemplo n.º 14
0
 public void SerializeObject(object obj, OutputChannel channel)
 {
     foreach (var prop in this.properties)
         channel.Write(this.type.GetProperty(prop.Name).GetValue(obj, null));
 }
Ejemplo n.º 15
0
        public void WriteString()
        {
            MemoryStream stream = new MemoryStream();
            OutputChannel channel = new OutputChannel(new BinaryWriter(stream));

            channel.Write("foo");

            stream.Seek(0, SeekOrigin.Begin);

            BinaryReader reader = new BinaryReader(stream);

            Assert.AreEqual((byte)Types.String, reader.ReadByte());
            Assert.AreEqual("foo", reader.ReadString());
            Assert.AreEqual(-1, reader.PeekChar());

            reader.Close();
        }
Ejemplo n.º 16
0
        public void SerializeDeserializeType()
        {
            var serializer = new TypeSerializer(typeof(Person));

            MemoryStream stream = new MemoryStream();
            OutputChannel output = new OutputChannel(new BinaryWriter(stream));
            output.Write(serializer);
            stream.Seek(0, SeekOrigin.Begin);

            InputChannel channel = new InputChannel(new BinaryReader(stream));

            var result = channel.Read(true);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(TypeSerializer));

            var newserializer = (TypeSerializer)result;
            var props = newserializer.Properties;

            Assert.IsNotNull(props);
            Assert.AreEqual(3, props.Count);

            Assert.AreEqual("Id", props[0].Name);
            Assert.AreEqual(Types.Integer, props[0].Type);
            Assert.IsNull(props[0].TypeName);

            Assert.AreEqual("FirstName", props[1].Name);
            Assert.AreEqual(Types.String, props[1].Type);
            Assert.IsNull(props[1].TypeName);

            Assert.AreEqual("LastName", props[2].Name);
            Assert.AreEqual(Types.String, props[2].Type);
            Assert.IsNull(props[2].TypeName);
        }
Ejemplo n.º 17
0
        public void WriteLongInteger()
        {
            MemoryStream stream = new MemoryStream();
            OutputChannel channel = new OutputChannel(new BinaryWriter(stream));

            long ln = 123;

            channel.Write(ln);

            stream.Seek(0, SeekOrigin.Begin);

            BinaryReader reader = new BinaryReader(stream);

            Assert.AreEqual((byte)Types.Long, reader.ReadByte());
            Assert.AreEqual(ln, reader.ReadInt64());
            Assert.AreEqual(-1, reader.PeekChar());

            reader.Close();
        }