jdk/test/sun/nio/cs/FindEncoderBugs.java
author sherman
Tue, 19 May 2009 16:03:02 -0700
changeset 2914 1375969fd02e
parent 2294 4259115772f7
child 2921 d9d491a5a169
permissions -rw-r--r--
6843079: Putback for the new EUC_TW is not complete Summary: Putback the files missed in last putback Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     1
/*
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     2
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     4
 *
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     8
 *
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    13
 * accompanied this code).
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    14
 *
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    18
 *
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    21
 * have any questions.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    22
 */
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    23
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    24
/*
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    25
 * @test
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    26
 * @bug 6233345 6381699 6381702 6381705 6381706
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    27
 * @summary Encode many char sequences in many ways
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    28
 * @run main/timeout=1200 FindEncoderBugs
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    29
 * @author Martin Buchholz
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    30
 */
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    31
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    32
import java.util.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    33
import java.util.regex.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    34
import java.nio.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    35
import java.nio.charset.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    36
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    37
public class FindEncoderBugs {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    38
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    39
    static boolean isBroken(String csn) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    40
        if (csn.equals("x-COMPOUND_TEXT")) return true;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    41
        if (csn.equals("x-IBM834")) return true;  // stateful korean
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    42
        if (csn.equals("x-IBM933")) return true;  // stateful korean
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    43
        if (csn.equals("x-IBM970")) return true;  // stateful korean
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    44
        if (csn.equals("x-IBM949")) return true;  // stateful korean
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    45
        if (csn.equals("x-IBM949C")) return true; // stateful korean
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    46
        return false;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    47
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    48
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    49
    static <T extends Comparable<? super T>> List<T> sort(Collection<T> c) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    50
        List<T> list = new ArrayList<T>(c);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    51
        Collections.sort(list);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    52
        return list;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    53
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    54
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    55
    static class TooManyFailures extends RuntimeException {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    56
        private static final long serialVersionUID = 0L;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    57
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    58
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    59
    static String string(byte[] a) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    60
        final StringBuilder sb = new StringBuilder();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    61
        for (byte b : a) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    62
            if (sb.length() != 0) sb.append(' ');
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    63
            sb.append(String.format("%02x", b & 0xff));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    64
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    65
        return sb.toString();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    66
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    67
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    68
    static String string(char[] a) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    69
        final StringBuilder sb = new StringBuilder();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    70
        for (char c : a) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    71
            if (sb.length() != 0) sb.append(' ');
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    72
            sb.append(String.format("\\u%04x", (int) c));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    73
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    74
        return sb.toString();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    75
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    76
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    77
    static class Reporter {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    78
        // Some machinery to make sure only a small number of errors
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    79
        // that are "too similar" are reported.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    80
        static class Counts extends HashMap<String, Long> {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    81
            private static final long serialVersionUID = -1;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    82
            long inc(String signature) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    83
                Long count = get(signature);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    84
                if (count == null) count = 0L;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    85
                put(signature, count+1);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    86
                return count+1;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    87
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    88
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    89
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    90
        final Counts failureCounts = new Counts();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    91
        final static long maxFailures = 2;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    92
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    93
        final static Pattern hideBytes = Pattern.compile("\"[0-9a-f ]+\"");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    94
        final static Pattern hideChars = Pattern.compile("\\\\u[0-9a-f]{4}");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    95
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    96
        boolean bug(String format, Object... args) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    97
            String signature = String.format(format, args);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    98
            //      signature = hideBytes.matcher(signature).replaceAll("\"??\"");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    99
            //      signature = hideChars.matcher(signature).replaceAll("\\u????");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   100
            failed++;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   101
            if (failureCounts.inc(signature) <= maxFailures) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   102
                System.out.printf(format, args);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   103
                System.out.println();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   104
                return true;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   105
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   106
            return false;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   107
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   108
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   109
        void summarize() {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   110
            for (String key : sort(failureCounts.keySet()))
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   111
                System.out.printf("-----%n%s%nfailures=%d%n",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   112
                                  key, failureCounts.get(key));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   113
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   114
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   115
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   116
    static final Reporter reporter = new Reporter();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   117
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   118
    static class Result {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   119
        final int limit;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   120
        final int ipos;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   121
        final boolean direct;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   122
        final char[] ia;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   123
        final byte[] oa;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   124
        final CoderResult cr;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   125
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   126
        private static byte[] toByteArray(ByteBuffer bb) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   127
            byte[] bytes = new byte[bb.position()];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   128
            for (int i = 0; i < bytes.length; i++)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   129
                bytes[i] = bb.get(i);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   130
            return bytes;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   131
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   132
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   133
        Result(CharBuffer ib, ByteBuffer ob, CoderResult cr) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   134
            ipos = ib.position();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   135
            ia = toArray(ib);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   136
            oa = toArray(ob);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   137
            direct = ib.isDirect();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   138
            limit = ob.limit();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   139
            this.cr = cr;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   140
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   141
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   142
        static char[] toArray(CharBuffer b) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   143
            int pos = b.position();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   144
            char[] a = new char[b.limit()];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   145
            b.position(0);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   146
            b.get(a);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   147
            b.position(pos);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   148
            return a;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   149
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   150
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   151
        static byte[] toArray(ByteBuffer b) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   152
            byte[] a = new byte[b.position()];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   153
            b.position(0);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   154
            b.get(a);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   155
            return a;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   156
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   157
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   158
        static boolean eq(Result x, Result y) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   159
            return x == y ||
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   160
                (x != null && y != null &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   161
                 (Arrays.equals(x.oa, y.oa) &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   162
                  x.ipos == y.ipos &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   163
                  x.cr == y.cr));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   164
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   165
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   166
        public String toString() {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   167
            return String.format("\"%s\"[%d/%d] => %s \"%s\"[%d/%d]%s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   168
                                 string(ia), ipos, ia.length,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   169
                                 cr, string(oa), oa.length, limit,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   170
                                 (direct ? " (direct)" : ""));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   171
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   172
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   173
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   174
    static class CharsetTester {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   175
        private final Charset cs;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   176
        private final boolean hasBom;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   177
        private static final int maxFailures = 5;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   178
        private int failures = 0;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   179
        // private static final long maxCharsetFailures = Long.MAX_VALUE;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   180
        private static final long maxCharsetFailures = 10000L;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   181
        private final long failed0 = failed;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   182
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   183
        // legend: r=regular d=direct In=Input Ou=Output
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   184
        static final int maxBufSize = 20;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   185
        static final CharBuffer[] rInBuffers = new CharBuffer[maxBufSize];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   186
        static final CharBuffer[] dInBuffers = new CharBuffer[maxBufSize];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   187
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   188
        static final ByteBuffer[] rOuBuffers = new ByteBuffer[maxBufSize];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   189
        static final ByteBuffer[] dOuBuffers = new ByteBuffer[maxBufSize];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   190
        static {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   191
            for (int i = 0; i < maxBufSize; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   192
                rInBuffers[i] = CharBuffer.allocate(i);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   193
                dInBuffers[i] = ByteBuffer.allocateDirect(i*2).asCharBuffer();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   194
                rOuBuffers[i] = ByteBuffer.allocate(i);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   195
                dOuBuffers[i] = ByteBuffer.allocateDirect(i);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   196
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   197
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   198
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   199
        CharsetTester(Charset cs) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   200
            this.cs = cs;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   201
            this.hasBom =
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   202
                cs.name().matches(".*BOM.*") ||
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   203
                cs.name().equals("UTF-16");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   204
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   205
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   206
        static boolean bug(String format, Object... args) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   207
            return reporter.bug(format, args);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   208
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   209
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   210
        static boolean hasBom(byte[] a) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   211
            switch (a.length) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   212
            case 2: case 4:
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   213
                int sum = 0;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   214
                for (byte x : a)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   215
                    sum += x;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   216
                return sum == (byte) 0xfe + (byte) 0xff;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   217
            default: return false;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   218
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   219
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   220
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   221
        void testSurrogates() {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   222
            int failures = 0;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   223
            for (int i = 0; i < 10; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   224
                Result r = test(new char[] { randomHighSurrogate() });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   225
                if (r == null) break;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   226
                if (! (r.cr.isUnderflow() &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   227
                       r.ipos == 0))
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   228
                    bug("Lone high surrogate not UNDERFLOW: %s %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   229
                        cs, r);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   230
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   231
            for (int i = 0; i < 10; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   232
                Result r = test(new char[] { randomLowSurrogate() });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   233
                if (r == null) break;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   234
                if (! (r.cr.isMalformed() && r.cr.length() == 1))
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   235
                    bug("Lone low surrogate not MALFORMED[1]: %s %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   236
                        cs, r);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   237
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   238
            char[] chars = new char[2];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   239
            for (int i = 0; i < 10; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   240
                chars[0] = randomLowSurrogate(); // Always illegal
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   241
                chars[1] = randomChar();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   242
                Result r = test(chars);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   243
                if (r == null) break;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   244
                if (! (r.cr.isMalformed() &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   245
                       r.cr.length() == 1 &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   246
                       (r.ipos == 0 || (hasBom && hasBom(r.oa))))) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   247
                    if (failures++ > 5) return;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   248
                    bug("Unpaired low surrogate not MALFORMED[1]: %s %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   249
                        cs, r);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   250
                }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   251
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   252
            for (int i = 0; i < 10; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   253
                chars[0] = randomHighSurrogate();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   254
                do {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   255
                    chars[1] = randomChar();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   256
                } while (Character.isLowSurrogate(chars[1]));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   257
                Result r = test(chars);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   258
                if (r == null) break;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   259
                if (! (r.cr.isMalformed() &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   260
                       r.cr.length() == 1 &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   261
                       (r.ipos == 0 || (hasBom && hasBom(r.oa))))) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   262
                    if (failures++ > 5) return;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   263
                    bug("Unpaired high surrogate not MALFORMED[1]: %s %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   264
                        cs, r);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   265
                }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   266
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   267
            for (int i = 0; i < 1000; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   268
                chars[0] = randomHighSurrogate();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   269
                chars[1] = randomLowSurrogate();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   270
                Result r = test(chars);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   271
                if (r == null) break;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   272
                if (! ((r.cr.isUnmappable() &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   273
                        r.cr.length() == 2 &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   274
                        r.oa.length == 0)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   275
                       ||
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   276
                       (r.cr.isUnderflow() &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   277
                        r.oa.length > 0 &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   278
                        r.ipos == 2))) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   279
                    if (failures++ > 5) return;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   280
                    bug("Legal supplementary character bug: %s %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   281
                        cs, r);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   282
                }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   283
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   284
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   285
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   286
//              if (! (r.cr.isMalformed() &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   287
//                     r.cr.length() == 1 &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   288
//                     (rob.position() == 0 || hasBom(rob)))) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   289
//                  if (failures++ > 5) return;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   290
//                  bug("Unpaired surrogate not malformed: %s %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   291
//                               cs, r);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   292
//              }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   293
//          }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   294
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   295
//                  dib.clear(); dib.put(chars); dib.flip();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   296
//                  rib.position(0);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   297
//                  rob.clear(); rob.limit(lim);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   298
//                  for (CharBuffer ib : new CharBuffer[] { rib, dib }) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   299
//                      Result r = recode(ib, rob);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   300
//                      if (! (r.cr.isMalformed() &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   301
//                             r.cr.length() == 1 &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   302
//                             (rob.position() == 0 || hasBom(rob)))) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   303
//                          if (failures++ > 5) return;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   304
//                          bug("Unpaired surrogate not malformed: %s %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   305
//                                       cs, r);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   306
//                      }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   307
//                  }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   308
//                  //}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   309
//              for (int i = 0; i < 10000; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   310
//                  chars[0] = randomHighSurrogate();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   311
//                  chars[1] = randomLowSurrogate();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   312
//                  dib.clear(); dib.put(chars); dib.flip();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   313
//                  rib.position(0);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   314
//                  rob.clear(); rob.limit(lim);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   315
//                  for (CharBuffer ib : new CharBuffer[] { rib, dib }) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   316
//                      Result r = recode(ib, rob);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   317
//                      if (! ((r.cr.isUnmappable() &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   318
//                              r.cr.length() == 2 &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   319
//                              rob.position() == 0)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   320
//                             ||
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   321
//                             (r.cr.isUnderflow() &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   322
//                              rob.position() > 0 &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   323
//                              ib.position() == 2))) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   324
//                          if (failures++ > 5) return;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   325
//                          bug("Legal supplementary character bug: %s %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   326
//                                       cs, r);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   327
//                      }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   328
//                  }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   329
//              }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   330
//          }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   331
//      }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   332
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   333
        Result recode(CharBuffer ib, ByteBuffer ob) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   334
            try {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   335
                byte canary = 22;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   336
                ib.clear();     // Prepare to read
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   337
                ob.clear();     // Prepare to write
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   338
                for (int i = 0; i < ob.limit(); i++)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   339
                    ob.put(i, canary);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   340
                CharsetEncoder coder = cs.newEncoder();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   341
                CoderResult cr = coder.encode(ib, ob, false);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   342
                equal(ib.limit(), ib.capacity());
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   343
                equal(ob.limit(), ob.capacity());
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   344
                Result r = new Result(ib, ob, cr);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   345
                if (cr.isError())
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   346
                    check(cr.length() > 0);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   347
                if (cr.isOverflow() && ob.remaining() > 10)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   348
                    bug("OVERFLOW, but there's lots of room: %s %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   349
                        cs, r);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   350
//              if (cr.isOverflow() && ib.remaining() == 0 && ! hasBom)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   351
//                  bug("OVERFLOW, yet remaining() == 0: %s %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   352
//                      cs, r);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   353
                if (cr.isError() && ib.remaining() < cr.length())
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   354
                    bug("remaining() < CoderResult.length(): %s %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   355
                        cs, r);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   356
//              if (ib.position() == 0
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   357
//                  && ob.position() > 0
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   358
//                  && ! hasBom(r.oa))
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   359
//                  bug("output only if input consumed: %s %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   360
//                       cs, r);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   361
                CoderResult cr2 = coder.encode(ib, ob, false);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   362
                if (ib.position() != r.ipos ||
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   363
                    ob.position() != r.oa.length ||
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   364
                    cr != cr2)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   365
                    bug("Coding operation not idempotent: %s%n    %s%n    %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   366
                        cs, r, new Result(ib, ob, cr2));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   367
                if (ob.position() < ob.limit() &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   368
                    ob.get(ob.position()) != canary)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   369
                    bug("Buffer overrun: %s %s %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   370
                        cs, r, ob.get(ob.position()));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   371
                return r;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   372
            } catch (Throwable t) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   373
                if (bug("Unexpected exception: %s %s %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   374
                        cs, t.getClass().getSimpleName(),
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   375
                        new Result(ib, ob, null)))
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   376
                    t.printStackTrace();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   377
                return null;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   378
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   379
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   380
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   381
        Result recode2(char[] ia, int n) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   382
            int len = ia.length;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   383
            CharBuffer rib = CharBuffer.wrap(ia);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   384
            CharBuffer dib = dInBuffers[len];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   385
            dib.clear(); dib.put(ia); dib.clear();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   386
            ByteBuffer rob = rOuBuffers[n];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   387
            ByteBuffer dob = dOuBuffers[n];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   388
            equal(rob.limit(), n);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   389
            equal(dob.limit(), n);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   390
            check(dib.isDirect());
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   391
            check(dob.isDirect());
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   392
            Result r1 = recode(rib, rob);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   393
            Result r2 = recode(dib, dob);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   394
            if (r1 != null && r2 != null && ! Result.eq(r1, r2))
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   395
                bug("Results differ for direct buffers: %s%n    %s%n    %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   396
                    cs, r1, r2);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   397
            return r1;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   398
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   399
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   400
        Result test(char[] ia) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   401
            if (failed - failed0 >= maxCharsetFailures)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   402
                throw new TooManyFailures();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   403
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   404
            Result roomy = recode2(ia, maxBufSize - 1);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   405
            if (roomy == null) return roomy;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   406
            int olen = roomy.oa.length;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   407
            if (olen > 0) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   408
                if (roomy.ipos == roomy.ia.length) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   409
                    Result perfectFit = recode2(ia, olen);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   410
                    if (! Result.eq(roomy, perfectFit))
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   411
                        bug("Results differ: %s%n    %s%n    %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   412
                            cs, roomy, perfectFit);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   413
                }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   414
                for (int i = 0; i < olen; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   415
                    Result claustrophobic = recode2(ia, i);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   416
                    if (claustrophobic == null) return roomy;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   417
                    if (roomy.cr.isUnderflow() &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   418
                        ! claustrophobic.cr.isOverflow())
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   419
                        bug("Expected OVERFLOW: %s%n    %s%n    %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   420
                            cs, roomy, claustrophobic);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   421
                }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   422
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   423
            return roomy;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   424
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   425
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   426
        void testExhaustively(char[] prefix, int n) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   427
            int len = prefix.length;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   428
            char[] ia = Arrays.copyOf(prefix, len + 1);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   429
            for (int i = 0; i < 0x10000; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   430
                ia[len] = (char) i;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   431
                if (n == 1)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   432
                    test(ia);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   433
                else
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   434
                    testExhaustively(ia, n - 1);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   435
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   436
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   437
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   438
        void testRandomly(char[] prefix, int n) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   439
            int len = prefix.length;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   440
            char[] ia = Arrays.copyOf(prefix, len + n);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   441
            for (int i = 0; i < 10000; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   442
                for (int j = 0; j < n; j++)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   443
                    ia[len + j] = randomChar();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   444
                test(ia);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   445
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   446
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   447
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   448
        void testPrefix(char[] prefix) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   449
            if (prefix.length > 0)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   450
                System.out.printf("Testing prefix %s%n", string(prefix));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   451
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   452
            test(prefix);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   453
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   454
            testExhaustively(prefix, 1);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   455
            // Can you spare a year of CPU time?
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   456
            //testExhaustively(prefix, 2);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   457
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   458
            testRandomly(prefix, 2);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   459
            testRandomly(prefix, 3);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   460
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   461
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   462
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   463
    private final static Random rnd = new Random();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   464
    private static char randomChar() {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   465
        return (char) rnd.nextInt(Character.MAX_VALUE);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   466
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   467
    private static char randomHighSurrogate() {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   468
        return (char) (Character.MIN_HIGH_SURROGATE + rnd.nextInt(1024));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   469
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   470
    private static char randomLowSurrogate() {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   471
        return (char) (Character.MIN_LOW_SURROGATE + rnd.nextInt(1024));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   472
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   473
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   474
    private static void testCharset(Charset cs) throws Throwable {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   475
        if (! cs.canEncode())
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   476
            return;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   477
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   478
        final String csn = cs.name();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   479
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   480
        if (isBroken(csn)) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   481
            System.out.printf("Skipping possibly broken charset %s%n", csn);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   482
            return;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   483
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   484
        System.out.println(csn);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   485
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   486
        CharsetTester tester = new CharsetTester(cs);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   487
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   488
        tester.testSurrogates();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   489
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   490
        tester.testPrefix(new char[] {});
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   491
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   492
        if (csn.equals("x-ISCII91")) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   493
            System.out.println("More ISCII testing...");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   494
            new CharsetTester(cs).testPrefix(new char[]{'\u094d'}); // Halant
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   495
            new CharsetTester(cs).testPrefix(new char[]{'\u093c'}); // Nukta
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   496
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   497
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   498
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   499
    private static void realMain(String[] args) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   500
        for (Charset cs : sort(Charset.availableCharsets().values())) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   501
            try {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   502
                testCharset(cs);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   503
            } catch (TooManyFailures e) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   504
                System.out.printf("Too many failures for %s%n", cs);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   505
            } catch (Throwable t) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   506
                unexpected(t);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   507
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   508
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   509
        reporter.summarize();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   510
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   511
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   512
    //--------------------- Infrastructure ---------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   513
    static volatile long passed = 0, failed = 0;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   514
    static void pass() {passed++;}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   515
    static void fail() {failed++; Thread.dumpStack();}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   516
    static void fail(String format, Object... args) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   517
        System.out.println(String.format(format, args)); failed++;}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   518
    static void fail(String msg) {System.out.println(msg); fail();}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   519
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   520
    static void check(boolean cond) {if (cond) pass(); else fail();}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   521
    static void equal(Object x, Object y) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   522
        if (x == null ? y == null : x.equals(y)) pass();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   523
        else fail(x + " not equal to " + y);}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   524
    public static void main(String[] args) throws Throwable {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   525
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   526
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   527
        if (failed > 0) throw new AssertionError("Some tests failed");}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   528
}