public void SortTest() { int[] data = { 6, 1, 3, 5, 7, 2, 4 }; IList<int> lst = new List<int>(); int expectedCount = 7; using (IForwardIterator<int> inputIterator=new ForwardIterator<int>(data)) { Algorithm.Sort(inputIterator); using (IOutputIterator<int> outputIterator = new BackInsertIterator<int>(lst)) { inputIterator.Begin(); Algorithm.Copy(inputIterator, outputIterator); } int actualCount = lst.Count(); Assert.IsTrue(expectedCount==actualCount); } bool isCorrectData = (lst[0] == 1 && lst[1] == 2 && lst[2] == 3 && lst[3] == 4 && lst[4] == 5 && lst[5]==6 && lst[6] == 7); Assert.IsTrue(isCorrectData); }
public void RandomShuffle() { int[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16 }; using (IForwardIterator <int> inputIterator = new ForwardIterator <int>(data)) { Algorithm.RandomShuffle(inputIterator); inputIterator.Begin(); bool isSorted = Algorithm.IsSorted(inputIterator); //we have random the data , therefore it should not be sorted //the chance of returning the sorted data is very small Assert.IsFalse(isSorted); } }
public void RotateTest2() { int[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; IList <int> lst = new List <int>(); using (IForwardIterator <int> inputIterator = new ForwardIterator <int>(data)) { IShallowClone cloneable = (IShallowClone)inputIterator; IForwardIterator <int> cloneObj = cloneable.ShallowClone() as IForwardIterator <int>; cloneObj.End(); ICursor cursor = (ICursor)cloneObj; cursor.SetPosition(cursor.GetPosition() - 2); //set the first element to 8 element position Algorithm.Rotate(inputIterator, cloneObj); inputIterator.Begin(); using (IOutputIterator <int> outputIterator = new BackInsertIterator <int>(lst)) { Algorithm.Copy(inputIterator, outputIterator); } } bool isCorrectData = (lst[0] == 8 && lst[1] == 9 && lst[2] == 1 && lst[3] == 2 && lst[4] == 3 && lst[5] == 4 && lst[6] == 5 && lst[7] == 6 && lst[8] == 7); Assert.IsTrue(isCorrectData); }
public void Shuffle() { int[] data = { 1, 2, 3, 4, 5, 6, 7, 8 }; int seed = 23; IList <int> lst1 = new List <int>(); IList <int> lst2 = new List <int>(); using (IForwardIterator <int> inputIterator = new ForwardIterator <int>(data)) { Algorithm.Shuffle(inputIterator, seed); inputIterator.Begin(); bool isSorted = Algorithm.IsSorted(inputIterator); //we have random the data , therefore it should not be sorted //the chance of returning the sorted data is very small Assert.IsFalse(isSorted); inputIterator.Begin(); //copy the random data to list 1 using (IOutputIterator <int> outputIterator = new BackInsertIterator <int>(lst1)) { Algorithm.Copy(inputIterator, outputIterator); } inputIterator.Begin(); //copy the random data to list 2 using (IOutputIterator <int> outputIterator = new BackInsertIterator <int>(lst2)) { Algorithm.Copy(inputIterator, outputIterator); } } using (IForwardIterator <int> inputIterator = new ForwardIterator <int>(data)) { //shuffle another time Algorithm.Shuffle(inputIterator, seed); //copy the random data to list 2 using (IOutputIterator <int> outputIterator = new BackInsertIterator <int>(lst1)) { Algorithm.Copy(inputIterator, outputIterator); } } bool isCorrectData = (lst1[0] == lst2[0] && lst1[1] == lst2[1] && lst1[2] == lst2[2] && lst1[3] == lst2[3] && lst1[4] == lst2[4] && lst1[5] == lst2[5] && lst1[6] == lst2[6] && lst1[7] == lst2[7]); //since we use the same seed,both shuffle data should be same Assert.IsTrue(isCorrectData); }