public void CanReadAndWriteBools() { const int MAX = 5; bool[] myBoools = new bool[MAX]; for (int i = 0; i < MAX; i++) { myBoools[i] = RandomTools.FiftyFifty(); } // Write the data, then read it back in. using (MemoryStream ms = new MemoryStream()) { for (int i = 0; i < MAX; i++) { EZWriter.Write(ms, myBoools[i]); } ms.Seek(0, SeekOrigin.Begin); for (int i = 0; i < MAX; i++) { bool nextBool = EZReader.ReadBool(ms); Assert.AreEqual(myBoools[i], nextBool, "Invalid boolean at index #{0}", i); } } }
public void CanReadAndWriteStringWithNullChar() { const int STR_LEN = 32; int rnd = RandomTools.RNG.Next(STR_LEN); string p1 = RandomTools.GetAlphaString(rnd); rnd = RandomTools.RNG.Next(STR_LEN); string p2 = RandomTools.GetAlphaString(rnd); string test = p1 + (char)0 + p2; using (MemoryStream ms = new MemoryStream()) { EZWriter.Write(ms, test); ms.Seek(0, SeekOrigin.Begin); string comp = EZReader.ReadString(ms); Assert.AreEqual(test, comp, "Strings don't match!"); } }
public void CanReadAndWriteString() { const int SIZE = 100; // The first string is null, because we need to be able to support this concept. List <string> testStrings = new List <string>() { null }; for (int i = 0; i < MAX_TEST; i++) { string test = RandomTools.GetAlphaString(SIZE); testStrings.Add(test); } foreach (var test in testStrings) { using (var ms = new MemoryStream()) { EZWriter.Write(ms, test); ms.Seek(0, SeekOrigin.Begin); string comp = EZReader.ReadString(ms); Assert.AreEqual(test, comp, "The strings should be the same!"); } } }
// -------------------------------------------------------------------------------------------------------------------------- public static EZList <T> ReadEZList <T>(Stream s, Func <Stream, T> readFunc) { EZList <T> res = new EZList <T>(); uint count = EZReader.ReadUInt32(s); for (int i = 0; i < count; i++) { T next = readFunc(s); res.Add(next); } return(res); }
public void CanReadWriteInt32() { const int MAX = 100; for (int i = 0; i < MAX; i++) { Int32 next = RandomTools.RNG.Next(int.MinValue, int.MaxValue); using (var ms = new MemoryStream()) { EZWriter.Write(ms, next); ms.Seek(0, SeekOrigin.Begin); int comp = EZReader.ReadInt32(ms); Assert.AreEqual(next, comp); } } }
public void CanReadAndWriteUint() { // const uint test = 0xF00010FF; for (int i = 0; i < MAX_TEST; i++) { // WOW! The C# preference for int over everything sure is annoying! uint test = (uint)RandomTools.RNG.Next((int)uint.MinValue, int.MaxValue); using (var ms = new MemoryStream()) { EZWriter.Write(ms, test); ms.Seek(0, SeekOrigin.Begin); uint comp = EZReader.ReadUInt32(ms); Assert.AreEqual(test, comp, "The uints should be the same!"); } } }
// -------------------------------------------------------------------------------------------------------------------------- // TODO: This won't work for lists or dictionaries! private static void ReadWriteTest <T>(T data) { using (var ms = new MemoryStream()) { MethodInfo writer = typeof(EZWriter).GetMethod("Write", new Type[] { typeof(Stream), typeof(T) }); if (writer == null) { throw new InvalidOperationException(string.Format("Could not find write method for type {0}!", typeof(T))); } writer.Invoke(null, new object[] { ms, data }); ms.Seek(0, SeekOrigin.Begin); T comp = EZReader.Read <T>(ms); //List<string> comp = EZReader.ReadList<string>(ms); ObjectInspector inspector = new ObjectInspector(); inspector.CompareObjects((object)data, (object)comp, true); } }
public void CanReadWriteInt64() { const int MAX = 100; for (int i = 0; i < MAX; i++) { byte[] data = new byte[sizeof(Int64)]; RandomTools.RNG.NextBytes(data); Int64 next = BitConverter.ToInt64(data, 0); using (var ms = new MemoryStream()) { EZWriter.Write(ms, next); ms.Seek(0, SeekOrigin.Begin); Int64 comp = EZReader.ReadInt64(ms); Assert.AreEqual <Int64>(next, comp); } } }
public void CanReadWriteList() { // MethodInfo writer = typeof(EZWriter).GetMethod("Write", new Type[] { typeof(Stream), typeof(T) }); // writer.Invoke(null, new object[] { ms, data }); // TODO: when it works, use 'ReadWriteTest(x)' List <string> strList = new List <string>() { "ABC", "DEF", "GHI" }; using (var ms = new MemoryStream()) { EZWriter.Write(ms, strList); ms.Seek(0, SeekOrigin.Begin); List <string> comp = EZReader.ReadList <string>(ms); ObjectInspector inspector = new ObjectInspector(); inspector.CompareObjects(strList, comp, true); } List <int> intList = new List <int>() { 1, 2, 3, 4, 5 }; using (var ms = new MemoryStream()) { EZWriter.Write(ms, intList); ms.Seek(0, SeekOrigin.Begin); List <int> comp = EZReader.ReadList <int>(ms); ObjectInspector inspector = new ObjectInspector(); inspector.CompareObjects(intList, comp, true); } }
public void CanUseGenericEZReaderMethod() { // A more difficult one... const int MAX = 10; Int32[] data = new Int32[MAX]; for (int i = 0; i < MAX; i++) { data[i] = i; } MemoryStream m = new MemoryStream(); EZWriter.Write(m, data); m.Seek(0, SeekOrigin.Begin); Int32[] comp = EZReader.Read <Int32[]>(m); ObjectInspector oi = new ObjectInspector(); var result = oi.CompareArrays <int>(data, comp); // Make sure that it matches! Assert.IsTrue(result.Item1, result.Item2); // Let's do a string to make sure! string testString = RandomTools.GetRandomCharString(MAX); m = new MemoryStream(); EZWriter.Write(m, testString); m.Seek(0, SeekOrigin.Begin); string compString = EZReader.Read <string>(m); Assert.AreEqual(testString, compString); }
public void CanSerializeDoubles() { double[] testDoubles = new[] { 1.25d, 0.546d, 21.581d }; using (var ms = new MemoryStream()) { foreach (var item in testDoubles) { EZWriter.Write(ms, item); } ms.Seek(0, SeekOrigin.Begin); double[] others = new double[testDoubles.Length]; for (int i = 0; i < others.Length; i++) { others[i] = EZReader.ReadDouble(ms); } ObjectInspector inspector = new ObjectInspector(); inspector.CompareObjects <double[]>(testDoubles, others, true); } }
public void CanSerializeFloats() { float[] testFloats = new[] { 1.25f, 0.546f, 21.581f }; using (var ms = new MemoryStream()) { foreach (var item in testFloats) { EZWriter.Write(ms, item); } ms.Seek(0, SeekOrigin.Begin); float[] others = new float[testFloats.Length]; for (int i = 0; i < others.Length; i++) { others[i] = EZReader.ReadFloat(ms); } ObjectInspector inspector = new ObjectInspector(); inspector.CompareObjects(testFloats, others, true); } }