using System; using System.IO; using UnitTestSharp; namespace Vellum.UnitTests { public class SavedGridTests : TestFixture { public static SaveSession Session() { return SaveConvertersTests.TestConverters().CreateSession(); } /// /// Returns a grid table whose lengths and items are the numbers given. /// public static SaveNode Grid(int[] lengths, params int[] items) { SaveNode returnMe = SaveNode.NewTable(); returnMe.Entries["lengths"] = SavedItems.ToNode(lengths, Session()); returnMe.Entries["items"] = SavedItems.ToNode(items, Session()); return returnMe; } public static Array Loaded(SaveNode node, Type arrayType) { return SavedGrid.FromNode(node, arrayType, GridAccessors.ForRank(arrayType.GetArrayRank()), Session()); } public class ToNodeTests : TestFixture { public void DimensionThatDoesntStartAtZero_Throws() { Array shifted = Array.CreateInstance(typeof(int), new[] { 2, 2 }, new[] { 1, 0 }); CheckThrow(typeof(NotSupportedException)); SavedGrid.ToNode(shifted, GridAccessors.ForRank(2), Session()); } public void ArrayOfAnyNumberOfDimensions_SavesItsLengths() { SaveNode node = SavedGrid.ToNode(new int[1, 2, 3, 4], GridAccessors.ForRank(4), Session()); CheckEqual(4, node.RequireEntry("lengths").Items.Count); CheckEqual(24, node.RequireEntry("items").Items.Count); } } public class FromNodeTests : TestFixture { public void Lengths_MakeTheArray() { var loaded = (int[,])Loaded(Grid(new[] { 2, 2 }, 1, 2, 3, 4), typeof(int[,])); CheckEqual(3, loaded[1, 0]); } public void FewerLengthsThanTheRank_Throws() { CheckThrow(typeof(InvalidDataException)); Loaded(Grid(new[] { 4 }, 1, 2, 3, 4), typeof(int[,])); } public void MoreLengthsThanTheRank_Throws() { CheckThrow(typeof(InvalidDataException)); Loaded(Grid(new[] { 1, 2, 2 }, 1, 2, 3, 4), typeof(int[,])); } public void NegativeLength_Throws() { CheckThrow(typeof(InvalidDataException)); Loaded(Grid(new[] { -2, -2 }, 1, 2, 3, 4), typeof(int[,])); } public void FewerItemsThanTheLengthsHold_Throws() { CheckThrow(typeof(InvalidDataException)); Loaded(Grid(new[] { 2, 3 }, 1, 2, 3, 4), typeof(int[,])); } public void MoreItemsThanTheLengthsHold_Throws() { CheckThrow(typeof(InvalidDataException)); Loaded(Grid(new[] { 1, 2 }, 1, 2, 3, 4), typeof(int[,])); } public void LengthsWhoseProductOverflows_Throw() { CheckThrow(typeof(InvalidDataException)); Loaded(Grid(new[] { int.MaxValue, int.MaxValue, int.MaxValue }, 1), typeof(int[,,])); } public void HugeLengthBesideAZeroLength_IsAnEmptyArray() { var loaded = (int[,])Loaded(Grid(new[] { 1000000, 0 }), typeof(int[,])); CheckEqual(1000000, loaded.GetLength(0)); } public void MissingLengthsOrItems_Throw() { SaveNode node = SaveNode.NewTable(); node.Entries["items"] = SaveNode.NewList(); CheckThrow(typeof(InvalidDataException)); Loaded(node, typeof(int[,])); } } public class AllocateTests : TestFixture { public void Lengths_MakeAnEmptyArray() { var made = (int[,])SavedGrid.Allocate(Grid(new[] { 2, 3 }, 1, 2, 3, 4, 5, 6), typeof(int[,]), Session()); CheckEqual(2, made.GetLength(0)); CheckEqual(3, made.GetLength(1)); CheckEqual(0, made[1, 2]); } public void LengthsThatDontFitTheItems_Throw() { CheckThrow(typeof(InvalidDataException)); SavedGrid.Allocate(Grid(new[] { 2, 2 }, 1, 2, 3), typeof(int[,]), Session()); } public void FewerLengthsThanTheRank_Throws() { CheckThrow(typeof(InvalidDataException)); SavedGrid.Allocate(Grid(new[] { 2 }, 1, 2), typeof(int[,]), Session()); } } public class FillTests : TestFixture { public void ItemsGoInRowByRow() { SaveNode node = Grid(new[] { 2, 2 }, 1, 2, 3, 4); var made = (int[,])SavedGrid.Allocate(node, typeof(int[,]), Session()); SavedGrid.Fill(made, node, GridAccessors.ForRank(2), Session()); CheckEqual(1, made[0, 0]); CheckEqual(2, made[0, 1]); CheckEqual(3, made[1, 0]); CheckEqual(4, made[1, 1]); } public void ThreeDimensions_AreFilledToo() { SaveNode node = Grid(new[] { 1, 2, 2 }, 1, 2, 3, 4); var made = (int[,,])SavedGrid.Allocate(node, typeof(int[,,]), Session()); SavedGrid.Fill(made, node, GridAccessors.ForRank(3), Session()); CheckEqual(4, made[0, 1, 1]); } public void NodeWithoutItems_Throws() { var made = new int[1, 1]; CheckThrow(typeof(InvalidDataException)); SavedGrid.Fill(made, SaveNode.NewTable(), GridAccessors.ForRank(2), Session()); } } } }