1 /*
   2  * Copyright (c) 2012, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package org.openjdk.tests.shapegen;
  27 
  28 import org.openjdk.tests.shapegen.ClassCase.Kind;
  29 
  30 import java.util.Collection;
  31 import java.util.Set;
  32 import java.util.HashSet;
  33 import java.util.Collections;
  34 import java.util.ArrayList;
  35 import java.util.List;
  36 
  37 import static org.openjdk.tests.shapegen.ClassCase.Kind.*;
  38 
  39 import static java.lang.Math.pow;
  40 
  41 /**
  42  *
  43  * @author Robert Field
  44  */
  45 public final class HierarchyGenerator {
  46 
  47     private int okcnt = 0;
  48     private int errcnt = 0;
  49     private Set<Hierarchy> uniqueOK = new HashSet<>();
  50     private Set<Hierarchy> uniqueErr = new HashSet<>();
  51 
  52     /**
  53      * @param args the command line arguments
  54      */
  55     public HierarchyGenerator() {
  56         organize("exhaustive interface", iExhaustive(2));
  57         organize("exhaustive class", cExhaustive());
  58         organize("shapes interface", iShapes());
  59         organize("shapes class/interface", ciShapes());
  60 
  61         System.out.printf("\nExpect OK:    %d -- unique %d",   okcnt,  uniqueOK.size());
  62         System.out.printf("\nExpect Error: %d -- unique %d\n", errcnt, uniqueErr.size());
  63     }
  64 
  65     public Collection<Hierarchy> getOK() {
  66         return uniqueOK;
  67     }
  68 
  69     public Collection<Hierarchy> getErr() {
  70         return uniqueErr;
  71     }
  72 
  73     private void organize(String tname, List<Hierarchy> totest) {
  74         System.out.printf("\nGenerating %s....\n", tname);
  75         int nodefault = 0;
  76         List<Hierarchy> ok = new ArrayList<>();
  77         List<Hierarchy> err = new ArrayList<>();
  78         for (Hierarchy cc : totest) {
  79             if (cc.anyDefaults()) {
  80                 //System.out.printf("  %s\n", cc);
  81                 if (cc.get_OK()) {
  82                     ok.add(cc);
  83                 } else {
  84                     err.add(cc);
  85                 }
  86             } else {
  87                 ++nodefault;
  88             }
  89         }
  90 
  91         errcnt += err.size();
  92         okcnt += ok.size();
  93         uniqueErr.addAll(err);
  94         uniqueOK.addAll(ok);
  95 
  96         System.out.printf("  %5d No default\n  %5d Error\n  %5d OK\n  %5d Total\n",
  97                 nodefault, err.size(), ok.size(), totest.size());
  98     }
  99 
 100     public List<Hierarchy> iExhaustive(int idepth) {
 101         List<ClassCase> current = new ArrayList<>();
 102         for (int i = 0; i < idepth; ++i) {
 103             current = ilayer(current);
 104         }
 105         return wrapInClassAndHierarchy(current);
 106     }
 107 
 108     private List<ClassCase> ilayer(List<ClassCase> srcLayer) {
 109         List<ClassCase> lay = new ArrayList<>();
 110         for (int i = (int) pow(2, srcLayer.size()) - 1; i >= 0; --i) {
 111             List<ClassCase> itfs = new ArrayList<>();
 112             for (int b = srcLayer.size() - 1; b >= 0; --b) {
 113                 if ((i & (1<<b)) != 0) {
 114                     itfs.add(srcLayer.get(b));
 115                 }
 116             }
 117             lay.add(new ClassCase(IVAC, null, itfs));
 118             lay.add(new ClassCase(IPRESENT, null, itfs));
 119             lay.add(new ClassCase(IDEFAULT, null, itfs));
 120             lay.add(new ClassCase(IDEFAULT, null, itfs));
 121         }
 122         return lay;
 123     }
 124 
 125     public List<Hierarchy> cExhaustive() {
 126         final Kind[] iKinds = new Kind[]{IDEFAULT, IVAC, IPRESENT, null};
 127         final Kind[] cKinds = new Kind[]{CNONE, CABSTRACT, CCONCRETE};
 128         List<Hierarchy> totest = new ArrayList<>();
 129         for (int i1 = 0; i1 < iKinds.length; ++i1) {
 130             for (int i2 = 0; i2 < iKinds.length; ++i2) {
 131                 for (int i3 = 0; i3 < iKinds.length; ++i3) {
 132                     for (int c1 = 0; c1 < cKinds.length; ++c1) {
 133                         for (int c2 = 0; c2 < cKinds.length; ++c2) {
 134                             for (int c3 = 0; c3 < cKinds.length; ++c3) {
 135                                 totest.add( new Hierarchy(
 136                                         new ClassCase(cKinds[c1],
 137                                             new ClassCase(cKinds[c2],
 138                                                 new ClassCase(cKinds[c3],
 139                                                     null,
 140                                                     iList(iKinds[i1])
 141                                                 ),
 142                                                 iList(iKinds[i2])
 143                                             ),
 144                                             iList(iKinds[i3])
 145                                         )));
 146                             }
 147                         }
 148                     }
 149                 }
 150             }
 151         }
 152         return totest;
 153     }
 154 
 155     public static final List<ClassCase> EMPTY_LIST = new ArrayList<>();
 156 
 157     private List<ClassCase> iList(Kind kind) {
 158         if (kind == null) {
 159             return EMPTY_LIST;
 160         } else {
 161             List<ClassCase> itfs = new ArrayList<>();
 162             itfs.add(new ClassCase(kind, null, EMPTY_LIST));
 163             return itfs;
 164         }
 165     }
 166 
 167     public List<Hierarchy> ciShapes() {
 168         return wrapInHierarchy(TTShape.allCases(true));
 169     }
 170 
 171     public List<Hierarchy> iShapes() {
 172         return wrapInClassAndHierarchy(TTShape.allCases(false));
 173     }
 174 
 175     public List<Hierarchy> wrapInClassAndHierarchy(List<ClassCase> ihs) {
 176         List<Hierarchy> totest = new ArrayList<>();
 177         for (ClassCase cc : ihs) {
 178             List<ClassCase> interfaces = new ArrayList<>();
 179             interfaces.add(cc);
 180             totest.add(new Hierarchy(new ClassCase(CNONE, null, interfaces)));
 181         }
 182         return totest;
 183     }
 184 
 185     public List<Hierarchy> wrapInHierarchy(List<ClassCase> ihs) {
 186         List<Hierarchy> totest = new ArrayList<>();
 187         for (ClassCase cc : ihs) {
 188             totest.add(new Hierarchy(cc));
 189         }
 190         return totest;
 191     }
 192 }