You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Thibault Kruse (JIRA)" <ji...@apache.org> on 2015/09/10 14:57:46 UTC

[jira] [Commented] (GROOVY-7581) Multiple bugs in DefaultTypeTransformation.compareToWithEqualityCheck()

    [ https://issues.apache.org/jira/browse/GROOVY-7581?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14738706#comment-14738706 ] 

Thibault Kruse commented on GROOVY-7581:
----------------------------------------

A test classis available at 
https://github.com/tkruse/incubator-groovy/commit/023be12d89e13543ae1e760094ddc1f9aa4980f4

> Multiple bugs in DefaultTypeTransformation.compareToWithEqualityCheck()
> -----------------------------------------------------------------------
>
>                 Key: GROOVY-7581
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7581
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: Thibault Kruse
>
> The purpose of compareToWithEqualityCheck() seems to be to allow comparing instances of different classes that cannot easily be compared with Java. All Numbers should be comparable to each other, numbers should be comparable to chars by the chars int value, and Strings, GStrings and chars should be inter-comparable. The comparison should be symmetric and generally simulate Comparable.compareTo(), meaning:
> EDIT: [ wrong: - a value of -1, 0, 1 should be returned]
> - an integer value returned compatible with java.lang.Comparable.compareTo()
> - if compareToWithEqualityCheck(a, b) returns a value, then compareToWithEqualityCheck(b, a) == -compareToWithEqualityCheck(a, b) should hold
> - if compareToWithEqualityCheck(a, b) thorws an exception, then compareToWithEqualityCheck(a, b) should also throw one (of the same type)
> That's what the method seemingly attempts, so this will be considered the methods specification.
> However, there are several cases where these requirements do nothold, as shown by the following failing asserts:
> {code}
>  static class MyNumber extends Number {
>         def n
>         MyNumber(n) {
>             this.n = n
>         }
>         int intValue(){n}
>         long longValue(){n}
>         float floatValue(){n}
>         double doubleValue(){n}
>         int hashCode(){-n}
>         boolean equals(other) {
>             if (other instanceof MyNumber) { return n==other.n}
>             return false
>         }
>         String toString(){"MyNumber($n)"}
>     }
>     static class MyNumberComparable extends MyNumber implements Comparable<MyNumber> {
>         MyNumberComparable(Object n) {
>             super(n)
>         }
>         int compareTo(Object other) {
>             return n <=>  (MyNumber) other;
>         }
>     }
> // works
> checkCompareToSymmetricSmallerThan(numComp1, 50)
> // Bug, NumberFormatException, comparison to all other Number types works
> // checkCompareToSymmetricSmallerThan(numComp1, 50G)
> // assert compareTo('aa1', '2' as Character) == 47
> // bug, classCast exception. Should be some negative value?
> // assert compareTo('2' as Character, 'aa1') == 1
> // assert compareTo("aa${1}", '2' as Character) == 47
> // bug, classCast exception. Should be some negative value?
> //assert compareTo('2' as Character, "aa${1}") == 1
> MyNumber number2 = new MyNumber(50)
> // works
> assert compareTo(49, number2) == -1
> // bug: GroovyRuntimeException, but other way round works
> //assert compareTo(number2, 49) == -1
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)