1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  *  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  *  This code is free software; you can redistribute it and/or modify it
   6  *  under the terms of the GNU General Public License version 2 only, as
   7  *  published by the Free Software Foundation.
   8  *
   9  *  This code is distributed in the hope that it will be useful, but WITHOUT
  10  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  *  version 2 for more details (a copy is included in the LICENSE file that
  13  *  accompanied this code).
  14  *
  15  *  You should have received a copy of the GNU General Public License version
  16  *  2 along with this work; if not, write to the Free Software Foundation,
  17  *  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  *   Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  *  or visit www.oracle.com if you need additional information or have any
  21  *  questions.
  22  *
  23  */
  24 
  25 /*
  26  * @test
  27  * @run testng TestLayoutPaths
  28  */
  29 
  30 import jdk.incubator.foreign.GroupLayout;
  31 import jdk.incubator.foreign.MemoryLayouts;
  32 import jdk.incubator.foreign.MemoryLayout;
  33 import jdk.incubator.foreign.MemoryLayout.PathElement;
  34 import jdk.incubator.foreign.SequenceLayout;
  35 
  36 import org.testng.annotations.*;
  37 import static org.testng.Assert.*;
  38 
  39 public class TestLayoutPaths {
  40 
  41     @Test(expectedExceptions = IllegalArgumentException.class)
  42     public void testBadSelectFromSeq() {
  43         SequenceLayout seq = MemoryLayout.ofSequence(MemoryLayouts.JAVA_INT);
  44         seq.offset(PathElement.groupElement("foo"));
  45     }
  46 
  47     @Test(expectedExceptions = IllegalArgumentException.class)
  48     public void testBadSelectFromStruct() {
  49         GroupLayout g = MemoryLayout.ofStruct(MemoryLayouts.JAVA_INT);
  50         g.offset(PathElement.sequenceElement());
  51     }
  52 
  53     @Test(expectedExceptions = IllegalArgumentException.class)
  54     public void testBadSelectFromValue() {
  55         SequenceLayout seq = MemoryLayout.ofSequence(MemoryLayouts.JAVA_INT);
  56         seq.offset(PathElement.sequenceElement(), PathElement.sequenceElement());
  57     }
  58 
  59     @Test(expectedExceptions = IllegalArgumentException.class)
  60     public void testUnknownStructField() {
  61         GroupLayout g = MemoryLayout.ofStruct(MemoryLayouts.JAVA_INT);
  62         g.offset(PathElement.groupElement("foo"));
  63     }
  64 
  65     @Test(expectedExceptions = NullPointerException.class)
  66     public void testNullGroupElementName() {
  67         GroupLayout g = MemoryLayout.ofStruct(MemoryLayouts.JAVA_INT);
  68         g.offset(PathElement.groupElement(null));
  69     }
  70 
  71     @Test(expectedExceptions = IllegalArgumentException.class)
  72     public void testOutOfBoundsSeqIndex() {
  73         SequenceLayout seq = MemoryLayout.ofSequence(5, MemoryLayouts.JAVA_INT);
  74         seq.offset(PathElement.sequenceElement(6));
  75     }
  76 
  77     @Test(expectedExceptions = IllegalArgumentException.class)
  78     public void testNegativeSeqIndex() {
  79         SequenceLayout seq = MemoryLayout.ofSequence(5, MemoryLayouts.JAVA_INT);
  80         seq.offset(PathElement.sequenceElement(-2));
  81     }
  82 
  83     @Test(expectedExceptions = IllegalArgumentException.class)
  84     public void testOutOfBoundsSeqRange() {
  85         SequenceLayout seq = MemoryLayout.ofSequence(5, MemoryLayouts.JAVA_INT);
  86         seq.offset(PathElement.sequenceElement(6, 2));
  87     }
  88 
  89     @Test(expectedExceptions = IllegalArgumentException.class)
  90     public void testNegativeSeqRange() {
  91         SequenceLayout seq = MemoryLayout.ofSequence(5, MemoryLayouts.JAVA_INT);
  92         seq.offset(PathElement.sequenceElement(-2, 2));
  93     }
  94 
  95     @Test(expectedExceptions = IllegalArgumentException.class)
  96     public void testIncompleteAccess() {
  97         SequenceLayout seq = MemoryLayout.ofSequence(5, MemoryLayout.ofStruct(MemoryLayouts.JAVA_INT));
  98         seq.varHandle(int.class, PathElement.sequenceElement());
  99     }
 100 
 101     @Test
 102     public void testBadContainerAlign() {
 103         GroupLayout g = MemoryLayout.ofStruct(MemoryLayouts.JAVA_INT.withBitAlignment(16).withName("foo")).withBitAlignment(8);
 104         try {
 105             g.offset(PathElement.groupElement("foo"));
 106         } catch (Throwable ex) {
 107             throw new AssertionError(ex); // should be ok!
 108         }
 109         try {
 110             g.varHandle(int.class, PathElement.groupElement("foo")); //ok
 111             assertTrue(false); //should fail!
 112         } catch (UnsupportedOperationException ex) {
 113             //ok
 114         } catch (Throwable ex) {
 115             throw new AssertionError(ex); //should fail!
 116         }
 117     }
 118 
 119     @Test
 120     public void testBadAlignOffset() {
 121         GroupLayout g = MemoryLayout.ofStruct(MemoryLayouts.PAD_8, MemoryLayouts.JAVA_INT.withBitAlignment(16).withName("foo"));
 122         try {
 123             g.offset(PathElement.groupElement("foo"));
 124         } catch (Throwable ex) {
 125             throw new AssertionError(ex); // should be ok!
 126         }
 127         try {
 128             g.varHandle(int.class, PathElement.groupElement("foo")); //ok
 129             assertTrue(false); //should fail!
 130         } catch (UnsupportedOperationException ex) {
 131             //ok
 132         } catch (Throwable ex) {
 133             throw new AssertionError(ex); //should fail!
 134         }
 135     }
 136 }
 137