Click here to Skip to main content
15,888,610 members
Articles / Programming Languages / C#

AltSerializer - An Alternate Binary Serializer

Rate me:
Please Sign up or sign in to vote.
4.93/5 (27 votes)
19 Sep 20066 min read 141.6K   1.9K   69  
The AltSerializer is a replacement for the binary serializer built in to .NET.
using System;
using System.Collections.Generic;
using System.Text;

using AltSerialize;
using NUnit.Framework;
using AltSerializeTest.TestObjects;

namespace AltSerializeTest
{
    [TestFixture]
    public class TestArrays : SerializerTestBase
    {
        public override string Name
        {
            get { return "Array Test"; }
        }

        /// <summary>
        /// Test all value type arrays with a length of zero.
        /// </summary>
        [Test]
        public void ZeroLengthArrayTest()
        {
            foreach (Type type in AllTypes)
            {
                DebugOut("Testing type " + type.FullName + "[0]");
                object valueObject = Array.CreateInstance(type, 0);
                object resultObject = SerializeObject(valueObject);
                Assert.IsTrue(CompareObject(valueObject, resultObject), "Failed on type " + valueObject.GetType().FullName);
            }
        }
        
        public void ArrayTest(Type type, int arraySize)
        {
            Array valueObject = Array.CreateInstance(type, arraySize);
            for (int i = 0; i < arraySize; i++)
            {
                valueObject.SetValue(GetRandomData(type), i);
            }
            object resultObject = SerializeObject(valueObject);
            Assert.IsTrue(CompareObject(valueObject, resultObject), "Failed on type " + valueObject.GetType().FullName);
        }

        /// <summary>
        /// Test all value type arrays with random data.
        /// </summary>
        [Test]
        public void ArrayTest()
        {
            foreach (Type type in AllTypes)
            {
                DebugOut("Testing type " + type.FullName + "[]");
                ArrayTest(type, 100);
            }
        }

        /// <summary>
        /// Tests random arrays of objects.
        /// </summary>
        [Test]
        public void ObjectArrayTest()
        {
            for (int i = 0; i < 100; i++)
            {
                object[] objArray = new object[Rand.Next(1, 100)];
                for (int j = 0; j < objArray.Length; j++)
                {
                    objArray[j] = GetRandomData(ValueTypes[Rand.Next(0, ValueTypes.Length)]);
                }
                object resultArray = SerializeObject(objArray);
                Assert.IsTrue(CompareObject(objArray, resultArray), "Failed", new object[] { objArray, resultArray });
            }
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I've been writing code since I started learning BASIC on the Apple //c. Since then, I've learned C, C++, a few assembly languages. I went to college for Computer Science and Mathematics at the University of Texas at Dallas.

Comments and Discussions