Skip to content

Instantly share code, notes, and snippets.

@kclejeune
Last active February 13, 2018 03:07
Show Gist options
  • Save kclejeune/6b3518c99731e7ac20f7ecb88ac6f7d5 to your computer and use it in GitHub Desktop.
Save kclejeune/6b3518c99731e7ac20f7ecb88ac6f7d5 to your computer and use it in GitHub Desktop.
Modified testing files for Kolacinski EECS 233
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import org.testng.annotations.*;
import static org.testng.Assert.*;
/**
* @author Rich
*/
public class boolArrayListNGTest
{
/*
* Set up a few Objects for use in test fixture
*/
int nBits = 4;
/*
* Initialize input sequences:
*/
Boolean[] seq0;
Boolean[] seq1;
Boolean[] seq2;
Boolean[] seq3;
public boolArrayListNGTest()
{
}
@BeforeClass
public static void setUpClass()
{
}
@AfterClass
public static void tearDownClass()
{
}
@BeforeMethod
public void setUpMethod()
{
/*
* Set up test fixture...
*/
/*
* Initialize input sequences:
* seq0 = (0010)_2 = (2)_10
* seq1 = (0110)_2 = (6)_10
* seq2 = (1110)_2 = (-2)_10
* seq3 = (1010)_2 = (-6)_10
*/
seq0 = new Boolean[]{false, false, true, false};
seq1 = new Boolean[]{false, true, true, false};
seq2 = new Boolean[]{true, true, true, false};
seq3 = new Boolean[]{true, false, true, false};
}
@AfterMethod
public void tearDownMethod()
{
}
/**
* Test of size method, of class boolArrayList.
*/
@Test
public void testSize()
{
System.out.println("size");
boolArrayList instance = new boolArrayList();
//boolArrayList instance = new boolArrayList(10);
/*
* Query empty object
*/
int expResult = 0;
int result = instance.size();
assertEquals(result, expResult);
/*
* Add some elements to instance
*/
for(int i = 0; i < nBits; i++)
{
instance.insert(i, seq0[i]);
}
/*
* Query size
*/
expResult = nBits;
result = instance.size();
assertEquals(result, expResult);
}
/**
* Test of insert method, of class boolArrayList.
*/
@Test
public void testInsert()
{
System.out.println("insert");
boolArrayList instance0 = new boolArrayList();
boolArrayList instance1 = new boolArrayList();
/*
* Add some elements to instance
*/
for(int i = 0; i < nBits; i++)
{
instance0.insert(i, seq0[i]);
}
/*
* Test encapsulation:
* Next line shoule be illegal...
* comment out as needed.
* N.B. Didn't specify names of object fields so this is not nearly
* general enough to use with everybody's code
*/
//boolean temp = instance0.array[0];
/*
* Query individual elements
*/
boolean expResult;
boolean result;
for(int i = 0; i < nBits; i++)
{
result = instance0.lookup(i);
expResult = seq0[i];
assertEquals(result, expResult);
}
/*
* Check corner case - add element beyond last current element
*/
int offset = 4;
for(int i = 0; i < nBits; i++)
{
instance1.insert(i + offset, seq0[i]);
}
/*
* Query individual elements
*/
for(int i = 0; i < nBits; i++)
{
result = instance1.lookup(i);
expResult = seq0[i];
assertEquals(result, expResult);
}
}
/**
* Test of exception for insert method, of class boolArrayList.
* N.B. For some reason, the "expected" parameter is not being
* recognized. Receiving error:
* cannot find symbol
* symbol: method expected()
* location: @interface Test
* <p>
* Commenting out use of @Test annotation and implementing try/catch
* idiom.... In any case, may have different types of exceptions selected
* (not specified in handout).
* <p>
* Note also, an exception not specified for the remove method in handout
* (though I should have specified one...). This is just for style points.
*/
//@Test(expected = IndexOutOfBoundsException.class)
@Test
public void testInsertException()
{
System.out.println("insert exception");
boolArrayList instance = new boolArrayList();
//boolArrayList instance = new boolArrayList(10);
/*
* Try inserting elements before nonexistent
*/
int target = -1;
boolean toInsert = true;
boolean result;
try
{
instance.insert(target, toInsert);
result = false;
}
catch(IndexOutOfBoundsException e)
{
// Not going to test message or the like...
result = true;
}
assertTrue(result);
/*
* Try removing elements a nonexistent element from the list...
*/
target = -1;
try
{
instance.insert(target, toInsert);
result = false;
}
catch(IndexOutOfBoundsException e)
{
// Not going to test message or the like...
result = true;
}
assertTrue(result);
}
/**
* Test of remove method, of class boolArrayList.
*/
@Test
public void testRemove()
{
System.out.println("remove");
boolArrayList instance0 = new boolArrayList();
boolArrayList instance1 = new boolArrayList();
boolArrayList instance2 = new boolArrayList();
boolArrayList instance3 = new boolArrayList();
/*
* Add some elements to instances
*/
for(int i = 0; i < nBits; i++)
{
instance0.insert(i, seq0[i]);
instance1.insert(i, seq1[i]);
instance2.insert(i, seq2[i]);
instance3.insert(i, seq3[i]);
}
/*
* Remove leading (first) element
*/
int toRemove = 0;
instance0.remove(toRemove);
/*
* Make sure bookeeping done properly...
*/
int expIntResult;
int intResult;
intResult = instance0.size();
expIntResult = nBits - 1;
assertEquals(intResult, expIntResult);
/*
* Query individual elements
*/
boolean expResult;
boolean result;
for(int i = 0; i < nBits - 1; i++)
{
result = instance0.lookup(i);
expResult = seq0[i + 1];
assertEquals(result, expResult);
}
/*
* Remove trailing (last) element
*/
toRemove = nBits - 1;
instance1.remove(toRemove);
/*
* Make sure bookeeping done properly...
*/
intResult = instance1.size();
expIntResult = nBits - 1;
assertEquals(intResult, expIntResult);
/*
* Query individual elements
*/
for(int i = 0; i < nBits - 1; i++)
{
result = instance1.lookup(i);
expResult = seq1[i];
assertEquals(result, expResult);
}
/*
* Remove an arbitrary element
*/
toRemove = 2;
instance2.remove(toRemove);
/*
* Make sure bookeeping done properly...
*/
intResult = instance2.size();
expIntResult = nBits - 1;
assertEquals(intResult, expIntResult);
/*
* Query individual elements
*/
// Elements preceeding removed item
for(int i = 0; i < toRemove; i++)
{
result = instance2.lookup(i);
expResult = seq2[i];
assertEquals(result, expResult);
}
// Elements following removed item
for(int i = toRemove; i < nBits - 1; i++)
{
result = instance2.lookup(i);
expResult = seq2[i + 1];
assertEquals(result, expResult);
}
/*
* Remove non-existent element
*/
toRemove = 6;
instance3.remove(toRemove);
/*
* Make sure bookkeeping done properly...
*/
intResult = instance3.size();
expIntResult = nBits;
assertEquals(intResult, expIntResult);
/*
* Query individual elements
*/
for(int i = 0; i < nBits; i++)
{
result = instance3.lookup(i);
expResult = seq3[i];
assertEquals(result, expResult);
}
}
/**
* Test of lookup method, of class boolArrayList.
*/
@Test
public void testLookup()
{
System.out.println("lookup");
boolArrayList instance = new boolArrayList();
//boolArrayList instance = new boolArrayList(10);
/*
* Add some elements to instances
*/
for(int i = 0; i < nBits; i++)
{
instance.insert(i, seq0[i]);
}
/*
* Query individual elements
*/
boolean expResult;
boolean result;
for(int i = 0; i < nBits; i++)
{
result = instance.lookup(i);
expResult = seq0[i];
assertEquals(result, expResult);
}
}
/**
* Test of exception for lookup method, of class boolArrayList.
* N.B. For some reason, the "expected" parameter is not being
* recognized. See above comment for testRemoveException.
* <p>
* Commenting out use of @Test annotation and implementing try/catch
* idiom.... In any case, may have different types of exceptions selected
* (not specified in handout).
*/
//@Test(expected = IndexOutOfBoundsException.class)
@Test
public void testLookupException()
{
System.out.println("lookup exception");
boolArrayList instance = new boolArrayList();
//boolArrayList instance = new boolArrayList(10);
/*
* Try looking up elements from an empty list...
*/
int toLookup = 0;
boolean result;
try
{
instance.lookup(toLookup);
result = false;
}
catch(IndexOutOfBoundsException e)
{
// Not going to test message or the like...
result = true;
}
assertTrue(result);
/*
* Add some elements to instance
*/
for(int i = 0; i < nBits; i++)
{
instance.insert(i, seq0[i]);
}
/*
* Try looking up elements beyond existing list...
*/
toLookup = -1;
try
{
instance.lookup(toLookup);
result = false;
}
catch(IndexOutOfBoundsException e)
{
// Not going to test message or the like...
result = true;
}
assertTrue(result);
toLookup = nBits;
try
{
instance.lookup(toLookup);
result = false;
}
catch(IndexOutOfBoundsException e)
{
// Not going to test message or the like...
result = true;
}
assertTrue(result);
}
/**
* Test of negateAll method, of class boolArrayList.
*/
@Test
public void testNegateAll()
{
System.out.println("negateAll");
boolArrayList instance = new boolArrayList();
//boolArrayList instance = new boolArrayList(10);
/*
* Add some elements to instances
*/
for(int i = 0; i < nBits; i++)
{
instance.insert(i, seq0[i]);
}
/*
* Negate elements
*/
instance.negateAll();
/*
* Query individual elements
*/
boolean expResult;
boolean result;
for(int i = 0; i < nBits; i++)
{
result = instance.lookup(i);
expResult = !seq0[i]; // Negation of stored result
assertEquals(result, expResult);
}
}
/**
* Test of exception for negateAll method, of class boolArrayList.
* N.B. For some reason, the "expected" parameter is not being
* recognized. See above comment for testRemoveException.
* <p>
* Commenting out use of @Test annotation and implementing try/catch
* idiom.... In any case, may have different types of exceptions selected
* (not specified in handout).
* <p>
* Also set up to return a flag for successful completion, not necessarily
* going to have code that tosses an exception.
*/
//@Test(expected = IllegalArgumentException.class)
@Test
public void testNegateAllException()
{
System.out.println("negateAll exception");
boolArrayList instance = new boolArrayList();
/*
* Try negating elements from an empty list...
*/
boolean result;
boolean flagResult;
try
{
//flagResult = instance.negateAll();
instance.negateAll();
//result = !flagResult;
result = false;
}
catch(IllegalArgumentException e)
{
// Not going to test message or the like...
result = true;
}
assertTrue(result);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import org.testng.annotations.*;
import static org.testng.Assert.*;
/**
* @author Rich
*/
public class boolLinkedListNGTest
{
/*
* Set up a few Objects for use in test fixture
*/
int nBits = 4;
/*
* Initialize input sequences:
*/
Boolean[] seq0;
Boolean[] seq1;
Boolean[] seq2;
Boolean[] seq3;
@BeforeMethod
public void setUpMethod()
{
/*
* Set up test fixture...
*/
/*
* Initialize input sequences:
* seq0 = (0010)_2 = (2)_10
* seq1 = (0110)_2 = (6)_10
* seq2 = (1110)_2 = (-2)_10
* seq3 = (1010)_2 = (-6)_10
*/
seq0 = new Boolean[]{false, false, true, false};
seq1 = new Boolean[]{false, true, true, false};
seq2 = new Boolean[]{true, true, true, false};
seq3 = new Boolean[]{true, false, true, false};
}
/**
* Test of size method, of class boolLinkedList.
*/
@Test
public void testSize()
{
System.out.println("size");
boolLinkedList instance = new boolLinkedList();
/*
* Query empty object
*/
int expResult = 0;
int result = instance.size();
assertEquals(result, expResult);
/*
* Add some elements to instance
*/
for(int i = 0; i < nBits; i++)
{
instance.insert(i, seq0[i]);
}
/*
* Query size
*/
expResult = nBits;
result = instance.size();
assertEquals(result, expResult);
}
/**
* Test of insert method, of class boolLinkedList.
*/
@Test
public void testInsert()
{
System.out.println("insert");
boolLinkedList instance0 = new boolLinkedList();
boolLinkedList instance1 = new boolLinkedList();
/*
* Add some elements to instance
*/
for(int i = 0; i < nBits; i++)
{
instance0.insert(i, seq0[i]);
}
/*
* Query individual elements
*/
boolean expResult;
boolean result;
for(int i = 0; i < nBits; i++)
{
result = instance0.lookup(i);
expResult = seq0[i];
assertEquals(result, expResult);
}
/*
* Check corner case - add element beyond last current element
*/
int offset = 4;
for(int i = 0; i < nBits; i++)
{
instance1.insert(i + offset, seq0[i]);
}
/*
* Query individual elements
*/
for(int i = 0; i < nBits; i++)
{
result = instance1.lookup(i);
expResult = seq0[i];
assertEquals(result, expResult);
}
}
/**
* Test of exception for insert method, of class boolArrayList.
* N.B. For some reason, the "expected" parameter is not being
* recognized. Receiving error:
* cannot find symbol
* symbol: method expected()
* location: @interface Test
* <p>
* Commenting out use of @Test annotation and implementing try/catch
* idiom.... In any case, may have different types of exceptions selected
* (not specified in handout).
* <p>
* Note also, an exception not specified for the remove method in handout
* (though I should have specified one...). This is just for style points.
*/
//@Test(expected = IndexOutOfBoundsException.class)
@Test
public void testInsertException()
{
System.out.println("insert exception");
boolLinkedList instance = new boolLinkedList();
/*
* Try inserting elements before nonexistent
*/
int target = -1;
boolean toInsert = true;
boolean result;
try
{
instance.insert(target, toInsert);
result = false;
}
catch(IndexOutOfBoundsException e)
{
// Not going to test message or the like...
result = true;
}
assertTrue(result);
/*
* Try removing elements a nonexistent element from the list...
*/
target = -1;
try
{
instance.insert(target, toInsert);
result = false;
}
catch(IndexOutOfBoundsException e)
{
// Not going to test message or the like...
result = true;
}
assertTrue(result);
}
/**
* Test of remove method, of class boolLinkedList.
*/
@Test
public void testRemove()
{
System.out.println("remove");
boolLinkedList instance0 = new boolLinkedList();
boolLinkedList instance1 = new boolLinkedList();
boolLinkedList instance2 = new boolLinkedList();
boolLinkedList instance3 = new boolLinkedList();
/*
* Add some elements to instances
*/
for(int i = 0; i < nBits; i++)
{
instance0.insert(i, seq0[i]);
instance1.insert(i, seq1[i]);
instance2.insert(i, seq2[i]);
instance3.insert(i, seq3[i]);
}
/*
* Remove leading (first) element
*/
int toRemove = 0;
instance0.remove(toRemove);
/*
* Make sure bookeeping done properly...
*/
int expIntResult;
int intResult;
intResult = instance0.size();
expIntResult = nBits - 1;
assertEquals(intResult, expIntResult);
/*
* Query individual elements
*/
boolean expResult;
boolean result;
for(int i = 0; i < nBits - 1; i++)
{
result = instance0.lookup(i);
expResult = seq0[i + 1];
assertEquals(result, expResult);
}
/*
* Remove trailing (last) element
*/
toRemove = nBits - 1;
instance1.remove(toRemove);
/*
* Make sure bookeeping done properly...
*/
intResult = instance1.size();
expIntResult = nBits - 1;
assertEquals(intResult, expIntResult);
/*
* Query individual elements
*/
for(int i = 0; i < nBits - 1; i++)
{
result = instance1.lookup(i);
expResult = seq1[i];
assertEquals(result, expResult);
}
/*
* Remove an arbitrary element
*/
toRemove = 2;
instance2.remove(toRemove);
/*
* Make sure bookeeping done properly...
*/
intResult = instance2.size();
expIntResult = nBits - 1;
assertEquals(intResult, expIntResult);
/*
* Query individual elements
*/
// Elements preceeding removed item
for(int i = 0; i < toRemove; i++)
{
result = instance2.lookup(i);
expResult = seq2[i];
assertEquals(result, expResult);
}
// Elements following removed item
for(int i = toRemove; i < nBits - 1; i++)
{
result = instance2.lookup(i);
expResult = seq2[i + 1];
assertEquals(result, expResult);
}
/*
* Remove non-existent element
*/
toRemove = 6;
instance3.remove(toRemove);
/*
* Make sure bookeeping done properly...
*/
intResult = instance3.size();
expIntResult = nBits;
assertEquals(intResult, expIntResult);
/*
* Query individual elements
*/
for(int i = 0; i < nBits; i++)
{
result = instance3.lookup(i);
expResult = seq3[i];
assertEquals(result, expResult);
}
}
/**
* Test of lookup method, of class boolLinkedList.
*/
@Test
public void testLookup()
{
System.out.println("lookup");
boolLinkedList instance = new boolLinkedList();
/*
* Add some elements to instances
*/
for(int i = 0; i < nBits; i++)
{
instance.insert(i, seq0[i]);
}
/*
* Query individual elements
*/
boolean expResult;
boolean result;
for(int i = 0; i < nBits; i++)
{
result = instance.lookup(i);
expResult = seq0[i];
assertEquals(result, expResult);
}
}
/**
* Test of exception for lookup method, of class boolArrayList.
* N.B. For some reason, the "expected" parameter is not being
* recognized. See above comment for testRemoveException.
* <p>
* Commenting out use of @Test annotation and implementing try/catch
* idiom.... In any case, may have different types of exceptions selected
* (not specified in handout).
*/
//@Test(expected = IndexOutOfBoundsException.class)
@Test
public void testLookupException()
{
System.out.println("lookup exception");
boolLinkedList instance = new boolLinkedList();
/*
* Try looking up elements from an empty list...
*/
int toLookup = 0;
boolean result;
try
{
instance.lookup(toLookup);
result = false;
}
catch(IndexOutOfBoundsException e)
{
// Not going to test message or the like...
result = true;
}
assertTrue(result);
/*
* Add some elements to instance
*/
for(int i = 0; i < nBits; i++)
{
instance.insert(i, seq0[i]);
}
/*
* Try looking up elements beyond existing list...
*/
toLookup = -1;
try
{
instance.lookup(toLookup);
result = false;
}
catch(IndexOutOfBoundsException e)
{
// Not going to test message or the like...
result = true;
}
assertTrue(result);
toLookup = nBits;
try
{
instance.lookup(toLookup);
result = false;
}
catch(IndexOutOfBoundsException e)
{
// Not going to test message or the like...
result = true;
}
assertTrue(result);
}
/**
* Test of negateAll method, of class boolLinkedList.
*/
@Test
public void testNegateAll()
{
System.out.println("negateAll");
boolLinkedList instance = new boolLinkedList();
/*
* Add some elements to instances
*/
for(int i = 0; i < nBits; i++)
{
instance.insert(i, seq0[i]);
}
/*
* Negate elements
*/
instance.negateAll();
/*
* Query individual elements
*/
boolean expResult;
boolean result;
for(int i = 0; i < nBits; i++)
{
result = instance.lookup(i);
expResult = !seq0[i]; // Negation of stored result
assertEquals(result, expResult);
}
}
/**
* Test of exception for negateAll method, of class boolArrayList.
* N.B. For some reason, the "expected" parameter is not being
* recognized. See above comment for testRemoveException.
* <p>
* Commenting out use of @Test annotation and implementing try/catch
* idiom.... In any case, may have different types of exceptions selected
* (not specified in handout).
* <p>
* Also set up to return a flag for successful completion, not necessarily
* going to have code that tosses an exception.
*/
//@Test(expected = IllegalArgumentException.class)
@Test
public void testNegateAllException()
{
System.out.println("negateAll exception");
boolLinkedList instance = new boolLinkedList();
/*
* Try negating elements from an empty list...
*/
boolean result;
boolean flagResult;
try
{
instance.negateAll();
result = false;
}
catch(IllegalArgumentException e)
{
// Not going to test message or the like...
result = true;
}
assertTrue(result);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import org.testng.annotations.*;
import static org.testng.Assert.assertEquals;
/**
* @author Rich
*/
public class boolListDemoNGTest
{
/*
* Set up a few Objects for use in test fixture
*/
int nBits = 4;
/*
* Initialize input sequences:
*/
Boolean[] seq0;
Boolean[] seq1;
Boolean[] seq2;
Boolean[] seq3;
/*
* Initialize corresponding test values
*/
private Integer test0;
private Integer test1;
private Integer test2;
private Integer test3;
@BeforeMethod
public void setUpMethod()
{
/*
* Set up test fixture...
*/
/*
* Initialize input sequences:
* seq0 = (0010)_2 = (2)_10
* seq1 = (0110)_2 = (6)_10
* seq2 = (1110)_2 = (-2)_10
* seq3 = (1010)_2 = (-6)_10
*/
seq0 = new Boolean[]{false, false, true, false};
seq1 = new Boolean[]{false, true, true, false};
seq2 = new Boolean[]{true, true, true, false};
seq3 = new Boolean[]{true, false, true, false};
/*
* Initialize test values
*/
test0 = 2;
test1 = 6;
test2 = -2;
test3 = -6;
}
/**
* Test of boolToSigned method, of class boolListDemo.
*/
@Test
//public void testBoolToSigned() throws IOException {
public void testBoolToSigned()
{
System.out.println("boolToSigned");
/*
* With boolList interface
*/
// Using array lists...
boolList instance0 = new boolArrayList();
boolList instance1 = new boolArrayList();
boolList instance2 = new boolArrayList();
boolList instance3 = new boolArrayList();
// and using linked lists.
boolList instance4 = new boolLinkedList();
boolList instance5 = new boolLinkedList();
boolList instance6 = new boolLinkedList();
boolList instance7 = new boolLinkedList();
/*
* Add some elements to instances
* N.B. Storing elements in reverse order so that rightmost element
* of storage array (e.g. seq0,...) corresponds to LSB, which
* should be indexed as 0 in the boolList objects.
*/
/* This code won't work if we can't specify the position of the entry */
int k;
for(int i = 0; i < nBits; i++)
{
k = (nBits - 1) - i;
instance0.insert(i, seq0[k]);
instance1.insert(i, seq1[k]);
instance2.insert(i, seq2[k]);
instance3.insert(i, seq3[k]);
//
instance4.insert(i, seq0[k]);
instance5.insert(i, seq1[k]);
instance6.insert(i, seq2[k]);
instance7.insert(i, seq3[k]);
}
/*
* Check computation of signed integers...
*/
int result;
int expResult;
/*
* seq0 = (0010)_2 = (2)_10
*/
expResult = 2;
System.out.println(instance0);
// Array list
result = boolListDemo.boolToSigned(instance0);
assertEquals(result, expResult);
// Linked list
result = boolListDemo.boolToSigned(instance4);
assertEquals(result, expResult);
// seq1 = (0110)_2 = (6)_10
expResult = 6;
// Array list
result = boolListDemo.boolToSigned(instance1);
assertEquals(result, expResult);
// Linked list
result = boolListDemo.boolToSigned(instance5);
assertEquals(result, expResult);
// seq2 = (1110)_2 = (-2)_10
expResult = -2;
// Array list
result = boolListDemo.boolToSigned(instance2);
assertEquals(result, expResult);
// Linked list
result = boolListDemo.boolToSigned(instance6);
assertEquals(result, expResult);
// seq3 = (1010)_2 = (-6)_10
expResult = -6;
// Array list
result = boolListDemo.boolToSigned(instance3);
assertEquals(result, expResult);
// Linked list
result = boolListDemo.boolToSigned(instance7);
assertEquals(result, expResult);
}
}
@kclejeune
Copy link
Author

updated according to assignment description:

remove() does not throw an exception in the out of bounds case, so testRemoveException() has been removed entirely

@kclejeune
Copy link
Author

added corrected boolLinkedList unit tests

@kclejeune
Copy link
Author

added corrected boolListDemo unit tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment