dequebash.java

来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 388 行 · 第 1/2 页

JAVA
388
字号
            }
            break;
        case 3:
            if (size() == 0) {
                int result = -1;
                boolean threw = false;
                try {
                    result = ((Integer)deque.removeLast()).intValue();
                } catch(NoSuchElementException e) {
                    threw = true;
                }
                if (!threw)
                    throw new Exception("Remove-no exception: " + result);
            } else { // deque nonempty
                int result = ((random() & 1) == 0?
                              ((Integer)deque.removeLast()).intValue() :
                              ((Integer)deque.pollLast()).intValue());
                if (result != --nextTail)
                    throw new Exception(
                                        "Removed "+ result + " expecting "+(nextTail + 1));
            }
            break;
        default:
            throw new Exception("How'd we get here");
        }
    }


    private static void testEqual(Deque d1, Deque d2)
        throws Exception
    {
        if (d1.size() != d2.size())
            throw new Exception("Size " + d1.size() + " != " + d2.size());
        Iterator it = d2.iterator();
        for(Iterator iitr = d1.iterator(); iitr.hasNext();) {
            int i = ((Integer)iitr.next()).intValue();
            int j = ((Integer)it.next()).intValue();
            if (j != i)
                throw new Exception("Element " + j + " != " + i);
        }

        for(Iterator iitr = d1.iterator(); iitr.hasNext();) {
            int i = ( (Integer) iitr.next()).intValue();
            if (!d2.contains(new Integer(i)))
                throw new Exception("d2 doesn't contain " + i);
        }
        for(Iterator iitr = d2.iterator(); iitr.hasNext();) {
            int i = ( (Integer) iitr.next()).intValue();
            if (!d1.contains(new Integer(i)))
                throw new Exception("d2 doesn't contain " + i);
        }
        if (d1.contains(new Integer(Integer.MIN_VALUE)))
            throw new Exception("d2 contains Integer.MIN_VALUE");
        if (d2.contains(new Integer(Integer.MIN_VALUE)))
            throw new Exception("d2 contains Integer.MIN_VALUE");
        if (d1.contains(null))
            throw new Exception("d1 contains null");
        if (d2.contains(null))
            throw new Exception("d2 contains null");

        if (!d1.containsAll(d2))
            throw new Exception("d1 doesn't contain all of d2");
        if (!d2.containsAll(d1))
            throw new Exception("d2 doesn't contain all of d1");
        Collection c = Collections.singleton(new Integer(Integer.MIN_VALUE));
        if (d1.containsAll(c))
            throw new Exception("d1 contains all of {Integer.MIN_VALUE }");
        if (d2.containsAll(c))
            throw new Exception("d2 contains all of {Integer.MIN_VALUE }");
    }

    private static Object deepCopy(Object o) {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(o);
            oos.flush();
            ByteArrayInputStream bin = new ByteArrayInputStream(
                bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bin);
            return (Object) ois.readObject();
        } catch(Exception e) {
            throw new IllegalArgumentException(e.toString());
        }
    }

    private static void testRemove(Deque deque) throws Exception {
        Deque copy = null;
        switch(random() & 1) {
          case 0:
            copy = (Deque) deque.getClass().
                getConstructor(new Class[] {Collection.class}).
                    newInstance(new Object[] {deque});
            break;
          case 1:
            copy = (Deque)deepCopy(deque);
            break;
          default:
            throw new Exception("How'd we get here");
        }

        int numRemoved = 0;
        for (Iterator it = copy.iterator(); it.hasNext(); ) {
            if ((((Integer)it.next()).intValue() & 1) == 0) {
                it.remove();
                numRemoved++;
            }
        }

        if (copy.size() + numRemoved != size())
            throw new Exception((copy.size() + numRemoved) + " != " + size());
        for (Iterator itr = copy.iterator(); itr.hasNext();) {
            int i = ((Integer)itr.next()).intValue();
            if ((i & 1) == 0)
                throw new Exception("Even number still present: " + i);
        }
        List elements = Arrays.asList(copy.toArray(new Integer[0]));
        Collections.shuffle(elements);
        for (Iterator itr = elements.iterator(); itr.hasNext();) {
            int e = ((Integer)itr.next()).intValue();
            if (!copy.contains(new Integer(e)))
                throw new Exception(e + " missing.");

            boolean removed = false;
            switch(random(3)) {
                case 0:  removed = copy.remove(new Integer(e));                break;
                case 1:  removed = copy.removeFirstOccurrence(new Integer(e)); break;
                case 2:  removed = copy.removeLastOccurrence(new Integer(e));  break;
                default: throw new Exception("How'd we get here");
            }
            if (!removed)
                throw new Exception(e + " not removed.");

            if (copy.contains(new Integer(e)))
                throw new Exception(e + " present after removal.");
        }

        testEmpty(copy);

        copy = (Deque) deque.getClass().
            getConstructor(new Class[] {Collection.class}).
                newInstance(new Object[] {deque});
        copy.retainAll(deque);
        testEqual(deque, copy);
        copy.removeAll(deque);
        testEmpty(copy);
    }

    static boolean checkedThrows;

    private static void testEmpty(Deque deque) throws Exception {
        if (!deque.isEmpty())
            throw new Exception("Deque isn't empty");
        if (deque.size() != 0)
            throw new Exception("Deque size isn't zero");
        if (!(deque.pollFirst() == null))
            throw new Exception("pollFirst lies");
        if (!(deque.poll() == null))
            throw new Exception("poll lies");
        if (!(deque.peekFirst() == null))
            throw new Exception("peekFirst lies");
        if (!(deque.peek() == null))
            throw new Exception("peek lies");
        if (!(deque.pollLast() == null))
            throw new Exception("pollLast lies");
        if (!(deque.peekLast() == null))
            throw new Exception("peekLast lies");

        if (!checkedThrows) {
            checkedThrows = true;
            boolean threw = false;
            int result = 666;
            try {
                result = ((random() & 1) == 0?
                          ((Integer)deque.getFirst()).intValue() :
                          ((Integer)deque.element()).intValue());
            } catch(NoSuchElementException e) {
                threw = true;
            }
            if (!threw)
                throw new Exception("getFirst-no exception: "+result);
            threw = false;
            try {
                result = ((Integer)deque.getLast()).intValue();
            } catch(NoSuchElementException e) {
                threw = true;
            }
            if (!threw)
                throw new Exception("getLast-no exception: "+result);
        }

    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?