alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (CollectionAsserts.java)

This example Java source code file (CollectionAsserts.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

arraylist, collectionasserts, comparable, comparator, integer, iterable, iterator, premature, set, string, stringbuilder, suppresswarnings, unexpected, util

The CollectionAsserts.java Java example source code

/*
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

/**
 * @library
 * CollectionAssert -- assertion methods for lambda test cases
 */
public class CollectionAsserts {

    private CollectionAsserts() {
        // no instances
    }

    public static void assertCountSum(Iterable<? super Integer> it, int count, int sum) {
        assertCountSum(it.iterator(), count, sum);
    }

    public static void assertCountSum(Iterator<? super Integer> it, int count, int sum) {
        int c = 0;
        int s = 0;
        while (it.hasNext()) {
            int i = (Integer) it.next();
            c++;
            s += i;
        }

        assertEquals(c, count);
        assertEquals(s, sum);
    }

    public static void assertConcat(Iterator<Character> it, String result) {
        StringBuilder sb = new StringBuilder();
        while (it.hasNext()) {
            sb.append(it.next());
        }

        assertEquals(result, sb.toString());
    }

    public static<T extends Comparable void assertSorted(Iterator i) {
        if (!i.hasNext())
            return;
        T last = i.next();
        while (i.hasNext()) {
            T t = i.next();
            assertTrue(last.compareTo(t) <= 0);
            assertTrue(t.compareTo(last) >= 0);
            last = t;
        }
    }

    public static<T> void assertSorted(Iterator i, Comparator comp) {
        if (!i.hasNext())
            return;
        T last = i.next();
        while (i.hasNext()) {
            T t = i.next();
            assertTrue(comp.compare(last, t) <= 0);
            assertTrue(comp.compare(t, last) >= 0);
            last = t;
        }
    }

    public static<T extends Comparable void assertSorted(Iterable iter) {
        assertSorted(iter.iterator());
    }

    public static<T> void assertSorted(Iterable iter, Comparator comp) {
        assertSorted(iter.iterator(), comp);
    }

    public static <T> void assertUnique(Iterable iter) {
        assertUnique(iter.iterator());
    }

    public static<T> void assertUnique(Iterator iter) {
        if (!iter.hasNext()) {
            return;
        }

        Set<T> uniq = new HashSet<>();
        while(iter.hasNext()) {
            T each = iter.next();
            assertTrue(!uniq.contains(each));
            uniq.add(each);
        }
    }

    public static<T> void assertContents(Iterable actual, Iterable expected) {
        assertContents(actual, expected, null);
    }

    public static<T> void assertContents(Iterable actual, Iterable expected, String msg) {
        assertContents(actual.iterator(), expected.iterator(), msg);
    }

    public static<T> void assertContents(Iterator actual, Iterator expected) {
        assertContents(actual, expected, null);
    }

    public static<T> void assertContents(Iterator actual, Iterator expected, String msg) {
        List<T> history = new ArrayList<>();

        while (expected.hasNext()) {
            if (!actual.hasNext()) {
                List<T> expectedData = new ArrayList<>(history);
                while (expected.hasNext())
                    expectedData.add(expected.next());
                fail(String.format("%s Premature end of data; expected=%s, found=%s",
                    (msg == null ? "" : msg), expectedData, history));
            }
            T a = actual.next();
            T e = expected.next();
            history.add(a);

            if (!Objects.equals(a, e))
                fail(String.format("%s Data mismatch; preceding=%s, nextExpected=%s, nextFound=%s",
                    (msg == null ? "" : msg), history, e, a));
        }
        if (actual.hasNext()) {
            List<T> rest = new ArrayList<>();
            while (actual.hasNext())
                rest.add(actual.next());
            fail(String.format("%s Unexpected data %s after %s",
                (msg == null ? "" : msg), rest, history));
        }
    }

    @SafeVarargs
    @SuppressWarnings("varargs")
    public static<T> void assertContents(Iterator actual, T... expected) {
        assertContents(actual, Arrays.asList(expected).iterator());
    }

    public static<T extends Comparable void assertContentsUnordered(Iterable actual, Iterable expected) {
        assertContentsUnordered(actual, expected, null);
    }

    public static<T extends Comparable void assertContentsUnordered(Iterable actual, Iterable expected, String msg) {
        List<T> allExpected = new ArrayList<>();
        for (T t : expected) {
            allExpected.add(t);
        }

        for (T t : actual) {
            assertTrue(allExpected.remove(t), msg + " element '" + String.valueOf(t) + "' not found");
        }

        assertTrue(allExpected.isEmpty(), msg + "expected contained additional elements");
    }

    static <T> void assertSplitContents(Iterable> splits, Iterable list) {
        Iterator<Iterable mI = splits.iterator();
        Iterator<T> pI = null;
        Iterator<T> lI = list.iterator();

        while (lI.hasNext()) {
            if (pI == null)
                pI = mI.next().iterator();
            while (!pI.hasNext()) {
                if (!mI.hasNext()) {
                    break;
                }
                else {
                    pI = mI.next().iterator();
                }
            }
            assertTrue(pI.hasNext());
            T pT = pI.next();
            T lT = lI.next();
            assertEquals(pT, lT);
        }

        if (pI != null) {
            assertTrue(!pI.hasNext());
        }

        while(mI.hasNext()) {
            pI = mI.next().iterator();
            assertTrue(!pI.hasNext());
        }
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java CollectionAsserts.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.