From a2ea38d2c93d29ee01b22c09b6f154da0d4974f1 Mon Sep 17 00:00:00 2001 From: Paul Sandoz Date: Thu, 22 Mar 2018 09:07:08 -0700 Subject: [PATCH] 8199773: (bf) XXXBuffer:compareTo method is not working as expected Reviewed-by: alanb --- .../classes/java/nio/StringCharBuffer.java | 4 +- .../classes/java/nio/X-Buffer.java.template | 4 +- .../java/nio/Buffer/EqualsCompareTest.java | 93 ++++++++++--------- 3 files changed, 52 insertions(+), 49 deletions(-) diff --git a/src/java.base/share/classes/java/nio/StringCharBuffer.java b/src/java.base/share/classes/java/nio/StringCharBuffer.java index 26b355bc819..6552ed33b4e 100644 --- a/src/java.base/share/classes/java/nio/StringCharBuffer.java +++ b/src/java.base/share/classes/java/nio/StringCharBuffer.java @@ -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(); } diff --git a/src/java.base/share/classes/java/nio/X-Buffer.java.template b/src/java.base/share/classes/java/nio/X-Buffer.java.template index 2b76b9fdac6..b59755b1fd5 100644 --- a/src/java.base/share/classes/java/nio/X-Buffer.java.template +++ b/src/java.base/share/classes/java/nio/X-Buffer.java.template @@ -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(); } diff --git a/test/jdk/java/nio/Buffer/EqualsCompareTest.java b/test/jdk/java/nio/Buffer/EqualsCompareTest.java index 11cc028f425..a7520397e26 100644 --- a/test/jdk/java/nio/Buffer/EqualsCompareTest.java +++ b/test/jdk/java/nio/Buffer/EqualsCompareTest.java @@ -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 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); + } } } }