本文整理汇总了C#中Microsoft.VisualStudio.TestTools.UnitTesting.List.ToSublist方法的典型用法代码示例。如果您正苦于以下问题:C# List.ToSublist方法的具体用法?C# List.ToSublist怎么用?C# List.ToSublist使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.VisualStudio.TestTools.UnitTesting.List
的用法示例。
在下文中一共展示了List.ToSublist方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestHeap_PriorityQueue
public void TestHeap_PriorityQueue()
{
Random random = new Random();
// build a random list
var list = new List<int>(100);
Sublist.Generate(100, i => random.Next(100)).AddTo(list.ToSublist());
// make a heap
list.ToSublist().MakeHeap().InPlace();
Assert.IsTrue(list.ToSublist().IsHeap(), "The list is not a heap."); // confirm we have a heap
// let's push a value onto the heap and make it the highest priority
list.Add(100);
list.ToSublist().HeapAdd();
Assert.AreEqual(100, list[0], "The value was not moved to the top of the heap.");
Assert.IsTrue(list.ToSublist().IsHeap(), "The list is not a heap.");
// now let's remove it
list.ToSublist().HeapRemove();
Assert.AreEqual(100, list[list.Count - 1], "The value not moved to the bottom of the heap.");
Assert.AreEqual(list.Count - 1, list.ToSublist().IsHeap(), "Could not find the end of the heap.");
list.RemoveAt(list.Count - 1);
Assert.IsTrue(list.ToSublist().IsHeap(), "The list is not a heap.");
// we can sort a heap
list.ToSublist().HeapSort();
Assert.IsTrue(list.ToSublist().IsSorted(), "The list was not sorted.");
}
示例2: TestSymmetricExceptAdd_FindUniqueAcrossLists
public void TestSymmetricExceptAdd_FindUniqueAcrossLists()
{
Random random = new Random();
// build two lists
var list1 = new List<int>(100);
Sublist.Generate(100, i => random.Next(100)).AddTo(list1.ToSublist());
var list2 = new List<int>(100);
Sublist.Generate(100, i => random.Next(100)).AddTo(list2.ToSublist());
// make the lists sets
list1.ToSublist(list1.ToSublist().MakeSet().InPlace()).Clear();
list2.ToSublist(list2.ToSublist().MakeSet().InPlace()).Clear();
// find the unique values
var difference = new List<int>();
list1.ToSublist().SymmetricExcept(list2.ToSublist()).AddTo(difference.ToSublist());
// this is the opposite of the intersection, so they should share no items
var intersection = new List<int>();
list1.ToSublist().Intersect(list2.ToSublist()).AddTo(intersection.ToSublist());
bool result = intersection.ToSublist().FindAny(difference.ToSublist());
Assert.IsFalse(result, "Found items in common in the intersection and symmetric difference.");
}
示例3: TestIntersectAdd_FindNumbersDivisibleByTwoAndThree
public void TestIntersectAdd_FindNumbersDivisibleByTwoAndThree()
{
Random random = new Random();
// build all multiples of two
var list1 = new List<int>(50);
Sublist.Generate(50, i => random.Next(50) * 2).AddTo(list1.ToSublist());
// build all multiples of three
var list2 = new List<int>(33);
Sublist.Generate(33, i => random.Next(33) * 3).AddTo(list2.ToSublist());
// make sets
list1.ToSublist(list1.ToSublist().MakeSet().InPlace()).Clear();
list2.ToSublist(list2.ToSublist().MakeSet().InPlace()).Clear();
// find the intersection
var destination = new List<int>();
list1.ToSublist().Intersect(list2.ToSublist()).AddTo(destination.ToSublist());
// make sure all values are divisible by two and three
bool result = destination.ToSublist().Find(i => i % 2 != 0 || i % 3 != 0);
Assert.IsFalse(result, "Some of the items didn't meet the criteria.");
// the result should be all multiple of six
var expected = new List<int>(17); // space for zero
Sublist.Generate(17, i => i * 6).AddTo(expected.ToSublist());
bool containsAll = destination.ToSublist().IsSubset(expected.ToSublist());
Assert.IsTrue(containsAll, "Some of the items weren't multiples of six.");
}
示例4: TestRotateLeftAdd
public void TestRotateLeftAdd()
{
Random random = new Random();
// build list of numbers that divide evenly into 100 and pick one
var divisors = new List<int>(Enumerable.Range(1, 50).Where(i => 100 % i == 0));
var samples = new List<int>(1);
divisors.ToSublist().RandomSamples(1, random).AddTo(samples.ToSublist());
int repeat = samples[0];
// build a list with the numbers 0-4 reoccurring
var list = new List<int>(100);
Sublist.Generate(100, i => i % repeat).AddTo(list.ToSublist());
// try different shifts, looking for reoccurrences
int shift = 1;
while (shift != 100)
{
var rotated = new List<int>(list.Count);
list.ToSublist().RotateLeft(shift).AddTo(rotated.ToSublist());
if (list.ToSublist().IsEqualTo(rotated.ToSublist()))
{
break;
}
++shift;
}
Assert.AreEqual(repeat, shift, "Did not detect the reoccurrence where expected.");
}
示例5: TestCompareTo_ItemByItemComparison
public void TestCompareTo_ItemByItemComparison()
{
Random random = new Random();
var list1 = new List<int>(10);
Sublist.Generate(10, i => random.Next(0, 10)).AddTo(list1.ToSublist());
var list2 = new List<int>(10);
Sublist.Generate(10, i => random.Next(0, 10)).AddTo(list2.ToSublist());
// we know list1 and list2 should be equal to themselves
Assert.AreEqual(0, list1.ToSublist().CompareTo(list1.ToSublist()), "The first list did not equal itself.");
Assert.AreEqual(0, list2.ToSublist().CompareTo(list2.ToSublist()), "The second list did not equal itself.");
// we can use mismatch to confirm that Comparer returned the correct value
int result = list1.ToSublist().CompareTo(list2.ToSublist());
int difference = list1.ToSublist().Mismatch(list2.ToSublist());
if (difference == list1.Count)
{
Assert.AreEqual(0, result, "Mismatch found no differences, but CompareTo did not return zero.");
}
else
{
int first = list1[difference];
int second = list2[difference];
int expected = Comparer<int>.Default.Compare(first, second);
Assert.AreEqual(expected, result, "The mismatching items did not compare the same as the result of CompareTo.");
}
}
示例6: TestPartitionAdd_BreakApartEvensAndOdds
public void TestPartitionAdd_BreakApartEvensAndOdds()
{
Random random = new Random();
// build a list
var list = new List<int>(100);
Sublist.Generate(100, i => random.Next(0, 100)).AddTo(list.ToSublist());
// partition into two
var evens = new List<int>();
var odds = new List<int>();
list.ToSublist().Partition(i => i % 2 == 0).AddTo(evens.ToSublist(), odds.ToSublist());
// sort all three lists -- we need to check if all values were added
list.ToSublist().Sort().InPlace();
evens.ToSublist().Sort().InPlace();
odds.ToSublist().Sort().InPlace();
bool hasOdd = evens.ToSublist().Find(i => i % 2 != 0);
Assert.IsFalse(hasOdd, "Some odds were added to the wrong list.");
bool hasEven = odds.ToSublist().Find(i => i % 2 == 0);
Assert.IsFalse(hasEven, "Some evens were added to the wrong list.");
var combined = new List<int>(100);
evens.ToSublist().AddTo(combined.ToSublist());
odds.ToSublist().AddTo(combined.ToSublist());
combined.ToSublist().Sort().InPlace();
bool hasAllItems = list.ToSublist().IsEqualTo(combined.ToSublist());
Assert.IsTrue(hasAllItems, "Not all items were partitioned.");
}
示例7: TestUnionCopy
public void TestUnionCopy()
{
Random random = new Random();
// build two lists
var list1 = new List<int>(50);
Sublist.Generate(50, i => random.Next(100)).AddTo(list1.ToSublist());
var list2 = new List<int>(50);
Sublist.Generate(50, i => random.Next(100)).AddTo(list2.ToSublist());
// we must make both lists sets
list1.ToSublist(list1.ToSublist().MakeSet().InPlace()).Clear();
list2.ToSublist(list2.ToSublist().MakeSet().InPlace()).Clear();
// now we'll build a new set containing all the items
var destination = new List<int>(100);
Sublist.Generate(100, 0).AddTo(destination.ToSublist());
int result = list1.ToSublist().Union(list2.ToSublist()).CopyTo(destination.ToSublist());
destination.ToSublist(result).Clear();
// make sure the new set contains both of the original sets
bool contains1 = list1.ToSublist().IsSubset(destination.ToSublist());
Assert.IsTrue(contains1, "The union did not contain all the items from the first set.");
bool contains2 = list2.ToSublist().IsSubset(destination.ToSublist());
Assert.IsTrue(contains1, "The union did not contain all the items from the second set.");
}
示例8: TestZipCopy_MultiplyTwoLists
public void TestZipCopy_MultiplyTwoLists()
{
Random random = new Random();
// build the first list
var list1 = new List<int>(100);
Sublist.Generate(100, i => random.Next(100)).AddTo(list1.ToSublist());
// build the second list
var list2 = new List<int>(100);
Sublist.Generate(100, i => random.Next(100)).AddTo(list2.ToSublist());
var destination = new List<int>(100);
Sublist.Generate(100, 0).AddTo(destination.ToSublist());
// multiply the values at each index together
int destinationIndex = list1.ToSublist().Zip(list2.ToSublist(), (i, j) => i * j).CopyTo(destination.ToSublist());
Assert.AreEqual(destination.Count, destinationIndex, "Not all the values were multiplied.");
// check that each value in the destination is the product
for (int index = 0; index != destination.Count; ++index)
{
int product = list1[index] * list2[index];
Assert.AreEqual(product, destination[index], "The destination did not hold the product at index = {0}.", index);
}
}
示例9: TestUpperBound_BuildSet
public void TestUpperBound_BuildSet()
{
Random random = new Random();
// build a list
var list = new List<int>(100);
Sublist.Generate(100, i => random.Next(100)).AddTo(list.ToSublist());
// only add unique items in sorted order
var set = new List<int>();
foreach (int value in list)
{
int index = set.ToSublist().UpperBound(value);
if (index == 0 || set[index - 1] != value)
{
set.Insert(index, value);
}
}
// check that all items are present, sorted and unique
list.ToSublist().Sort().InPlace();
Assert.IsTrue(set.ToSublist().IsSorted(), "The set is not sorted.");
bool hasValues = set.ToSublist().IsSubset(list.ToSublist());
Assert.IsTrue(hasValues, "Not all of the values were copied.");
Assert.IsFalse(set.ToSublist().FindDuplicates(), "A duplicate was found.");
}
示例10: TestPartialSortAdd_GrabTopValues
public void TestPartialSortAdd_GrabTopValues()
{
Random random = new Random();
// build a list
var list = new List<int>(100);
Sublist.Generate(100, i => random.Next(100)).AddTo(list.ToSublist());
// create a list to hold the top three
const int numberOfItems = 10;
var destination = new List<int>(numberOfItems);
// store the top three results
Func<int, int, int> comparison = (x, y) => Comparer<int>.Default.Compare(y, x);
list.ToSublist().PartialSort(numberOfItems, comparison).AddTo(destination.ToSublist());
// grab the three largest values from largest to smallest
var expected = new List<int>(numberOfItems);
for (int round = 0; round != numberOfItems; ++round)
{
int maxIndex = list.ToSublist().Maximum();
expected.Add(list[maxIndex]);
list.RemoveAt(maxIndex);
}
Assert.IsTrue(expected.ToSublist().IsEqualTo(destination.ToSublist()), "The top values weren't grabbed.");
}
示例11: TestGenerateAdd_Counting
public void TestGenerateAdd_Counting()
{
List<int> values = new List<int>();
Sublist.Generate(10, i => i).AddTo(values.ToSublist());
int[] expected = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Assert.IsTrue(expected.ToSublist().IsEqualTo(values.ToSublist()), "The items were not set as expected.");
}
示例12: TestMergeCopy_CombineSortedLists_StaySorted
public void TestMergeCopy_CombineSortedLists_StaySorted()
{
Random random = new Random();
// builds the first list
var list1 = new List<int>(50);
Sublist.Generate(50, i => random.Next(100)).AddTo(list1.ToSublist());
// builds the second list
var list2 = new List<int>(50);
Sublist.Generate(50, i => random.Next(100)).AddTo(list2.ToSublist());
// merging requires sorted lists
list1.ToSublist().Sort().InPlace();
list2.ToSublist().Sort().InPlace();
// merge the lists
var destination = new List<int>(100);
Sublist.Generate(100, 0).AddTo(destination.ToSublist());
int result = list1.ToSublist().Merge(list2.ToSublist()).CopyTo(destination.ToSublist());
Assert.AreEqual(destination.Count, result, "Not all of the items were copied.");
// make sure the destination is still sorted
bool isSorted = destination.ToSublist().IsSorted();
Assert.IsTrue(isSorted, "Merge caused the items to become unsorted.");
}
示例13: TestReverseAdd_InefficientPalindromeChecker
public void TestReverseAdd_InefficientPalindromeChecker()
{
var list = new List<int>() { 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, };
var copy = new List<int>(list.Count);
list.ToSublist().Reverse().AddTo(copy.ToSublist());
Assert.IsTrue(list.ToSublist().IsEqualTo(copy.ToSublist()), "The list was not reversed as expected.");
}
示例14: TestWhereAdd_CopyEvenItems
public void TestWhereAdd_CopyEvenItems()
{
var list = new List<int>() { 1, 2, 3, 4, 5, 6 };
var destination = new List<int>();
// only keep the even items
list.ToSublist().Where(item => item % 2 == 0).AddTo(destination.ToSublist());
int[] expected = { 2, 4, 6 };
Assert.IsTrue(expected.ToSublist().IsEqualTo(destination.ToSublist()), "The items were not where they were expected.");
}
示例15: TestAddTo_IEnumerableSource_MiddleOfList_AddsItemsToBackOfList
public void TestAddTo_IEnumerableSource_MiddleOfList_AddsItemsToBackOfList()
{
var source = Enumerable.Range(3, 2);
var list = new List<int>() { 1, 2, 5, 6 };
var destination = list.ToSublist(2, 0);
destination = source.AddTo(destination);
var expected = new List<int>() { 1, 2, 3, 4, 5, 6 }.ToSublist();
Assert.AreEqual(6, list.Count, "The items were not added to the original list.");
Assert.AreEqual(2, destination.Count, "The size of the sublist was not adjusted.");
Assert.IsTrue(expected.IsEqualTo(list.ToSublist()), "The items were not added as expected.");
}