HierarchyGenerator.java revision 3014:a3dd196e5341
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.
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
24package org.openjdk.tests.shapegen;
25
26import org.openjdk.tests.shapegen.ClassCase.Kind;
27
28import java.util.Collection;
29import java.util.Set;
30import java.util.HashSet;
31import java.util.Collections;
32import java.util.ArrayList;
33import java.util.List;
34
35import static org.openjdk.tests.shapegen.ClassCase.Kind.*;
36
37import static java.lang.Math.pow;
38
39/**
40 *
41 * @author Robert Field
42 */
43public final class HierarchyGenerator {
44
45    private int okcnt = 0;
46    private int errcnt = 0;
47    private Set<Hierarchy> uniqueOK = new HashSet<>();
48    private Set<Hierarchy> uniqueErr = new HashSet<>();
49
50    /**
51     * @param args the command line arguments
52     */
53    public HierarchyGenerator() {
54        organize("exhaustive interface", iExhaustive(2));
55        organize("exhaustive class", cExhaustive());
56        organize("shapes interface", iShapes());
57        organize("shapes class/interface", ciShapes());
58
59        System.out.printf("\nExpect OK:    %d -- unique %d",   okcnt,  uniqueOK.size());
60        System.out.printf("\nExpect Error: %d -- unique %d\n", errcnt, uniqueErr.size());
61    }
62
63    public Collection<Hierarchy> getOK() {
64        return uniqueOK;
65    }
66
67    public Collection<Hierarchy> getErr() {
68        return uniqueErr;
69    }
70
71    private void organize(String tname, List<Hierarchy> totest) {
72        System.out.printf("\nGenerating %s....\n", tname);
73        int nodefault = 0;
74        List<Hierarchy> ok = new ArrayList<>();
75        List<Hierarchy> err = new ArrayList<>();
76        for (Hierarchy cc : totest) {
77            if (cc.anyDefaults()) {
78                //System.out.printf("  %s\n", cc);
79                if (cc.get_OK()) {
80                    ok.add(cc);
81                } else {
82                    err.add(cc);
83                }
84            } else {
85                ++nodefault;
86            }
87        }
88
89        errcnt += err.size();
90        okcnt += ok.size();
91        uniqueErr.addAll(err);
92        uniqueOK.addAll(ok);
93
94        System.out.printf("  %5d No default\n  %5d Error\n  %5d OK\n  %5d Total\n",
95                nodefault, err.size(), ok.size(), totest.size());
96    }
97
98    public List<Hierarchy> iExhaustive(int idepth) {
99        List<ClassCase> current = new ArrayList<>();
100        for (int i = 0; i < idepth; ++i) {
101            current = ilayer(current);
102        }
103        return wrapInClassAndHierarchy(current);
104    }
105
106    private List<ClassCase> ilayer(List<ClassCase> srcLayer) {
107        List<ClassCase> lay = new ArrayList<>();
108        for (int i = (int) pow(2, srcLayer.size()) - 1; i >= 0; --i) {
109            List<ClassCase> itfs = new ArrayList<>();
110            for (int b = srcLayer.size() - 1; b >= 0; --b) {
111                if ((i & (1<<b)) != 0) {
112                    itfs.add(srcLayer.get(b));
113                }
114            }
115            lay.add(new ClassCase(IVAC, null, itfs));
116            lay.add(new ClassCase(IPRESENT, null, itfs));
117            lay.add(new ClassCase(IDEFAULT, null, itfs));
118            lay.add(new ClassCase(IDEFAULT, null, itfs));
119        }
120        return lay;
121    }
122
123    public List<Hierarchy> cExhaustive() {
124        final Kind[] iKinds = new Kind[]{IDEFAULT, IVAC, IPRESENT, null};
125        final Kind[] cKinds = new Kind[]{CNONE, CABSTRACT, CCONCRETE};
126        List<Hierarchy> totest = new ArrayList<>();
127        for (int i1 = 0; i1 < iKinds.length; ++i1) {
128            for (int i2 = 0; i2 < iKinds.length; ++i2) {
129                for (int i3 = 0; i3 < iKinds.length; ++i3) {
130                    for (int c1 = 0; c1 < cKinds.length; ++c1) {
131                        for (int c2 = 0; c2 < cKinds.length; ++c2) {
132                            for (int c3 = 0; c3 < cKinds.length; ++c3) {
133                                totest.add( new Hierarchy(
134                                        new ClassCase(cKinds[c1],
135                                            new ClassCase(cKinds[c2],
136                                                new ClassCase(cKinds[c3],
137                                                    null,
138                                                    iList(iKinds[i1])
139                                                ),
140                                                iList(iKinds[i2])
141                                            ),
142                                            iList(iKinds[i3])
143                                        )));
144                            }
145                        }
146                    }
147                }
148            }
149        }
150        return totest;
151    }
152
153    public static final List<ClassCase> EMPTY_LIST = new ArrayList<>();
154
155    private List<ClassCase> iList(Kind kind) {
156        if (kind == null) {
157            return EMPTY_LIST;
158        } else {
159            List<ClassCase> itfs = new ArrayList<>();
160            itfs.add(new ClassCase(kind, null, EMPTY_LIST));
161            return itfs;
162        }
163    }
164
165    public List<Hierarchy> ciShapes() {
166        return wrapInHierarchy(TTShape.allCases(true));
167    }
168
169    public List<Hierarchy> iShapes() {
170        return wrapInClassAndHierarchy(TTShape.allCases(false));
171    }
172
173    public List<Hierarchy> wrapInClassAndHierarchy(List<ClassCase> ihs) {
174        List<Hierarchy> totest = new ArrayList<>();
175        for (ClassCase cc : ihs) {
176            List<ClassCase> interfaces = new ArrayList<>();
177            interfaces.add(cc);
178            totest.add(new Hierarchy(new ClassCase(CNONE, null, interfaces)));
179        }
180        return totest;
181    }
182
183    public List<Hierarchy> wrapInHierarchy(List<ClassCase> ihs) {
184        List<Hierarchy> totest = new ArrayList<>();
185        for (ClassCase cc : ihs) {
186            totest.add(new Hierarchy(cc));
187        }
188        return totest;
189    }
190}
191