8199773: (bf) XXXBuffer:compareTo method is not working as expected

Reviewed-by: alanb
This commit is contained in:
Paul Sandoz 2018-03-22 09:07:08 -07:00
parent 917c015610
commit a2ea38d2c9
3 changed files with 52 additions and 49 deletions
src/java.base/share/classes/java/nio
test/jdk/java/nio/Buffer

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -149,7 +149,7 @@ class StringCharBuffer // package-private
that, that.position(),
Math.min(this.remaining(), that.remaining()));
if (i >= 0) {
return Character.compare(this.get(this.position() + i), that.get(this.position() + i));
return Character.compare(this.get(this.position() + i), that.get(that.position() + i));
}
return this.remaining() - that.remaining();
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -1337,7 +1337,7 @@ public abstract class $Type$Buffer
that, that.position(),
Math.min(this.remaining(), that.remaining()));
if (i >= 0) {
return compare(this.get(this.position() + i), that.get(this.position() + i));
return compare(this.get(this.position() + i), that.get(that.position() + i));
}
return this.remaining() - that.remaining();
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -45,7 +45,7 @@ import java.util.stream.IntStream;
/*
* @test
* @bug 8193085
* @bug 8193085 8199773
* @summary tests for buffer equals and compare
* @run testng EqualsCompareTest
*/
@ -120,8 +120,9 @@ public class EqualsCompareTest {
abstract T construct(int length, ByteOrder bo);
@SuppressWarnings("unchecked")
T slice(T a, int from, int to) {
return (T) a.position(from).limit(to).slice();
T slice(T a, int from, int to, boolean dupOtherwiseSlice) {
a = (T) a.position(from).limit(to);
return (T) (dupOtherwiseSlice ? a.duplicate() : a.slice());
}
@SuppressWarnings("unchecked")
@ -607,58 +608,60 @@ public class EqualsCompareTest {
BiFunction<BT, Integer, B> bConstructor) {
int n = arraySizeFor(bt.elementType);
for (int s : ranges(0, n)) {
B a = aConstructor.apply(bt, s);
B b = bConstructor.apply(bt, s);
for (boolean dupOtherwiseSlice : new boolean[]{ false, true }) {
for (int s : ranges(0, n)) {
B a = aConstructor.apply(bt, s);
B b = bConstructor.apply(bt, s);
for (int aFrom : ranges(0, s)) {
for (int aTo : ranges(aFrom, s)) {
int aLength = aTo - aFrom;
for (int aFrom : ranges(0, s)) {
for (int aTo : ranges(aFrom, s)) {
int aLength = aTo - aFrom;
B as = aLength != s
? bt.slice(a, aFrom, aTo)
: a;
B as = aLength != s
? bt.slice(a, aFrom, aTo, dupOtherwiseSlice)
: a;
for (int bFrom : ranges(0, s)) {
for (int bTo : ranges(bFrom, s)) {
int bLength = bTo - bFrom;
for (int bFrom : ranges(0, s)) {
for (int bTo : ranges(bFrom, s)) {
int bLength = bTo - bFrom;
B bs = bLength != s
? bt.slice(b, bFrom, bTo)
: b;
B bs = bLength != s
? bt.slice(b, bFrom, bTo, dupOtherwiseSlice)
: b;
boolean eq = bt.pairWiseEquals(as, bs);
Assert.assertEquals(bt.equals(as, bs), eq);
Assert.assertEquals(bt.equals(bs, as), eq);
if (eq) {
Assert.assertEquals(bt.compare(as, bs), 0);
Assert.assertEquals(bt.compare(bs, as), 0);
}
else {
int aCb = bt.compare(as, bs);
int bCa = bt.compare(bs, as);
int v = Integer.signum(aCb) * Integer.signum(bCa);
Assert.assertTrue(v == -1);
boolean eq = bt.pairWiseEquals(as, bs);
Assert.assertEquals(bt.equals(as, bs), eq);
Assert.assertEquals(bt.equals(bs, as), eq);
if (eq) {
Assert.assertEquals(bt.compare(as, bs), 0);
Assert.assertEquals(bt.compare(bs, as), 0);
}
else {
int aCb = bt.compare(as, bs);
int bCa = bt.compare(bs, as);
int v = Integer.signum(aCb) * Integer.signum(bCa);
Assert.assertTrue(v == -1);
}
}
}
}
if (aLength > 0 && !a.isReadOnly()) {
for (int i = aFrom; i < aTo; i++) {
B c = aConstructor.apply(bt, a.capacity());
B cs = aLength != s
? bt.slice(c, aFrom, aTo)
: c;
if (aLength > 0 && !a.isReadOnly()) {
for (int i = aFrom; i < aTo; i++) {
B c = aConstructor.apply(bt, a.capacity());
B cs = aLength != s
? bt.slice(c, aFrom, aTo, dupOtherwiseSlice)
: c;
// Create common prefix with a length of i - aFrom
bt.set(c, i, -1);
// Create common prefix with a length of i - aFrom
bt.set(c, i, -1);
Assert.assertFalse(bt.equals(c, a));
Assert.assertFalse(bt.equals(c, a));
int cCa = bt.compare(cs, as);
int aCc = bt.compare(as, cs);
int v = Integer.signum(cCa) * Integer.signum(aCc);
Assert.assertTrue(v == -1);
int cCa = bt.compare(cs, as);
int aCc = bt.compare(as, cs);
int v = Integer.signum(cCa) * Integer.signum(aCc);
Assert.assertTrue(v == -1);
}
}
}
}