Exemple #1
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);
        }
        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);
        }
Exemple #3
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());
        }
Exemple #4
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());
        }
        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());
        }
Exemple #6
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());
        }
Exemple #7
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());
        }
Exemple #8
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());
        }
Exemple #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());
        }
Exemple #10
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());
        }
Exemple #11
0
        public void ReadInvalidData()
        {
            MemoryStream stream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(stream);

            writer.Write((byte)255);
            stream.Seek(0, SeekOrigin.Begin);

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

            try
            {
                channel.Read();
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(InvalidDataException));
            }
        }