Changeset 7109 in josm for trunk/test/unit
 Timestamp:
 20140512T00:40:09+02:00 (10 years ago)
 Location:
 trunk/test/unit/org/openstreetmap/josm
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

trunk/test/unit/org/openstreetmap/josm/TestUtils.java
r7068 r7109 5 5 import static org.junit.Assert.assertThat; 6 6 import static org.junit.Assert.assertTrue; 7 import static org.junit.Assert.fail; 7 8 9 import java.util.Arrays; 10 import java.util.Comparator; 8 11 import java.util.Map; 9 12 10 13 import org.junit.Test; 11 import org.openstreetmap.josm.Main;12 14 import org.openstreetmap.josm.data.osm.Node; 13 15 import org.openstreetmap.josm.data.osm.OsmPrimitive; … … 65 67 } 66 68 69 /** 70 * Checks that the given Comparator respects its contract on the given table. 71 * @param comparator The comparator to test 72 * @param array The array sorted for test purpose 73 */ 74 public static <T> void checkComparableContract(Comparator<T> comparator, T[] array) { 75 System.out.println("Validating Comparable contract on array of "+array.length+" elements"); 76 // Check each compare possibility 77 for (int i=0; i<array.length; i++) { 78 T r1 = array[i]; 79 for (int j=i; j<array.length; j++) { 80 T r2 = array[j]; 81 int a = comparator.compare(r1, r2); 82 int b = comparator.compare(r2, r1); 83 if (i==j  a==b) { 84 if (a != 0  b != 0) { 85 fail(getFailMessage(r1, r2, a, b)); 86 } 87 } else { 88 if (a != b) { 89 fail(getFailMessage(r1, r2, a, b)); 90 } 91 } 92 for (int k=j; k<array.length; k++) { 93 T r3 = array[k]; 94 int c = comparator.compare(r1, r3); 95 int d = comparator.compare(r2, r3); 96 if (a > 0 && d > 0) { 97 if (c <= 0) { 98 fail(getFailMessage(r1, r2, r3, a, b, c, d)); 99 } 100 } else if (a == 0 && d == 0) { 101 if (c != 0) { 102 fail(getFailMessage(r1, r2, r3, a, b, c, d)); 103 } 104 } else if (a < 0 && d < 0) { 105 if (c >= 0) { 106 fail(getFailMessage(r1, r2, r3, a, b, c, d)); 107 } 108 } 109 } 110 } 111 } 112 // Sort relation array 113 Arrays.sort(array, comparator); 114 } 115 116 private static <T> String getFailMessage(T o1, T o2, int a, int b) { 117 return new StringBuilder("Compared\no1: ").append(o1).append("\no2: ") 118 .append(o2).append("\ngave: ").append(a).append("/").append(b) 119 .toString(); 120 } 121 122 private static <T> String getFailMessage(T o1, T o2, T o3, int a, int b, int c, int d) { 123 return new StringBuilder(getFailMessage(o1, o2, a, b)) 124 .append("\nCompared\no1: ").append(o1).append("\no3: ").append(o3).append("\ngave: ").append(c) 125 .append("\nCompared\no2: ").append(o2).append("\no3: ").append(o3).append("\ngave: ").append(d) 126 .toString(); 127 } 67 128 } 
trunk/test/unit/org/openstreetmap/josm/gui/DefaultNameFormatterTest.java
r7100 r7109 3 3 4 4 import static org.junit.Assert.assertTrue; 5 import static org.junit.Assert.fail;6 5 7 6 import java.io.FileInputStream; … … 9 8 import java.io.InputStream; 10 9 import java.util.ArrayList; 11 import java.util.Arrays;12 10 import java.util.Comparator; 13 11 … … 75 73 Relation[] relations = new ArrayList<>(ds.getRelations()).toArray(new Relation[0]); 76 74 77 // Check each compare possibility 78 for (int i=0; i<relations.length; i++) { 79 Relation r1 = relations[i]; 80 for (int j=i; j<relations.length; j++) { 81 Relation r2 = relations[j]; 82 int a = comparator.compare(r1, r2); 83 int b = comparator.compare(r2, r1); 84 if (i==j  a==b) { 85 if (a != 0  b != 0) { 86 fail(getFailMessage(r1, r2, a, b)); 87 } 88 } else { 89 if (a != b) { 90 fail(getFailMessage(r1, r2, a, b)); 91 } 92 } 93 for (int k=j; k<relations.length; k++) { 94 Relation r3 = relations[k]; 95 int c = comparator.compare(r1, r3); 96 int d = comparator.compare(r2, r3); 97 if (a > 0 && d > 0) { 98 if (c <= 0) { 99 fail(getFailMessage(r1, r2, r3, a, b, c, d)); 100 } 101 } else if (a == 0 && d == 0) { 102 if (c != 0) { 103 fail(getFailMessage(r1, r2, r3, a, b, c, d)); 104 } 105 } else if (a < 0 && d < 0) { 106 if (c >= 0) { 107 fail(getFailMessage(r1, r2, r3, a, b, c, d)); 108 } 109 } 110 } 111 } 112 } 113 // Sort relation array 114 Arrays.sort(relations, comparator); 75 TestUtils.checkComparableContract(comparator, relations); 115 76 } 116 77 } 117 118 private static String getFailMessage(Relation r1, Relation r2, int a, int b) {119 return new StringBuilder("Compared\nr1: ").append(r1).append("\nr2: ")120 .append(r2).append("\ngave: ").append(a).append("/").append(b)121 .toString();122 }123 124 private static String getFailMessage(Relation r1, Relation r2, Relation r3, int a, int b, int c, int d) {125 return new StringBuilder(getFailMessage(r1, r2, a, b))126 .append("\nCompared\nr1: ").append(r1).append("\nr3: ").append(r3).append("\ngave: ").append(c)127 .append("\nCompared\nr2: ").append(r2).append("\nr3: ").append(r3).append("\ngave: ").append(d)128 .toString();129 }130 78 }
Note:
See TracChangeset
for help on using the changeset viewer.