Created
October 22, 2017 18:25
-
-
Save ClassCubeGists/4910fa1d5f84c2e7e69ad15b207e8408 to your computer and use it in GitHub Desktop.
JUnit test code used for example on https://moodle.classcube.com
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 java.lang.reflect.Field; | |
import java.util.Arrays; | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
import org.junit.Before; | |
public class ImageGridTest { | |
private int rows; | |
private int cols; | |
private Pixel[] source; | |
private Pixel[][] grid; | |
public ImageGridTest() { | |
} | |
@Before | |
public void setup() throws Exception { | |
randomGrid(); | |
} | |
@Test( timeout = 1000 ) | |
public void testConstructor() throws Exception { | |
ImageGrid g = new ImageGrid( rows, cols, source ); | |
Pixel[][] actual = getField( g, "grid" ); | |
assertArrayEquals( "grid not correctly built in ImageGrid constructor", grid, actual ); | |
} | |
@Test( timeout = 1000 ) | |
public void testCountRedOnly() throws Exception { | |
int redCount = 0; | |
for ( Pixel[] row : grid ) { | |
for ( Pixel cell : row ) { | |
if ( cell.getRed() > 0 && cell.getBlue() == 0 && cell.getGreen() == 0 ) { | |
redCount++; | |
} | |
} | |
} | |
ImageGrid g = new ImageGrid( rows, cols, source ); | |
setField( g, "grid", grid ); | |
int actual = g.countRedOnly(); | |
assertEquals( "countRedOnly() incorrect", redCount, actual ); | |
} | |
@Test( timeout = 1000 ) | |
public void testCountGray() throws Exception { | |
int grayCount = 0; | |
for ( Pixel[] row : grid ) { | |
for ( Pixel cell : row ) { | |
if ( cell.getRed() == cell.getGreen() && cell.getRed() == cell.getBlue() ) { | |
grayCount++; | |
} | |
} | |
} | |
ImageGrid g = new ImageGrid( rows, cols, source ); | |
setField( g, "grid", grid ); | |
int actual = g.countGray(); | |
assertEquals( "countGray() incorrect", grayCount, actual ); | |
} | |
@Test( timeout = 1000 ) | |
public void testNoRed() throws Exception { | |
int noRed = 0; | |
for ( Pixel[] row : grid ) { | |
for ( Pixel cell : row ) { | |
if ( cell.getRed() <= 0 ) { | |
noRed++; | |
} | |
} | |
} | |
ImageGrid g = new ImageGrid( rows, cols, source ); | |
setField( g, "grid", grid ); | |
int actual = g.countNoRed(); | |
assertEquals( "countNoRed() incorrect", noRed, actual ); | |
} | |
/** | |
* Creates a random grid of Pixel elements, with a random number of gray, | |
* red, and not red for testing. | |
* | |
* @return | |
*/ | |
private void randomGrid() throws Exception { | |
int rows = (int) ( Math.random() * 50 ) + 10; | |
int cols = (int) ( Math.random() * 50 ) + 10; | |
this.rows = rows; | |
this.cols = cols; | |
Pixel[] source = new Pixel[ rows * cols ]; | |
// Fill in random Pixels | |
for ( int i = 0; i < source.length; i++ ) { | |
int r = (int) ( Math.random() * 256 ); | |
int g = (int) ( Math.random() * 256 ); | |
int b = (int) ( Math.random() * 256 ); | |
source[ i ] = new Pixel( r, g, b ); | |
setField( source[ i ], "red", r ); | |
setField( source[ i ], "green", g ); | |
setField( source[ i ], "blue", b ); | |
} | |
// Randomly fill in red only, no red, and grays | |
int grayCount = (int) ( Math.random() * 10 ) + 1; | |
int redOnlyCount = (int) ( Math.random() * 10 ) + 1; | |
int noRedCount = (int) ( Math.random() * 10 ) + 1; | |
// Set all 3 values to the same as red | |
for ( int i = 0; i < grayCount; i++ ) { | |
int pos = (int) ( Math.random() * source.length ); | |
int red = getField( source[ pos ], "red" ); | |
setField( source[ pos ], "green", red ); | |
setField( source[ pos ], "blue", red ); | |
} | |
// Set blue and green to 0 for red only | |
for ( int i = 0; i < redOnlyCount; i++ ) { | |
int pos = (int) ( Math.random() * source.length ); | |
setField( source[ pos ], "green", 0 ); | |
setField( source[ pos ], "blue", 0 ); | |
} | |
// Set red to 0 | |
for ( int i = 0; i < noRedCount; i++ ) { | |
int pos = (int) ( Math.random() * source.length ); | |
setField( source[ pos ], "red", 0 ); | |
} | |
this.source = source; | |
// Build the matrix | |
Pixel[][] out = new Pixel[ rows ][ cols ]; | |
int idx = 0; | |
for ( int r = 0; r < out.length; r++ ) { | |
for ( int c = 0; c < out[ r ].length; c++ ) { | |
out[ r ][ c ] = source[ idx++ ]; | |
} | |
} | |
this.grid = out; | |
} | |
private <T> T getField( Object instance, String fieldName ) throws Exception { | |
Field fld = instance.getClass().getDeclaredField( fieldName ); | |
fld.setAccessible( true ); | |
return (T) fld.get( instance ); | |
} | |
private void setField( Object instance, String fieldName, Object value ) throws Exception { | |
Field fld = instance.getClass().getDeclaredField( fieldName ); | |
fld.setAccessible( true ); | |
if ( value instanceof Integer ) { | |
fld.setInt( instance, (int) value ); | |
} else { | |
fld.set( instance, value ); | |
} | |
} | |
} |
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 java.lang.reflect.Field; | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
public class PixelTest { | |
public PixelTest() { | |
} | |
@Test( timeout = 250 ) | |
public void testConstructorValid() throws Exception { | |
Pixel p = new Pixel( 100, 150, 200 ); | |
int red = getField( p, "red" ); | |
int green = getField( p, "green" ); | |
int blue = getField( p, "blue" ); | |
assertEquals( "red instance variable not correctly set in Pixel constructor", 100, red ); | |
assertEquals( "green instance variable not correctly set in Pixel constructor", 150, green ); | |
assertEquals( "blue instance variable not correctly set in Pixel constructor", 200, blue ); | |
} | |
/** | |
* Tests constructor with negative values | |
* | |
* @throws Exception | |
*/ | |
@Test( timeout = 250 ) | |
public void testConstructorBlueNegative() throws Exception { | |
Pixel p = new Pixel( 10, 20, -30 ); | |
int red = getField( p, "red" ); | |
int green = getField( p, "green" ); | |
int blue = getField( p, "blue" ); | |
assertEquals( "red instance variable not correctly set in Pixel constructor with a negative value", 10, red ); | |
assertEquals( "green instance variable not correctly set in Pixel constructor with a negative value", 20, green ); | |
assertEquals( "blue instance variable not correctly set in Pixel constructor with a negative value", 0, blue ); | |
} | |
@Test( timeout = 250 ) | |
public void testConstructorRedGreenNegative() throws Exception { | |
Pixel p = new Pixel( -10, -20, 30 ); | |
int red = getField( p, "red" ); | |
int green = getField( p, "green" ); | |
int blue = getField( p, "blue" ); | |
assertEquals( "red instance variable not correctly set in Pixel constructor with a negative value", 0, red ); | |
assertEquals( "green instance variable not correctly set in Pixel constructor with a negative value", 0, green ); | |
assertEquals( "blue instance variable not correctly set in Pixel constructor with a negative value", 30, blue ); | |
} | |
/** | |
* Tests constructor with negative values | |
* | |
* @throws Exception | |
*/ | |
@Test( timeout = 250 ) | |
public void testConstructorTooLarge() throws Exception { | |
Pixel p = new Pixel( 256, 300, 305 ); | |
int red = getField( p, "red" ); | |
int green = getField( p, "green" ); | |
int blue = getField( p, "blue" ); | |
assertEquals( "red instance variable not correctly set in Pixel constructor with too large a value", 255, red ); | |
assertEquals( "green instance variable not correctly set in Pixel constructor with too large a value", 255, green ); | |
assertEquals( "blue instance variable not correctly set in Pixel constructor with too large a value", 255, blue ); | |
} | |
@Test( timeout = 250 ) | |
public void testGetRed() throws Exception { | |
Pixel p = new Pixel( 0, 0, 0 ); | |
int[] values = { 0, 10, 20, 45, 90, 175, 205, 255 }; | |
for ( int v : values ) { | |
setField( p, "red", v ); | |
assertEquals( "getRed failed", v, p.getRed() ); | |
} | |
} | |
@Test( timeout = 250 ) | |
public void testGetGreen() throws Exception { | |
Pixel p = new Pixel( 0, 0, 0 ); | |
int[] values = { 0, 10, 20, 45, 90, 175, 205, 255 }; | |
for ( int v : values ) { | |
setField( p, "green", v ); | |
assertEquals( "getGreen failed", v, p.getGreen() ); | |
} | |
} | |
@Test( timeout = 250 ) | |
public void testGetBlue() throws Exception { | |
Pixel p = new Pixel( 0, 0, 0 ); | |
int[] values = { 0, 10, 20, 45, 90, 175, 205, 255 }; | |
for ( int v : values ) { | |
setField( p, "blue", v ); | |
assertEquals( "getBlue failed", v, p.getBlue() ); | |
} | |
} | |
@Test( timeout = 250 ) | |
public void testSetRed() throws Exception { | |
Pixel p = new Pixel( 0, 0, 0 ); | |
int[] values = { 0, 50, 150, 200, 255, -1, -10, Integer.MIN_VALUE, 256, 260, 500, Integer.MAX_VALUE }; | |
for ( int v : values ) { | |
p.setRed( v ); | |
int actual = getField( p, "red" ); | |
int exp = v; | |
if ( exp < 0 ) { | |
exp = 0; | |
} else if ( exp > 255 ) { | |
exp = 255; | |
} | |
assertEquals( "red instance variable incorrect after setRed( " + v + " )", exp, actual ); | |
} | |
} | |
@Test( timeout = 250 ) | |
public void testSetGreen() throws Exception { | |
Pixel p = new Pixel( 0, 0, 0 ); | |
int[] values = { 0, 50, 150, 200, 255, -1, -10, Integer.MIN_VALUE, 256, 260, 500, Integer.MAX_VALUE }; | |
for ( int v : values ) { | |
p.setGreen( v ); | |
int actual = getField( p, "green" ); | |
int exp = v; | |
if ( exp < 0 ) { | |
exp = 0; | |
} else if ( exp > 255 ) { | |
exp = 255; | |
} | |
assertEquals( "green instance variable incorrect after setGreen( " + v + " )", exp, actual ); | |
} | |
} | |
@Test( timeout = 250 ) | |
public void testSetBlue() throws Exception { | |
Pixel p = new Pixel( 0, 0, 0 ); | |
int[] values = { 0, 50, 150, 200, 255, -1, -10, Integer.MIN_VALUE, 256, 260, 500, Integer.MAX_VALUE }; | |
for ( int v : values ) { | |
p.setBlue( v ); | |
int actual = getField( p, "blue" ); | |
int exp = v; | |
if ( exp < 0 ) { | |
exp = 0; | |
} else if ( exp > 255 ) { | |
exp = 255; | |
} | |
assertEquals( "blue instance variable incorrect after setBlue( " + v + " )", exp, actual ); | |
} | |
} | |
private <T> T getField( Object instance, String fieldName ) throws Exception { | |
Field fld = instance.getClass().getDeclaredField( fieldName ); | |
fld.setAccessible( true ); | |
return (T) fld.get( instance ); | |
} | |
private void setField( Object instance, String fieldName, Object value ) throws Exception { | |
Field fld = instance.getClass().getDeclaredField( fieldName ); | |
fld.setAccessible( true ); | |
if ( value instanceof Integer ) { | |
fld.setInt( instance, (int) value ); | |
} else { | |
fld.set( instance, value ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment