From 3d0396bc0025e5e64f340fcb318f01a029361a7d Mon Sep 17 00:00:00 2001 From: JoePfeiffer Date: Sat, 25 Sep 2021 14:20:06 -0600 Subject: [PATCH] update unit tests to match new motor code (also fixed a couple bugs the unittests turned up!) --- .../database/motor/ThrustCurveMotorSet.java | 5 ++- .../motor/DesignationComparator.java | 2 +- .../sf/openrocket/motor/ThrustCurveMotor.java | 4 +- .../database/ThrustCurveMotorSetTest.java | 38 ++++++++----------- .../motor/ThrustCurveMotorTest.java | 12 ++++++ 5 files changed, 34 insertions(+), 27 deletions(-) diff --git a/core/src/net/sf/openrocket/database/motor/ThrustCurveMotorSet.java b/core/src/net/sf/openrocket/database/motor/ThrustCurveMotorSet.java index 38479dee5..782c82e32 100644 --- a/core/src/net/sf/openrocket/database/motor/ThrustCurveMotorSet.java +++ b/core/src/net/sf/openrocket/database/motor/ThrustCurveMotorSet.java @@ -221,7 +221,7 @@ public class ThrustCurveMotorSet implements Comparable { return false; } - if (!designation.equalsIgnoreCase(m.getDesignation())) + if (!commonName.equalsIgnoreCase(m.getCommonName())) return false; if (caseInfo != null && !caseInfo.equalsIgnoreCase(m.getCaseInfo())) @@ -348,11 +348,12 @@ public class ThrustCurveMotorSet implements Comparable { @Override public int compare(ThrustCurveMotor o1, ThrustCurveMotor o2) { + // 1. Designation if (!o1.getDesignation().equals(o2.getDesignation())) { return o1.getDesignation().compareTo(o2.getDesignation()); } - + // 2. Number of data points (more is better) if (o1.getSampleSize() != o2.getSampleSize()) { return o2.getSampleSize() - o1.getSampleSize(); diff --git a/core/src/net/sf/openrocket/motor/DesignationComparator.java b/core/src/net/sf/openrocket/motor/DesignationComparator.java index 2c0e8f1f4..651a2ca02 100644 --- a/core/src/net/sf/openrocket/motor/DesignationComparator.java +++ b/core/src/net/sf/openrocket/motor/DesignationComparator.java @@ -90,4 +90,4 @@ public class DesignationComparator implements Comparator { return COLLATOR.compare(o1, o2); } } -} \ No newline at end of file +} diff --git a/core/src/net/sf/openrocket/motor/ThrustCurveMotor.java b/core/src/net/sf/openrocket/motor/ThrustCurveMotor.java index d5256d186..69396cf40 100644 --- a/core/src/net/sf/openrocket/motor/ThrustCurveMotor.java +++ b/core/src/net/sf/openrocket/motor/ThrustCurveMotor.java @@ -158,7 +158,7 @@ public class ThrustCurveMotor implements Motor, Comparable, Se * @return the simplified designation, or the string itself if the format was not detected */ private static final Pattern SIMPLIFY_PATTERN = Pattern.compile("^[0-9]*[ -]*([A-Z][0-9]+).*"); - static String simplifyDesignation(String str) { + public static String simplifyDesignation(String str) { str = str.trim(); Matcher m = SIMPLIFY_PATTERN.matcher(str); if (m.matches()) { @@ -244,7 +244,7 @@ public class ThrustCurveMotor implements Motor, Comparable, Se // If I don't have a motor common name (will be the case if I read the thrustcurve from a flle) // apply the motor code simplification heuristics to generate a common name if (motor.commonName.equals("")) { - motor.commonName = motor.designation; + motor.commonName = simplifyDesignation(motor.designation); } diff --git a/core/test/net/sf/openrocket/database/ThrustCurveMotorSetTest.java b/core/test/net/sf/openrocket/database/ThrustCurveMotorSetTest.java index bd17f6b70..572b9a823 100644 --- a/core/test/net/sf/openrocket/database/ThrustCurveMotorSetTest.java +++ b/core/test/net/sf/openrocket/database/ThrustCurveMotorSetTest.java @@ -22,6 +22,7 @@ public class ThrustCurveMotorSetTest { private static final ThrustCurveMotor motor1 = new ThrustCurveMotor.Builder() .setManufacturer(Manufacturer.getManufacturer("A")) + .setCommonName("F12") .setDesignation("F12X") .setDescription("Desc") .setMotorType(Motor.Type.UNKNOWN) @@ -36,6 +37,7 @@ public class ThrustCurveMotorSetTest { private static final ThrustCurveMotor motor2 = new ThrustCurveMotor.Builder() .setManufacturer(Manufacturer.getManufacturer("A")) + .setCommonName("F12") .setDesignation("F12H") .setDescription("Desc") .setMotorType(Motor.Type.SINGLE) @@ -50,7 +52,7 @@ public class ThrustCurveMotorSetTest { private static final ThrustCurveMotor motor3 = new ThrustCurveMotor.Builder() .setManufacturer(Manufacturer.getManufacturer("A")) - .setDesignation("F12") + .setCode("F12") .setDescription("Desc") .setMotorType(Motor.Type.UNKNOWN) .setStandardDelays(new double[] { 0, Motor.PLUGGED_DELAY }) @@ -65,7 +67,7 @@ public class ThrustCurveMotorSetTest { private static final ThrustCurveMotor motor4 = new ThrustCurveMotor.Builder() .setManufacturer(Manufacturer.getManufacturer("A")) .setDesignation("F12") - .setDesignation("Desc") + .setDescription("Desc") .setMotorType(Motor.Type.HYBRID) .setStandardDelays(new double[] { 0 }) .setDiameter(0.024) @@ -75,20 +77,7 @@ public class ThrustCurveMotorSetTest { .setCGPoints(new Coordinate[] { Coordinate.NUL, Coordinate.NUL, Coordinate.NUL }) .setDigest("digestD") .build(); - - - @Test - public void testSimplifyDesignation() { - assertEquals("J115", ThrustCurveMotorSet.simplifyDesignation("J115")); - assertEquals("J115", ThrustCurveMotorSet.simplifyDesignation(" J115 ")); - assertEquals("H115", ThrustCurveMotorSet.simplifyDesignation("241H115-KS")); - assertEquals("J115", ThrustCurveMotorSet.simplifyDesignation("384 J115")); - assertEquals("J115", ThrustCurveMotorSet.simplifyDesignation("384-J115")); - assertEquals("A2", ThrustCurveMotorSet.simplifyDesignation("A2T")); - assertEquals("1/2A2T", ThrustCurveMotorSet.simplifyDesignation("1/2A2T")); - assertEquals("MicroMaxxII", ThrustCurveMotorSet.simplifyDesignation("Micro Maxx II")); - } - + @Test public void testAdding() { ThrustCurveMotorSet set = new ThrustCurveMotorSet(); @@ -108,7 +97,7 @@ public class ThrustCurveMotorSetTest { assertEquals(1, set.getMotors().size()); assertEquals(motor1, set.getMotors().get(0)); assertEquals(Collections.emptyList(), set.getDelays()); - + // Add motor1 again assertTrue(set.matches(motor1)); set.addMotor(motor1); @@ -120,12 +109,12 @@ public class ThrustCurveMotorSetTest { assertEquals(1, set.getMotors().size()); assertEquals(motor1, set.getMotors().get(0)); assertEquals(Collections.emptyList(), set.getDelays()); - + // Add motor2 assertTrue(set.matches(motor2)); set.addMotor(motor2); assertEquals(motor1.getManufacturer(), set.getManufacturer()); - assertEquals(motor3.getDesignation(), set.getDesignation()); + assertEquals(motor3.getCommonName(), set.getCommonName()); assertEquals(Motor.Type.SINGLE, set.getType()); assertEquals(motor1.getDiameter(), set.getDiameter(), 0.00001); assertEquals(motor1.getLength(), set.getLength(), 0.00001); @@ -133,21 +122,26 @@ public class ThrustCurveMotorSetTest { assertEquals(motor2, set.getMotors().get(0)); assertEquals(motor1, set.getMotors().get(1)); assertEquals(Arrays.asList(5.0), set.getDelays()); - + // Add motor3 assertTrue(set.matches(motor3)); set.addMotor(motor3); assertEquals(motor1.getManufacturer(), set.getManufacturer()); - assertEquals(motor3.getDesignation(), set.getDesignation()); + assertEquals(motor3.getCommonName(), set.getCommonName()); assertEquals(Motor.Type.SINGLE, set.getType()); assertEquals(motor1.getDiameter(), set.getDiameter(), 0.00001); assertEquals(motor1.getLength(), set.getLength(), 0.00001); assertEquals(3, set.getMotors().size()); + System.out.println("motor set"); + System.out.println(set.getMotors()); + System.out.println(motor3); + System.out.println(motor2); + System.out.println(motor1); assertEquals(motor3, set.getMotors().get(0)); assertEquals(motor2, set.getMotors().get(1)); assertEquals(motor1, set.getMotors().get(2)); assertEquals(Arrays.asList(0.0, 5.0, Motor.PLUGGED_DELAY), set.getDelays()); - + // Test that adding motor4 fails assertFalse(set.matches(motor4)); try { diff --git a/core/test/net/sf/openrocket/motor/ThrustCurveMotorTest.java b/core/test/net/sf/openrocket/motor/ThrustCurveMotorTest.java index f2c93cfb4..6ceac6eab 100644 --- a/core/test/net/sf/openrocket/motor/ThrustCurveMotorTest.java +++ b/core/test/net/sf/openrocket/motor/ThrustCurveMotorTest.java @@ -186,4 +186,16 @@ public class ThrustCurveMotorTest { } + @Test + public void testSimplifyDesignation() { + assertEquals("J115", ThrustCurveMotor.Builder.simplifyDesignation("J115")); + assertEquals("J115", ThrustCurveMotor.Builder.simplifyDesignation(" J115 ")); + assertEquals("H115", ThrustCurveMotor.Builder.simplifyDesignation("241H115-KS")); + assertEquals("J115", ThrustCurveMotor.Builder.simplifyDesignation("384 J115")); + assertEquals("J115", ThrustCurveMotor.Builder.simplifyDesignation("384-J115")); + assertEquals("A2", ThrustCurveMotor.Builder.simplifyDesignation("A2T")); + assertEquals("1/2A2T", ThrustCurveMotor.Builder.simplifyDesignation("1/2A2T")); + assertEquals("MicroMaxxII", ThrustCurveMotor.Builder.simplifyDesignation("Micro Maxx II")); + } + }