From 595feec69b8a6378d3bdae28f4d63b290cc328d3 Mon Sep 17 00:00:00 2001 From: "Simon (darkside) Jackson" Date: Mon, 9 Mar 2020 16:31:23 +0000 Subject: [PATCH 1/3] * 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 --- ...ture_02_MixedRealityToolkitUtilityTests.cs | 2 +- .../Core/TestFixture_03_ExtensionsTests.cs | 58 +++++++++++++++++++ .../TestFixture_03_ExtensionsTests.cs.meta | 11 ++++ .../Extensions/ArrayExtensions.cs | 35 ++++++++++- 4 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 XRTK-Core/Assets/XRTK.Tests/Core/TestFixture_03_ExtensionsTests.cs create mode 100644 XRTK-Core/Assets/XRTK.Tests/Core/TestFixture_03_ExtensionsTests.cs.meta diff --git a/XRTK-Core/Assets/XRTK.Tests/Core/TestFixture_02_MixedRealityToolkitUtilityTests.cs b/XRTK-Core/Assets/XRTK.Tests/Core/TestFixture_02_MixedRealityToolkitUtilityTests.cs index baea47177..9c5a378a9 100644 --- a/XRTK-Core/Assets/XRTK.Tests/Core/TestFixture_02_MixedRealityToolkitUtilityTests.cs +++ b/XRTK-Core/Assets/XRTK.Tests/Core/TestFixture_02_MixedRealityToolkitUtilityTests.cs @@ -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; diff --git a/XRTK-Core/Assets/XRTK.Tests/Core/TestFixture_03_ExtensionsTests.cs b/XRTK-Core/Assets/XRTK.Tests/Core/TestFixture_03_ExtensionsTests.cs new file mode 100644 index 000000000..f36855d51 --- /dev/null +++ b/XRTK-Core/Assets/XRTK.Tests/Core/TestFixture_03_ExtensionsTests.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/XRTK-Core/Assets/XRTK.Tests/Core/TestFixture_03_ExtensionsTests.cs.meta b/XRTK-Core/Assets/XRTK.Tests/Core/TestFixture_03_ExtensionsTests.cs.meta new file mode 100644 index 000000000..ce67e006e --- /dev/null +++ b/XRTK-Core/Assets/XRTK.Tests/Core/TestFixture_03_ExtensionsTests.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2d48db35a735f46459ef28176027ae87 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/XRTK-Core/Packages/com.xrtk.core/Extensions/ArrayExtensions.cs b/XRTK-Core/Packages/com.xrtk.core/Extensions/ArrayExtensions.cs index 863228a39..5ad1fd38e 100644 --- a/XRTK-Core/Packages/com.xrtk.core/Extensions/ArrayExtensions.cs +++ b/XRTK-Core/Packages/com.xrtk.core/Extensions/ArrayExtensions.cs @@ -37,12 +37,43 @@ public static bool IsValidArray(this Array array) /// The array to extend /// The item to add to the array /// - public static T[] AddItem(this T[] array, T newItem, int insertAtIndex = 0) + public static T[] AddItem(this T[] array, T newItem) { + // Initialise the array if it is null + if (array == null) + { + array = new T[0]; + } + + //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; } + + /// + /// Initialises an array with a default item + /// + /// + /// The array to initialise + /// The default item to set all the array items to + /// + public static T[] InitialiseArray(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; + } } } \ No newline at end of file From 6758f9ac60545531ce385c457f304be0347a7d71 Mon Sep 17 00:00:00 2001 From: "Simon (Darkside) Jackson" Date: Tue, 10 Mar 2020 13:33:59 +0000 Subject: [PATCH 2/3] Update XRTK-Core/Packages/com.xrtk.core/Extensions/ArrayExtensions.cs Co-Authored-By: Stephen Hodgson --- .../Packages/com.xrtk.core/Extensions/ArrayExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/XRTK-Core/Packages/com.xrtk.core/Extensions/ArrayExtensions.cs b/XRTK-Core/Packages/com.xrtk.core/Extensions/ArrayExtensions.cs index 5ad1fd38e..f4a20cf6b 100644 --- a/XRTK-Core/Packages/com.xrtk.core/Extensions/ArrayExtensions.cs +++ b/XRTK-Core/Packages/com.xrtk.core/Extensions/ArrayExtensions.cs @@ -42,7 +42,7 @@ public static T[] AddItem(this T[] array, T newItem) // Initialise the array if it is null if (array == null) { - array = new T[0]; + return new[] { newItem }; } //Extend the array, copy the items and add the new one @@ -76,4 +76,4 @@ public static T[] InitialiseArray(this T[] array, T newItem) return array; } } -} \ No newline at end of file +} From 75727b6e9b2220bf2f098f796a11e57b06d17c7b Mon Sep 17 00:00:00 2001 From: "Simon (darkside) Jackson" Date: Tue, 10 Mar 2020 13:41:53 +0000 Subject: [PATCH 3/3] Applied review patches --- XRTK-Core/Packages/com.xrtk.core/Extensions/ArrayExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/XRTK-Core/Packages/com.xrtk.core/Extensions/ArrayExtensions.cs b/XRTK-Core/Packages/com.xrtk.core/Extensions/ArrayExtensions.cs index f4a20cf6b..19341c21e 100644 --- a/XRTK-Core/Packages/com.xrtk.core/Extensions/ArrayExtensions.cs +++ b/XRTK-Core/Packages/com.xrtk.core/Extensions/ArrayExtensions.cs @@ -64,7 +64,7 @@ public static T[] InitialiseArray(this T[] array, T newItem) // Initialise the array if it is null if (array == null) { - array = new T[1]; + return new[] { newItem }; } //Set all the items to the provided value