Skip to content

Commit

Permalink
#35 Updated SerializationTests
Browse files Browse the repository at this point in the history
  • Loading branch information
stewienj committed Dec 19, 2024
1 parent f1f2aae commit 1b0daff
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions ExamplesAndTests/Swordfish.NET.UnitTestV3/SerializationTests.cs
Original file line number Diff line number Diff line change
@@ -1,80 +1,86 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Swordfish.NET.Collections;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
#if NETCOREAPP
using System.Text.Json;
#endif

namespace Swordfish.NET.UnitTestV3
{
[TestClass]
public class SerializationTests
{
#if !NET6_0_OR_GREATER
[TestMethod]
public void ConcurrentObservableCollectionSerializationTest()
#if NETCOREAPP
private T RoundTripSerialization<T>(T collection) where T : class
{
var bytes = JsonSerializer.SerializeToUtf8Bytes(collection,
new JsonSerializerOptions { WriteIndented = false });
return JsonSerializer.Deserialize<T>(new ReadOnlySpan<byte>(bytes));
}
#else
private T RoundTripSerialization<T>(T collection) where T : class
{
var serializer = new BinaryFormatter();
var stream = new MemoryStream();
serializer.Serialize(stream, collection);
stream.Position = 0;
return serializer.Deserialize(stream) as T;
}
#endif

[TestMethod]
public void ConcurrentObservableCollectionSerializationTest()
{
var collection = new ConcurrentObservableCollection<string>();
for (int i = 0; i < 10; i++)
collection.Add("TestItem" + (i + 1).ToString());
serializer.Serialize(stream, collection);
stream.Position = 0;
collection = serializer.Deserialize(stream) as ConcurrentObservableCollection<string>;

collection = RoundTripSerialization(collection);

for (int i = 0; i < 10; i++)
Assert.AreEqual("TestItem" + (i + 1).ToString(), collection[i]);
}
#endif

#if !NET6_0_OR_GREATER
[TestMethod]
public void ConcurrentObservableDictionarySerializationTest()
{
var serializer = new BinaryFormatter();
var stream = new MemoryStream();
var collection = new ConcurrentObservableDictionary<string, int>();
for (int i = 0; i < 10; i++)
collection.Add("TestItem" + (i + 1).ToString(), i);
serializer.Serialize(stream, collection);
stream.Position = 0;
collection = serializer.Deserialize(stream) as ConcurrentObservableDictionary<string, int>;

collection = RoundTripSerialization(collection);

for (int i = 0; i < 10; i++)
Assert.AreEqual(i, collection["TestItem" + (i + 1).ToString()]);
}
#endif

#if !NET6_0_OR_GREATER
[TestMethod]
public void ConcurrentObservableSortedDictionarySerializationTest()
{
var serializer = new BinaryFormatter();
var stream = new MemoryStream();
var collection = new ConcurrentObservableSortedDictionary<string, int>();
for (int i = 0; i < 10; i++)
collection.Add("TestItem" + (i + 1).ToString(), i);
serializer.Serialize(stream, collection);
stream.Position = 0;
collection = serializer.Deserialize(stream) as ConcurrentObservableSortedDictionary<string, int>;

collection = RoundTripSerialization(collection);

for (int i = 0; i < 10; i++)
Assert.AreEqual(i, collection["TestItem" + (i + 1).ToString()]);
}
#endif

#if !NET6_0_OR_GREATER
[TestMethod]
public void ConcurrentObservableHashSetSerializationTest()
{
var serializer = new BinaryFormatter();
var stream = new MemoryStream();
var collection = new ConcurrentObservableHashSet<string>();
for (int i = 0; i < 10; i++)
collection.Add("TestItem" + (i + 1).ToString());
serializer.Serialize(stream, collection);
stream.Position = 0;
collection = serializer.Deserialize(stream) as ConcurrentObservableHashSet<string>;

collection = RoundTripSerialization(collection);

Assert.AreEqual(10, collection.Count);
for (int i = 0; i < 10; i++)
Assert.IsTrue(collection.Contains("TestItem" + (i + 1).ToString()));
}
#endif
}
}

0 comments on commit 1b0daff

Please sign in to comment.