Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

New array Extension plus tests #461

Merged
merged 3 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) XRTK. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using NUnit.Framework;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) XRTK. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using NUnit.Framework;
using UnityEngine;
using XRTK.Extensions;

namespace XRTK.Tests.Core
{
public class TestFixture_03_ExtensionsTests
{
[Test]
public void Test_01_Array_AddItem_Extension()
SimonDarksideJ marked this conversation as resolved.
Show resolved Hide resolved
{
Vector2[] testArray = null;
Vector2[] newArray = null;
try
{
newArray = testArray.AddItem(Vector2.zero);
}
catch { }

Assert.IsNotNull(newArray);

testArray = new Vector2[5];
newArray = testArray.AddItem(Vector2.one);

Assert.IsTrue(newArray[newArray.Length - 1] == Vector2.one);
Assert.IsTrue(newArray.Length > testArray.Length);
Assert.IsTrue(newArray.Length - testArray.Length == 1);
}

[Test]
public void Test_02_Array_InitialiseArray_Extension()
{
Vector2[] testArray = null;
Vector2[] newArray = null;
try
{
newArray = testArray.InitialiseArray(Vector2.one);
}
catch { }

Assert.IsNotNull(newArray);
Assert.IsTrue(newArray.Length == 1);
Assert.IsTrue(newArray[0] == Vector2.one);

testArray = new Vector2[5];
for (int i = 0; i < testArray.Length; i++)
{
testArray[i] = Vector2.one;
}
newArray = testArray.InitialiseArray(Vector2.zero);

Assert.IsTrue(newArray[0] == Vector2.zero);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 33 additions & 2 deletions XRTK-Core/Packages/com.xrtk.core/Extensions/ArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,43 @@ public static bool IsValidArray(this Array array)
/// <param name="array">The array to extend</param>
/// <param name="newItem">The item to add to the array</param>
/// <returns></returns>
public static T[] AddItem<T>(this T[] array, T newItem, int insertAtIndex = 0)
SimonDarksideJ marked this conversation as resolved.
Show resolved Hide resolved
public static T[] AddItem<T>(this T[] array, T newItem)
{
// Initialise the array if it is null
if (array == null)
{
array = new T[0];
SimonDarksideJ marked this conversation as resolved.
Show resolved Hide resolved
}

//Extend the array, copy the items and add the new one
var newArray = new T[array.Length + 1];
array.CopyTo(newArray, insertAtIndex);
array.CopyTo(newArray, 0);
newArray[array.Length] = newItem;
return newArray;
}

/// <summary>
/// Initialises an array with a default item
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array">The array to initialise</param>
/// <param name="newItem">The default item to set all the array items to</param>
/// <returns></returns>
public static T[] InitialiseArray<T>(this T[] array, T newItem)
{
// Initialise the array if it is null
if (array == null)
{
array = new T[1];
}

//Set all the items to the provided value
for (int i = 0; i < array.Length; i++)
{
array[i] = newItem;
}

return array;
}
}
}