I need help creating a parametized JUnit test case for the following class.
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import static java.lang.Integer.valueOf;
public class IntegerSet {
/**
* The only instance variable in the class. No other variables allowed.
*/
private List list;
public int size() {
return list.size();
}
public boolean isEmpty() {
return list.isEmpty();
}
/**
* Initializes the empty set.
*/
public IntegerSet() {
list = new LinkedList();
}
/**
* Initializes a set with the numbers in the argument.
*/
public IntegerSet(Integer[] integers) {
this();
if (integers == null) {
throw new NullPointerException(\"The argument cannot be null\");
} else {
for (Integer i : integers) {
if (!exists(i)) {
this.insertElement(i);
}
}
}
Collections.sort(list);
}
/**
* Inserts an integer to the set if the integer does not exist in the set
*/
public void insertElement(int i) {
if (!list.contains(i)) {
list.add(i);
}
}
/**
* Inserts to the set all the integers in the argument which do not exist in
* the set.
*/
public void insertAll(Integer[] data) {
if (data == null) {
throw new NullPointerException(\"The argument cannot be null\");
// throw new
// IllegalArgumentException(\"The argument cannot be null\");
} else {
Arrays.sort(data);
for (Integer num : data) {
if (!exists(num)) {
insertElement(num);
}
}
}
}
/**
* Deletes an integer from the set if it exists in the set.
*/
public void deleteElement(int i) {
list.remove(i);
}
/**
* Deletes all the elements in the set.
*/
public void deleteAll() {
list.clear();
}
/**
* Returns true if the argument exists in the set, false otherwise.
*/
public boolean exists(int i) {
return list.contains(i);
}
public String toString() {
char delimiter = \' \';
String str = \"[\";
for (Integer i : list) {
str += i;
str += delimiter;
}
return str + \"]\";
}
/**
* Finds the union of two sets. A null pointer is considered an empty set.
*
* Return \"new IntegerSet()\" when the result is an empty set.
*/
public static IntegerSet union(IntegerSet set1, IntegerSet set2) {
IntegerSet union = new IntegerSet();
if (set1.isEmpty() && set2.isEmpty()) {
return union;
}
if (!set1.list.isEmpty()) {
for (Integer num : set1.list) {
if (!union.list.contains(num)) {
union.list.add(num);
}
}
}
if (!set2.isEmpty()) {
for (Integer num : set2.list) {
if (!union.list.contains(num)) {
union.list.add(num);
}
}
}
return union;
}
/**
* Finds the intersection of two sets. A null pointer is considered an empty
* set.
*
* Return \"new IntegerSet()\" when the result is an empty set.
*/
public static IntegerSet intersection(IntegerSet set1, IntegerSet set2) {
IntegerSet intersection = new IntegerSet();
// check for null pointers
if (set1.list.isEmpty() || set2.list.isEmpty()) {
return intersection;
}
for (Integer num : set1.list) {
if ((!intersection.list.contains(num)) && set2.list.contains(num)) {
intersection.list.add(num);
}
}
return intersection;
}
/**
* Builds an array with the ele.
Measures of Dispersion and Variability: Range, QD, AD and SD
Create Parametized JUnit Tests for IntegerSet Class
1. I need help creating a parametized JUnit test case for the following class.
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import static java.lang.Integer.valueOf;
public class IntegerSet {
/**
* The only instance variable in the class. No other variables allowed.
*/
private List list;
public int size() {
return list.size();
}
public boolean isEmpty() {
return list.isEmpty();
}
/**
* Initializes the empty set.
*/
public IntegerSet() {
list = new LinkedList();
}
/**
* Initializes a set with the numbers in the argument.
*/
public IntegerSet(Integer[] integers) {
this();
if (integers == null) {
throw new NullPointerException("The argument cannot be null");
} else {
for (Integer i : integers) {
if (!exists(i)) {
this.insertElement(i);
}
2. }
}
Collections.sort(list);
}
/**
* Inserts an integer to the set if the integer does not exist in the set
*/
public void insertElement(int i) {
if (!list.contains(i)) {
list.add(i);
}
}
/**
* Inserts to the set all the integers in the argument which do not exist in
* the set.
*/
public void insertAll(Integer[] data) {
if (data == null) {
throw new NullPointerException("The argument cannot be null");
// throw new
// IllegalArgumentException("The argument cannot be null");
} else {
Arrays.sort(data);
for (Integer num : data) {
if (!exists(num)) {
insertElement(num);
}
}
}
}
/**
* Deletes an integer from the set if it exists in the set.
*/
public void deleteElement(int i) {
list.remove(i);
}
3. /**
* Deletes all the elements in the set.
*/
public void deleteAll() {
list.clear();
}
/**
* Returns true if the argument exists in the set, false otherwise.
*/
public boolean exists(int i) {
return list.contains(i);
}
public String toString() {
char delimiter = ' ';
String str = "[";
for (Integer i : list) {
str += i;
str += delimiter;
}
return str + "]";
}
/**
* Finds the union of two sets. A null pointer is considered an empty set.
*
* Return "new IntegerSet()" when the result is an empty set.
*/
public static IntegerSet union(IntegerSet set1, IntegerSet set2) {
IntegerSet union = new IntegerSet();
if (set1.isEmpty() && set2.isEmpty()) {
return union;
}
if (!set1.list.isEmpty()) {
for (Integer num : set1.list) {
if (!union.list.contains(num)) {
union.list.add(num);
4. }
}
}
if (!set2.isEmpty()) {
for (Integer num : set2.list) {
if (!union.list.contains(num)) {
union.list.add(num);
}
}
}
return union;
}
/**
* Finds the intersection of two sets. A null pointer is considered an empty
* set.
*
* Return "new IntegerSet()" when the result is an empty set.
*/
public static IntegerSet intersection(IntegerSet set1, IntegerSet set2) {
IntegerSet intersection = new IntegerSet();
// check for null pointers
if (set1.list.isEmpty() || set2.list.isEmpty()) {
return intersection;
}
for (Integer num : set1.list) {
if ((!intersection.list.contains(num)) && set2.list.contains(num)) {
intersection.list.add(num);
}
}
return intersection;
}
/**
* Builds an array with the elements in the set.
*/
public Integer[] toArray() {
5. Integer[] alist;
Object[] s = list.toArray();
alist = new Integer[s.length];
for (int i = 0; i < s.length; i++) {
alist[i] = valueOf((Integer) s[i]);
}
Arrays.sort(alist);
return alist;
}
}
I'm not very good with JUnit. Could I get some help with how I'm supposed to set up a test case
like this?
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class IntegerSetTest {
public IntegerSetTest() {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void test() {
fail("Not yet implemented");
}
@Parameters
6. public void testIntersectionWithNullInput() {
}
@Parameters
public void testExists() {
}
@Parameters
public void testIsEmpty() {
}
@Parameters
public void testUnion() {
}
@Parameters
public void testCreateSetFromArray() {
}
@Parameters
public void testCreateSetFromNull() {
}
@Parameters
public void testDeleteAll() {
}
@Parameters
public void testDeleteEntry(){
}
@Parameters
public void testINsertAll(){
}
@Parameters
public void testAllNull(){
7. }
@Parameters
public void testIntersection(){
}
@Parameters
public void testUnionWithNullInput(){
}
}
Solution
package com.game;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.Parameterized.Parameters;
@FixMethodOrder
public class IntegerSetTest {
public IntegerSetTest() {
}
@Test
public void testUnion() {
Integer[] int1 = {1,2};
IntegerSet set1 = new IntegerSet(int1);
Integer[] int2 = {3,4};
8. IntegerSet set2 = new IntegerSet(int2);
IntegerSet unionResult = new IntegerSet();
IntegerSet set = unionResult.union(set1, set2);
Assert.assertEquals("Union of two integer sets are equal",4, set.size());
}
@Test
public void testCreateSetFromArray() {
Integer[] int1 = {1,2};
IntegerSet set1 = new IntegerSet(int1);
Assert.assertEquals("Successfully created a set from Array",2,set1.size());
}
@Test
public void testCreateSetFromNull() {
try{
IntegerSet set1 = new IntegerSet(null);
}catch(Exception e){
Assert.assertEquals("Successfully got exception error needed","The argument cannot be
null",e.getMessage());
}
}
@Test
public void testDeleteAll() {
Integer[] int1 = {1,2};
IntegerSet set1 = new IntegerSet(int1);
set1.deleteAll();
Assert.assertEquals("Successfully deletes all entries from Set",0,set1.size());
}
@Parameters
9. public void testDeleteEntry(){
}
@Parameters
public void testINsertAll(){
Integer[] int1 = {1,2};
IntegerSet set1 = new IntegerSet();
set1.insertAll(int1);
Assert.assertEquals("Successfully inserts all entries from Set",2,set1.size());
}
}