test/testlibrary_tests/AssertsTest.java

Mon, 28 Jul 2014 15:06:38 -0700

author
fzhinkin
date
Mon, 28 Jul 2014 15:06:38 -0700
changeset 6997
dbb05f6d93c4
parent 5782
5b1191bf0b4b
child 6876
710a3c8b516e
permissions
-rw-r--r--

8051344: JVM crashed in Compile::start() during method parsing w/ UseRTMDeopt turned on
Summary: call rtm_deopt() only if there were no compilation bailouts before.
Reviewed-by: kvn

ehelin@5531 1 /*
ehelin@5531 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
ehelin@5531 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ehelin@5531 4 *
ehelin@5531 5 * This code is free software; you can redistribute it and/or modify it
ehelin@5531 6 * under the terms of the GNU General Public License version 2 only, as
ehelin@5531 7 * published by the Free Software Foundation.
ehelin@5531 8 *
ehelin@5531 9 * This code is distributed in the hope that it will be useful, but WITHOUT
ehelin@5531 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ehelin@5531 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ehelin@5531 12 * version 2 for more details (a copy is included in the LICENSE file that
ehelin@5531 13 * accompanied this code).
ehelin@5531 14 *
ehelin@5531 15 * You should have received a copy of the GNU General Public License version
ehelin@5531 16 * 2 along with this work; if not, write to the Free Software Foundation,
ehelin@5531 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ehelin@5531 18 *
ehelin@5531 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ehelin@5531 20 * or visit www.oracle.com if you need additional information or have any
ehelin@5531 21 * questions.
ehelin@5531 22 */
ehelin@5531 23
ehelin@5531 24 import static com.oracle.java.testlibrary.Asserts.*;
ehelin@5531 25
ehelin@5531 26 /* @test
ehelin@5531 27 * @summary Tests the different assertions in the Assert class
ehelin@5531 28 * @library /testlibrary
ehelin@5531 29 */
ehelin@5531 30 public class AssertsTest {
ehelin@5531 31 private static class Foo implements Comparable<Foo> {
ehelin@5531 32 final int id;
ehelin@5531 33 public Foo(int id) {
ehelin@5531 34 this.id = id;
ehelin@5531 35 }
ehelin@5531 36
ehelin@5531 37 public int compareTo(Foo f) {
ehelin@5531 38 return new Integer(id).compareTo(new Integer(f.id));
ehelin@5531 39 }
ehelin@5531 40 }
ehelin@5531 41
ehelin@5531 42 public static void main(String[] args) throws Exception {
ehelin@5531 43 testLessThan();
ehelin@5531 44 testLessThanOrEqual();
ehelin@5531 45 testEquals();
ehelin@5531 46 testGreaterThanOrEqual();
ehelin@5531 47 testGreaterThan();
ehelin@5531 48 testNotEquals();
ehelin@5531 49 testNull();
ehelin@5531 50 testNotNull();
ehelin@5531 51 testTrue();
ehelin@5531 52 testFalse();
ehelin@5531 53 }
ehelin@5531 54
ehelin@5531 55 private static void testLessThan() throws Exception {
ehelin@5531 56 expectPass(Assertion.LT, 1, 2);
ehelin@5531 57
ehelin@5531 58 expectFail(Assertion.LT, 2, 2);
ehelin@5531 59 expectFail(Assertion.LT, 2, 1);
ehelin@5531 60 expectFail(Assertion.LT, null, 2);
ehelin@5531 61 expectFail(Assertion.LT, 2, null);
ehelin@5531 62 }
ehelin@5531 63
ehelin@5531 64 private static void testLessThanOrEqual() throws Exception {
ehelin@5531 65 expectPass(Assertion.LTE, 1, 2);
ehelin@5531 66 expectPass(Assertion.LTE, 2, 2);
ehelin@5531 67
ehelin@5531 68 expectFail(Assertion.LTE, 3, 2);
ehelin@5531 69 expectFail(Assertion.LTE, null, 2);
ehelin@5531 70 expectFail(Assertion.LTE, 2, null);
ehelin@5531 71 }
ehelin@5531 72
ehelin@5531 73 private static void testEquals() throws Exception {
ehelin@5531 74 expectPass(Assertion.EQ, 1, 1);
ehelin@5531 75 expectPass(Assertion.EQ, null, null);
ehelin@5531 76
ehelin@5531 77 Foo f1 = new Foo(1);
ehelin@5531 78 expectPass(Assertion.EQ, f1, f1);
ehelin@5531 79
ehelin@5531 80 Foo f2 = new Foo(1);
ehelin@5531 81 expectFail(Assertion.EQ, f1, f2);
ehelin@5531 82 expectFail(Assertion.LTE, null, 2);
ehelin@5531 83 expectFail(Assertion.LTE, 2, null);
ehelin@5531 84 }
ehelin@5531 85
ehelin@5531 86 private static void testGreaterThanOrEqual() throws Exception {
ehelin@5531 87 expectPass(Assertion.GTE, 1, 1);
ehelin@5531 88 expectPass(Assertion.GTE, 2, 1);
ehelin@5531 89
ehelin@5531 90 expectFail(Assertion.GTE, 1, 2);
ehelin@5531 91 expectFail(Assertion.GTE, null, 2);
ehelin@5531 92 expectFail(Assertion.GTE, 2, null);
ehelin@5531 93 }
ehelin@5531 94
ehelin@5531 95 private static void testGreaterThan() throws Exception {
ehelin@5531 96 expectPass(Assertion.GT, 2, 1);
ehelin@5531 97
ehelin@5531 98 expectFail(Assertion.GT, 1, 1);
ehelin@5531 99 expectFail(Assertion.GT, 1, 2);
ehelin@5531 100 expectFail(Assertion.GT, null, 2);
ehelin@5531 101 expectFail(Assertion.GT, 2, null);
ehelin@5531 102 }
ehelin@5531 103
ehelin@5531 104 private static void testNotEquals() throws Exception {
ehelin@5531 105 expectPass(Assertion.NE, null, 1);
ehelin@5531 106 expectPass(Assertion.NE, 1, null);
ehelin@5531 107
ehelin@5531 108 Foo f1 = new Foo(1);
ehelin@5531 109 Foo f2 = new Foo(1);
ehelin@5531 110 expectPass(Assertion.NE, f1, f2);
ehelin@5531 111
ehelin@5531 112 expectFail(Assertion.NE, null, null);
ehelin@5531 113 expectFail(Assertion.NE, f1, f1);
ehelin@5531 114 expectFail(Assertion.NE, 1, 1);
ehelin@5531 115 }
ehelin@5531 116
ehelin@5531 117 private static void testNull() throws Exception {
ehelin@5531 118 expectPass(Assertion.NULL, null);
ehelin@5531 119
ehelin@5531 120 expectFail(Assertion.NULL, 1);
ehelin@5531 121 }
ehelin@5531 122
ehelin@5531 123 private static void testNotNull() throws Exception {
ehelin@5531 124 expectPass(Assertion.NOTNULL, 1);
ehelin@5531 125
ehelin@5531 126 expectFail(Assertion.NOTNULL, null);
ehelin@5531 127 }
ehelin@5531 128
ehelin@5531 129 private static void testTrue() throws Exception {
ehelin@5531 130 expectPass(Assertion.TRUE, true);
ehelin@5531 131
ehelin@5531 132 expectFail(Assertion.TRUE, false);
ehelin@5531 133 }
ehelin@5531 134
ehelin@5531 135 private static void testFalse() throws Exception {
ehelin@5531 136 expectPass(Assertion.FALSE, false);
ehelin@5531 137
ehelin@5531 138 expectFail(Assertion.FALSE, true);
ehelin@5531 139 }
ehelin@5531 140
ehelin@5531 141 private static <T extends Comparable<T>> void expectPass(Assertion assertion, T ... args)
ehelin@5531 142 throws Exception {
ehelin@5531 143 Assertion.run(assertion, args);
ehelin@5531 144 }
ehelin@5531 145
ehelin@5531 146 private static <T extends Comparable<T>> void expectFail(Assertion assertion, T ... args)
ehelin@5531 147 throws Exception {
ehelin@5531 148 try {
ehelin@5531 149 Assertion.run(assertion, args);
ehelin@5531 150 } catch (RuntimeException e) {
ehelin@5531 151 return;
ehelin@5531 152 }
ehelin@5531 153 throw new Exception("Expected " + Assertion.format(assertion, (Object[]) args) +
ehelin@5531 154 " to throw a RuntimeException");
ehelin@5531 155 }
ehelin@5531 156
ehelin@5531 157 }
ehelin@5531 158
ehelin@5531 159 enum Assertion {
ehelin@5531 160 LT, LTE, EQ, GTE, GT, NE, NULL, NOTNULL, FALSE, TRUE;
ehelin@5531 161
ehelin@5531 162 public static <T extends Comparable<T>> void run(Assertion assertion, T ... args) {
ehelin@5531 163 String msg = "Expected " + format(assertion, args) + " to pass";
ehelin@5531 164 switch (assertion) {
ehelin@5531 165 case LT:
ehelin@5531 166 assertLessThan(args[0], args[1], msg);
ehelin@5531 167 break;
ehelin@5531 168 case LTE:
ehelin@5531 169 assertLessThanOrEqual(args[0], args[1], msg);
ehelin@5531 170 break;
ehelin@5531 171 case EQ:
ehelin@5531 172 assertEquals(args[0], args[1], msg);
ehelin@5531 173 break;
ehelin@5531 174 case GTE:
ehelin@5531 175 assertGreaterThanOrEqual(args[0], args[1], msg);
ehelin@5531 176 break;
ehelin@5531 177 case GT:
ehelin@5531 178 assertGreaterThan(args[0], args[1], msg);
ehelin@5531 179 break;
ehelin@5531 180 case NE:
ehelin@5531 181 assertNotEquals(args[0], args[1], msg);
ehelin@5531 182 break;
ehelin@5531 183 case NULL:
ehelin@5531 184 assertNull(args == null ? args : args[0], msg);
ehelin@5531 185 break;
ehelin@5531 186 case NOTNULL:
ehelin@5531 187 assertNotNull(args == null ? args : args[0], msg);
ehelin@5531 188 break;
ehelin@5531 189 case FALSE:
ehelin@5531 190 assertFalse((Boolean) args[0], msg);
ehelin@5531 191 break;
ehelin@5531 192 case TRUE:
ehelin@5531 193 assertTrue((Boolean) args[0], msg);
ehelin@5531 194 break;
ehelin@5531 195 default:
ehelin@5531 196 // do nothing
ehelin@5531 197 }
ehelin@5531 198 }
ehelin@5531 199
ehelin@5531 200 public static String format(Assertion assertion, Object ... args) {
ehelin@5531 201 switch (assertion) {
ehelin@5531 202 case LT:
ehelin@5531 203 return asString("assertLessThan", args);
ehelin@5531 204 case LTE:
ehelin@5531 205 return asString("assertLessThanOrEqual", args);
ehelin@5531 206 case EQ:
ehelin@5531 207 return asString("assertEquals", args);
ehelin@5531 208 case GTE:
ehelin@5531 209 return asString("assertGreaterThanOrEquals", args);
ehelin@5531 210 case GT:
ehelin@5531 211 return asString("assertGreaterThan", args);
ehelin@5531 212 case NE:
ehelin@5531 213 return asString("assertNotEquals", args);
ehelin@5531 214 case NULL:
ehelin@5531 215 return asString("assertNull", args);
ehelin@5531 216 case NOTNULL:
ehelin@5531 217 return asString("assertNotNull", args);
ehelin@5531 218 case FALSE:
ehelin@5531 219 return asString("assertFalse", args);
ehelin@5531 220 case TRUE:
ehelin@5531 221 return asString("assertTrue", args);
ehelin@5531 222 default:
ehelin@5531 223 return "";
ehelin@5531 224 }
ehelin@5531 225 }
ehelin@5531 226
ehelin@5531 227 private static String asString(String assertion, Object ... args) {
ehelin@5531 228 if (args == null) {
ehelin@5531 229 return String.format("%s(null)", assertion);
ehelin@5531 230 }
ehelin@5531 231 if (args.length == 1) {
ehelin@5531 232 return String.format("%s(%s)", assertion, args[0]);
ehelin@5531 233 } else {
ehelin@5531 234 return String.format("%s(%s, %s)", assertion, args[0], args[1]);
ehelin@5531 235 }
ehelin@5531 236 }
ehelin@5531 237 }

mercurial