|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.graal.python.test.nodes.storage; |
| 42 | + |
| 43 | +import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.GetItemSliceNode; |
| 44 | +import com.oracle.graal.python.runtime.sequence.storage.BoolSequenceStorage; |
| 45 | +import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage; |
| 46 | +import com.oracle.graal.python.runtime.sequence.storage.DoubleSequenceStorage; |
| 47 | +import com.oracle.graal.python.runtime.sequence.storage.IntSequenceStorage; |
| 48 | +import com.oracle.graal.python.runtime.sequence.storage.LongSequenceStorage; |
| 49 | +import com.oracle.graal.python.runtime.sequence.storage.ObjectSequenceStorage; |
| 50 | +import com.oracle.graal.python.test.PythonTests; |
| 51 | +import com.oracle.truffle.api.frame.VirtualFrame; |
| 52 | +import com.oracle.truffle.api.nodes.RootNode; |
| 53 | +import org.junit.After; |
| 54 | +import org.junit.Before; |
| 55 | +import org.junit.Test; |
| 56 | + |
| 57 | +import static org.junit.Assert.assertEquals; |
| 58 | +import static org.junit.Assert.assertFalse; |
| 59 | +import static org.junit.Assert.assertTrue; |
| 60 | + |
| 61 | +public class GetItemSliceNodeTests { |
| 62 | + |
| 63 | + @Before |
| 64 | + public void setUp() { |
| 65 | + PythonTests.enterContext(); |
| 66 | + } |
| 67 | + |
| 68 | + @After |
| 69 | + public void tearDown() { |
| 70 | + PythonTests.closeContext(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void intGetSlice() { |
| 75 | + var storage = new RootNode(null) { |
| 76 | + @Child @SuppressWarnings("FieldMayBeFinal") private GetItemSliceNode getItemSliceNode = GetItemSliceNode.create(); |
| 77 | + |
| 78 | + @Override |
| 79 | + public Object execute(VirtualFrame frame) { |
| 80 | + var intStorage = new IntSequenceStorage(new int[]{1, 2, 3, 4, 5, 6}); |
| 81 | + return getItemSliceNode.execute(intStorage, 1, 4, 1, 3); |
| 82 | + } |
| 83 | + }.getCallTarget().call(); |
| 84 | + |
| 85 | + assertEquals(IntSequenceStorage.class, storage.getClass()); |
| 86 | + var intStorage = (IntSequenceStorage) storage; |
| 87 | + for (int i = 0; i < 3; i++) { |
| 88 | + assertEquals(i + 2, intStorage.getInternalIntArray()[i]); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void longGetSlice() { |
| 94 | + var storage = new RootNode(null) { |
| 95 | + @Child @SuppressWarnings("FieldMayBeFinal") private GetItemSliceNode getItemSliceNode = GetItemSliceNode.create(); |
| 96 | + |
| 97 | + @Override |
| 98 | + public Object execute(VirtualFrame frame) { |
| 99 | + var longStorage = new LongSequenceStorage(new long[]{1, 2, 3, 4, 5, 6}); |
| 100 | + return getItemSliceNode.execute(longStorage, 1, 4, 1, 3); |
| 101 | + } |
| 102 | + }.getCallTarget().call(); |
| 103 | + |
| 104 | + assertEquals(LongSequenceStorage.class, storage.getClass()); |
| 105 | + var longStorage = (LongSequenceStorage) storage; |
| 106 | + for (int i = 0; i < 3; i++) { |
| 107 | + assertEquals(i + 2, longStorage.getInternalLongArray()[i]); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void doubleGetSlice() { |
| 113 | + var storage = new RootNode(null) { |
| 114 | + @Child @SuppressWarnings("FieldMayBeFinal") private GetItemSliceNode getItemSliceNode = GetItemSliceNode.create(); |
| 115 | + |
| 116 | + @Override |
| 117 | + public Object execute(VirtualFrame frame) { |
| 118 | + var doubleStorage = new DoubleSequenceStorage(new double[]{1, 2, 3, 4, 5, 6}); |
| 119 | + return getItemSliceNode.execute(doubleStorage, 1, 4, 1, 3); |
| 120 | + } |
| 121 | + }.getCallTarget().call(); |
| 122 | + |
| 123 | + assertEquals(DoubleSequenceStorage.class, storage.getClass()); |
| 124 | + var doubleStorage = (DoubleSequenceStorage) storage; |
| 125 | + for (int i = 0; i < 3; i++) { |
| 126 | + assertEquals(i + 2, doubleStorage.getInternalDoubleArray()[i], 0); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void byteGetSlice() { |
| 132 | + var storage = new RootNode(null) { |
| 133 | + @Child @SuppressWarnings("FieldMayBeFinal") private GetItemSliceNode getItemSliceNode = GetItemSliceNode.create(); |
| 134 | + |
| 135 | + @Override |
| 136 | + public Object execute(VirtualFrame frame) { |
| 137 | + var byteStorage = new ByteSequenceStorage(new byte[]{1, 2, 3, 4, 5, 6}); |
| 138 | + return getItemSliceNode.execute(byteStorage, 1, 4, 1, 3); |
| 139 | + } |
| 140 | + }.getCallTarget().call(); |
| 141 | + |
| 142 | + assertEquals(ByteSequenceStorage.class, storage.getClass()); |
| 143 | + var byteStorage = (ByteSequenceStorage) storage; |
| 144 | + for (int i = 0; i < 3; i++) { |
| 145 | + assertEquals(i + 2, byteStorage.getInternalByteArray()[i]); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void objectGetSlice() { |
| 151 | + var storage = new RootNode(null) { |
| 152 | + @Child @SuppressWarnings("FieldMayBeFinal") private GetItemSliceNode getItemSliceNode = GetItemSliceNode.create(); |
| 153 | + |
| 154 | + @Override |
| 155 | + public Object execute(VirtualFrame frame) { |
| 156 | + var objectStorage = new ObjectSequenceStorage(new Object[]{1, 2, 3, 4, 5, 6}); |
| 157 | + return getItemSliceNode.execute(objectStorage, 1, 4, 1, 3); |
| 158 | + } |
| 159 | + }.getCallTarget().call(); |
| 160 | + |
| 161 | + assertEquals(ObjectSequenceStorage.class, storage.getClass()); |
| 162 | + var objectStorage = (ObjectSequenceStorage) storage; |
| 163 | + for (int i = 0; i < 3; i++) { |
| 164 | + assertEquals(i + 2, objectStorage.getInternalObjectArray()[i]); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void boolGetSlice() { |
| 170 | + var storage = new RootNode(null) { |
| 171 | + @Child @SuppressWarnings("FieldMayBeFinal") private GetItemSliceNode getItemSliceNode = GetItemSliceNode.create(); |
| 172 | + |
| 173 | + @Override |
| 174 | + public Object execute(VirtualFrame frame) { |
| 175 | + var boolStorage = new BoolSequenceStorage(new boolean[]{true, false, false, true, true, false}); |
| 176 | + return getItemSliceNode.execute(boolStorage, 1, 4, 1, 3); |
| 177 | + } |
| 178 | + }.getCallTarget().call(); |
| 179 | + |
| 180 | + assertEquals(BoolSequenceStorage.class, storage.getClass()); |
| 181 | + var boolArray = ((BoolSequenceStorage) storage).getInternalBoolArray(); |
| 182 | + assertEquals(3, boolArray.length); |
| 183 | + assertFalse(boolArray[0]); |
| 184 | + assertFalse(boolArray[1]); |
| 185 | + assertTrue(boolArray[2]); |
| 186 | + } |
| 187 | +} |
0 commit comments