Wed, 09 May 2012 13:07:42 -0700
Added tag jdk8-b37 for changeset bfcf92bfefb8
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | /** |
aoqi@0 | 26 | * @test |
aoqi@0 | 27 | * @bug 7047069 |
aoqi@0 | 28 | * @summary Array can dynamically change size when assigned to an object field |
aoqi@0 | 29 | * |
aoqi@0 | 30 | * @run main/othervm -Xbatch Test7047069 |
aoqi@0 | 31 | */ |
aoqi@0 | 32 | |
aoqi@0 | 33 | import java.util.*; |
aoqi@0 | 34 | import java.awt.geom.*; |
aoqi@0 | 35 | |
aoqi@0 | 36 | public class Test7047069 { |
aoqi@0 | 37 | static boolean verbose; |
aoqi@0 | 38 | |
aoqi@0 | 39 | static final int GROW_SIZE = 24; // Multiple of cubic & quad curve size |
aoqi@0 | 40 | |
aoqi@0 | 41 | float squareflat; // Square of the flatness parameter |
aoqi@0 | 42 | // for testing against squared lengths |
aoqi@0 | 43 | |
aoqi@0 | 44 | int limit; // Maximum number of recursion levels |
aoqi@0 | 45 | |
aoqi@0 | 46 | float hold[] = new float[14]; // The cache of interpolated coords |
aoqi@0 | 47 | // Note that this must be long enough |
aoqi@0 | 48 | // to store a full cubic segment and |
aoqi@0 | 49 | // a relative cubic segment to avoid |
aoqi@0 | 50 | // aliasing when copying the coords |
aoqi@0 | 51 | // of a curve to the end of the array. |
aoqi@0 | 52 | // This is also serendipitously equal |
aoqi@0 | 53 | // to the size of a full quad segment |
aoqi@0 | 54 | // and 2 relative quad segments. |
aoqi@0 | 55 | |
aoqi@0 | 56 | int holdEnd; // The index of the last curve segment |
aoqi@0 | 57 | // being held for interpolation |
aoqi@0 | 58 | |
aoqi@0 | 59 | int holdIndex; // The index of the curve segment |
aoqi@0 | 60 | // that was last interpolated. This |
aoqi@0 | 61 | // is the curve segment ready to be |
aoqi@0 | 62 | // returned in the next call to |
aoqi@0 | 63 | // currentSegment(). |
aoqi@0 | 64 | |
aoqi@0 | 65 | int levels[]; // The recursion level at which |
aoqi@0 | 66 | // each curve being held in storage |
aoqi@0 | 67 | // was generated. |
aoqi@0 | 68 | |
aoqi@0 | 69 | int levelIndex; // The index of the entry in the |
aoqi@0 | 70 | // levels array of the curve segment |
aoqi@0 | 71 | // at the holdIndex |
aoqi@0 | 72 | |
aoqi@0 | 73 | public static void subdivide(float src[], int srcoff, |
aoqi@0 | 74 | float left[], int leftoff, |
aoqi@0 | 75 | float right[], int rightoff) |
aoqi@0 | 76 | { |
aoqi@0 | 77 | float x1 = src[srcoff + 0]; |
aoqi@0 | 78 | float y1 = src[srcoff + 1]; |
aoqi@0 | 79 | float ctrlx = src[srcoff + 2]; |
aoqi@0 | 80 | float ctrly = src[srcoff + 3]; |
aoqi@0 | 81 | float x2 = src[srcoff + 4]; |
aoqi@0 | 82 | float y2 = src[srcoff + 5]; |
aoqi@0 | 83 | if (left != null) { |
aoqi@0 | 84 | left[leftoff + 0] = x1; |
aoqi@0 | 85 | left[leftoff + 1] = y1; |
aoqi@0 | 86 | } |
aoqi@0 | 87 | if (right != null) { |
aoqi@0 | 88 | right[rightoff + 4] = x2; |
aoqi@0 | 89 | right[rightoff + 5] = y2; |
aoqi@0 | 90 | } |
aoqi@0 | 91 | x1 = (x1 + ctrlx) / 2f; |
aoqi@0 | 92 | y1 = (y1 + ctrly) / 2f; |
aoqi@0 | 93 | x2 = (x2 + ctrlx) / 2f; |
aoqi@0 | 94 | y2 = (y2 + ctrly) / 2f; |
aoqi@0 | 95 | ctrlx = (x1 + x2) / 2f; |
aoqi@0 | 96 | ctrly = (y1 + y2) / 2f; |
aoqi@0 | 97 | if (left != null) { |
aoqi@0 | 98 | left[leftoff + 2] = x1; |
aoqi@0 | 99 | left[leftoff + 3] = y1; |
aoqi@0 | 100 | left[leftoff + 4] = ctrlx; |
aoqi@0 | 101 | left[leftoff + 5] = ctrly; |
aoqi@0 | 102 | } |
aoqi@0 | 103 | if (right != null) { |
aoqi@0 | 104 | right[rightoff + 0] = ctrlx; |
aoqi@0 | 105 | right[rightoff + 1] = ctrly; |
aoqi@0 | 106 | right[rightoff + 2] = x2; |
aoqi@0 | 107 | right[rightoff + 3] = y2; |
aoqi@0 | 108 | } |
aoqi@0 | 109 | } |
aoqi@0 | 110 | |
aoqi@0 | 111 | public static double getFlatnessSq(float coords[], int offset) { |
aoqi@0 | 112 | return Line2D.ptSegDistSq(coords[offset + 0], coords[offset + 1], |
aoqi@0 | 113 | coords[offset + 4], coords[offset + 5], |
aoqi@0 | 114 | coords[offset + 2], coords[offset + 3]); |
aoqi@0 | 115 | } |
aoqi@0 | 116 | |
aoqi@0 | 117 | public Test7047069() { |
aoqi@0 | 118 | this.squareflat = .0001f * .0001f; |
aoqi@0 | 119 | holdIndex = hold.length - 6; |
aoqi@0 | 120 | holdEnd = hold.length - 2; |
aoqi@0 | 121 | hold[holdIndex + 0] = (float) (Math.random() * 100); |
aoqi@0 | 122 | hold[holdIndex + 1] = (float) (Math.random() * 100); |
aoqi@0 | 123 | hold[holdIndex + 2] = (float) (Math.random() * 100); |
aoqi@0 | 124 | hold[holdIndex + 3] = (float) (Math.random() * 100); |
aoqi@0 | 125 | hold[holdIndex + 4] = (float) (Math.random() * 100); |
aoqi@0 | 126 | hold[holdIndex + 5] = (float) (Math.random() * 100); |
aoqi@0 | 127 | levelIndex = 0; |
aoqi@0 | 128 | this.limit = 10; |
aoqi@0 | 129 | this.levels = new int[limit + 1]; |
aoqi@0 | 130 | } |
aoqi@0 | 131 | |
aoqi@0 | 132 | /* |
aoqi@0 | 133 | * Ensures that the hold array can hold up to (want) more values. |
aoqi@0 | 134 | * It is currently holding (hold.length - holdIndex) values. |
aoqi@0 | 135 | */ |
aoqi@0 | 136 | void ensureHoldCapacity(int want) { |
aoqi@0 | 137 | if (holdIndex - want < 0) { |
aoqi@0 | 138 | int have = hold.length - holdIndex; |
aoqi@0 | 139 | int newsize = hold.length + GROW_SIZE; |
aoqi@0 | 140 | float newhold[] = new float[newsize]; |
aoqi@0 | 141 | System.arraycopy(hold, holdIndex, |
aoqi@0 | 142 | newhold, holdIndex + GROW_SIZE, |
aoqi@0 | 143 | have); |
aoqi@0 | 144 | if (verbose) System.err.println("old hold = "+hold+"["+hold.length+"]"); |
aoqi@0 | 145 | if (verbose) System.err.println("replacement hold = "+newhold+"["+newhold.length+"]"); |
aoqi@0 | 146 | hold = newhold; |
aoqi@0 | 147 | if (verbose) System.err.println("new hold = "+hold+"["+hold.length+"]"); |
aoqi@0 | 148 | if (verbose) System.err.println("replacement hold still = "+newhold+"["+newhold.length+"]"); |
aoqi@0 | 149 | holdIndex += GROW_SIZE; |
aoqi@0 | 150 | holdEnd += GROW_SIZE; |
aoqi@0 | 151 | } |
aoqi@0 | 152 | } |
aoqi@0 | 153 | |
aoqi@0 | 154 | private boolean next() { |
aoqi@0 | 155 | if (holdIndex >= holdEnd) { |
aoqi@0 | 156 | return false; |
aoqi@0 | 157 | } |
aoqi@0 | 158 | |
aoqi@0 | 159 | int level = levels[levelIndex]; |
aoqi@0 | 160 | while (level < limit) { |
aoqi@0 | 161 | if (getFlatnessSq(hold, holdIndex) < squareflat) { |
aoqi@0 | 162 | break; |
aoqi@0 | 163 | } |
aoqi@0 | 164 | |
aoqi@0 | 165 | ensureHoldCapacity(4); |
aoqi@0 | 166 | subdivide(hold, holdIndex, |
aoqi@0 | 167 | hold, holdIndex - 4, |
aoqi@0 | 168 | hold, holdIndex); |
aoqi@0 | 169 | holdIndex -= 4; |
aoqi@0 | 170 | |
aoqi@0 | 171 | // Now that we have subdivided, we have constructed |
aoqi@0 | 172 | // two curves of one depth lower than the original |
aoqi@0 | 173 | // curve. One of those curves is in the place of |
aoqi@0 | 174 | // the former curve and one of them is in the next |
aoqi@0 | 175 | // set of held coordinate slots. We now set both |
aoqi@0 | 176 | // curves level values to the next higher level. |
aoqi@0 | 177 | level++; |
aoqi@0 | 178 | levels[levelIndex] = level; |
aoqi@0 | 179 | levelIndex++; |
aoqi@0 | 180 | levels[levelIndex] = level; |
aoqi@0 | 181 | } |
aoqi@0 | 182 | |
aoqi@0 | 183 | // This curve segment is flat enough, or it is too deep |
aoqi@0 | 184 | // in recursion levels to try to flatten any more. The |
aoqi@0 | 185 | // two coordinates at holdIndex+4 and holdIndex+5 now |
aoqi@0 | 186 | // contain the endpoint of the curve which can be the |
aoqi@0 | 187 | // endpoint of an approximating line segment. |
aoqi@0 | 188 | holdIndex += 4; |
aoqi@0 | 189 | levelIndex--; |
aoqi@0 | 190 | return true; |
aoqi@0 | 191 | } |
aoqi@0 | 192 | |
aoqi@0 | 193 | public static void main(String argv[]) { |
aoqi@0 | 194 | verbose = (argv.length > 0); |
aoqi@0 | 195 | for (int i = 0; i < 100000; i++) { |
aoqi@0 | 196 | Test7047069 st = new Test7047069(); |
aoqi@0 | 197 | while (st.next()) {} |
aoqi@0 | 198 | } |
aoqi@0 | 199 | } |
aoqi@0 | 200 | } |