/*
 * #%L
 * ImageJ software for multidimensional image processing and analysis.
 * %%
 * Copyright (C) 2014 - 2015 Board of Regents of the University of
 * Wisconsin-Madison, University of Konstanz and Brian Northan.
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * #L%
 */
package net.imagej.ops.copy;

import static org.junit.Assert.assertEquals;

import java.util.Random;

import net.imagej.ops.AbstractOpTest;
import net.imglib2.Cursor;
import net.imglib2.RandomAccess;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.img.Img;
import net.imglib2.img.array.ArrayImgFactory;
import net.imglib2.type.numeric.real.DoubleType;

import org.junit.Before;
import org.junit.Test;

/**
 * Test {@link CopyImg}.
 * 
 * @author Tim-Oliver Buchholz, University of Konstanz
 *
 */
public class CopyImgTest extends AbstractOpTest {
	private Img<DoubleType> input;

	@Before
	public void createData() {
		input = new ArrayImgFactory<DoubleType>().create(
				new int[] { 120, 100 }, new DoubleType());

		final Random r = new Random(System.currentTimeMillis());

		final Cursor<DoubleType> inc = input.cursor();

		while (inc.hasNext()) {
			inc.next().set(r.nextDouble());
		}
	}

	@Test
	public void copyImgNoOutputTest() {
		@SuppressWarnings("unchecked")
		RandomAccessibleInterval<DoubleType> output = (RandomAccessibleInterval<DoubleType>) ops
				.run(CopyImg.class, input);

		Cursor<DoubleType> inc = input.localizingCursor();
		RandomAccess<DoubleType> outRA = output.randomAccess();

		while (inc.hasNext()) {
			inc.fwd();
			outRA.setPosition(inc);
			assertEquals(inc.get().get(), outRA.get().get(), 0.0);
		}
	}

	@Test
	public void copyImgWithOutputTest() {
		Img<DoubleType> output = input.factory().create(input,
				input.firstElement());

		ops.run(CopyImg.class, output, input);

		final Cursor<DoubleType> inc = input.cursor();
		final Cursor<DoubleType> outc = output.cursor();

		while (inc.hasNext()) {
			assertEquals(inc.next().get(), outc.next().get(), 0.0);
		}
	}
}

@@ -2489,16 +2489,128 @@
 est() %7B%0A
+%09%09Img%3CDoubleType%3E inputCopy = input.factory().create(input, input.firstElement());%0A%09%09copy(input, inputCopy);%0A%09%09%0A
 %09%09@Suppr
@@ -2811,16 +2811,80 @@
 rsor();%0A
+%09%09RandomAccess%3CDoubleType%3E inCopyRA = inputCopy.randomAccess();%0A
 %09%09Random
@@ -2974,16 +2974,46 @@
 .fwd();%0A
+%09%09%09inCopyRA.setPosition(inc);%0A
 %09%09%09outRA
@@ -3095,59 +3095,232 @@
 ;%0A%09%09
-%7D%0A%09%7D%0A%0A%09@Test%0A%09public void copyImgWithOutputTest() %7B
+%09assertEquals(inc.get().get(), inCopyRA.get().get(), 0.0);%0A%09%09%7D%0A%09%7D%0A%0A%09@Test%0A%09public void copyImgWithOutputTest() %7B%0A%09%09Img%3CDoubleType%3E inputCopy = input.factory().create(input, input.firstElement());%0A%09%09copy(input, inputCopy);%0A%09%09
 %0A%09%09I
@@ -3492,16 +3492,73 @@
 rsor();%0A
+%09%09final Cursor%3CDoubleType%3E inCopyc = inputCopy.cursor();%0A
 %09%09final 
@@ -3682,21 +3682,316 @@
 et(), 0.0);%0A
+%09%09%09assertEquals(inc.get().get(), inCopyc.next().get(), 0.0);%0A%09%09%7D%0A%09%7D%0A%0A%09private void copy(Img%3CDoubleType%3E from, Img%3CDoubleType%3E to) %7B%0A%09%09final Cursor%3CDoubleType%3E fromc = from.cursor();%0A%09%09final Cursor%3CDoubleType%3E toc = to.cursor();%0A%09%09while (fromc.hasNext()) %7B%0A%09%09%09toc.next().set(fromc.next().get());%0A
 %09%09%7D%0A%09%7D%0A%7D%0A

Check equality between the output and a copy of the input