Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobTernes committed Mar 22, 2024
1 parent 1eee1b1 commit 19368c9
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 20 deletions.
6 changes: 0 additions & 6 deletions src/main/java/fabulator/object/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ public Location() {
this.y = 0;
}

public double distSquared(Location other) {
double dx = this.x - other.getX();
double dy = this.y - other.getY();
return dx * dx + dy * dy;
}

public void add(Location other) {
this.x += other.getX();
this.y += other.getY();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/fabulator/object/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.Getter;

@Getter
public class Version { // TODO: tests
public class Version {

private int major;
private int minor;
Expand Down
46 changes: 35 additions & 11 deletions src/test/java/fabulator/lookup/LineMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,65 @@
import testutil.TestUtils;

import java.util.List;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

public class LineMapTest {

private static LineMap lineMap;
private static Line line1;
private static Line line2;
private static Line line3;
private static Line line4;
private static Line line5;
private static Line line6;

@BeforeAll
@DisplayName("Initialize LineMap")
static void initializeLineMap() {
TestUtils.initializeJavaFX();
lineMap = new LineMap(10);
}

@Test
@DisplayName("Test Insertion and Retrieval")
void testInsertionAndRetrieval() {
Group lineGroup = new Group();

Line line1 = new LineBuilder()
line1 = new LineBuilder()
.setStart(0, 0)
.setEnd(8, 0)
.build();
Line line2 = new LineBuilder()
line2 = new LineBuilder()
.setStart(8, 0)
.setEnd(8, 4)
.build();
Line line3 = new LineBuilder()
line3 = new LineBuilder()
.setStart(8, 0)
.setEnd(8, -4)
.build();
Line line4 = new LineBuilder()
line4 = new LineBuilder()
.setStart(8, 0)
.setEnd(12, 0)
.build();
Line line5 = new LineBuilder()
line5 = new LineBuilder()
.setStart(12, 0)
.setEnd(16, 0)
.build();
line6 = new LineBuilder()
.setStart(17, 0)
.setEnd(20, 0)
.build();

lineGroup.getChildren().addAll(
line1, line2, line3, line4, line5
line1, line2, line3, line4, line5, line6
);

lineMap.addAll(
line1, line2, line3, line4, line5
line1, line2, line3, line4, line5, line6
);
}

@Test
@DisplayName("Test retrieval of lines")
void testRetrievalOfLines() {
List<Line> atLine1 = lineMap.adjacentTo(line1);
assertEquals(3, atLine1.size());
assertTrue(atLine1.contains(line2));
Expand All @@ -73,4 +84,17 @@ void testInsertionAndRetrieval() {
assertFalse(atLine5.contains(line3));
assertFalse(atLine5.contains(line5));
}

@Test
@DisplayName("Test getting all Lines at a Line")
public void testGettingAllLinesAtALine() {
Set<Line> linesAtLine1 = lineMap.allLinesAt(line1);

assertEquals(5, linesAtLine1.size());
assertTrue(linesAtLine1.contains(line1));
assertTrue(linesAtLine1.contains(line2));
assertTrue(linesAtLine1.contains(line3));
assertTrue(linesAtLine1.contains(line4));
assertTrue(linesAtLine1.contains(line5));
}
}
93 changes: 93 additions & 0 deletions src/test/java/fabulator/object/LocationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package fabulator.object;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class LocationTest {

@Test
@DisplayName("Test creation of Location objects")
public void testCreationOfLocationObjects() {
Location l1 = new Location(1,2);
Location l2 = new Location (42.0, 6.9);

assertEquals(1, l1.getX());
assertEquals(2, l1.getY());

assertEquals(42.0, l2.getX());
assertEquals(6.9, l2.getY());

assertTrue(l1.valid());
assertTrue(l2.valid());
}

@Test
@DisplayName("Test comparison of Location objects")
public void testComparisonOfLocationObjects() {
Location l1 = new Location(12.3, 4.56);
Location l2 = new Location(12.3, 4.56);
Location l3 = new Location(12.34, 4.56);

assertEquals(l1, l2);
assertNotEquals(l2, l3);
}

@Test
@DisplayName("Test invalid Location")
public void testInvalidLocation() {
Location l1 = new Location(Double.NaN,12.3456);
Location l2 = new Location(1, Double.NaN);

assertFalse(l1.valid());
assertFalse(l2.valid());
}

@Test
@DisplayName("Test adding Location objects")
public void testAddingLocationObjects() {
Location l1 = new Location(-1, 2);
Location l2 = new Location(44, 33);

l1.add(l2);
assertEquals(43, l1.getX());
assertEquals(35, l1.getY());

l2.add(-1, 1);
assertEquals(43, l2.getX());
assertEquals(34, l2.getY());
}

@Test
@DisplayName("Test scaleInverse method of Location")
public void testScaleInverseMethodOfLocation() {
Location l1 = new Location(3, 6);
Location l2 = new Location(123, -123);

l1.scaleInverse(3);
l2.scaleInverse(7);

assertEquals(1, l1.getX());
assertEquals(2, l1.getY());

assertEquals(123d/7d, l2.getX());
assertEquals(-123d/7d, l2.getY());
}

@Test
@DisplayName("Test average of Location objects")
public void testAverageOfLocationObjects() {
Location l1 = new Location(0, 22);
Location l2 = new Location(-14, 7);
Location l3 = new Location(3, -2);

assertEquals(l1, Location.averageOf(l1));
assertEquals(l2, Location.averageOf(l2));

Location totalAverage = Location.averageOf(l1, l2, l3);

assertEquals(-11d/3d, totalAverage.getX());
assertEquals(27d/3d, totalAverage.getY());
}
}
61 changes: 61 additions & 0 deletions src/test/java/fabulator/object/VersionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package fabulator.object;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class VersionTest {

@Test
@DisplayName("Test creating Version from String")
public void testCreateVersionFromString() {
Version v1 = new Version("1.2.3");

assertEquals(1, v1.getMajor());
assertEquals(2, v1.getMinor());
assertEquals(3, v1.getPatch());

Version v2 = new Version("3.22.111");

assertEquals(3, v2.getMajor());
assertEquals(22, v2.getMinor());
assertEquals(111, v2.getPatch());
}

@Test
@DisplayName("Test comparing Version objects")
public void testComparingVersionObjects() {
Version v1 = new Version("1.2.3");
Version v2 = new Version("1.2.3");
Version v3 = new Version("2.2.4");
Version v4 = new Version("0.99.99");
Version v5 = new Version("0.100.0");

assertFalse(Version.outdated(v1, v2));
assertFalse(Version.outdated(v1, v3));
assertTrue(Version.outdated(v3, v1));
assertTrue(Version.outdated(v1, v4));
assertFalse(Version.outdated(v4, v5));
assertTrue(Version.outdated(v5, v4));
}

@Test
@DisplayName("Test Version to String conversion")
public void testVersionToStringConversion() {
assertEquals("1.2.3", new Version("1.2.3").toString());
assertEquals("1.22.333", new Version("1.22.333").toString());
assertEquals("4.0.4", new Version("4.0.4").toString());
assertEquals("0.0.0", new Version("0.0.0").toString());
}

@Test
@DisplayName("Test Version creation with faulty input")
public void testVersionCreationWithFaultyInput() {
assertThrows(NumberFormatException.class, () -> new Version("a.b.c"));
assertThrows(NumberFormatException.class, () -> new Version("1.2.3.4"));
assertThrows(NumberFormatException.class, () -> new Version("5.6"));
assertThrows(NumberFormatException.class, () -> new Version(""));
assertThrows(NumberFormatException.class, () -> new Version(null));
}
}
54 changes: 54 additions & 0 deletions src/test/java/fabulator/parse/FasmParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package fabulator.parse;

import fabulator.lookup.BitstreamConfiguration;
import fabulator.lookup.Net;
import fabulator.object.DiscreteLocation;
import javafx.util.Pair;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.net.URL;

import static org.junit.jupiter.api.Assertions.*;

public class FasmParserTest {

private static BitstreamConfiguration config;

@BeforeAll
@DisplayName("Test parsing fasm file")
public static void testParsingFasmFile() {
URL fasmSourceUrl = FasmParserTest.class.getResource("/parse/test_user_design.fasm");
assertNotNull(fasmSourceUrl);

String fileName = fasmSourceUrl.getFile();
FasmParser parser = new FasmParser(fileName);

config = parser.getConfig();
}

@Test
@DisplayName("Test FasmParser correctness")
public void testFasmParserCorrectness() {
Net n1 = config.getNetMap().get("test_net_1");
Net n2 = config.getNetMap().get("test_net_2");
Net n3 = config.getNetMap().get("test_net_3");

assertEquals(4, n1.getEntries().size());
assertEquals(0, n2.getEntries().size());
assertEquals(2, n3.getEntries().size());

Pair<DiscreteLocation, BitstreamConfiguration.ConnectedPorts> firstEntry;
firstEntry = n1.getEntries().get(0);

DiscreteLocation firstLocation = firstEntry.getKey();
String portA = firstEntry.getValue().getPortA();
String portB = firstEntry.getValue().getPortB();

assertEquals(0, firstLocation.getX());
assertEquals(1, firstLocation.getY());
assertEquals("test_port_1", portA);
assertEquals("test_port_2", portB);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fabulator.builder;
package fabulator.ui.builder;

import fabulator.language.Text;
import fabulator.ui.builder.*;
import fabulator.ui.style.StyleClass;
import fabulator.ui.style.UiColor;
import javafx.geometry.Pos;
Expand Down
11 changes: 11 additions & 0 deletions src/test/resources/parse/test_user_design.fasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# routing for net 'test_net_1'
X0Y1.test_port_1.test_port_2
X0Y1.test_port_2.test_port_3
X2Y1.test_port_1.test_port_4
X0Y3.test_port_5.test_port_7

# routing for net 'test_net_2'

# routing for net 'test_net_3'
X11Y1.test_port_4.test_port_1
X7Y13.test_port7.test_port_6

0 comments on commit 19368c9

Please sign in to comment.