package net.imagej.convert;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;

import net.imglib2.type.numeric.IntegerType;
import net.imglib2.type.numeric.integer.*;
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.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.scijava.Context;
import org.scijava.convert.ConvertService;

import net.imglib2.type.numeric.NumericType;
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.type.numeric.real.FloatType;
import net.imglib2.type.operators.ValueEquals;

@RunWith(Parameterized.class)
public class NumberAndTypeConversionTest {
	
	@Parameters(name="Type conversion test {index}: {0} <=> {1}")
	public static Iterable<Object[]> data() {
		return Arrays.asList(new Object[][] {
				{new Double(Double.POSITIVE_INFINITY), new DoubleType(Double.POSITIVE_INFINITY)},
				{new Float(Float.NEGATIVE_INFINITY), new FloatType(Float.NEGATIVE_INFINITY)},
				{new Integer(Integer.MAX_VALUE), new IntType(Integer.MAX_VALUE)},
				{new Long(Long.MAX_VALUE), new LongType(Long.MAX_VALUE)},
				{new Integer(255), new UnsignedByteType(255)},
				// Do one for each RealType
				{new Byte((byte) 0), new ByteType((byte) 0)},
				{new Integer(0), new IntType(0)},
				{new Long(0), new LongType(0l)},
				{new Short((short) 0), new ShortType((short) 0)},
				{new Double(0.), new DoubleType(0.)},
				{new Float(0.), new FloatType(0f)}
			});
	}

	@Parameter(0)
	public Object number;

	@Parameter(1)
	public Object numericType;

	private ConvertService convertService;

	@Before
	public void setUp() {
		final Context ctx = new Context();
		convertService = ctx.getService(ConvertService.class);
	}

	@After
	public void tearDown() {
		convertService.getContext().dispose();
		convertService = null;
	}

	/**
	 * Test conversion from {@link Number} to {@link NumericType}
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Test
	public void testNumberToNumericType() {
		assertTrue("Conversion support from number to numeric type", convertService.supports(number, numericType.getClass()));
		ValueEquals converted = (ValueEquals) convertService.convert(number, numericType.getClass());
		assertTrue("Converted type value equality", converted.valueEquals(numericType));
	}

	/**
	 * Test conversion from {@link Number} to {@link NumericType}
	 */
	@Test
	public void testNumericTypeToNumber() {
		assertTrue("Conversion support from numeric type to number", convertService.supports(numericType, number.getClass()));
		Number converted = (Number) convertService.convert(numericType, number.getClass());
		assertEquals("Converted value equality", number, converted);
	}
}

@@ -141,53 +141,8 @@
 s;%0A%0A
-import net.imglib2.type.numeric.IntegerType;%0A
 impo
@@ -914,27 +914,17 @@
  %7B%0A%09%09%09%09%7B
-new Double(
+ 
 Double.P
@@ -935,25 +935,24 @@
 IVE_INFINITY
-)
 , new Double
@@ -989,26 +989,17 @@
 %7D,%0A%09%09%09%09%7B
-new Float(
+ 
 Float.NE
@@ -1013,17 +1013,16 @@
 INFINITY
-)
 , new Fl
@@ -1061,28 +1061,17 @@
 %7D,%0A%09%09%09%09%7B
-new Integer(
+ 
 Integer.
@@ -1075,25 +1075,24 @@
 er.MAX_VALUE
-)
 , new IntTyp
@@ -1119,25 +1119,17 @@
 %7D,%0A%09%09%09%09%7B
-new Long(
+ 
 Long.MAX
@@ -1134,17 +1134,16 @@
 AX_VALUE
-)
 , new Lo
@@ -1176,24 +1176,12 @@
 %09%09%09%7B
-new Integer(
+ 
 255
-)
 , ne
@@ -1247,124 +1247,366 @@
 %09%09%09%7B
-new Byte((byte) 0), new ByteType((byte) 0)%7D,%0A%09%09%09%09%7Bnew Integer(0), new IntType(0)%7D,%0A%09%09%09%09%7Bnew Long(0), new Long
+ 0, new UnsignedByteType((byte) 0)%7D,%0A%09%09%09%09%7B 0L, new Unsigned2BitType((byte) 0)%7D,%0A%09%09%09%09%7B 0L, new Unsigned4BitType((byte) 0)%7D,%0A%09%09%09%09%7B 0L, new Unsigned12BitType((byte) 0)%7D,%0A%09%09%09%09%7B 0L, new UnsignedIntType((byte) 0)%7D,%0A%09%09%09%09%7B 0, new UnsignedShortType((byte) 0)%7D,%0A%09%09%09%09%7B 0L, new UnsignedLongType((byte) 0)%7D,%0A%09%09%09%09%7B (byte) 0, new ByteType((byte) 0)%7D,%0A%09%09%09%09%7B 0, new Int
 Type(0
-l
 )%7D,%0A
@@ -1614,18 +1614,38 @@
 %09%09%09%7B
-new Short(
+ 0L, new LongType(0L)%7D,%0A%09%09%09%09%7B 
 (sho
@@ -1649,17 +1649,16 @@
 short) 0
-)
 , new Sh
@@ -1687,22 +1687,11 @@
 %09%09%09%7B
-new Double(
+ 
 0.
-)
 , ne
@@ -1718,21 +1718,12 @@
 %09%09%09%7B
-new Float(0.)
+ 0.F
 , ne

Clean up array