using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Azimuth; namespace Azimuth.UnitTests { public class PermutationsTests : UnitTestSharp.TestFixture { public class ValidatePermutationList : UnitTestSharp.TestFixture { public void ExampleThatPasses() { var permutations = new int[] { 2, 3, 4, 5, 0, 1, }; // If it doesn't crash, it passes Permutations.ValidatePermutationList(permutations, 6, permutations.Length); } public void DuplicateIndex() { var permutations = new int[] { 2, 3, 4, 5, 2, 1, }; CheckThrow(typeof(Exception)); Permutations.ValidatePermutationList(permutations, 6, permutations.Length); } public void NegativeIndex() { var permutations = new int[] { 2, 3, 4, 5, -1, 1, }; CheckThrow(typeof(Exception)); Permutations.ValidatePermutationList(permutations, 6, permutations.Length); } public void VectorLengthTooShort() { var permutations = new int[] { 2, 3, 4, 5, 0, 1, }; CheckThrow(typeof(Exception)); Permutations.ValidatePermutationList(permutations, 5, permutations.Length); } public void PartialPermutation() { var permutations = new int[] { 98, 97, 96, 0, 1, 2, }; // If it doesn't crash it passes Permutations.ValidatePermutationList(permutations, 99, permutations.Length); } public void Empty() { var permutations = new int[] { }; // If it doesn't crash it passes Permutations.ValidatePermutationList(permutations, 0, permutations.Length); } } public class ValidatePermutationCycles : UnitTestSharp.TestFixture { public void ExampleThatPasses() { var permutations = new int[] { 2, 0, 1, 3, 2, 3, 4, }; // If it doesn't crash, it passes Permutations.ValidatePermutationCycle(permutations, 5); } public void VectorLengthTooSmall() { var permutations = new int[] { 2, 0, 1, 3, 2, 3, 4, }; CheckThrow(typeof(Exception)); Permutations.ValidatePermutationCycle(permutations, 4); } public void NegativeIndex() { var permutations = new int[] { 2, 0, 1, 3, -1, 3, 4, }; CheckThrow(typeof(Exception)); Permutations.ValidatePermutationCycle(permutations, 5); } public void DuplicateIndex() { var permutations = new int[] { 2, 0, 1, 3, 0, 3, 4, }; CheckThrow(typeof(Exception)); Permutations.ValidatePermutationCycle(permutations, 5); } public void NegativeLength() { var permutations = new int[] { 2, 0, 1, -3, 2, 3, 4, }; CheckThrow(typeof(Exception)); Permutations.ValidatePermutationCycle(permutations, 5); } public void LengthTooLong() { var permutations = new int[] { 2, 0, 1, 4, 2, 3, 4, }; CheckThrow(typeof(Exception)); Permutations.ValidatePermutationCycle(permutations, 5); } public void Empty() { var permutations = new int[] { }; // No crash then it passes Permutations.ValidatePermutationCycle(permutations, 4); } public void ZeroThenTrash() { var permutations = new int[] { 0, -1, -2, -3, }; // No crash then it passes Permutations.ValidatePermutationCycle(permutations, 4); } } public class ConstructPermutationCycles : UnitTestSharp.TestFixture { public void Basic() { var permutationList = new int[] { 4, 3, 2, 0, 1 }; var permutationCycles = new int[6]; var expectedPermutationList = (IList)permutationList.Clone(); Permutations.ConstructPermutationCycles(permutationCycles, permutationList); var expectedPermutationCycles = new int[] { 4, 0, 4, 1, 3, 0 }; CheckEqual(expectedPermutationList, permutationList); CheckEqual(expectedPermutationCycles, permutationCycles); } public void Identity() { var permutationList = new int[] { 0, 1, 2, 3, 4, }; var permutationCycles = new int[6]; var expectedPermutationList = (IList)permutationList.Clone(); Permutations.ConstructPermutationCycles(permutationCycles, permutationList); var expectedPermutationCycles = new int[] { 0, 0, 0, 0, 0, 0, }; CheckEqual(expectedPermutationList, permutationList); CheckEqual(expectedPermutationCycles, permutationCycles); } public void DirtyArray() { var permutationList = new int[] { 0, 1, 2, 3, 4, }; var permutationCycles = new int[] { -1, -2, -3, -4, -5, -6, }; var expectedPermutationList = (IList)permutationList.Clone(); Permutations.ConstructPermutationCycles(permutationCycles, permutationList); var expectedPermutationCycles = new int[] { 0, -2, -3, -4, -5, -6, }; CheckEqual(expectedPermutationList, permutationList); CheckEqual(expectedPermutationCycles, permutationCycles); } public void FullCycle() { var permutationList = new int[] { 4, 0, 1, 2, 3, }; var permutationCycles = new int[6]; var expectedPermutationList = (IList)permutationList.Clone(); Permutations.ConstructPermutationCycles(permutationCycles, permutationList); var expectedPermutationCycles = new int[] { 5, 0, 4, 3, 2, 1, }; CheckEqual(expectedPermutationList, permutationList); CheckEqual(expectedPermutationCycles, permutationCycles); } public void ExplicitArguments() { var permutationList = new int[] { 4, 0, 1, 2, 3, }; var permutationCycles = new int[6]; var expectedPermutationList = (IList)permutationList.Clone(); Permutations.ConstructPermutationCycles(permutationCycles, permutationList, 5); var expectedPermutationCycles = new int[] { 5, 0, 4, 3, 2, 1, }; CheckEqual(expectedPermutationList, permutationList); CheckEqual(expectedPermutationCycles, permutationCycles); } public void BigCyclesArray() { var permutationList = new int[] { 4, 0, 1, 2, 3, }; var permutationCycles = new int[7]; var expectedPermutationList = (IList)permutationList.Clone(); Permutations.ConstructPermutationCycles(permutationCycles, permutationList); var expectedPermutationCycles = new int[] { 5, 0, 4, 3, 2, 1, 0 }; CheckEqual(expectedPermutationList, permutationList); CheckEqual(expectedPermutationCycles, permutationCycles); } public void CyclesTooSmall() { var permutationList = new int[] { 4, 0, 1, 2, 3, }; var permutationCycles = new int[5]; CheckThrow(typeof(Exception)); Permutations.ConstructPermutationCycles(permutationCycles, permutationList); } public void ListHasDuplicates() { var permutationList = new int[] { 4, 0, 1, 0, 3, }; var permutationCycles = new int[6]; CheckThrow(typeof(Exception)); Permutations.ConstructPermutationCycles(permutationCycles, permutationList); } public void SubLength() { var permutationList = new int[] { 4, 0, 1, 2, 3, 5, }; var permutationCycles = new int[6]; var expectedPermutationList = (IList)permutationList.Clone(); Permutations.ConstructPermutationCycles(permutationCycles, permutationList, 5); var expectedPermutationCycles = new int[] { 5, 0, 4, 3, 2, 1, }; CheckEqual(expectedPermutationList, permutationList); CheckEqual(expectedPermutationCycles, permutationCycles); } public void PartialList() { // What we do if we're only pivoting to get the first couple of columns and ignoring the rest: // we close the loop on any cycles after we reach an element that's beyond the index of the // permutation list. var permutationList = new int[] { 98, 32, 11, 4, 3, }; var permutationCycles = new int[12]; var expectedPermutationList = (IList)permutationList.Clone(); Permutations.ConstructPermutationCycles(permutationCycles, permutationList); var expectedPermutationCycles = new int[] { 2, 0, 98, 2, 1, 32, 2, 2, 11, 2, 3, 4, }; CheckEqual(expectedPermutationList, permutationList); CheckEqual(expectedPermutationCycles, permutationCycles); } public void NegativeElementInList() { var permutationList = new int[] { 4, -3, 2, 0, 1 }; var permutationCycles = new int[6]; CheckThrow(typeof(Exception)); Permutations.ConstructPermutationCycles(permutationCycles, permutationList); } public void SwapSecondAndThird() { var permutationList = new int[] { 0, 2, 1, 3}; var permutationCycles = new int[6]; var expectedPermutationCycles = new int[] { 2, 1, 2, 0, 0, 0 }; Permutations.ConstructPermutationCycles(permutationCycles, permutationList); CheckEqual(expectedPermutationCycles, permutationCycles); } } } }