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

Commit

Permalink
New array Extension plus tests (#461)
Browse files Browse the repository at this point in the history
* * Added new "InitialiseArray" array extension to set all values of an array to a new value, including Tests
* Added null check on Array AddItem
Fixed license in Core test 2

* Update XRTK-Core/Packages/com.xrtk.core/Extensions/ArrayExtensions.cs

Co-Authored-By: Stephen Hodgson <StephenHodgson@users.noreply.github.com>

* Applied review patches

Co-authored-by: Stephen Hodgson <StephenHodgson@users.noreply.github.com>
  • Loading branch information
SimonDarksideJ and StephenHodgson authored Mar 10, 2020
1 parent 87b479c commit 6c7ada8
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 4 deletions.
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
58 changes: 58 additions & 0 deletions XRTK-Core/Assets/XRTK.Tests/Core/TestFixture_03_ExtensionsTests.cs
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()
{
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.

37 changes: 34 additions & 3 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)
public static T[] AddItem<T>(this T[] array, T newItem)
{
// Initialise the array if it is null
if (array == null)
{
return new[] { newItem };
}

//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)
{
return new[] { newItem };
}

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

return array;
}
}
}
}

0 comments on commit 6c7ada8

Please sign in to comment.