Exemplo n.º 1
0
        public void TestGeneralBinaryPackerFunctionality()
        {
            byte[]    bytearray = new byte[1024];
            int       position  = 0;
            TestClass testclass = new TestClass();

            testclass.booleanvaluetrue = true;
            testclass.charvalue        = 'C';
            testclass.doublevalue      = 123.4567890;
            testclass.intvalue         = 1234567890;
            testclass.Country          = "Spain";
            testclass.name             = "Test class name";
            testclass.indexes          = new int[] { 5, 1, 4, 2, 3 };
            testclass.childclass       = new ChildClass();
            testclass.childclass.name  = "name inside child class";

            BinaryPacker bp = new BinaryPacker();

            //Write several objects into the buffer
            bp.WriteValueToBuffer(bytearray, ref position, testclass);
            bp.WriteValueToBuffer(bytearray, ref position, "The quick brown fox.");
            bp.WriteValueToBuffer(bytearray, ref position, "Rain in Spain.");

            bp.PackObjectUsingSpecifiedAttributes(bytearray, ref position, testclass, new Type[] { typeof(AttributePack) });

            byte[] bytearraytowrite = Encoding.UTF8.GetBytes("Hello world");
            bp.WriteValueToBuffer(bytearray, ref position, bytearraytowrite);


            FractalSplineBox box = new FractalSplineBox();

            box.name = "Test box";


            box.pos = new Vector3(1, 123.123, 150.150);

            bp.PackObjectUsingSpecifiedAttributes(bytearray, ref position, box, new Type[] { typeof(Replicate) });

            position = 0;

            //verify all the items we put in
            TestClass outputobject = (TestClass) new BinaryPacker().ReadValueFromBuffer(bytearray, ref position, typeof(TestClass));

            string error = "Trouble unpacking TestClass from BinaryPacker";

            Assert.AreEqual(true, outputobject.booleanvaluetrue, error);
            Assert.AreEqual('C', outputobject.charvalue, error);
            Assert.AreEqual(123.4567890, outputobject.doublevalue, error);
            Assert.AreEqual(1234567890, outputobject.intvalue, error);
            Assert.AreEqual("Spain", outputobject.Country, error);
            Assert.AreEqual("Test class name", outputobject.name, error);
            Assert.AreEqual(5, outputobject.indexes.Length, error);
            Assert.AreEqual(5, outputobject.indexes[0], error);
            Assert.AreEqual(1, outputobject.indexes[1], error);
            Assert.AreEqual(4, outputobject.indexes[2], error);
            Assert.AreEqual(2, outputobject.indexes[3], error);
            Assert.AreEqual(3, outputobject.indexes[4], error);
            Assert.AreEqual("name inside child class", outputobject.childclass.name, error);


            string sout = (string)new BinaryPacker().ReadValueFromBuffer(bytearray, ref position, typeof(string));

            Assert.AreEqual("The quick brown fox.", sout, "Could not unpack string");

            sout = (string)new BinaryPacker().ReadValueFromBuffer(bytearray, ref position, typeof(string));
            Assert.AreEqual("Rain in Spain.", sout, "Could not unpack second string");

            outputobject = new TestClass();
            bp.UnpackIntoObjectUsingSpecifiedAttributes(bytearray, ref position, outputobject, new Type[] { typeof(AttributePack) });
            Assert.AreEqual(null, outputobject.name, "Error in value of unset value in attribute packing");              // should be blank, because name member has a AttributePack attribute
            Assert.AreEqual("Spain", outputobject.Country, "Error in value of set value in attribute packing");          //Country does have an AttributePack attribute

            bytearraytowrite = (byte[])new BinaryPacker().ReadValueFromBuffer(bytearray, ref position, typeof(byte[]));
            Assert.AreEqual("Hello world", Encoding.UTF8.GetString(bytearraytowrite), "Error in unpacking UTF8");
            FractalSplineBox boxobject = new FractalSplineBox();

            new BinaryPacker().UnpackIntoObjectUsingSpecifiedAttributes(bytearray, ref position, boxobject, new Type[] { typeof(Replicate) });
            Assert.AreEqual("Test box", boxobject.name, "Error in unpacking FractelSplineBox name");
            Assert.AreEqual(new Vector3(1, 123.123, 150.150), box.pos, "Error in unpacking FractelSplineBox position");
        }