Created
February 15, 2018 02:47
-
-
Save kclejeune/69348fa6093895d502e42e7221969af7 to your computer and use it in GitHub Desktop.
EECS 233 JUnit equivalent of TestNG unit tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.*; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertTrue; | |
public class boolArrayListJUnit | |
{ | |
/* | |
* Set up a few Objects for use in test fixture | |
*/ | |
int nBits = 4; | |
/* | |
* Initialize input sequences: | |
*/ | |
Boolean[] seq0; | |
Boolean[] seq1; | |
Boolean[] seq2; | |
Boolean[] seq3; | |
@Before | |
public void setUp() throws Exception | |
{ | |
/* | |
* 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 boolArrayList. | |
*/ | |
@Test | |
public void testSize() throws Exception | |
{ | |
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() throws Exception | |
{ | |
System.out.println("insert"); | |
boolArrayList instance0 = new boolArrayList(); | |
boolArrayList instance1 = new boolArrayList(); | |
//boolArrayList instance0 = new boolArrayList(10); | |
//boolArrayList instance1 = new boolArrayList(10); | |
/* | |
* 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 = (boolean) 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 = (boolean) 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() throws Exception | |
{ | |
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() throws Exception | |
{ | |
System.out.println("remove"); | |
boolArrayList instance0 = new boolArrayList(); | |
boolArrayList instance1 = new boolArrayList(); | |
boolArrayList instance2 = new boolArrayList(); | |
boolArrayList instance3 = new boolArrayList(); | |
//boolArrayList instance0 = new boolArrayList(10); | |
//boolArrayList instance1 = new boolArrayList(10); | |
//boolArrayList instance2 = new boolArrayList(10); | |
//boolArrayList instance3 = new boolArrayList(10); | |
/* | |
* 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 - 2; i++) | |
{ | |
result = (boolean) 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 = (boolean) 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 = (boolean) instance2.lookup(i); | |
expResult = seq2[i]; | |
assertEquals(result, expResult); | |
} | |
// Elements following removed item | |
for(int i = toRemove; i < nBits - 1; i++) | |
{ | |
result = (boolean) 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 = (boolean) instance3.lookup(i); | |
expResult = seq3[i]; | |
assertEquals(result, expResult); | |
} | |
} | |
/** | |
* Test of exception for remove method, of class boolArrayList. | |
* N.B. For some reason, the "expected" parameter is not being | |
* recognized. See above comment for testRemoveException. | |
* | |
* 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). | |
* | |
* 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 testRemoveException() throws Exception { | |
// System.out.println("remove exception"); | |
// boolArrayList instance = new boolArrayList(); | |
//boolArrayList instance = new boolArrayList(10); | |
/* | |
* Try removing elements from an empty list... | |
*/ | |
// int toRemove = 0; | |
// boolean result; | |
// try { | |
// instance.remove(toRemove); | |
// result = false; | |
// } catch (IndexOutOfBoundsException e) { | |
// Not going to test message or the like... | |
// result = true; | |
// } | |
// assertTrue(result); | |
/* | |
* Add some elements to instances | |
*/ | |
// for (int i = 0; i < nBits; i++) { | |
// instance.insert(i, seq0[i]); | |
// } | |
/* | |
* Add some elements to instances | |
*/ | |
// for (int i = 0; i < nBits; i++) { | |
// instance.insert(i, seq0[i]);; | |
// } | |
/* | |
* Try removing elements a nonexistent element from the list... | |
*/ | |
// toRemove = -1; | |
// try { | |
// instance.remove(toRemove); | |
// result = true; | |
// } catch (IndexOutOfBoundsException e) { | |
// Not going to test message or the like... | |
// result = false; | |
// } | |
// assertTrue(result); | |
/* | |
* Try removing an (actual) element then removing an element that | |
* no longer "exists," i.e., the last element, which has moved from the | |
* kth to the (k-1)th entry as the overall length of the array has been | |
* decremented. | |
*/ | |
// toRemove = 2; // Remove arbitrary entry | |
// instance.remove(toRemove); | |
// toRemove = nBits - 1; // Remove entry at original end of array | |
// try { | |
// instance.remove(toRemove); | |
// result = false; | |
// } catch (IndexOutOfBoundsException e) { | |
// Not going to test message or the like... | |
// result = true; | |
// } | |
// assertTrue(result); | |
//} | |
/** | |
* Test of lookup method, of class boolArrayList. | |
*/ | |
@Test | |
public void testLookup() throws Exception | |
{ | |
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 = (boolean) 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() throws Exception | |
{ | |
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() throws Exception | |
{ | |
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 | |
*/ | |
boolean expFlagResult; | |
boolean flagResult; | |
expFlagResult = true; | |
//flagResult = instance.negateAll(); | |
instance.negateAll(); | |
/* | |
* Test output | |
*/ | |
//assertEquals(flagResult,expFlagResult); | |
/* | |
* Query individual elements | |
*/ | |
boolean expResult; | |
boolean result; | |
for(int i = 0; i < nBits; i++) | |
{ | |
result = (boolean) 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() throws Exception | |
{ | |
System.out.println("negateAll exception"); | |
boolArrayList instance = new boolArrayList(); | |
//boolArrayList instance = new boolArrayList(10); | |
/* | |
* 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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.*; | |
import static org.junit.Assert.*; | |
public class boolLinkedListJUnit | |
{ | |
@Before | |
public void setUp() throws Exception | |
{ | |
/* | |
* 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}; | |
} | |
/* | |
* Set up a few Objects for use in test fixture | |
*/ | |
int nBits = 4; | |
/* | |
* Initialize input sequences: | |
*/ | |
Boolean[] seq0; | |
Boolean[] seq1; | |
Boolean[] seq2; | |
Boolean[] seq3; | |
/** | |
* 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 = (boolean) 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 = (boolean) 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() throws Exception | |
{ | |
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 = (boolean) 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 = (boolean) 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 = (boolean) instance2.lookup(i); | |
expResult = seq2[i]; | |
assertEquals(result, expResult); | |
} | |
// Elements following removed item | |
for(int i = toRemove; i < nBits - 1; i++) | |
{ | |
result = (boolean) 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 = (boolean) instance3.lookup(i); | |
expResult = seq3[i]; | |
assertEquals(result, expResult); | |
} | |
} | |
// MODIFIED - remove testRemoveException(), since remove() doesn't throw errors as per assignment guidelines | |
/** | |
* Test of exception for remove 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> | |
* 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 testRemoveException() throws Exception | |
{ | |
System.out.println("remove exception"); | |
boolLinkedList instance = new boolLinkedList(); | |
*//* | |
* Try removing elements from an empty list... | |
*//* | |
int toRemove = 0; | |
boolean result; | |
try | |
{ | |
instance.remove(toRemove); | |
result = false; | |
} | |
catch(IndexOutOfBoundsException e) | |
{ | |
// Not going to test message or the like... | |
result = true; | |
} | |
assertTrue(result); | |
*//* | |
* Add some elements to instances | |
*//* | |
for(int i = 0; i < nBits; i++) | |
{ | |
instance.insert(i, seq0[i]); | |
; | |
} | |
*//* | |
* Try removing elements a nonexistent element from the list... | |
*//* | |
toRemove = -1; | |
try | |
{ | |
instance.remove(toRemove); | |
result = false; | |
} | |
catch(IndexOutOfBoundsException e) | |
{ | |
// Not going to test message or the like... | |
result = true; | |
} | |
assertTrue(result); | |
*//* | |
* Try removing an (actual) element then removing an element that | |
* no longer "exists," i.e., the last element, which has moved from the | |
* kth to the (k-1)th entry as the overall length of the array has been | |
* decremented. | |
*//* | |
toRemove = 2; // Remove arbitrary entry | |
instance.remove(toRemove); | |
toRemove = nBits - 1; // Remove entry at original end of array | |
try | |
{ | |
instance.remove(toRemove); | |
result = false; | |
} | |
catch(IndexOutOfBoundsException e) | |
{ | |
// Not going to test message or the like... | |
result = true; | |
} | |
assertTrue(result); | |
}*/ | |
/** | |
* 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 = (boolean) 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() throws Exception | |
{ | |
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 | |
*/ | |
boolean expFlagResult; | |
boolean flagResult; | |
expFlagResult = true; | |
//MODIFIED - instance.negateAll() returns void, not boolean. this won't compile. | |
instance.negateAll(); | |
//flagResult = instance.negateAll(); | |
//flagResult = !expFlagResult; | |
/* | |
* Test output | |
*/ | |
// MODIFIED - this won't work because flagResult can't be assigned. Use for loop below to verify accuracy. | |
// assertEquals(flagResult, expFlagResult); | |
/* | |
* Query individual elements | |
*/ | |
boolean expResult; | |
boolean result; | |
for(int i = 0; i < nBits; i++) | |
{ | |
result = (boolean) 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() throws Exception | |
{ | |
System.out.println("negateAll exception"); | |
boolLinkedList instance = new boolLinkedList(); | |
/* | |
* Try negating elements from an empty list... | |
*/ | |
boolean result; | |
boolean flagResult; | |
try | |
{ | |
//MODIFIED - instance.negateAll() returns void, not boolean - this won't compile | |
//flagResult = instance.negateAll(); | |
instance.negateAll(); | |
result = false; | |
} | |
catch(IllegalArgumentException e) | |
{ | |
// Not going to test message or the like... | |
result = true; | |
} | |
assertTrue(result); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.*; | |
import static org.junit.Assert.assertEquals; | |
public class boolListDemoJUnit | |
{ | |
/* | |
* 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; | |
@Before | |
public void setUp() throws Exception | |
{ | |
/* | |
* 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 boolToSigned method, of class boolListDemo. | |
*/ | |
//public void testBoolToSigned() throws IOException { | |
@Test | |
public void testBoolToSigned() throws Exception | |
{ | |
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(); | |
// Using mis-cased class name | |
//boolList instance0 = new BoolArrayList(); | |
//boolList instance1 = new BoolArrayList(); | |
//boolList instance2 = new BoolArrayList(); | |
//boolList instance3 = new BoolArrayList(); | |
// Using arbitrary constructor (for missing default constructor) | |
//boolList instance0 = new boolArrayList(10); | |
//boolList instance1 = new boolArrayList(10); | |
//boolList instance2 = new boolArrayList(10); | |
//boolList instance3 = new boolArrayList(10); | |
// and using linked lists. | |
boolList instance4 = new boolLinkedList(); | |
boolList instance5 = new boolLinkedList(); | |
boolList instance6 = new boolLinkedList(); | |
boolList instance7 = new boolLinkedList(); | |
// Using mis-cased class name and arbitrary constructor | |
//boolean temp = false; | |
//boolList instance4 = new BoolLinkedList(temp); | |
//boolList instance5 = new BoolLinkedList(temp); | |
//boolList instance6 = new BoolLinkedList(temp); | |
//boolList instance7 = new BoolLinkedList(temp); | |
/* | |
* Without interface | |
*/ | |
// Using array lists... | |
//boolArrayList instance0 = new boolArrayList(); | |
//boolArrayList instance1 = new boolArrayList(); | |
//boolArrayList instance2 = new boolArrayList(); | |
//boolArrayList 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 can't insert entries before 0th element */ | |
for(int i = 0; i < nBits; i++) | |
{ | |
instance0.insert(0, seq0[i]); | |
instance1.insert(0, seq1[i]); | |
instance2.insert(0, seq2[i]); | |
instance3.insert(0, seq3[i]); | |
// | |
instance4.insert(0, seq0[i]); | |
instance5.insert(0, seq1[i]); | |
instance6.insert(0, seq2[i]); | |
instance7.insert(0, seq3[i]); | |
} | |
/* 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; | |
// Array list | |
result = boolListDemo.boolToSigned(instance0); | |
//result = BoolListDemo.boolToSigned(instance0); | |
//result = Demo.boolToSigned(instance0); | |
//assertEquals(result,expResult); | |
// Linked list | |
result = boolListDemo.boolToSigned(instance4); | |
//result = BoolListDemo.boolToSigned(instance4); | |
//result = Demo.boolToSigned(instance4); | |
//assertEquals(result,expResult); | |
// seq1 = (0110)_2 = (6)_10 | |
expResult = 6; | |
// Array list | |
result = boolListDemo.boolToSigned(instance1); | |
//result = BoolListDemo.boolToSigned(instance1); | |
//result = Demo.boolToSigned(instance1); | |
assertEquals(result, expResult); | |
// Linked list | |
result = boolListDemo.boolToSigned(instance5); | |
//result = BoolListDemo.boolToSigned(instance5); | |
//result = Demo.boolToSigned(instance5); | |
assertEquals(result, expResult); | |
// seq2 = (1110)_2 = (-2)_10 | |
expResult = -2; | |
// Array list | |
result = boolListDemo.boolToSigned(instance2); | |
//result = BoolListDemo.boolToSigned(instance2); | |
//result = Demo.boolToSigned(instance2); | |
assertEquals(result, expResult); | |
// Linked list | |
result = boolListDemo.boolToSigned(instance6); | |
//result = BoolListDemo.boolToSigned(instance6); | |
//result = Demo.boolToSigned(instance6); | |
assertEquals(result, expResult); | |
// seq3 = (1010)_2 = (-6)_10 | |
expResult = -6; | |
// Array list | |
result = boolListDemo.boolToSigned(instance3); | |
//result = BoolListDemo.boolToSigned(instance3); | |
//result = Demo.boolToSigned(instance3); | |
assertEquals(result, expResult); | |
// Linked list | |
result = boolListDemo.boolToSigned(instance7); | |
//result = BoolListDemo.boolToSigned(instance7); | |
//result = Demo.boolToSigned(instance7); | |
assertEquals(result, expResult); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment