8199773: (bf) XXXBuffer:compareTo method is not working as expected
authorpsandoz
Thu, 22 Mar 2018 09:07:08 -0700
changeset 49282 271ef464fb3a
parent 49281 6712bdd93e4e
child 49283 a14ede52a278
child 49308 ab72158ccd9d
8199773: (bf) XXXBuffer:compareTo method is not working as expected Reviewed-by: alanb
src/java.base/share/classes/java/nio/StringCharBuffer.java
src/java.base/share/classes/java/nio/X-Buffer.java.template
test/jdk/java/nio/Buffer/EqualsCompareTest.java
--- a/src/java.base/share/classes/java/nio/StringCharBuffer.java	Thu Mar 22 08:41:06 2018 -0700
+++ b/src/java.base/share/classes/java/nio/StringCharBuffer.java	Thu Mar 22 09:07:08 2018 -0700
@@ -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 @@
                                         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();
     }
--- a/src/java.base/share/classes/java/nio/X-Buffer.java.template	Thu Mar 22 08:41:06 2018 -0700
+++ b/src/java.base/share/classes/java/nio/X-Buffer.java.template	Thu Mar 22 09:07:08 2018 -0700
@@ -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 @@
                                         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();
     }
--- a/test/jdk/java/nio/Buffer/EqualsCompareTest.java	Thu Mar 22 08:41:06 2018 -0700
+++ b/test/jdk/java/nio/Buffer/EqualsCompareTest.java	Thu Mar 22 09:07:08 2018 -0700
@@ -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 @@
 
 /*
  * @test
- * @bug 8193085
+ * @bug 8193085 8199773
  * @summary tests for buffer equals and compare
  * @run testng EqualsCompareTest
  */
@@ -120,8 +120,9 @@
         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,60 +608,62 @@
                         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, dupOtherwiseSlice)
+                                       : b;
 
-                            B bs = bLength != s
-                                   ? bt.slice(b, bFrom, bTo)
-                                   : 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);
+                        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);
+
+                                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);
                             }
                         }
                     }
-
-                    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;
-
-                            // Create common prefix with a length of i - aFrom
-                            bt.set(c, i, -1);
-
-                            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);
-                        }
-                    }
                 }
             }
         }