Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collections/AsLifoQueue.java
38812 views
/*1* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6301085 6192552 636560126* @summary Basic tests for asLifoQueue27* @author Martin Buchholz28*/2930import java.util.*;31import java.util.concurrent.*;3233public class AsLifoQueue {3435private static void realMain(String[] args) throws Throwable {36try {37Deque<String> deq = new ArrayDeque<String>();38check(deq.addAll(Arrays.asList("b", "a", "c")));39equal(deq.toString(), "[b, a, c]");40check(deq.add("d"));41equal(deq.toString(), "[b, a, c, d]");42Queue<String> q = Collections.asLifoQueue(deq);43check(q.add("e"));44equal(deq.toString(),"[e, b, a, c, d]");45} catch (Throwable t) { unexpected(t); }4647// Inspired by an excellent bug report by Jason Mehrens48try {49final Queue<String> q =50Collections.asLifoQueue(new LinkedBlockingDeque<String>(3));51check(q.isEmpty()); equal(q.size(), 0);52check(q.add("a")); check(! q.isEmpty()); equal(q.size(), 1);53check(q.offer("b"));54check(q.add("c"));55equal(q.size(), 3);56check(! q.offer("d"));57equal(q.size(), 3);58THROWS(IllegalStateException.class, () -> q.add("d"));59equal(q.size(), 3);60equal(q.toString(), "[c, b, a]");61equal(q.peek(), "c");62equal(q.element(), "c");63equal(q.remove(), "c");64equal(q.poll(), "b");65equal(q.peek(), "a");66equal(q.remove(), "a");67THROWS(NoSuchElementException.class, () -> q.remove());68equal(q.poll(), null);69check(q.isEmpty());70equal(q.size(), 0);71} catch (Throwable t) { unexpected(t); }72}7374//--------------------- Infrastructure ---------------------------75static volatile int passed = 0, failed = 0;76static void pass() {passed++;}77static void fail() {failed++; Thread.dumpStack();}78static void fail(String msg) {System.out.println(msg); fail();}79static void unexpected(Throwable t) {failed++; t.printStackTrace();}80static void check(boolean cond) {if (cond) pass(); else fail();}81static void equal(Object x, Object y) {82if (x == null ? y == null : x.equals(y)) pass();83else fail(x + " not equal to " + y);}84public static void main(String[] args) throws Throwable {85try {realMain(args);} catch (Throwable t) {unexpected(t);}86System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);87if (failed > 0) throw new AssertionError("Some tests failed");}88interface Fun {void f() throws Throwable;}89private static void THROWS(Class<? extends Throwable> k, Fun... fs) {90for (Fun f : fs)91try { f.f(); fail("Expected " + k.getName() + " not thrown"); }92catch (Throwable t) {93if (k.isAssignableFrom(t.getClass())) pass();94else unexpected(t);}}95}969798