本文整理汇总了C#中ConcurrentStack.CopyTo方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentStack.CopyTo方法的具体用法?C# ConcurrentStack.CopyTo怎么用?C# ConcurrentStack.CopyTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentStack
的用法示例。
在下文中一共展示了ConcurrentStack.CopyTo方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Push_5_Integers
public void Push_5_Integers()
{
var expected = new int[] { 4, 3, 2, 1, 0 };
ConcurrentStack<int> stack = new ConcurrentStack<int>();
for (var i = 0; i < 5; i++)
{
stack.Push(i);
}
var actual = new int[5];
stack.CopyTo(actual, 0);
CollectionAssert.AreEqual(expected, actual);
}
示例2: RunConcurrentStackTest5_CtorAndCopyToAndToArray
// Instantiates the stack w/ the enumerator ctor and validates the resulting copyto & toarray.
private static bool RunConcurrentStackTest5_CtorAndCopyToAndToArray(int count)
{
TestHarness.TestLog("* RunConcurrentStackTest5_CtorAndCopyToAndToArray()");
int[] arr = new int[count];
for (int i = 0; i < count; i++) arr[i] = i;
ConcurrentStack<int> s = new ConcurrentStack<int>(arr);
// try toarray.
int[] sa1 = s.ToArray();
if (sa1.Length != arr.Length)
{
TestHarness.TestLog(" > ToArray resulting array is diff length: got {0}, wanted {1}",
sa1.Length, arr.Length);
return false;
}
for (int i = 0; i < sa1.Length; i++)
{
if (sa1[i] != arr[count - i - 1])
{
TestHarness.TestLog(" > ToArray returned an array w/ diff contents: got {0}, wanted {1}",
sa1[i], arr[count - i - 1]);
return false;
}
}
int[] sa2 = new int[count];
s.CopyTo(sa2, 0);
if (sa2.Length != arr.Length)
{
TestHarness.TestLog(" > CopyTo(int[]) resulting array is diff length: got {0}, wanted {1}",
sa2.Length, arr.Length);
return false;
}
for (int i = 0; i < sa2.Length; i++)
{
if (sa2[i] != arr[count - i - 1])
{
TestHarness.TestLog(" > CopyTo(int[]) returned an array w/ diff contents: got {0}, wanted {1}",
sa2[i], arr[count - i - 1]);
return false;
}
}
object[] sa3 = new object[count]; // test array variance.
((System.Collections.ICollection)s).CopyTo(sa3, 0);
if (sa3.Length != arr.Length)
{
TestHarness.TestLog(" > CopyTo(object[]) resulting array is diff length: got {0}, wanted {1}",
sa3.Length, arr.Length);
return false;
}
for (int i = 0; i < sa3.Length; i++)
{
if ((int)sa3[i] != arr[count - i - 1])
{
TestHarness.TestLog(" > CopyTo(object[]) returned an array w/ diff contents: got {0}, wanted {1}",
sa3[i], arr[count - i - 1]);
return false;
}
}
return true;
}
示例3: Test8_Exceptions
public static void Test8_Exceptions()
{
ConcurrentStack<int> stack = null;
Assert.Throws<ArgumentNullException>(
() => stack = new ConcurrentStack<int>((IEnumerable<int>)null));
// "Test8_Exceptions: The constructor didn't throw ANE when null collection passed");
stack = new ConcurrentStack<int>();
//CopyTo
Assert.Throws<ArgumentNullException>(
() => stack.CopyTo(null, 0));
// "Test8_Exceptions: CopyTo didn't throw ANE when null array passed");
Assert.Throws<ArgumentOutOfRangeException>(
() => stack.CopyTo(new int[1], -1));
// "Test8_Exceptions: CopyTo didn't throw AORE when negative array index passed");
//PushRange
Assert.Throws<ArgumentNullException>(
() => stack.PushRange(null));
// "Test8_Exceptions: PushRange didn't throw ANE when null array passed");
Assert.Throws<ArgumentOutOfRangeException>(
() => stack.PushRange(new int[1], 0, -1));
// "Test8_Exceptions: PushRange didn't throw AORE when negative count passed");
Assert.Throws<ArgumentOutOfRangeException>(
() => stack.PushRange(new int[1], -1, 1));
// "Test8_Exceptions: PushRange didn't throw AORE when negative index passed");
Assert.Throws<ArgumentOutOfRangeException>(
() => stack.PushRange(new int[1], 2, 1));
// "Test8_Exceptions: PushRange didn't throw AORE when start index > array length");
Assert.Throws<ArgumentException>(
() => stack.PushRange(new int[1], 0, 10));
// "Test8_Exceptions: PushRange didn't throw AE when count + index > array length");
//PopRange
Assert.Throws<ArgumentNullException>(
() => stack.TryPopRange(null));
// "Test8_Exceptions: TryPopRange didn't throw ANE when null array passed");
Assert.Throws<ArgumentOutOfRangeException>(
() => stack.TryPopRange(new int[1], 0, -1));
// "Test8_Exceptions: TryPopRange didn't throw AORE when negative count passed");
Assert.Throws<ArgumentOutOfRangeException>(
() => stack.TryPopRange(new int[1], -1, 1));
// "Test8_Exceptions: TryPopRange didn't throw AORE when negative index passed");
Assert.Throws<ArgumentOutOfRangeException>(
() => stack.TryPopRange(new int[1], 2, 1));
// "Test8_Exceptions: TryPopRange didn't throw AORE when start index > array length");
Assert.Throws<ArgumentException>(
() => stack.TryPopRange(new int[1], 0, 10));
// "Test8_Exceptions: TryPopRange didn't throw AE when count + index > array length");
}
示例4: Test5_CtorAndCopyToAndToArray
// Instantiates the stack w/ the enumerator ctor and validates the resulting copyto & toarray.
private static void Test5_CtorAndCopyToAndToArray(int count)
{
int[] arr = new int[count];
for (int i = 0; i < count; i++) arr[i] = i;
ConcurrentStack<int> s = new ConcurrentStack<int>(arr);
// try toarray.
int[] sa1 = s.ToArray();
Assert.Equal(arr.Length, sa1.Length);
for (int i = 0; i < sa1.Length; i++)
{
Assert.Equal(arr[count - i - 1], sa1[i]);
}
int[] sa2 = new int[count];
s.CopyTo(sa2, 0);
Assert.Equal(arr.Length, sa2.Length);
for (int i = 0; i < sa2.Length; i++)
{
Assert.Equal(arr[count - i - 1], sa2[i]);
}
object[] sa3 = new object[count]; // test array variance.
((System.Collections.ICollection)s).CopyTo(sa3, 0);
Assert.Equal(arr.Length, sa3.Length);
for (int i = 0; i < sa3.Length; i++)
{
Assert.Equal(arr[count - i - 1], (int)sa3[i]);
}
}