|
| 1 | +/* |
| 2 | + * Copyright IBM Corp. and others 2024 |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under |
| 5 | + * the terms of the Eclipse Public License 2.0 which accompanies this |
| 6 | + * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * or the Apache License, Version 2.0 which accompanies this distribution and |
| 8 | + * is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 9 | + * |
| 10 | + * This Source Code may also be made available under the following |
| 11 | + * Secondary Licenses when the conditions for such availability set |
| 12 | + * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU |
| 13 | + * General Public License, version 2 with the GNU Classpath |
| 14 | + * Exception [1] and GNU General Public License, version 2 with the |
| 15 | + * OpenJDK Assembly Exception [2]. |
| 16 | + * |
| 17 | + * [1] https://www.gnu.org/software/classpath/license.html |
| 18 | + * [2] https://openjdk.org/legal/assembly-exception.html |
| 19 | + * |
| 20 | + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 |
| 21 | + */ |
| 22 | +package jit.test.tr.arrayTranslate; |
| 23 | + |
| 24 | +import org.testng.annotations.Test; |
| 25 | +import org.testng.AssertJUnit; |
| 26 | + |
| 27 | +@Test(groups = { "level.sanity","component.jit" }) |
| 28 | +public class ArrayTranslateTest { |
| 29 | + |
| 30 | + private static final int arrSize = 128; |
| 31 | + private char[] chars; |
| 32 | + private byte[] bytes; |
| 33 | + |
| 34 | + private static final char maxValueTRTO = 127; |
| 35 | + private static final char maxValueTRTO255 = 255; |
| 36 | + |
| 37 | + /* JIT-compile this method */ |
| 38 | + private int arrayTranslateTRTO(char[] sa, int sp, byte[] da, int dp, int len) |
| 39 | + { |
| 40 | + int i = 0; |
| 41 | + for (; i < len; i++) { |
| 42 | + char c = sa[sp++]; |
| 43 | + if (c > maxValueTRTO) break; |
| 44 | + da[dp++] = (byte)c; |
| 45 | + } |
| 46 | + return i; |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testArrayTranslateTRTO() |
| 51 | + { |
| 52 | + chars = new char[arrSize]; |
| 53 | + bytes = new byte[arrSize]; |
| 54 | + |
| 55 | + // Fill chars[] with safe characters |
| 56 | + for (int i = 0; i < arrSize; i++) { |
| 57 | + chars[i] = (char)('0' + (i >> 1)); |
| 58 | + } |
| 59 | + |
| 60 | + for (int offset = 0; offset <= 16; offset++) { |
| 61 | + for (int len = 0; len < arrSize-offset; len++) { |
| 62 | + int n = arrayTranslateTRTO(chars, offset, bytes, 0, len); |
| 63 | + AssertJUnit.assertEquals("TRTO: Wrong result: length=" + len, len, n); |
| 64 | + for (int i = 0; i < len; i++) { |
| 65 | + AssertJUnit.assertEquals("TRTO: Wrong value at bytes[" + i + "]", chars[offset+i], (char)bytes[i]); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + for (int len = 1; len <= 64; len++) { |
| 71 | + for (int i = 0; i < len; i++) { |
| 72 | + char c = chars[i]; |
| 73 | + chars[i] = maxValueTRTO + 1; |
| 74 | + int n = arrayTranslateTRTO(chars, 0, bytes, 0, len); |
| 75 | + AssertJUnit.assertEquals("TRTO: Wrong result for failure case: length=" + len, i, n); |
| 76 | + chars[i] = c; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /* JIT-compile this method */ |
| 82 | + private int arrayTranslateTRTO255(char[] sa, int sp, byte[] da, int dp, int len) |
| 83 | + { |
| 84 | + int i = 0; |
| 85 | + for (; i < len; i++) { |
| 86 | + char c = sa[sp++]; |
| 87 | + if (c > maxValueTRTO255) break; |
| 88 | + da[dp++] = (byte)c; |
| 89 | + } |
| 90 | + return i; |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testArrayTranslateTRTO255() |
| 95 | + { |
| 96 | + chars = new char[arrSize]; |
| 97 | + bytes = new byte[arrSize]; |
| 98 | + |
| 99 | + // Fill chars[] with safe characters |
| 100 | + for (int i = 0; i < arrSize; i++) { |
| 101 | + chars[i] = (char)('0' + (i >> 1)); |
| 102 | + } |
| 103 | + |
| 104 | + for (int offset = 0; offset <= 16; offset++) { |
| 105 | + for (int len = 0; len < arrSize-offset; len++) { |
| 106 | + int n = arrayTranslateTRTO255(chars, offset, bytes, 0, len); |
| 107 | + AssertJUnit.assertEquals("TRTO255: Wrong result: length=" + len, len, n); |
| 108 | + for (int i = 0; i < len; i++) { |
| 109 | + AssertJUnit.assertEquals("TRTO255: Wrong value at bytes[" + i + "]", chars[offset+i], (char)bytes[i]); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + for (int len = 1; len <= 64; len++) { |
| 115 | + for (int i = 0; i < len; i++) { |
| 116 | + char c = chars[i]; |
| 117 | + chars[i] = maxValueTRTO255 + 1; |
| 118 | + int n = arrayTranslateTRTO255(chars, 0, bytes, 0, len); |
| 119 | + AssertJUnit.assertEquals("TRTO255: Wrong result for failure case: length=" + len, i, n); |
| 120 | + chars[i] = c; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments