Set

A collection that contains no duplicate elements.

HashSet

public class HashSetTest {
    private HashSet<String> collection;

    @Before
    public void setUp() throws Exception {
        collection = new HashSet<>();
    }

    @After
    public void tearDown() throws Exception {
        collection.clear();
    }

    @Ignore
    @Test
    public void itNotGuaranteesToTheIterationOrder() throws Exception {
    }

    @Ignore
    @Test
    public void itSortElementsByDefault() throws Exception {
    }

    @Test
    public void itCanNotHaveDuplicateElement() throws Exception {
        // act
        collection.add("fake");
        collection.add("fake");

        // assert
        assertThat("it should have only 1 elements", collection.size(), is(1));
    }

    @Test
    public void itCanHaveNullElement() throws Exception {
        // act
        collection.add(null);

        // assert
        assertThat("it should have 1 elements", collection.size(), is(1));
        for (String element: collection) {
            assertThat("it has null element", element, is(nullValue()));
        }
    }
}

ArraySet

public class ArraySetTest {
    private ArraySet<String> collection;

    @Before
    public void setUp() throws Exception {
        collection = new ArraySet<>();
    }

    @After
    public void tearDown() throws Exception {
        collection.clear();
    }

    @Ignore
    @Test
    public void itHasOrderedElements() throws Exception {
        // arrange
        String[] result = new String[3];

        // act
        collection.add("1");
        collection.add("2");
        collection.add("3");

        // assert
        int index = 0;
        Iterator<String> iterator = collection.iterator();
        do {
            result[index] = iterator.next();
            index++;
        } while (iterator.hasNext());
        assertThat("it order elements to 123", result, arrayContaining("1", "2", "3"));
    }

    @Ignore
    @Test
    public void itSortElementsByDefault() throws Exception {
        // act
        collection.add("B");
        collection.add("A");
        collection.add("C");

        // assert
        String result = "";
        for (String element: collection) {
            result += element;
        }
        assertThat("it sortS elements from BAC to ABC", result, is("ABC"));
    }

    @Test
    public void itCanNotHaveDuplicateElement() throws Exception {
        // act
        collection.add("fake");
        collection.add("fake");

        // assert
        assertThat("it should have only 1 elements", collection.size(), is(1));
    }

    @Test
    public void itCanHaveNullElement() throws Exception {
        // act
        collection.add(null);

        // assert
        assertThat("it should have 1 elements", collection.size(), is(1));
        assertThat("it has null element", collection.valueAt(0), is(nullValue()));
    }
}

TreeSet

public class TreeSetTest {
    private TreeSet<String> collection;

    @Before
    public void setUp() throws Exception {
        collection = new TreeSet<>();
    }

    @After
    public void tearDown() throws Exception {
        collection.clear();
    }

    @Ignore
    @Test
    public void itHasOrderedElements() throws Exception {
        // arrange
        String[] result = new String[3];

        // act
        collection.add("1");
        collection.add("2");
        collection.add("3");

        // assert
        int index = 0;
        Iterator<String> iterator = collection.iterator();
        do {
            result[index] = iterator.next();
            index++;
        } while (iterator.hasNext());
        assertThat("it order elements to 123", result, arrayContaining("1", "2", "3"));
    }

    @Ignore
    @Test
    public void itSortElementsByDefault() throws Exception {
        // act
        collection.add("B");
        collection.add("A");
        collection.add("C");

        // assert
        String result = "";
        for (String element: collection) {
            result += element;
        }
        assertThat("it sortS elements from BAC to ABC", result, is("ABC"));
    }

    @Test
    public void itCanNotHaveDuplicateElement() throws Exception {
        // act
        collection.add("fake");
        collection.add("fake");

        // assert
        assertThat("it should have only 1 elements", collection.size(), is(1));
    }

    @Test(expected = NullPointerException.class)
    public void itCanNotHaveNullElement() throws Exception {
        // act
        collection.add(null);
    }
}

LinkedHashSet

public class LinkedHashSetTest {
    private LinkedHashSet<String> collection;

    @Before
    public void setUp() throws Exception {
        collection = new LinkedHashSet<>();
    }

    @After
    public void tearDown() throws Exception {
        collection.clear();
    }

    @Ignore
    @Test
    public void itNotGuaranteesToTheIterationOrder() throws Exception {
    }

    @Ignore
    @Test
    public void itSortElementsByDefault() throws Exception {
    }

    @Test
    public void itCanNotHaveDuplicateElement() throws Exception {
        // act
        collection.add("fake");
        collection.add("fake");

        // assert
        assertThat("it should have only 1 elements", collection.size(), is(1));
    }

    @Test
    public void itCanHaveNullElement() throws Exception {
        // act
        collection.add(null);

        // assert
        assertThat("it should have 1 elements", collection.size(), is(1));
        for (String element: collection) {
            assertThat("it has null element", element, is(nullValue()));
        }
    }
}

results matching ""

    No results matching ""