test sync

This commit is contained in:
Bane Stojkovic
2026-11-06 09:29:50 +01:00
parent f249ba6b9e
commit 35bf22edea
70 changed files with 794 additions and 470 deletions
@@ -106,7 +106,10 @@ public class Group implements Comparable<Group> {
@Override
public int compareTo(Group o) {
return toString().compareToIgnoreCase(o.toString());
String text = toString();
String otherText = o.toString();
int comparison = text.compareToIgnoreCase(otherText);
return comparison != 0 ? comparison : text.compareTo(otherText);
}
@Override
@@ -23,6 +23,7 @@ package org.diylc.netlist;
import java.awt.geom.Point2D;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
@@ -41,6 +42,13 @@ public class Node implements Comparable<Node> {
private static final Set<String> NON_NET_NAMES =
new HashSet<String>(Arrays.asList("NC", "RSV", "IC"));
/**
* Orders points top to bottom, then left to right. Breaks ties between parts that share a name:
* where a part sits is the same in every run, unlike the order a hash set yields it in.
*/
static final Comparator<Point2D> LOCATION_ORDER = Comparator.nullsLast(
Comparator.comparingDouble(Point2D::getY).thenComparingDouble(Point2D::getX));
private IDIYComponent<?> component;
private int pointIndex;
@@ -137,6 +145,15 @@ public class Node implements Comparable<Node> {
@Override
public int compareTo(Node o) {
return toString().compareToIgnoreCase(o.toString());
String name = toString();
String otherName = o.toString();
int comparison = name.compareToIgnoreCase(otherName);
if (comparison == 0) {
comparison = name.compareTo(otherName);
}
if (comparison == 0) {
comparison = LOCATION_ORDER.compare(getPoint2D(), o.getPoint2D());
}
return comparison != 0 ? comparison : Integer.compare(pointIndex, o.pointIndex);
}
}
@@ -21,6 +21,8 @@
*/
package org.diylc.netlist;
import java.awt.geom.Point2D;
import org.diylc.core.IDIYComponent;
import org.diylc.core.ISwitch;
@@ -84,9 +86,20 @@ public class Position implements Comparable<Position> {
@Override
public int compareTo(Position o) {
int comparison = getSwitchName().compareToIgnoreCase(o.getSwitchName());
return comparison != 0
? comparison
: Integer.compare(position, o.position);
if (comparison == 0) {
comparison = getSwitchName().compareTo(o.getSwitchName());
}
// switches that share a name keep apart by location, so a setup reads the same in every run
if (comparison == 0) {
comparison = Node.LOCATION_ORDER.compare(getSwitchLocation(), o.getSwitchLocation());
}
return comparison != 0 ? comparison : Integer.compare(position, o.position);
}
private Point2D getSwitchLocation() {
return theSwitch instanceof IDIYComponent<?> component && component.getControlPointCount() > 0
? component.getControlPoint(0)
: null;
}
String getSwitchName() {
@@ -124,6 +124,7 @@ public class SwitchSetup implements Comparable<SwitchSetup> {
}
// Same earliest full configuration: use the description as a tie-breaker.
return toString().compareToIgnoreCase(o.toString());
int comparison = toString().compareToIgnoreCase(o.toString());
return comparison != 0 ? comparison : toString().compareTo(o.toString());
}
}
@@ -0,0 +1,86 @@
/*
*
* DIY Layout Creator (DIYLC).
* Copyright (c) 2009-2025 held jointly by the individual authors.
*
* This file is part of DIYLC.
*
* DIYLC is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DIYLC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.diylc.netlist;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.diylc.testcomponents.OnePointTestComponent;
import org.diylc.testcomponents.SwitchTestComponent;
public class NetlistSortingTest {
@Test
public void namesDifferingOnlyInCaseSortTheSameEitherWay() {
Node lower = new Node(new OnePointTestComponent("1k 2w", "2", new Point2D.Double(0, 0)), 0);
Node upper = new Node(new OnePointTestComponent("1k 2W", "2", new Point2D.Double(0, 0)), 0);
Assert.assertEquals(sorted(lower, upper), sorted(upper, lower));
}
@Test
public void sameNamedPartsSortByLocation() {
Node top = new Node(new OnePointTestComponent("R1", "1", new Point2D.Double(50, 0)), 0);
Node bottom = new Node(new OnePointTestComponent("R1", "1", new Point2D.Double(0, 10)), 0);
Assert.assertEquals(Arrays.asList(top, bottom), sorted(bottom, top));
Assert.assertEquals(Arrays.asList(top, bottom), sorted(top, bottom));
}
@Test
public void sameNamedSwitchesKeepTheirPlaceInASetup() {
SwitchTestComponent upper = switchAt(0);
SwitchTestComponent lower = switchAt(10);
SwitchSetup upperOn = setup(new Position(lower, 1), new Position(upper, 0));
SwitchSetup lowerOn = setup(new Position(upper, 1), new Position(lower, 0));
Assert.assertEquals("SW1.ON:SW1.OFF", upperOn.toString());
Assert.assertEquals("SW1.OFF:SW1.ON", lowerOn.toString());
Assert.assertEquals(-Integer.signum(lowerOn.compareTo(upperOn)),
Integer.signum(upperOn.compareTo(lowerOn)));
Assert.assertNotEquals(0, upperOn.compareTo(lowerOn));
}
private static List<Node> sorted(Node... nodes) {
List<Node> list = new ArrayList<Node>(Arrays.asList(nodes));
Collections.sort(list);
return list;
}
private static SwitchTestComponent switchAt(double y) {
SwitchTestComponent component = new SwitchTestComponent("SW1");
component.setControlPoint(new Point2D.Double(0, y), 0);
component.setControlPoint(new Point2D.Double(10, y), 1);
return component;
}
private static SwitchSetup setup(Position... positions) {
return new SwitchSetup(new ArrayList<Position>(Arrays.asList(positions)));
}
}