Java - How to unit test problems with TimSort and "general contract breaches"

This question is related to "The comparison method violates his general contract!" - TimSort and GridLayout  and several other similar issues of "general contract breach". My question, in particular, is related to Ceekay's answer at the bottom of the page "How to test TimSort implementation". In my case, I fixed an application error that brought me here, which was due to a symmetry violation, but it is difficult for me to create a unit test to detect this violation (if the correction is commented out or not fixed in the future).

public class TickNumber implements Comparable<TickNumber> {
    protected String zone;
    protected String track;
}
public class GisTickNumber extends TickNumber implements Comparable<TickNumber> {
    private String suffix;
}

I did not take into account all the implementation details, but basically the Tick number is a 4-digit number, where the first two digits are the zone and the second is the track. GisTickNumbers can have alpha characters in the fields of a zone and / or track, and they can have an alpha suffix of one or two characters. Valid ticks are integers in a range [0000, 9999](even if they are represented as strings). All valid tick numbers are valid. Gis Tick numbers, but valid Gis Ticks may also look like A912, R123, 0123G, A346*.

, GisTick compareTo , compareTo . , 'this' 0000 Tick, 'that' 0000* Gis Tick, 0000.compareTo(0000*) 0. if 'this' 0000* Gis Tick 'that' 0000 Tick, 0000*.compareTo(0000) 1. ( , )

Ceekay ,

  • 32 .
  • [] .
  • 3 .

, [] , .

, TickNumber ( GisTickNumber) unit test, . , 100 , , 10 . , , , , , Collections.sort(testList) - " ()"?

  • , , unit test, .
+4
1

:
, toString() , , TickNumber unit test. , , , " " " ".

, , , TimsSort " ". ...

  • 64 (49 + 1 + 12 + 1 + 1)
  • 50, 49 50 0 ( )
    • " " , ( ), " ", .
  • 2 ( 8, 4)
    • " " 4 ( ).
  • ( - 1),
  • () , -

, , , , .

, 49 TickNumber, Tick = "9999" , - 49 Ticks "9910" , 50 -. (Pseudo, "9910" 49 "9999" Ticks.) Tick "9910" , . 12 GisTickNumber 8 ( "9915 *", "9920 *", "9922 *", "9931 *", "9933 *", "9934 *", "9936 *", 9939 * "), 4 (" 9907 * "," 9908 * "," 9909 * "," 9910 * "). , 4 - " ", . , TickNumber "9901 ", , " 9978 * "GisTickNumber, - . / . Unit test () , , , " 9901 " . ( , " 9901" )

. , TickNumber "9910" MIN_RUN'th. , MIN_RUN 32, 50 49, " ", "9910" , 32. ; , . ( )

, - "", " ", , .

TickNumber, , 49 "9999" Ticks 2 ( "9910" "9910 *" ) Ticks, .

0

Source: https://habr.com/ru/post/1620657/


All Articles