jdk/test/sun/nio/cs/FindDecoderBugs.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
child 5962 0913689fd3a0
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 796
diff changeset
     2
 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
796
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 796
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 796
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 796
diff changeset
    21
 * questions.
796
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 6380723
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    27
 * @summary Decode many byte sequences in many ways
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    28
 * @run main/timeout=1800 FindDecoderBugs
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 FindDecoderBugs {
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
        return false;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    42
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    43
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    44
    static <T extends Comparable<? super T>> List<T> sort(Collection<T> c) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    45
        List<T> list = new ArrayList<T>(c);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    46
        Collections.sort(list);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    47
        return list;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    48
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    49
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    50
    static class TooManyFailures extends RuntimeException {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    51
        private static final long serialVersionUID = 0L;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    52
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    53
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    54
    static String string(byte[] a) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    55
        final StringBuilder sb = new StringBuilder();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    56
        for (byte b : a) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    57
            if (sb.length() != 0) sb.append(' ');
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    58
            sb.append(String.format("%02x", b & 0xff));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    59
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    60
        return sb.toString();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    61
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    62
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    63
    static String string(char[] a) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    64
        final StringBuilder sb = new StringBuilder();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    65
        for (char c : a) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    66
            if (sb.length() != 0) sb.append(' ');
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    67
            sb.append(String.format("\\u%04x", (int) c));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    68
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    69
        return sb.toString();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    70
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    71
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    72
    static class Reporter {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    73
        // Some machinery to make sure only a small number of errors
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    74
        // that are "too similar" are reported.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    75
        static class Counts extends HashMap<String, Long> {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    76
            private static final long serialVersionUID = -1;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    77
            long inc(String signature) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    78
                Long count = get(signature);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    79
                if (count == null) count = 0L;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    80
                put(signature, count+1);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    81
                return count+1;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    82
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    83
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    84
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    85
        final Counts failureCounts = new Counts();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    86
        final static long maxFailures = 2;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    87
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    88
        final static Pattern hideBytes = Pattern.compile("\"[0-9a-f ]+\"");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    89
        final static Pattern hideChars = Pattern.compile("\\\\u[0-9a-f]{4}");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    90
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    91
        boolean bug(String format, Object... args) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    92
            String signature = String.format(format, args);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    93
            signature = hideBytes.matcher(signature).replaceAll("\"??\"");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    94
            signature = hideChars.matcher(signature).replaceAll("\\u????");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    95
            failed++;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    96
            if (failureCounts.inc(signature) <= maxFailures) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    97
                System.out.printf(format, args);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    98
                System.out.println();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    99
                return true;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   100
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   101
            return false;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   102
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   103
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   104
        void summarize() {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   105
            for (String key : sort(failureCounts.keySet()))
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   106
                System.out.printf("-----%n%s%nfailures=%d%n",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   107
                                  key, failureCounts.get(key));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   108
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   109
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   110
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   111
    static final Reporter reporter = new Reporter();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   112
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   113
    static class Result {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   114
        final int limit;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   115
        final int ipos;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   116
        final boolean direct;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   117
        final byte[] ia;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   118
        final char[] oa;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   119
        final CoderResult cr;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   120
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   121
        Result(ByteBuffer ib, CharBuffer ob, CoderResult cr) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   122
            ipos = ib.position();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   123
            ia = toArray(ib);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   124
            oa = toArray(ob);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   125
            direct = ib.isDirect();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   126
            limit = ob.limit();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   127
            this.cr = cr;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   128
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   129
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   130
        static byte[] toArray(ByteBuffer b) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   131
            int pos = b.position();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   132
            byte[] a = new byte[b.limit()];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   133
            b.position(0);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   134
            b.get(a);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   135
            b.position(pos);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   136
            return a;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   137
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   138
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   139
        static char[] toArray(CharBuffer b) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   140
            char[] a = new char[b.position()];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   141
            b.position(0);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   142
            b.get(a);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   143
            return a;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   144
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   145
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   146
        static boolean eq(Result x, Result y) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   147
            return x == y ||
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   148
                (x != null && y != null &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   149
                 (Arrays.equals(x.oa, y.oa) &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   150
                  x.ipos == y.ipos &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   151
                  x.cr == y.cr));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   152
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   153
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   154
        public String toString() {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   155
            return String.format("\"%s\"[%d/%d] => %s \"%s\"[%d/%d]%s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   156
                                 string(ia), ipos, ia.length,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   157
                                 cr, string(oa), oa.length, limit,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   158
                                 (direct ? " (direct)" : ""));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   159
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   160
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   161
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   162
    // legend: r=regular d=direct In=Input Ou=Output
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   163
    static final int maxBufSize = 20;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   164
    static final ByteBuffer[] ribs = new ByteBuffer[maxBufSize];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   165
    static final ByteBuffer[] dibs = new ByteBuffer[maxBufSize];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   166
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   167
    static final CharBuffer[] robs = new CharBuffer[maxBufSize];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   168
    static final CharBuffer[] dobs = new CharBuffer[maxBufSize];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   169
    static {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   170
        for (int i = 0; i < maxBufSize; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   171
            ribs[i] = ByteBuffer.allocate(i);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   172
            dibs[i] = ByteBuffer.allocateDirect(i);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   173
            robs[i] = CharBuffer.allocate(i);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   174
            dobs[i] = ByteBuffer.allocateDirect(i*2).asCharBuffer();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   175
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   176
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   177
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   178
    static class CharsetTester {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   179
        private final Charset cs;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   180
        private static final long maxFailures = 5;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   181
        private long failures = 0;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   182
        // private static final long maxCharsetFailures = Long.MAX_VALUE;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   183
        private static final long maxCharsetFailures = 10000L;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   184
        private final long failed0 = failed;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   185
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   186
        CharsetTester(Charset cs) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   187
            this.cs = cs;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   188
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   189
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   190
        static boolean bug(String format, Object... args) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   191
            return reporter.bug(format, args);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   192
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   193
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   194
        Result recode(ByteBuffer ib, CharBuffer ob) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   195
            try {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   196
                char canary = '\u4242';
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   197
                ib.clear();     // Prepare to read
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   198
                ob.clear();     // Prepare to write
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   199
                for (int i = 0; i < ob.limit(); i++)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   200
                    ob.put(i, canary);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   201
                CharsetDecoder coder = cs.newDecoder();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   202
                CoderResult cr = coder.decode(ib, ob, false);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   203
                equal(ib.limit(), ib.capacity());
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   204
                equal(ob.limit(), ob.capacity());
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   205
                Result r = new Result(ib, ob, cr);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   206
                if (cr.isError())
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   207
                    check(cr.length() > 0);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   208
                if (cr.isOverflow() && ob.remaining() > 10)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   209
                    bug("OVERFLOW, but there's lots of room: %s %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   210
                        cs, r);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   211
//              if (cr.isOverflow() && ib.remaining() == 0)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   212
//                  bug("OVERFLOW, yet remaining() == 0: %s %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   213
//                      cs, r);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   214
                if (cr.isError() && ib.remaining() < cr.length())
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   215
                    bug("remaining() < CoderResult.length(): %s %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   216
                        cs, r);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   217
//              if (ib.position() == 0 && ob.position() > 0)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   218
//                  reporter. bug("output only if input consumed: %s %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   219
//                                cs, r);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   220
                // Should we warn if cr.isUnmappable() ??
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   221
                CoderResult cr2 = coder.decode(ib, ob, false);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   222
                if (ib.position() != r.ipos ||
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   223
                    ob.position() != r.oa.length ||
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   224
                    cr != cr2)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   225
                    bug("Coding operation not idempotent: %s%n    %s%n    %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   226
                        cs, r, new Result(ib, ob, cr2));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   227
                if (ob.position() < ob.limit() &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   228
                    ob.get(ob.position()) != canary)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   229
                    bug("Buffer overrun: %s %s %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   230
                        cs, r, ob.get(ob.position()));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   231
                return r;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   232
            } catch (Throwable t) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   233
                if (bug("Unexpected exception: %s %s %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   234
                        cs, t.getClass().getSimpleName(),
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   235
                        new Result(ib, ob, null)))
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   236
                    t.printStackTrace();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   237
                return null;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   238
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   239
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   240
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   241
        Result recode2(byte[] ia, int n) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   242
            int len = ia.length;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   243
            ByteBuffer rib = ByteBuffer.wrap(ia);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   244
            ByteBuffer dib = dibs[len];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   245
            dib.clear(); dib.put(ia); dib.clear();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   246
            CharBuffer rob = robs[n];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   247
            CharBuffer dob = dobs[n];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   248
            equal(rob.limit(), n);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   249
            equal(dob.limit(), n);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   250
            check(dib.isDirect());
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   251
            check(dob.isDirect());
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   252
            Result r1 = recode(rib, rob);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   253
            Result r2 = recode(dib, dob);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   254
            if (r1 != null && r2 != null && ! Result.eq(r1, r2))
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   255
                bug("Results differ for direct buffers: %s%n    %s%n    %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   256
                    cs, r1, r2);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   257
            return r1;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   258
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   259
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   260
        Result test(byte[] ia) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   261
            if (failed - failed0 >= maxCharsetFailures)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   262
                throw new TooManyFailures();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   263
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   264
            Result roomy = recode2(ia, maxBufSize - 1);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   265
            if (roomy == null) return roomy;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   266
            int olen = roomy.oa.length;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   267
            if (olen > 0) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   268
                if (roomy.ipos == roomy.ia.length) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   269
                    Result perfectFit = recode2(ia, olen);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   270
                    if (! Result.eq(roomy, perfectFit))
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   271
                        bug("Results differ: %s%n    %s%n    %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   272
                            cs, roomy, perfectFit);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   273
                }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   274
                for (int i = 0; i < olen; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   275
                    Result claustrophobic = recode2(ia, i);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   276
                    if (claustrophobic == null) return roomy;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   277
                    if (roomy.cr.isUnderflow() &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   278
                        ! claustrophobic.cr.isOverflow())
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   279
                        bug("Expected OVERFLOW: %s%n    %s%n    %s",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   280
                            cs, roomy, claustrophobic);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   281
                }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   282
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   283
            return roomy;
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
        void testExhaustively(byte[] prefix, int n) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   287
            int len = prefix.length;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   288
            byte[] ia = Arrays.copyOf(prefix, len + 1);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   289
            for (int i = 0; i < 0x100; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   290
                ia[len] = (byte) i;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   291
                if (n == 1)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   292
                    test(ia);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   293
                else
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   294
                    testExhaustively(ia, n - 1);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   295
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   296
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   297
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   298
        void testRandomly(byte[] prefix, int n) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   299
            int len = prefix.length;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   300
            byte[] ia = Arrays.copyOf(prefix, len + n);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   301
            for (int i = 0; i < 10000; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   302
                for (int j = 0; j < n; j++)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   303
                    ia[len + j] = randomByte();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   304
                test(ia);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   305
            }
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
        void testPrefix(byte[] prefix) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   309
            if (prefix.length > 0)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   310
                System.out.printf("Testing prefix %s%n", string(prefix));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   311
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   312
            test(prefix);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   313
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   314
            testExhaustively(prefix, 1);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   315
            testExhaustively(prefix, 2);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   316
            // Can you spare a week of CPU time?
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   317
            // testExhaustively(cs, tester, prefix, 3);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   318
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   319
            testRandomly(prefix, 3);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   320
            testRandomly(prefix, 4);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   321
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   322
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   323
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   324
    private final static Random rnd = new Random();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   325
    private static byte randomByte() {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   326
        return (byte) rnd.nextInt(0x100);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   327
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   328
    private static byte[] randomBytes(int len) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   329
        byte[] a = new byte[len];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   330
        for (int i = 0; i < len; i++)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   331
            a[i] = randomByte();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   332
        return a;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   333
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   334
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   335
    private static final byte SS2 = (byte) 0x8e;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   336
    private static final byte SS3 = (byte) 0x8f;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   337
    private static final byte ESC = (byte) 0x1b;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   338
    private static final byte SO  = (byte) 0x0e;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   339
    private static final byte SI  = (byte) 0x0f;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   340
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   341
    private final static byte[][] stateChangers = {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   342
        {SS2}, {SS3}, {SO}, {SI}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   343
    };
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   344
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   345
    private final static byte[][]escapeSequences = {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   346
        {ESC, '(', 'B'},
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   347
        {ESC, '(', 'I'},
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   348
        {ESC, '(', 'J'},
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   349
        {ESC, '$', '@'},
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   350
        {ESC, '$', 'A'},
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   351
        {ESC, '$', ')', 'A'},
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   352
        {ESC, '$', ')', 'C'},
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   353
        {ESC, '$', ')', 'G'},
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   354
        {ESC, '$', '*', 'H'},
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   355
        {ESC, '$', '+', 'I'},
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   356
        {ESC, '$', 'B'},
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   357
        {ESC, 'N'},
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   358
        {ESC, 'O'},
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   359
        {ESC, '$', '(', 'D'},
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   360
    };
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   361
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   362
    private static boolean isStateChanger(Charset cs, byte[] ia) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   363
        Result r = new CharsetTester(cs).recode2(ia, 9);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   364
        return r == null ? false :
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   365
            (r.cr.isUnderflow() &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   366
             r.ipos == ia.length &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   367
             r.oa.length == 0);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   368
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   369
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   370
    private final static byte[][] incompletePrefixes = {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   371
        {ESC},
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   372
        {ESC, '('},
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   373
        {ESC, '$'},
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   374
        {ESC, '$', '(',},
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   375
    };
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   376
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   377
    private static boolean isIncompletePrefix(Charset cs, byte[] ia) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   378
        Result r = new CharsetTester(cs).recode2(ia, 9);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   379
        return r == null ? false :
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   380
            (r.cr.isUnderflow() &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   381
             r.ipos == 0 &&
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   382
             r.oa.length == 0);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   383
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   384
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   385
    private static void testCharset(Charset cs) throws Throwable {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   386
        final String csn = cs.name();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   387
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   388
        if (isBroken(csn)) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   389
            System.out.printf("Skipping possibly broken charset %s%n", csn);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   390
            return;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   391
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   392
        System.out.println(csn);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   393
        CharsetTester tester = new CharsetTester(cs);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   394
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   395
        tester.testPrefix(new byte[0]);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   396
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   397
        if (! csn.matches("(?:x-)?(?:UTF|JIS(?:_X)?0).*")) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   398
            for (byte[] prefix : stateChangers)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   399
                if (isStateChanger(cs, prefix))
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   400
                    tester.testPrefix(prefix);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   401
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   402
            for (byte[] prefix : incompletePrefixes)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   403
                if (isIncompletePrefix(cs, prefix))
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   404
                    tester.testPrefix(prefix);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   405
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   406
            if (isIncompletePrefix(cs, new byte[] {ESC}))
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   407
                for (byte[] prefix : escapeSequences)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   408
                    if (isStateChanger(cs, prefix))
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   409
                        tester.testPrefix(prefix);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   410
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   411
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   412
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   413
    private static void realMain(String[] args) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   414
        for (Charset cs : sort(Charset.availableCharsets().values())) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   415
            try {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   416
                testCharset(cs);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   417
            } catch (TooManyFailures e) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   418
                System.out.printf("Too many failures for %s%n", cs);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   419
            } catch (Throwable t) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   420
                unexpected(t);
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
        reporter.summarize();
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
    //--------------------- Infrastructure ---------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   427
    static volatile long passed = 0, failed = 0;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   428
    static void pass() {passed++;}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   429
    static void fail() {failed++; Thread.dumpStack();}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   430
    static void fail(String format, Object... args) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   431
        System.out.println(String.format(format, args)); failed++;}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   432
    static void fail(String msg) {System.out.println(msg); fail();}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   433
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   434
    static void check(boolean cond) {if (cond) pass(); else fail();}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   435
    static void equal(Object x, Object y) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   436
        if (x == null ? y == null : x.equals(y)) pass();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   437
        else fail(x + " not equal to " + y);}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   438
    public static void main(String[] args) throws Throwable {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   439
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   440
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   441
        if (failed > 0) throw new AssertionError("Some tests failed");}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   442
}