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

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -149,7 +149,7 @@ class StringCharBuffer // package-private
that, that.position(), that, that.position(),
Math.min(this.remaining(), that.remaining())); Math.min(this.remaining(), that.remaining()));
if (i >= 0) { 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(); return this.remaining() - that.remaining();
} }

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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(), that, that.position(),
Math.min(this.remaining(), that.remaining())); Math.min(this.remaining(), that.remaining()));
if (i >= 0) { 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(); return this.remaining() - that.remaining();
} }

View file

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