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\SpectrumGenerator.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\Scripts\VideoInput\Editor\UnitTest\ComponentFactoryTestCase.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 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;
}

View file

@ -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<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)
{
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,6 +9,7 @@ namespace cylvester
{
private IPdArray pdArray_;
private Rect selectionRect_;
private Rect noSelectionRect_;
private float[] dummyData_;
private Color standardColor_;
private Color selectedColor_;
@ -16,14 +17,11 @@ namespace cylvester
[SetUp]
public void SetUp()
{
pdArray_ = Substitute.For<IPdArray>();
selectionRect_ = new Rect {x = 9, y = 9, width = 3, height = 3};
dummyData_ = new float[100];
for (var i = 0; i < dummyData_.Length; ++i)
{
dummyData_[i] = i / (float)dummyData_.Length;
}
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};
standardColor_ = new Color(0f, 0f, 0f, 0.2f);
selectedColor_ = new Color(0f, 0f, 0f, 1f);
@ -39,20 +37,47 @@ namespace cylvester
}
[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;
@ -75,11 +100,10 @@ namespace cylvester
{
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));
}
}
}