1 /*
   2  * Copyright (c) 2014, 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 /* @test
  25  * @summary unit tests for java.lang.invoke.LambdaForm
  26  * @library /testlibrary
  27  * @build LambdaFormTest
  28  * @run main ClassFileInstaller java.lang.invoke.LambdaFormTest
  29  * @run main/othervm -Xbootclasspath/a:. java.lang.invoke.LambdaFormTest
  30  */
  31 package java.lang.invoke;
  32 
  33 import static java.lang.invoke.LambdaForm.BasicType.*;
  34 import java.util.Arrays;
  35 
  36 public class LambdaFormTest {
  37     static void testBasicType() {
  38         for (int i = 0; i < ARG_TYPE_LIMIT; i++) {
  39             assertEquals(i, ARG_TYPES[i].ordinal());
  40             assertEquals(ARG_TYPES[i], ALL_TYPES[i]);
  41         }
  42         for (int i = 0; i < TYPE_LIMIT; i++) {
  43             assertEquals(i, ALL_TYPES[i].ordinal());
  44         }
  45         assertEquals(ALL_TYPES[TYPE_LIMIT-1], V_TYPE);
  46         assertFalse(Arrays.asList(ARG_TYPES).contains(V_TYPE));
  47     }
  48 
  49     static void testShortenSignature() {
  50         for (String s : new String[] {
  51                 // invariant strings:
  52                 "L", "LL", "ILL", "LIL", "LLI", "IILL", "ILIL", "ILLI",
  53                 // a few mappings:
  54                 "LLL=L3", "LLLL=L4", "LLLLLLLLLL=L10",
  55                 "IIIDDD=I3D3", "IDDD=ID3", "IIDDD=IID3", "IIID=I3D", "IIIDD=I3DD"
  56             }) {
  57             String s2 = s.substring(s.indexOf('=')+1);
  58             String s1 = s.equals(s2) ? s : s.substring(0, s.length() - s2.length() - 1);
  59             // mix the above cases with before and after reps of Z*
  60             for (int k = -3; k <= 3; k++) {
  61                 String beg = (k < 0 ? "ZZZZ".substring(-k) : "");
  62                 String end = (k > 0 ? "ZZZZ".substring(+k) : "");
  63                 String ks1 = beg+s1+end;
  64                 String ks2 = LambdaForm.shortenSignature(beg)+s2+LambdaForm.shortenSignature(end);
  65                 String ks3 = LambdaForm.shortenSignature(ks1);
  66                 if (!ks2.equals(ks3)) {
  67                     throw new AssertionError(ks2 + " != " + ks3);
  68                 }
  69             }
  70         }
  71     }
  72 
  73     public static void main(String[] args) {
  74         testBasicType();
  75         testShortenSignature();
  76     }
  77 
  78     static void assertEquals(Object o1, Object o2) {
  79         if (o1 == null && o2 == null)  return;
  80         if (o1 != null && !o1.equals(o2)) {
  81             throw new AssertionError(o1 + " != " + o2);
  82         }
  83     }
  84 
  85     static void assertTrue(boolean b) {
  86         if (!b) {
  87             throw new AssertionError();
  88         }
  89     }
  90 
  91     static void assertFalse(boolean b) {
  92         assertTrue(!b);
  93     }
  94 }