0

When running my test file, my testUnion() and testIntersection() are failing. I have included some print statements in each for the purpose of debugging. In each of these methods, set1 and set2 are independent sets. set3 is a set with the desired outcome of the union or intersection of set1 and set2. Finally, set4 is the returned set from calling either the union or intersection operation on set1 and set2.

testUnion() seems to be behaving exactly as expected printing out set3 and set4 to be equal. However, I am still failing the assertEquals(set3, set4); and I'm not sure why. I've also tried doing assertTrue(set3 == set4); and have the same results.

testIntersection()'s set4 is empty. This is because the line "retval.add(element);" under the GenericSet.java is never being executed but I'm not sure why it is not getting past the if statement.

Here is my GenericSet.java file:

import java.util.ArrayList; public class GenericSet<T> { ArrayList<T> list = new ArrayList<>(); public boolean add(T element) { if (list.contains(element)) { return false; } list.add(element); return true; } public boolean contains(T element) { for (T item : list) { if (item.equals(element)) { return true; } } return false; } public int size() { return list.size(); } public GenericSet<T> union(GenericSet<T> set) { GenericSet<T> retval = new GenericSet<>(); for (T element : this.list) { retval.add(element); } for (T element : set.list) { retval.add(element); } return retval; } public GenericSet<T> intersection(GenericSet<T> set) { GenericSet<T> retval = new GenericSet<>(); for (T element : this.list) { if (set.contains(element)) { retval.add(element); } } return retval; } public String toList() { StringBuilder builder = new StringBuilder(); for (T element : list) { builder.append(element).append(", "); } // Remove the trailing ", " if there are elements if (!list.isEmpty()) { builder.delete(builder.length() - 2, builder.length()); } return builder.toString(); } } 

Here is my GenericSetTest.java file which I run coverage as 1 JUnit Test on:

import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; class GenericSetTest { @Test void testAdd() { GenericSet<Integer> set1 = new GenericSet<>(); /* GenericSet<Integer> set2 = new GenericSet<>(); set1.add(1); set2.add(1); System.out.println("testAdd:"); System.out.println("set1: " + set1.toList()); System.out.println("set2: " + set2.toList()); System.out.println(); */ assertTrue(set1.add(1)); //assertTrue(set1.equals(set2)); //set2.add(3); //assertFalse(set1 == set2); } @Test void testContains() { GenericSet<Integer> set = new GenericSet<>(); set.add(1); assertEquals(true, set.contains(1)); assertTrue(set.contains(1)); assertFalse(set.contains(3)); } @Test void testSize() { GenericSet<Integer> set = new GenericSet<>(); assertTrue(set.size() == 0); set.add(1); assertTrue(set.size() == 1); set.add(4); assertTrue(set.size() == 2); } @Test void testUnion() { GenericSet<Integer> set1 = new GenericSet<>(); GenericSet<Integer> set2 = new GenericSet<>(); GenericSet<Integer> set3 = new GenericSet<>(); set1.add(1); set1.add(2); set1.add(3); set2.add(3); set2.add(4); set3.add(1); set3.add(2); set3.add(3); set3.add(4); GenericSet<Integer> set4 = set1.union(set2); System.out.println("testUnion:"); System.out.println("set1: " + set1.toList()); System.out.println("set2: " + set2.toList()); System.out.println("set3: " + set3.toList()); System.out.println("set4: " + set4.toList()); System.out.println(); assertEquals(set3, set4); } @Test void testIntersection() { GenericSet<Integer> set1 = new GenericSet<>(); GenericSet<Integer> set2 = new GenericSet<>(); GenericSet<Integer> set3 = new GenericSet<>(); // set1 = {1, 2} set1.add(1); set1.add(2); // set2 = {3, 3} set2.add(3); set2.add(3); // set3 = {1, 2, 3} set3.add(1); set3.add(2); set3.add(3); // set4 = {1, 2, 3} GenericSet<Integer> set4 = set1.intersection(set2); System.out.println("testIntersection:"); System.out.println("set1: " + set1.toList()); System.out.println("set2: " + set2.toList()); System.out.println("set3: " + set3.toList()); System.out.println("set4: " + set4.toList()); System.out.println(); // Use assertEquals to compare the content of the sets assertEquals(set3, set4); } } 
5
  • 1
    You forgot to implement .equals() and .hashcode() Commented Aug 23, 2023 at 16:36
  • 2
    Does this answer your question? Why are two objects with same data not equal while using equals() method Commented Aug 23, 2023 at 17:18
  • @Sören I don't think I should need those? I don't understand why my testUnion() is not passing as when I print them out set3 and set4 are the same Commented Aug 23, 2023 at 17:29
  • @xampL - The answer from Sören is correct - if you don't implement equals, Java does a very simple check for equality, just checking that the references refer to the same object. Your implementation could be fairly simple as well, relying on comparing the contents of your internal lists, especially if you keep the lists sorted -- or even better, if your class wrapped a Java 'set'? Commented Aug 23, 2023 at 17:48
  • assertEquals() calls .equals() Commented Aug 23, 2023 at 18:55

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.