test/jdk/java/nio/Buffer/EqualsCompareTest.java
changeset 49282 271ef464fb3a
parent 48356 29e165bdf669
child 50596 7cf6578a6b0b
equal deleted inserted replaced
49281:6712bdd93e4e 49282:271ef464fb3a
     1 /*
     1 /*
     2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    43 import java.util.function.LongFunction;
    43 import java.util.function.LongFunction;
    44 import java.util.stream.IntStream;
    44 import java.util.stream.IntStream;
    45 
    45 
    46 /*
    46 /*
    47  * @test
    47  * @test
    48  * @bug 8193085
    48  * @bug 8193085 8199773
    49  * @summary tests for buffer equals and compare
    49  * @summary tests for buffer equals and compare
    50  * @run testng EqualsCompareTest
    50  * @run testng EqualsCompareTest
    51  */
    51  */
    52 
    52 
    53 public class EqualsCompareTest {
    53 public class EqualsCompareTest {
   118         }
   118         }
   119 
   119 
   120         abstract T construct(int length, ByteOrder bo);
   120         abstract T construct(int length, ByteOrder bo);
   121 
   121 
   122         @SuppressWarnings("unchecked")
   122         @SuppressWarnings("unchecked")
   123         T slice(T a, int from, int to) {
   123         T slice(T a, int from, int to, boolean dupOtherwiseSlice) {
   124             return (T) a.position(from).limit(to).slice();
   124             a = (T) a.position(from).limit(to);
       
   125             return (T) (dupOtherwiseSlice ? a.duplicate() : a.slice());
   125         }
   126         }
   126 
   127 
   127         @SuppressWarnings("unchecked")
   128         @SuppressWarnings("unchecked")
   128         E get(T a, int i) {
   129         E get(T a, int i) {
   129             try {
   130             try {
   605     void testBufferType(BT bt,
   606     void testBufferType(BT bt,
   606                         BiFunction<BT, Integer, B> aConstructor,
   607                         BiFunction<BT, Integer, B> aConstructor,
   607                         BiFunction<BT, Integer, B> bConstructor) {
   608                         BiFunction<BT, Integer, B> bConstructor) {
   608         int n = arraySizeFor(bt.elementType);
   609         int n = arraySizeFor(bt.elementType);
   609 
   610 
   610         for (int s : ranges(0, n)) {
   611         for (boolean dupOtherwiseSlice : new boolean[]{ false, true }) {
   611             B a = aConstructor.apply(bt, s);
   612             for (int s : ranges(0, n)) {
   612             B b = bConstructor.apply(bt, s);
   613                 B a = aConstructor.apply(bt, s);
   613 
   614                 B b = bConstructor.apply(bt, s);
   614             for (int aFrom : ranges(0, s)) {
   615 
   615                 for (int aTo : ranges(aFrom, s)) {
   616                 for (int aFrom : ranges(0, s)) {
   616                     int aLength = aTo - aFrom;
   617                     for (int aTo : ranges(aFrom, s)) {
   617 
   618                         int aLength = aTo - aFrom;
   618                     B as = aLength != s
   619 
   619                            ? bt.slice(a, aFrom, aTo)
   620                         B as = aLength != s
   620                            : a;
   621                                ? bt.slice(a, aFrom, aTo, dupOtherwiseSlice)
   621 
   622                                : a;
   622                     for (int bFrom : ranges(0, s)) {
   623 
   623                         for (int bTo : ranges(bFrom, s)) {
   624                         for (int bFrom : ranges(0, s)) {
   624                             int bLength = bTo - bFrom;
   625                             for (int bTo : ranges(bFrom, s)) {
   625 
   626                                 int bLength = bTo - bFrom;
   626                             B bs = bLength != s
   627 
   627                                    ? bt.slice(b, bFrom, bTo)
   628                                 B bs = bLength != s
   628                                    : b;
   629                                        ? bt.slice(b, bFrom, bTo, dupOtherwiseSlice)
   629 
   630                                        : b;
   630                             boolean eq = bt.pairWiseEquals(as, bs);
   631 
   631                             Assert.assertEquals(bt.equals(as, bs), eq);
   632                                 boolean eq = bt.pairWiseEquals(as, bs);
   632                             Assert.assertEquals(bt.equals(bs, as), eq);
   633                                 Assert.assertEquals(bt.equals(as, bs), eq);
   633                             if (eq) {
   634                                 Assert.assertEquals(bt.equals(bs, as), eq);
   634                                 Assert.assertEquals(bt.compare(as, bs), 0);
   635                                 if (eq) {
   635                                 Assert.assertEquals(bt.compare(bs, as), 0);
   636                                     Assert.assertEquals(bt.compare(as, bs), 0);
       
   637                                     Assert.assertEquals(bt.compare(bs, as), 0);
       
   638                                 }
       
   639                                 else {
       
   640                                     int aCb = bt.compare(as, bs);
       
   641                                     int bCa = bt.compare(bs, as);
       
   642                                     int v = Integer.signum(aCb) * Integer.signum(bCa);
       
   643                                     Assert.assertTrue(v == -1);
       
   644                                 }
   636                             }
   645                             }
   637                             else {
   646                         }
   638                                 int aCb = bt.compare(as, bs);
   647 
   639                                 int bCa = bt.compare(bs, as);
   648                         if (aLength > 0 && !a.isReadOnly()) {
   640                                 int v = Integer.signum(aCb) * Integer.signum(bCa);
   649                             for (int i = aFrom; i < aTo; i++) {
       
   650                                 B c = aConstructor.apply(bt, a.capacity());
       
   651                                 B cs = aLength != s
       
   652                                        ? bt.slice(c, aFrom, aTo, dupOtherwiseSlice)
       
   653                                        : c;
       
   654 
       
   655                                 // Create common prefix with a length of i - aFrom
       
   656                                 bt.set(c, i, -1);
       
   657 
       
   658                                 Assert.assertFalse(bt.equals(c, a));
       
   659 
       
   660                                 int cCa = bt.compare(cs, as);
       
   661                                 int aCc = bt.compare(as, cs);
       
   662                                 int v = Integer.signum(cCa) * Integer.signum(aCc);
   641                                 Assert.assertTrue(v == -1);
   663                                 Assert.assertTrue(v == -1);
   642                             }
   664                             }
   643                         }
       
   644                     }
       
   645 
       
   646                     if (aLength > 0 && !a.isReadOnly()) {
       
   647                         for (int i = aFrom; i < aTo; i++) {
       
   648                             B c = aConstructor.apply(bt, a.capacity());
       
   649                             B cs = aLength != s
       
   650                                    ? bt.slice(c, aFrom, aTo)
       
   651                                    : c;
       
   652 
       
   653                             // Create common prefix with a length of i - aFrom
       
   654                             bt.set(c, i, -1);
       
   655 
       
   656                             Assert.assertFalse(bt.equals(c, a));
       
   657 
       
   658                             int cCa = bt.compare(cs, as);
       
   659                             int aCc = bt.compare(as, cs);
       
   660                             int v = Integer.signum(cCa) * Integer.signum(aCc);
       
   661                             Assert.assertTrue(v == -1);
       
   662                         }
   665                         }
   663                     }
   666                     }
   664                 }
   667                 }
   665             }
   668             }
   666         }
   669         }