|  | 
|  | 1 | + | 
|  | 2 | +package net.imagej.ops.filter.addUniformNoise; | 
|  | 3 | + | 
|  | 4 | +import net.imagej.ops.AbstractOpTest; | 
|  | 5 | +import net.imagej.ops.Ops; | 
|  | 6 | +import net.imagej.ops.special.computer.Computers; | 
|  | 7 | +import net.imagej.ops.special.computer.UnaryComputerOp; | 
|  | 8 | +import net.imglib2.type.numeric.integer.UnsignedByteType; | 
|  | 9 | +import net.imglib2.type.numeric.real.DoubleType; | 
|  | 10 | + | 
|  | 11 | +import org.junit.Assert; | 
|  | 12 | +import org.junit.Test; | 
|  | 13 | + | 
|  | 14 | +/** | 
|  | 15 | + * Tests {@link AddUniformNoiseRealType}. | 
|  | 16 | + * | 
|  | 17 | + * @author Gabriel Selzer | 
|  | 18 | + */ | 
|  | 19 | +public class AddUniformNoiseTest extends AbstractOpTest { | 
|  | 20 | + | 
|  | 21 | +	/** | 
|  | 22 | +	 * Regression test for floating point values | 
|  | 23 | +	 */ | 
|  | 24 | +	@Test | 
|  | 25 | +	public void realTypeRegressionTest() { | 
|  | 26 | +		UnaryComputerOp<DoubleType, DoubleType> noiseFunc = Computers.unary(ops, | 
|  | 27 | +			Ops.Filter.AddUniformNoise.class, DoubleType.class, DoubleType.class, 0d, | 
|  | 28 | +			2d); | 
|  | 29 | + | 
|  | 30 | +		double[] actual = new double[9]; | 
|  | 31 | +		DoubleType temp = new DoubleType(); | 
|  | 32 | +		for (int i = 0; i < actual.length; i++) { | 
|  | 33 | +			noiseFunc.compute(new DoubleType(254), temp); | 
|  | 34 | +			actual[i] = temp.getRealDouble(); | 
|  | 35 | +		} | 
|  | 36 | + | 
|  | 37 | +		double[] expected = { 254.10073053454738, 255.88056371733813, | 
|  | 38 | +			255.15987104925694, 255.32176185269049, 254.92408976012726, | 
|  | 39 | +			255.04150474558148, 255.4924837511821, 254.66400205149753, | 
|  | 40 | +			254.45238896293924 }; | 
|  | 41 | +		Assert.assertArrayEquals(expected, actual, 1e-6); | 
|  | 42 | +	} | 
|  | 43 | + | 
|  | 44 | +	/** | 
|  | 45 | +	 * Ensures that the Op wraps correctly to the minimum of the data type when | 
|  | 46 | +	 * clampOutput is set to false | 
|  | 47 | +	 */ | 
|  | 48 | +	@Test | 
|  | 49 | +	public void wrappingUpperEndRegressionTest() { | 
|  | 50 | +		UnaryComputerOp<UnsignedByteType, UnsignedByteType> noiseFunc = Computers | 
|  | 51 | +			.unary(ops, Ops.Filter.AddUniformNoise.class, UnsignedByteType.class, | 
|  | 52 | +				UnsignedByteType.class, 0l, 3l, false); | 
|  | 53 | + | 
|  | 54 | +		int[] actual = new int[9]; | 
|  | 55 | +		UnsignedByteType temp = new UnsignedByteType(); | 
|  | 56 | +		for (int i = 0; i < actual.length; i++) { | 
|  | 57 | +			noiseFunc.compute(new UnsignedByteType(254), temp); | 
|  | 58 | +			actual[i] = temp.get(); | 
|  | 59 | +		} | 
|  | 60 | + | 
|  | 61 | +		int[] expected = { 0, 0, 1, 0, 255, 254, 0, 0, 0 }; | 
|  | 62 | +		Assert.assertArrayEquals(expected, actual); | 
|  | 63 | +	} | 
|  | 64 | + | 
|  | 65 | +	/** | 
|  | 66 | +	 * Ensures that the Op wraps correctly to the maximum of the data type when | 
|  | 67 | +	 * clampOutput is set to false | 
|  | 68 | +	 */ | 
|  | 69 | +	@Test | 
|  | 70 | +	public void wrappingLowerEndRegressionTest() { | 
|  | 71 | +		UnaryComputerOp<UnsignedByteType, UnsignedByteType> noiseFunc = Computers | 
|  | 72 | +			.unary(ops, Ops.Filter.AddUniformNoise.class, UnsignedByteType.class, | 
|  | 73 | +				UnsignedByteType.class, -3l, 0l, false); | 
|  | 74 | + | 
|  | 75 | +		int[] actual = new int[9]; | 
|  | 76 | +		UnsignedByteType temp = new UnsignedByteType(); | 
|  | 77 | +		for (int i = 0; i < actual.length; i++) { | 
|  | 78 | +			noiseFunc.compute(new UnsignedByteType(0), temp); | 
|  | 79 | +			actual[i] = temp.get(); | 
|  | 80 | +		} | 
|  | 81 | + | 
|  | 82 | +		int[] expected = { 255, 255, 0, 255, 254, 253, 255, 255, 255 }; | 
|  | 83 | +		Assert.assertArrayEquals(expected, actual); | 
|  | 84 | +	} | 
|  | 85 | + | 
|  | 86 | +	/** | 
|  | 87 | +	 * Ensures that the Op clamps to the minimum of the data type then clampOutput | 
|  | 88 | +	 * is set to true | 
|  | 89 | +	 */ | 
|  | 90 | +	@Test | 
|  | 91 | +	public void clampingLowerEndRegressionTest() { | 
|  | 92 | +		UnaryComputerOp<UnsignedByteType, UnsignedByteType> noiseFunc = Computers | 
|  | 93 | +			.unary(ops, Ops.Filter.AddUniformNoise.class, UnsignedByteType.class, | 
|  | 94 | +				UnsignedByteType.class, -3l, 0l); | 
|  | 95 | + | 
|  | 96 | +		int[] actual = new int[9]; | 
|  | 97 | +		UnsignedByteType temp = new UnsignedByteType(); | 
|  | 98 | +		for (int i = 0; i < actual.length; i++) { | 
|  | 99 | +			noiseFunc.compute(new UnsignedByteType(0), temp); | 
|  | 100 | +			actual[i] = temp.get(); | 
|  | 101 | +		} | 
|  | 102 | + | 
|  | 103 | +		int[] expected = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; | 
|  | 104 | +		Assert.assertArrayEquals(expected, actual); | 
|  | 105 | +	} | 
|  | 106 | + | 
|  | 107 | +} | 
0 commit comments