本文整理汇总了C#中DynamicArray.Insert方法的典型用法代码示例。如果您正苦于以下问题:C# DynamicArray.Insert方法的具体用法?C# DynamicArray.Insert怎么用?C# DynamicArray.Insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DynamicArray
的用法示例。
在下文中一共展示了DynamicArray.Insert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InsertTest
public void InsertTest()
{
int[] A = new int[] { 1, 2, 3, 4 };
DynamicArray<int> testArr = new DynamicArray<int>(A);
testArr.Insert(42, 3);
Assert.AreEqual(testArr[3], 42);
testArr.Insert(42, testArr.Capacity);
Assert.AreEqual(testArr[testArr.Capacity - 1], 42);
}
示例2: ToArray
public static void ToArray()
{
DynamicArray<int> darray = new DynamicArray<int>(1);
darray.Insert(Data, 0, 0);
Assert.That(darray, Is.EqualTo((int[])darray.ToArray()));
Assert.That(darray, Is.EqualTo((int[])darray));
}
示例3: GenericCopyTo
public static void GenericCopyTo()
{
DynamicArray<int> darray = new DynamicArray<int>(1);
DynamicArray<int> dcopy = new DynamicArray<int>(1);
darray.Insert(Data, 0, 0);
darray.CopyTo(dcopy, 0);
Assert.That(darray, Is.EqualTo(dcopy));
}
示例4: NonGenericCopyTo
public static void NonGenericCopyTo()
{
DynamicArray<int> darray = new DynamicArray<int>(1);
int[] copy = new int[Data.Length];
darray.Insert(Data, 0, 0);
((ICollection)darray).CopyTo(copy, 0);
Assert.That(Data, Is.EqualTo(copy));
}
示例5: AsGenericCollection
public void AsGenericCollection()
{
var data = new[] { 3, 2, 4, 5, 7, -3, -2, -4, -5, -7 };
var darray = new DynamicArray<int>(1);
darray.Insert(data, 0, 0);
Assert.That(darray, Is.EqualTo(data));
}
示例6: AsNonGenericCollection
public void AsNonGenericCollection()
{
var data = new[] { 3, 2, 4, 5, 7, -3, -2, -4, -5, -7 };
var darray = new DynamicArray<int>(1);
darray.Insert(data, 0, 0);
int i = 0;
foreach (int element in (IEnumerable)darray)
{
Assert.AreEqual(data[i++], element);
}
}
示例7: TestInsertMethod
public void TestInsertMethod()
{
DynamicArray<int> m3 = new DynamicArray<int>(9);
int[] arr = new[] { 1, 2, 3, 4, 5 };
foreach (var elem in arr)
{
m3.Add(elem);
}
m3.Insert(2, 9);
var a1 = m3[2];
Assert.AreEqual(a1, 9);
}
示例8: TestMethod1
public void TestMethod1()
{
try
{
DynamicArray<int> m1 = new DynamicArray<int>();
DynamicArray<int> m2 = new DynamicArray<int>(9);
int[] arr = new[] { 1, 2, 3, 4, 5 };
DynamicArray<int> m3 = new DynamicArray<int>(arr);
var a = m3.Length;
Assert.AreEqual(a, 5);
var b = m3.Capacity;
Assert.AreEqual(b, 5);
m3.Add(6);
var a1 = m3.Length;
Assert.AreEqual(a1, 6);
var b1 = m3.Capacity;
Assert.AreEqual(b1, 10);
int[] arr1 = new[] { 7, 8, 9, 10, 11, 12 };
m3.AddRange(arr1);
var a3 = m3.Length;
Assert.AreEqual(a3, 12);
var b3 = m3.Capacity;
Assert.AreEqual(b3, 20);
var flag1 = m3.Remove(7);
var a4 = m3.Length;
Assert.AreEqual(a4, 11);
Assert.AreEqual(flag1, true);
//m3.Insert(26,14); //Попытка вставить вне границ массива
m3.Insert(6, 7);
var b4 = m3[6];
Assert.AreEqual(b4, 7);
int k = 1;
foreach (var elem in m3)
{
var a5 = elem;
Assert.AreEqual(a5, k);
k++;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Assert.AreEqual(0, 1);
}
}
示例9: TestMethod1
public void TestMethod1()
{
try
{
DynamicArray<int> arr1 = new DynamicArray<int>();
DynamicArray<int> arr2 = new DynamicArray<int>(20);
int[] arr3 = new[] { 1, 2, 3, 4, 5 };
DynamicArray<int> arr4 = new DynamicArray<int>(arr3);
var a = arr4.Length;
Assert.AreEqual(a, 5);
var b = arr4.Capacity;
Assert.AreEqual(b, 5);
arr4.Add(6);
var a1 = arr4.Length;
Assert.AreEqual(a1, 6);
var b1 = arr4.Capacity;
Assert.AreEqual(b1, 10);
int[] arr31 = new[] { 7, 8, 9, 10, 11, 12 };
arr4.AddRange(arr31);
var a3 = arr4.Length;
Assert.AreEqual(a3, 12);
var b3 = arr4.Capacity;
Assert.AreEqual(b3, 20);
var flag1 = arr4.Remove(7);
var a4 = arr4.Length;
Assert.AreEqual(a4, 11);
Assert.AreEqual(flag1, true);
arr4.Insert(6, 7);
var b4 = arr4[6];
Assert.AreEqual(b4, 7);
int k = 1;
foreach (var elem in arr4)
{
var a5 = elem;
Assert.AreEqual(a5, k);
k++;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Assert.AreEqual(0, 1);
}
}
示例10: ExceptionOnIncorrectIndexesSize
public static void ExceptionOnIncorrectIndexesSize()
{
var dynArray = new DynamicArray<int>(3);
var tooLargeIndexes = new int[dynArray.Rank + 1];
var tooSmallIndexes = new int[dynArray.Rank - 1];
// Indexer get
Assert.Throws<ArgumentException>(() => { int i = dynArray[tooSmallIndexes]; });
Assert.Throws<ArgumentException>(() => { int i = dynArray[tooLargeIndexes]; });
// Indexer set
Assert.Throws<ArgumentException>(() => dynArray[tooLargeIndexes] = 10);
Assert.Throws<ArgumentException>(() => dynArray[tooSmallIndexes] = 10);
// GetValue
Assert.Throws<ArgumentException>(() => { int i = dynArray.GetValue(tooSmallIndexes); });
Assert.Throws<ArgumentException>(() => { int i = dynArray.GetValue(tooLargeIndexes); });
// SetValue
Assert.Throws<ArgumentException>(() => dynArray.SetValue(10, tooLargeIndexes));
Assert.Throws<ArgumentException>(() => dynArray.SetValue(10, tooSmallIndexes));
// Insert
Assert.Throws<ArgumentException>(
() => dynArray.Insert(new[,,] { { { 1 }, { 2 } }, { { 1 }, { 2 } } }, 0, tooSmallIndexes));
Assert.Throws<ArgumentException>(
() => dynArray.Insert(new[,,] { { { 1 }, { 2 } }, { { 1 }, { 2 } } }, 0, tooLargeIndexes));
// Resize
Assert.Throws<ArgumentException>(() => dynArray.Resize(tooLargeIndexes));
Assert.Throws<ArgumentException>(() => dynArray.Resize(tooSmallIndexes));
// Extend
Assert.Throws<ArgumentException>(() => dynArray.Extend(tooLargeIndexes));
Assert.Throws<ArgumentException>(() => dynArray.Extend(tooSmallIndexes));
}
示例11: Resize2D
public static void Resize2D()
{
// Should have B_COLS < A_COLS
const int A_ROWS = 4;
const int A_COLS = 3;
const int B_ROWS = 5;
const int B_COLS = 2;
// Arrays O, A and B
object[,] arrA = ArrayHelper.New().NewArray<object>(A_ROWS, A_COLS).FillWith("A").As<object[,]>();
object[,] arrB = ArrayHelper.New().NewArray<object>(B_ROWS, B_COLS).FillWith("B").As<object[,]>();
// NOTE capacities are set to the minimum to force buffer resize
DynamicArray<object> dynArray = new DynamicArray<object>(2, 1, 1);
// Insert B at 0,0
dynArray.Insert(arrB, 0, 0, 0);
ArrayHelper.Print(dynArray);
DynamicArrayAssert.AreElementsEqual(dynArray, arrB);
// Insert A right before B in dim 1
dynArray.Insert(arrA, 0, 0, 0);
ArrayHelper.Print(dynArray);
DynamicArrayAssert.Included(dynArray, arrA, 0, 0);
DynamicArrayAssert.Included(dynArray, arrB, A_ROWS, 0);
DynamicArrayAssert.CountsEqual(dynArray, A_ROWS + B_ROWS, A_COLS);
// Remove B_ROWS lines and add NCols columns
const int NCols = 5;
dynArray.Resize(A_ROWS, A_COLS + NCols);
ArrayHelper.Print(dynArray);
DynamicArrayAssert.Included(dynArray, arrA, 0, 0);
DynamicArrayAssert.AreElementsDefault(dynArray, new int[] { A_ROWS, NCols }, 0, A_COLS);
DynamicArrayAssert.CountsEqual(dynArray, A_ROWS, A_COLS + NCols);
// Add NRows rows
const int NRows = 10;
dynArray.ResizeDim(0, A_ROWS + NRows);
ArrayHelper.Print(dynArray);
DynamicArrayAssert.CountsEqual(dynArray, A_ROWS + NRows, A_COLS + NCols);
DynamicArrayAssert.Included(dynArray, arrA, 0, 0);
DynamicArrayAssert.AreElementsDefault(dynArray, new int[] { NRows, A_COLS + NCols }, A_ROWS, 0);
DynamicArrayAssert.AreElementsDefault(dynArray, new int[] { A_ROWS + NRows, NCols }, 0, A_COLS);
// Shrink to (NFewRows, NFewCols)
const int NFewCols = 2;
const int NFewRows = 3;
dynArray.Resize(NFewRows, NFewCols);
ArrayHelper.Print(dynArray);
DynamicArrayAssert.Included(dynArray, arrA, Zero2DIdx, Zero2DIdx, new int[] { NFewRows, NFewCols });
// Enlarge to (NManyRows, NManyCols)
const int NManyRows = 10;
const int NManyCols = 7;
dynArray.Resize(NManyRows, NManyCols);
ArrayHelper.Print(dynArray);
DynamicArrayAssert.Included(dynArray, arrA, Zero2DIdx, Zero2DIdx, new int[] { NFewRows, NFewCols });
DynamicArrayAssert.AreElementsDefault(dynArray, new[] { NManyRows, NManyCols - NFewCols }, 0, NFewCols);
DynamicArrayAssert.AreElementsDefault(dynArray, new[] { NManyRows - NFewRows, NFewCols }, NFewRows, 0);
}
示例12: InvalidateEnumerator
public void InvalidateEnumerator(
[ValueSource("InvalidateActions")]Action<DynamicArray<int>> invalidator)
{
// Build a DynamicArray
var darray = new DynamicArray<int>(2);
darray.Insert(new[,] { { 3, 2, 4, 5, 7 }, { -3, -2, -4, -5, -7 } }, 0, 0, 0);
// Get the IEnumerator<T>
var enumerator = darray.GetEnumerator();
// Advance on step and check current value
enumerator.MoveNext();
Assert.AreEqual(3, enumerator.Current);
// Invalidate the array
invalidator(darray);
// Current must *NOT* rhrow if invalidation is right after MoveNext
// see http://msdn.microsoft.com/en-us/library/system.collections.ienumerator.aspx
Assert.AreEqual(3, enumerator.Current);
// Move the invalidated enumerator (InvalidOperationException expected)
Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext());
// Reset the invalidated enumerator (InvalidOperationException expected)
Assert.Throws<InvalidOperationException>(enumerator.Reset);
}
示例13: ExceptionOnInsertArrayOfInvalidRank
public void ExceptionOnInsertArrayOfInvalidRank()
{
DynamicArray<int> dynArray = new DynamicArray<int>(2);
Assert.Throws<RankException>(() => dynArray.Insert(new int[5], 0, 0, 0));
Assert.Throws<RankException>(() => dynArray.Insert(new int[5, 10, 12], 0, 0, 0));
}
示例14: ExceptionOnInsertArrayWithANullLength
public void ExceptionOnInsertArrayWithANullLength()
{
var dynArray = new DynamicArray<int>(2);
Assert.Throws<ArgumentException>(() => dynArray.Insert(new int[5, 0], 0, 0, 0));
}
示例15: Insert2D
public static void Insert2D()
{
const int O_ROWS = 4;
const int O_COLS = 2;
const int A_ROWS = 4;
const int A_COLS = 3;
const int B_ROWS = 2;
const int B_COLS = 2;
// Arrays O, A and B
object[,] oarray = ArrayHelper.New().NewArray<object>(O_ROWS, O_COLS).FillWith("O").As<object[,]>();
object[,] aarray = ArrayHelper.New().NewArray<object>(A_ROWS, A_COLS).FillWith("A").As<object[,]>();
object[,] barray = ArrayHelper.New().NewArray<object>(B_ROWS, B_COLS).FillWith("B").As<object[,]>();
// NOTE capacities are set to the minimum to force buffer resize
DynamicArray<object> dynArray = new DynamicArray<object>(2, 1, 1);
// Insert O at (O_ROW_POS, O_COL_POS)
const int O_ROW_POS = 0;
const int O_COL_POS = 2;
dynArray.Insert(oarray, 0, O_ROW_POS, O_COL_POS);
ArrayHelper.Print(dynArray);
DynamicArrayAssert.Included(dynArray, oarray, O_ROW_POS, O_COL_POS);
DynamicArrayAssert.CountsEqual(dynArray, O_ROW_POS + O_ROWS, O_COL_POS + O_COLS);
// Insert A at (A_ROW_POS, A_COL_POS) in dim 0
// Must have: (A_ROW_POS > O_ROW_POS) && (A_ROW_POS < O_ROW_POS + O_ROWS)
// in order to insert A "within" O
const int A_ROW_POS = 1;
const int A_COL_POS = 1;
Assert.IsTrue(A_ROW_POS > O_ROW_POS);
Assert.IsTrue(A_ROW_POS < O_ROW_POS + O_ROWS);
dynArray.Insert(aarray, 0, A_ROW_POS, A_COL_POS);
ArrayHelper.Print(dynArray);
DynamicArrayAssert.Included(dynArray, aarray, A_ROW_POS, A_COL_POS);
DynamicArrayAssert.Included(
dynArray,
oarray,
new int[] { O_ROW_POS, O_COL_POS },
Zero2DIdx,
new int[] { A_ROW_POS - O_ROW_POS, O_COLS });
DynamicArrayAssert.Included(
dynArray,
oarray,
new int[] { A_ROW_POS + A_ROWS, O_COL_POS },
new int[] { A_ROW_POS - O_ROW_POS, 0 },
new int[] { O_ROWS, O_COLS });
// Insert B at (B_ROW_POS, B_COL_POS) in dim 1
const int B_ROW_POS = 2;
const int B_COL_POS = 1;
dynArray.Insert(barray, 1, B_ROW_POS, B_COL_POS);
ArrayHelper.Print(dynArray);
DynamicArrayAssert.Included(dynArray, barray, B_ROW_POS, B_COL_POS);
DynamicArrayAssert.Included(
dynArray,
aarray,
new int[] { B_ROW_POS, B_COL_POS + B_COLS },
new int[] { B_ROW_POS - A_ROW_POS, B_COL_POS - A_COL_POS },
new int[] { B_ROW_POS - A_ROW_POS + B_ROWS, A_COLS });
}