jdk/test/java/util/Spliterator/SpliteratorTraversingAndSplittingTest.java
changeset 28667 2245cc40bf5d
parent 20183 584504d38d79
child 34347 4a17f9e90a0f
equal deleted inserted replaced
28666:49cdfa0ea390 28667:2245cc40bf5d
     1 /*
     1 /*
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2013, 2015, 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.
    23 
    23 
    24 /**
    24 /**
    25  * @test
    25  * @test
    26  * @summary Spliterator traversing and splitting tests
    26  * @summary Spliterator traversing and splitting tests
    27  * @run testng SpliteratorTraversingAndSplittingTest
    27  * @run testng SpliteratorTraversingAndSplittingTest
    28  * @bug 8020016
    28  * @bug 8020016 8071477
    29  */
    29  */
    30 
    30 
    31 import org.testng.annotations.DataProvider;
    31 import org.testng.annotations.DataProvider;
    32 import org.testng.annotations.Test;
    32 import org.testng.annotations.Test;
    33 
    33 
    83 import static org.testng.Assert.assertEquals;
    83 import static org.testng.Assert.assertEquals;
    84 
    84 
    85 @Test
    85 @Test
    86 public class SpliteratorTraversingAndSplittingTest {
    86 public class SpliteratorTraversingAndSplittingTest {
    87 
    87 
    88     private static List<Integer> SIZES = Arrays.asList(0, 1, 10, 100, 1000);
    88     private static final List<Integer> SIZES = Arrays.asList(0, 1, 10, 100, 1000);
       
    89 
       
    90     private static final String LOW = new String(new char[] {Character.MIN_LOW_SURROGATE});
       
    91     private static final String HIGH = new String(new char[] {Character.MIN_HIGH_SURROGATE});
       
    92     private static final String HIGH_LOW = HIGH + LOW;
       
    93     private static final String CHAR_HIGH_LOW = "A" + HIGH_LOW;
       
    94     private static final String HIGH_LOW_CHAR = HIGH_LOW + "A";
       
    95     private static final String CHAR_HIGH_LOW_CHAR = "A" + HIGH_LOW + "A";
       
    96 
       
    97     private static final List<String> STRINGS = generateTestStrings();
       
    98 
       
    99     private static List<String> generateTestStrings() {
       
   100         List<String> strings = new ArrayList<>();
       
   101         for (int n : Arrays.asList(1, 2, 3, 16, 17)) {
       
   102             strings.add(generate("A", n));
       
   103             strings.add(generate(LOW, n));
       
   104             strings.add(generate(HIGH, n));
       
   105             strings.add(generate(HIGH_LOW, n));
       
   106             strings.add(generate(CHAR_HIGH_LOW, n));
       
   107             strings.add(generate(HIGH_LOW_CHAR, n));
       
   108             strings.add(generate(CHAR_HIGH_LOW_CHAR, n));
       
   109         }
       
   110         return strings;
       
   111     }
       
   112 
       
   113     private static String generate(String s, int n) {
       
   114         StringBuilder sb = new StringBuilder();
       
   115         for (int i = 0; i < n; i++) {
       
   116             sb.append(s);
       
   117         }
       
   118         return sb.toString();
       
   119     }
    89 
   120 
    90     private static class SpliteratorDataBuilder<T> {
   121     private static class SpliteratorDataBuilder<T> {
    91         List<Object[]> data;
   122         List<Object[]> data;
    92 
   123 
    93         List<T> exp;
   124         List<T> exp;
   562                     append("size=").append(exp.size()).
   593                     append("size=").append(exp.size()).
   563                     append("}");
   594                     append("}");
   564         }
   595         }
   565     }
   596     }
   566 
   597 
       
   598     private static class SpliteratorOfIntCharDataBuilder {
       
   599         List<Object[]> data;
       
   600 
       
   601         String s;
       
   602 
       
   603         List<Integer> expChars;
       
   604 
       
   605         List<Integer> expCodePoints;
       
   606 
       
   607         SpliteratorOfIntCharDataBuilder(List<Object[]> data, String s) {
       
   608             this.data = data;
       
   609             this.s = s;
       
   610             this.expChars = transform(s, false);
       
   611             this.expCodePoints = transform(s, true);
       
   612         }
       
   613 
       
   614         static List<Integer> transform(String s, boolean toCodePoints) {
       
   615             List<Integer> l = new ArrayList<>();
       
   616 
       
   617             if (!toCodePoints) {
       
   618                 for (int i = 0; i < s.length(); i++) {
       
   619                     l.add((int) s.charAt(i));
       
   620                 }
       
   621             }
       
   622             else {
       
   623                 for (int i = 0; i < s.length();) {
       
   624                     char c1 = s.charAt(i++);
       
   625                     int cp = c1;
       
   626                     if (Character.isHighSurrogate(c1) && i < s.length()) {
       
   627                         char c2 = s.charAt(i);
       
   628                         if (Character.isLowSurrogate(c2)) {
       
   629                             i++;
       
   630                             cp = Character.toCodePoint(c1, c2);
       
   631                         }
       
   632                     }
       
   633                     l.add(cp);
       
   634                 }
       
   635             }
       
   636             return l;
       
   637         }
       
   638 
       
   639         void add(String description, Function<String, CharSequence> f) {
       
   640             description = description.replace("%s", s);
       
   641             {
       
   642                 Supplier<Spliterator.OfInt> supplier = () -> f.apply(s).chars().spliterator();
       
   643                 data.add(new Object[]{description + ".chars().spliterator()", expChars, supplier});
       
   644             }
       
   645             {
       
   646                 Supplier<Spliterator.OfInt> supplier = () -> f.apply(s).codePoints().spliterator();
       
   647                 data.add(new Object[]{description + ".codePoints().spliterator()", expCodePoints, supplier});
       
   648             }
       
   649         }
       
   650     }
       
   651 
   567     static Object[][] spliteratorOfIntDataProvider;
   652     static Object[][] spliteratorOfIntDataProvider;
   568 
   653 
   569     @DataProvider(name = "Spliterator.OfInt")
   654     @DataProvider(name = "Spliterator.OfInt")
   570     public static Object[][] spliteratorOfIntDataProvider() {
   655     public static Object[][] spliteratorOfIntDataProvider() {
   571         if (spliteratorOfIntDataProvider != null) {
   656         if (spliteratorOfIntDataProvider != null) {
   613             }
   698             }
   614             db.add("new Spliterators.AbstractIntAdvancingSpliterator()",
   699             db.add("new Spliterators.AbstractIntAdvancingSpliterator()",
   615                    () -> new IntSpliteratorFromArray(exp));
   700                    () -> new IntSpliteratorFromArray(exp));
   616         }
   701         }
   617 
   702 
       
   703         // Class for testing default methods
       
   704         class CharSequenceImpl implements CharSequence {
       
   705             final String s;
       
   706 
       
   707             public CharSequenceImpl(String s) {
       
   708                 this.s = s;
       
   709             }
       
   710 
       
   711             @Override
       
   712             public int length() {
       
   713                 return s.length();
       
   714             }
       
   715 
       
   716             @Override
       
   717             public char charAt(int index) {
       
   718                 return s.charAt(index);
       
   719             }
       
   720 
       
   721             @Override
       
   722             public CharSequence subSequence(int start, int end) {
       
   723                 return s.subSequence(start, end);
       
   724             }
       
   725 
       
   726             @Override
       
   727             public String toString() {
       
   728                 return s;
       
   729             }
       
   730         }
       
   731 
       
   732         for (String string : STRINGS) {
       
   733             SpliteratorOfIntCharDataBuilder cdb = new SpliteratorOfIntCharDataBuilder(data, string);
       
   734             cdb.add("\"%s\"", s -> s);
       
   735             cdb.add("new CharSequenceImpl(\"%s\")", CharSequenceImpl::new);
       
   736             cdb.add("new StringBuilder(\"%s\")", StringBuilder::new);
       
   737             cdb.add("new StringBuffer(\"%s\")", StringBuffer::new);
       
   738         }
       
   739 
   618         return spliteratorOfIntDataProvider = data.toArray(new Object[0][]);
   740         return spliteratorOfIntDataProvider = data.toArray(new Object[0][]);
   619     }
   741     }
   620 
   742 
   621     private static int[] arrayIntRange(int upTo) {
   743     private static int[] arrayIntRange(int upTo) {
   622         int[] exp = new int[upTo];
   744         int[] exp = new int[upTo];