Skip to content
Snippets Groups Projects
Commit c4b1f90f authored by Leonard Kupper's avatar Leonard Kupper
Browse files

Add tests for FunctionalRasterizer and Rectangle

parent ea3c9507
No related branches found
No related tags found
1 merge request!8Feat/rasterizer 10
package de.tudresden.inf.mci.brailleplot.rendering;
/**
* A dummy class for BarCharts.
* @author Leonard Kupper
* @version 2019.06.28
*/
public class BarChartDummy implements Renderable {
//private double[] dummy = {0.21, 0.13, -0.3, 0.52, -1.02, 0.65, 0.77, -0.33};
private double[] dummy = {500,36,357,473,75,220,356};
final String getTitle() {
return "I am a bar chart.";
}
final double[] getCategories() {
return dummy;
}
final int getCategoryCount() {
return dummy.length;
}
final double getValue(final int categoryIndex) {
return dummy[categoryIndex];
}
public double getValueRangeSize() {
return (getValueRangeMax() - getValueRangeMin());
}
public double getValueRangeMax() {
double max = Double.NEGATIVE_INFINITY;
for (double value : dummy) {
if (value > max) {
max = value;
}
}
return max;
}
public double getValueRangeMin() {
double min = Double.POSITIVE_INFINITY;
for (double value : dummy) {
if (value < min) {
min = value;
}
}
return min;
}
}
package de.tudresden.inf.mci.brailleplot.rendering;
import de.tudresden.inf.mci.brailleplot.configparser.ConfigurationParser;
import de.tudresden.inf.mci.brailleplot.configparser.Format;
import de.tudresden.inf.mci.brailleplot.configparser.JavaPropertiesConfigurationParser;
import de.tudresden.inf.mci.brailleplot.configparser.Printer;
import diagrams.BarChart;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.io.File;
public class FunctionalRasterizerTest {
public static final String mDefaultConfig = getResource("default.properties").getAbsolutePath();
public static final String mBaseConfig = getResource("base_format.properties").getAbsolutePath();
public static Printer mPrinter;
public static Format mFormat;
public static File getResource(String fileName) {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
File resourceFile = new File(classLoader.getResource(fileName).getFile());
return resourceFile;
}
@BeforeAll
public static void initialize() {
Assertions.assertDoesNotThrow(
() -> {
ConfigurationParser parser = new JavaPropertiesConfigurationParser(mBaseConfig, mDefaultConfig);
mPrinter = parser.getPrinter();
mFormat = parser.getFormat("test");
}
);
}
// Invalid use test cases.
@Test
public void testInvalidDirectCall() {
// Create FunctionalRasterizer for Text
FunctionalRasterizer<Text> textRasterizer = new FunctionalRasterizer<>(Text.class, (data, canvas) -> {
// dummy
});
// The FunctionalRasterizer should not be called directly, it is meant to be called by its RenderingBase
// which decides which rasterizer to use based on the Renderable type.
// Directly passing the wrong Renderable type must cause exception:
Assertions.assertThrows(IllegalArgumentException.class, () -> {
AbstractRasterCanvas testCanvas = new SixDotBrailleRasterCanvas(mPrinter, mFormat);
// Pass Image to Text rasterizer.
textRasterizer.rasterize(new Image(getResource("dummy.bmp")), testCanvas);
});
}
}
package de.tudresden.inf.mci.brailleplot.rendering;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class RectangleTest {
Rectangle rect1 = new Rectangle(4.5, 2.75, 8.3, 16);
Rectangle rect2 = new Rectangle(8.5, 6.75, 8.3, 16);
Rectangle rect3 = new Rectangle(0, 0, 3, 2);
@Test
public void testDimensions() {
Assertions.assertEquals(4.5, rect1.getX());
Assertions.assertEquals(2.75, rect1.getY());
Assertions.assertEquals(8.3, rect1.getWidth());
Assertions.assertEquals(16, rect1.getHeight());
Assertions.assertEquals(12.8, rect1.getRight());
Assertions.assertEquals(18.75, rect1.getBottom());
}
@Test
public void testIntegerCoordinates() {
Rectangle.IntWrapper intRect;
// with rational original coordinates (get rounded)
intRect = rect1.intWrapper();
Assertions.assertEquals(5, intRect.getX());
Assertions.assertEquals(3, intRect.getY());
Assertions.assertEquals(8, intRect.getWidth());
Assertions.assertEquals(16, intRect.getHeight());
Assertions.assertEquals(12, intRect.getRight());
Assertions.assertEquals(18, intRect.getBottom());
// with natural original coordinates
intRect = rect3.intWrapper();
Assertions.assertEquals(0, intRect.getX());
Assertions.assertEquals(0, intRect.getY());
Assertions.assertEquals(3, intRect.getWidth());
Assertions.assertEquals(2, intRect.getHeight());
Assertions.assertEquals(2, intRect.getRight());
Assertions.assertEquals(1, intRect.getBottom());
}
@Test
public void testScaling() {
Rectangle scaledRect = rect1.scaledBy(5.5, 0.4);
Assertions.assertEquals(4.5 * 5.5, scaledRect.getX());
Assertions.assertEquals(2.75 * 0.4, scaledRect.getY());
Assertions.assertEquals(8.3 * 5.5, scaledRect.getWidth());
Assertions.assertEquals(16 * 0.4, scaledRect.getHeight());
Assertions.assertEquals(12.8 * 5.5, scaledRect.getRight());
Assertions.assertEquals(18.75 * 0.4, scaledRect.getBottom());
}
@Test
public void testIntersecting() {
Rectangle itsct;
// Non-empty intersection
itsct = rect1.intersectedWith(rect2);
Assertions.assertEquals(8.5, itsct.getX());
Assertions.assertEquals(6.75, itsct.getY());
Assertions.assertEquals(12.8 - 8.5, itsct.getWidth());
Assertions.assertEquals(18.75 - 6.75, itsct.getHeight());
Assertions.assertEquals(12.8, itsct.getRight());
Assertions.assertEquals(18.75, itsct.getBottom());
// Swapping order of rectangles gives equivalent intersection
itsct = rect2.intersectedWith(rect1);
Assertions.assertEquals(8.5, itsct.getX());
Assertions.assertEquals(6.75, itsct.getY());
Assertions.assertEquals(12.8 - 8.5, itsct.getWidth());
Assertions.assertEquals(18.75 - 6.75, itsct.getHeight());
Assertions.assertEquals(12.8, itsct.getRight());
Assertions.assertEquals(18.75, itsct.getBottom());
// Empty intersection
itsct = rect1.intersectedWith(rect3);
Assertions.assertEquals(4.5, itsct.getX());
Assertions.assertEquals(2.75, itsct.getY());
Assertions.assertEquals(0, itsct.getWidth());
Assertions.assertEquals(0, itsct.getHeight());
Assertions.assertEquals(4.5, itsct.getRight());
Assertions.assertEquals(2.75, itsct.getBottom());
}
@Test
public void testCropping() {
Assertions.assertDoesNotThrow(() -> {
Rectangle copy, crop;
// Create copy by selecting whole width / height (should not throw)
copy = rect1.fromTop(16);
copy = rect1.fromBottom(16);
copy = rect1.fromLeft(8.3);
copy = rect1.fromBottom(8.3);
// Create copy with copy constructor
copy = new Rectangle(rect1);
crop = copy.removeFromTop(1);
Assertions.assertEquals(2.75, crop.getY());
Assertions.assertEquals(1, crop.getHeight());
crop = copy.removeFromLeft(2);
Assertions.assertEquals(4.5, crop.getX());
Assertions.assertEquals(2, crop.getWidth());
crop = copy.removeFromBottom(3);
Assertions.assertEquals(18.75, crop.getBottom());
Assertions.assertEquals(3, crop.getHeight());
crop = copy.removeFromRight(4);
Assertions.assertEquals(12.8, crop.getRight());
Assertions.assertEquals(4, crop.getWidth());
Assertions.assertEquals(4.5 + 2, copy.getX());
Assertions.assertEquals(2.75 + 1, copy.getY());
Assertions.assertEquals(8.3 - 2 - 4, copy.getWidth());
Assertions.assertEquals(16 - 1 - 3, copy.getHeight());
Assertions.assertEquals(12.8 - 4, copy.getRight());
Assertions.assertEquals(18.75 - 3, copy.getBottom());
});
}
@Test
public void testOutOfSpace() {
Assertions.assertThrows(Rectangle.OutOfSpaceException.class, () -> {
Rectangle copy, crop;
copy = new Rectangle(rect1);
// selecting more space than available
crop = copy.fromTop(16.01);
});
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment