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/07 17:19:45 UTC

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

Thibault Kruse created GROOVY-7581:
--------------------------------------

             Summary: 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:

- a value of -1, 0, 1 should be returned
- 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)


// bug, returns 47?
// assert compareTo('aa1', '2' as Character) == 1

// bug, classCast exception
// assert compareTo('2' as Character, 'aa1') == 1

// bug, returns 47?
// assert compareTo("aa${1}", '2' as Character) == 1

// bug, classCast exception
//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)