add more unit tests for rectangular selection system

This commit is contained in:
Chikashi Miyama 2019-10-01 23:01:56 +02:00
parent 1267a9715a
commit b7fa663c97
6 changed files with 129 additions and 54 deletions

View file

@ -64,6 +64,7 @@
<Compile Include="Assets\Editor\RectangularSelection.cs" /> <Compile Include="Assets\Editor\RectangularSelection.cs" />
<Compile Include="Assets\Editor\SpectrumGenerator.cs" /> <Compile Include="Assets\Editor\SpectrumGenerator.cs" />
<Compile Include="Assets\Editor\UnitTest\UnitTest_ChangeObserver.cs" /> <Compile Include="Assets\Editor\UnitTest\UnitTest_ChangeObserver.cs" />
<Compile Include="Assets\Editor\UnitTest\UnitTest_RectangularSelection.cs" />
<Compile Include="Assets\Editor\UnitTest\UnitTest_SpectrumGenerator.cs" /> <Compile Include="Assets\Editor\UnitTest\UnitTest_SpectrumGenerator.cs" />
<Compile Include="Assets\Scripts\VideoInput\Editor\UnitTest\ComponentFactoryTestCase.cs" /> <Compile Include="Assets\Scripts\VideoInput\Editor\UnitTest\ComponentFactoryTestCase.cs" />
<Compile Include="Assets\Scripts\VideoInput\Editor\UnitTest\KinectSensorTestCase.cs" /> <Compile Include="Assets\Scripts\VideoInput\Editor\UnitTest\KinectSensorTestCase.cs" />

View file

@ -37,11 +37,13 @@ namespace cylvester
var width = selectedArea_.width / paintSpace.width; var width = selectedArea_.width / paintSpace.width;
var height = selectedArea_.height / paintSpace.height; var height = selectedArea_.height / paintSpace.height;
var selectionRect = new Rect(); var selectionRect = new Rect
selectionRect.x = xPos * textureWidth_; {
selectionRect.y = yPos * textureHeight_; x = xPos * textureWidth_,
selectionRect.width = width * textureWidth_; y = yPos * textureHeight_,
selectionRect.height = height * textureHeight_; width = width * textureWidth_,
height = height * textureHeight_
};
return selectionRect; return selectionRect;
} }

View file

@ -1,3 +1,4 @@
using System;
using UnityEngine; using UnityEngine;
namespace cylvester namespace cylvester
@ -19,37 +20,60 @@ namespace cylvester
public int Update(IPdArray pdArray, Rect selectionRect) public int Update(IPdArray pdArray, Rect selectionRect)
{ {
var numPixels = 0; if (pdArray != null)
for (var x = 0; x < Spectrum.width; x++)
{ {
for (var y = 0; y < Spectrum.height; y++) return UpdatePlayMode(pdArray, selectionRect);
{
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);
}
} }
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(); Spectrum.Apply();
return numPixels; 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<int, int> 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) private bool IsInSelection(int x, int y, ref Rect selectionRect)
{ {
var inRectHorizontally = selectionRect.x < x && x < selectionRect.x + (selectionRect.width-1); var inRectHorizontally = selectionRect.x < x && x < selectionRect.x + (selectionRect.width-1);

View file

@ -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);
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5cc4a0c2b4a342a7920a3aab6bf77c25
timeCreated: 1569963352

View file

@ -9,59 +9,84 @@ namespace cylvester
{ {
private IPdArray pdArray_; private IPdArray pdArray_;
private Rect selectionRect_; private Rect selectionRect_;
private Rect noSelectionRect_;
private float[] dummyData_; private float[] dummyData_;
private Color standardColor_; private Color standardColor_;
private Color selectedColor_; private Color selectedColor_;
[SetUp] [SetUp]
public void SetUp() public void SetUp()
{ {
pdArray_ = Substitute.For<IPdArray>();
selectionRect_ = new Rect {x = 9, y = 9, width = 3, height = 3};
dummyData_ = new float[100]; dummyData_ = new float[100];
pdArray_ = Substitute.For<IPdArray>();
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); standardColor_ = new Color(0f, 0f, 0f, 0.2f);
selectedColor_ = new Color(0f, 0f, 0f, 1f); selectedColor_ = new Color(0f, 0f, 0f, 1f);
} }
[Test] [Test]
public void Construction() public void Construction()
{ {
var spectrumGenerator = new SpectrumGenerator(100, 101); var spectrumGenerator = new SpectrumGenerator(100, 101);
Assert.AreEqual(100, spectrumGenerator.Spectrum.width); Assert.AreEqual(100, spectrumGenerator.Spectrum.width);
Assert.AreEqual(101, spectrumGenerator.Spectrum.height); Assert.AreEqual(101, spectrumGenerator.Spectrum.height);
} }
[Test] [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); 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] [Test]
public void Update_array_unavailable() public void Update_array_unavailable()
{ {
var noSelection = new Rect {width = 0, height = 0};
var spectrumGenerator = new SpectrumGenerator(100, 100); var spectrumGenerator = new SpectrumGenerator(100, 100);
var validPixels = spectrumGenerator.Update(null, noSelection); var validPixels = spectrumGenerator.Update(null, noSelectionRect_);
Assert.AreEqual(0, validPixels); Assert.AreEqual(0, validPixels);
var texture = spectrumGenerator.Spectrum; var texture = spectrumGenerator.Spectrum;
var pixels = texture.GetPixels(); var pixels = texture.GetPixels();
foreach (var pixel in pixels) foreach (var pixel in pixels)
Assert.AreEqual(standardColor_, pixel); Assert.AreEqual(standardColor_, pixel);
} }
[Test] [Test]
public void Update_array_unavailable_with_selection() public void Update_array_unavailable_with_selection()
{ {
@ -70,16 +95,15 @@ namespace cylvester
Assert.AreEqual(0, validPixels); Assert.AreEqual(0, validPixels);
var texture = spectrumGenerator.Spectrum; var texture = spectrumGenerator.Spectrum;
for (var x = 0; x < 100; x++) for (var x = 0; x < 100; x++)
{ {
for (var y = 0; y < 100; y++) 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)); Assert.AreEqual(selectedColor_, texture.GetPixel(x, y));
else else
Assert.AreEqual(standardColor_, texture.GetPixel(x, y)); Assert.AreEqual(standardColor_, texture.GetPixel(x, y));
} }
} }
} }