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 
  32 package java.lang.invoke;
  33 
  34 public class LambdaFormTest {
  35     static void testShortenSignature() {
  36         for (String s : new String[] {
  37                 // invariant strings:
  38                 "L", "LL", "ILL", "LIL", "LLI", "IILL", "ILIL", "ILLI",
  39                 // a few mappings:
  40                 "LLL=L3", "LLLL=L4", "LLLLLLLLLL=L10",
  41                 "IIIDDD=I3D3", "IDDD=ID3", "IIDDD=IID3", "IIID=I3D", "IIIDD=I3DD"
  42             }) {
  43             String s2 = s.substring(s.indexOf('=')+1);
  44             String s1 = s.equals(s2) ? s : s.substring(0, s.length() - s2.length() - 1);
  45             // mix the above cases with before and after reps of Z*
  46             for (int k = -3; k <= 3; k++) {
  47                 String beg = (k < 0 ? "ZZZZ".substring(-k) : "");
  48                 String end = (k > 0 ? "ZZZZ".substring(+k) : "");
  49                 String ks1 = beg+s1+end;
  50                 String ks2 = LambdaForm.shortenSignature(beg)+s2+LambdaForm.shortenSignature(end);
  51                 String ks3 = LambdaForm.shortenSignature(ks1);
  52                 if (!ks2.equals(ks3)) {
  53                     throw new AssertionError(ks2 + " != " + ks3);
  54                 }
  55             }
  56         }
  57     }
  58 
  59     public static void main(String[] args) {
  60         testShortenSignature();
  61     }
  62 }