From b7fa663c972e8ef104eb2615735a987908d456dd Mon Sep 17 00:00:00 2001 From: Chikashi Miyama Date: Tue, 1 Oct 2019 23:01:56 +0200 Subject: [PATCH] add more unit tests for rectangular selection system --- UnityProject/Assembly-CSharp-Editor.csproj | 1 + .../Assets/Editor/RectangularSelection.cs | 12 +-- .../Assets/Editor/SpectrumGenerator.cs | 74 ++++++++++++------- .../UnitTest/UnitTest_RectangularSelection.cs | 21 ++++++ .../UnitTest_RectangularSelection.cs.meta | 3 + .../UnitTest/UnitTest_SpectrumGenerator.cs | 72 ++++++++++++------ 6 files changed, 129 insertions(+), 54 deletions(-) create mode 100644 UnityProject/Assets/Editor/UnitTest/UnitTest_RectangularSelection.cs create mode 100644 UnityProject/Assets/Editor/UnitTest/UnitTest_RectangularSelection.cs.meta diff --git a/UnityProject/Assembly-CSharp-Editor.csproj b/UnityProject/Assembly-CSharp-Editor.csproj index b40cf12..1e76d60 100644 --- a/UnityProject/Assembly-CSharp-Editor.csproj +++ b/UnityProject/Assembly-CSharp-Editor.csproj @@ -64,6 +64,7 @@ + diff --git a/UnityProject/Assets/Editor/RectangularSelection.cs b/UnityProject/Assets/Editor/RectangularSelection.cs index 4eb76be..1b9f18e 100644 --- a/UnityProject/Assets/Editor/RectangularSelection.cs +++ b/UnityProject/Assets/Editor/RectangularSelection.cs @@ -37,11 +37,13 @@ namespace cylvester var width = selectedArea_.width / paintSpace.width; var height = selectedArea_.height / paintSpace.height; - var selectionRect = new Rect(); - selectionRect.x = xPos * textureWidth_; - selectionRect.y = yPos * textureHeight_; - selectionRect.width = width * textureWidth_; - selectionRect.height = height * textureHeight_; + var selectionRect = new Rect + { + x = xPos * textureWidth_, + y = yPos * textureHeight_, + width = width * textureWidth_, + height = height * textureHeight_ + }; return selectionRect; } diff --git a/UnityProject/Assets/Editor/SpectrumGenerator.cs b/UnityProject/Assets/Editor/SpectrumGenerator.cs index 373e15b..cfd6e32 100644 --- a/UnityProject/Assets/Editor/SpectrumGenerator.cs +++ b/UnityProject/Assets/Editor/SpectrumGenerator.cs @@ -1,3 +1,4 @@ +using System; using UnityEngine; namespace cylvester @@ -19,37 +20,60 @@ namespace cylvester public int Update(IPdArray pdArray, Rect selectionRect) { - var numPixels = 0; - for (var x = 0; x < Spectrum.width; x++) + if (pdArray != null) { - for (var y = 0; y < Spectrum.height; y++) - { - var color = Color.black; - var validPixel = false; - - if (pdArray != null) - { - var magnitude = pdArray.Data[x] * 20f; - validPixel = magnitude > y; - color = validPixel ? Color.green : Color.black; - } - - if (IsInSelection(x, y, ref selectionRect)) - { - color.a = 1f; - if (validPixel) - numPixels++; - } - else - color.a = 0.2f; - - Spectrum.SetPixel(x, y, color); - } + return UpdatePlayMode(pdArray, selectionRect); } + UpdateEditMode(selectionRect); + return 0; + } + + private int UpdatePlayMode(IPdArray pdArray, Rect selectionRect) + { + var numPixels = 0; + var data = pdArray.Data; + OnAllPixels((x, y) => + { + var magnitude = data[x] * 20f; + var validPixel = magnitude > y; + var color = validPixel ? Color.green : Color.black; + + if (IsInSelection(x, y, ref selectionRect)) + { + color.a = 1f; + if (validPixel) + numPixels++; + } + else + color.a = 0.2f; + + Spectrum.SetPixel(x, y, color); + }); Spectrum.Apply(); return numPixels; } + private void UpdateEditMode(Rect selectionRect) + { + OnAllPixels((x, y) => + { + var color = Color.black; + if (IsInSelection(x, y, ref selectionRect)) + color.a = 1f; + else + color.a = 0.2f; + Spectrum.SetPixel(x, y, color); + }); + Spectrum.Apply(); + } + + private void OnAllPixels(Action action) + { + for (var x = 0; x < Spectrum.width; x++) + for (var y = 0; y < Spectrum.height; y++) + action(x, y); + } + private bool IsInSelection(int x, int y, ref Rect selectionRect) { var inRectHorizontally = selectionRect.x < x && x < selectionRect.x + (selectionRect.width-1); diff --git a/UnityProject/Assets/Editor/UnitTest/UnitTest_RectangularSelection.cs b/UnityProject/Assets/Editor/UnitTest/UnitTest_RectangularSelection.cs new file mode 100644 index 0000000..a535f53 --- /dev/null +++ b/UnityProject/Assets/Editor/UnitTest/UnitTest_RectangularSelection.cs @@ -0,0 +1,21 @@ +using NUnit.Framework; +using UnityEngine; + +namespace cylvester +{ + [TestFixture] + public class UnitTest_RectangularSelection + { + [Test] + public void Update() + { + var paintSpace = new Rect(0, 0, 100, 100); // GUI + var rectangularSelection = new RectangularSelection(1000, 1000); // texture 10 times larger + rectangularSelection.Start(new Vector2(10, 10)); + var selectionInTexture = rectangularSelection.Update(new Vector2(20, 20), ref paintSpace); + + var expected = new Rect(100, 100, 100, 100); + Assert.AreEqual(expected, selectionInTexture); + } + } +} \ No newline at end of file diff --git a/UnityProject/Assets/Editor/UnitTest/UnitTest_RectangularSelection.cs.meta b/UnityProject/Assets/Editor/UnitTest/UnitTest_RectangularSelection.cs.meta new file mode 100644 index 0000000..e75e0c9 --- /dev/null +++ b/UnityProject/Assets/Editor/UnitTest/UnitTest_RectangularSelection.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 5cc4a0c2b4a342a7920a3aab6bf77c25 +timeCreated: 1569963352 \ No newline at end of file diff --git a/UnityProject/Assets/Editor/UnitTest/UnitTest_SpectrumGenerator.cs b/UnityProject/Assets/Editor/UnitTest/UnitTest_SpectrumGenerator.cs index e3418f8..31ab927 100644 --- a/UnityProject/Assets/Editor/UnitTest/UnitTest_SpectrumGenerator.cs +++ b/UnityProject/Assets/Editor/UnitTest/UnitTest_SpectrumGenerator.cs @@ -9,59 +9,84 @@ namespace cylvester { private IPdArray pdArray_; private Rect selectionRect_; + private Rect noSelectionRect_; private float[] dummyData_; - private Color standardColor_; + private Color standardColor_; private Color selectedColor_; - + [SetUp] public void SetUp() { - pdArray_ = Substitute.For(); - selectionRect_ = new Rect {x = 9, y = 9, width = 3, height = 3}; dummyData_ = new float[100]; + pdArray_ = Substitute.For(); + pdArray_.Data.Returns(dummyData_); + selectionRect_ = new Rect {x = 9, y = 9, width = 3, height = 3}; + noSelectionRect_ = new Rect {width = 0, height = 0}; - for (var i = 0; i < dummyData_.Length; ++i) - { - dummyData_[i] = i / (float)dummyData_.Length; - } - standardColor_ = new Color(0f, 0f, 0f, 0.2f); selectedColor_ = new Color(0f, 0f, 0f, 1f); } - + [Test] public void Construction() { var spectrumGenerator = new SpectrumGenerator(100, 101); - + Assert.AreEqual(100, spectrumGenerator.Spectrum.width); Assert.AreEqual(101, spectrumGenerator.Spectrum.height); } - + [Test] - public void Update_array_available() + public void Update_array_available_loud() { + for (var i = 0; i < dummyData_.Length; ++i) + dummyData_[i] = 100f; // loud sound + var spectrumGenerator = new SpectrumGenerator(100, 100); - spectrumGenerator.Update(pdArray_, selectionRect_); - - + var validPixel = spectrumGenerator.Update(pdArray_, selectionRect_); + + Assert.AreEqual(1, validPixel); } - + + [Test] + public void Update_array_available_soft() + { + for (var i = 0; i < dummyData_.Length; ++i) + dummyData_[i] = 0.001f; // soft sound + + var spectrumGenerator = new SpectrumGenerator(100, 100); + var validPixel = spectrumGenerator.Update(pdArray_, selectionRect_); + + Assert.AreEqual(0, validPixel); + } + + [Test] + public void Update_array_available_loud_no_selection() + { + for (var i = 0; i < dummyData_.Length; ++i) + dummyData_[i] = 100f; // loud sound + + var spectrumGenerator = new SpectrumGenerator(100, 100); + var validPixel = spectrumGenerator.Update(pdArray_, noSelectionRect_); + + Assert.AreEqual(0, validPixel); + } + + [Test] public void Update_array_unavailable() { - var noSelection = new Rect {width = 0, height = 0}; var spectrumGenerator = new SpectrumGenerator(100, 100); - var validPixels = spectrumGenerator.Update(null, noSelection); + var validPixels = spectrumGenerator.Update(null, noSelectionRect_); Assert.AreEqual(0, validPixels); var texture = spectrumGenerator.Spectrum; var pixels = texture.GetPixels(); - + foreach (var pixel in pixels) Assert.AreEqual(standardColor_, pixel); } - + [Test] public void Update_array_unavailable_with_selection() { @@ -70,16 +95,15 @@ namespace cylvester Assert.AreEqual(0, validPixels); var texture = spectrumGenerator.Spectrum; - + for (var x = 0; x < 100; x++) { for (var y = 0; y < 100; y++) { - if(x == 10 && y == 90) // because vertically inverted + if (x == 10 && y == 90) // because vertically inverted Assert.AreEqual(selectedColor_, texture.GetPixel(x, y)); else Assert.AreEqual(standardColor_, texture.GetPixel(x, y)); - } } }