gt: 		d = DTypeUtils.toShortArray(ldata, idata.length);
context: /*-
   * Copyright (c) 2012, 2016 Diamond Light Source Ltd.
   *
   * All rights reserved. This program and the accompanying materials
   * are made available under the terms of the Eclipse Public License v1.0
   * which accompanies this distribution, and is available at
   * http://www.eclipse.org/legal/epl-v10.html
   */
  
  package org.eclipse.january.dataset;
  
  import static org.junit.Assert.assertArrayEquals;
  import static org.junit.Assert.assertEquals;
  import static org.junit.Assert.assertTrue;
  import static org.junit.Assert.fail;
  
  import java.util.Arrays;
  
  import org.eclipse.january.asserts.TestUtils;
  import org.eclipse.january.dataset.CompoundDataset;
  import org.eclipse.january.dataset.CompoundDoubleDataset;
  import org.eclipse.january.dataset.CompoundIntegerDataset;
  import org.eclipse.january.dataset.DTypeUtils;
  import org.eclipse.january.dataset.Dataset;
  import org.eclipse.january.dataset.DatasetFactory;
  import org.eclipse.january.dataset.DatasetUtils;
  import org.eclipse.january.dataset.IndexIterator;
  import org.eclipse.january.dataset.Maths;
  import org.eclipse.january.dataset.PositionIterator;
  import org.eclipse.january.dataset.RGBDataset;
  import org.eclipse.january.dataset.Random;
  import org.eclipse.january.dataset.Slice;
  import org.junit.Test;
  
  public class AbstractCompoundDatasetTest {
  	long[] ldata = {0, 1, 2, 3, 4, 5};
  	int[] idata = {0, 1, 2, 3, 4, 5};
  	short[] sdata = {0, 1, 2, 3, 4, 5};
  	byte[] bdata = {0, 1, 2, 3, 4, 5};
  	double[] ddata = {0., 1., 2., 3., 4., 5.};
  	float[] fdata = {0.f, 1.f, 2.f, 3.f, 4.f, 5.f};
  
  	@Test
  	public void testSlice() {
  		int isize, size, type;
  		isize = 5;
  		size = 1024;
  		type = Dataset.ARRAYFLOAT64;
  		testSliceND(isize, size, type);
  
  		type = Dataset.COMPLEX128;
  		testSliceND(2, size, type);
  
  		testSliceND(size, Dataset.FLOAT64);
  		testSliceND(size, Dataset.INT16);
  	}
  
  	private void testSliceND(int isize, int size, int type) {
  		// 1D
  		CompoundDataset ta;
  		ta = DatasetFactory.createRange(isize, 0, size, 1, type);
  		testSlicedDataset(ta);
  
  		// 2D
  		ta = DatasetFactory.createRange(isize, 0, size, 1, type).reshape(16, size / 16);
  		System.out.println(" Shape: " + Arrays.toString(ta.getShape()));
  		testSlicedDataset(ta);
  
  		ta = DatasetFactory.createRange(isize, 0, size, 1, type).reshape(size / 32, 32);
  		System.out.println(" Shape: " + Arrays.toString(ta.getShape()));
  		testSlicedDataset(ta);
  
  		// 3D
  		ta = DatasetFactory.createRange(isize, 0, size, 1, type).reshape(16, 8, size / (16 * 8));
  		System.out.println(" Shape: " + Arrays.toString(ta.getShape()));
  		testSlicedDataset(ta);
  
  		ta = DatasetFactory.createRange(isize, 0, size, 1, type).reshape(size / (16 * 8), 16, 8);
  		System.out.println(" Shape: " + Arrays.toString(ta.getShape()));
  		testSlicedDataset(ta);
  	}
  
  	private void testSliceND(int size, int type) {
  		// 1D
  		CompoundDataset ta;
  		ta = DatasetUtils.createCompoundDatasetFromLastAxis(DatasetFactory.createRange(size, type).reshape(size/16, 16), true);
  		testSlicedDataset(ta);
  		testElementViews(ta);
  
  		// 2D
  		ta = DatasetUtils.createCompoundDatasetFromLastAxis(DatasetFactory.createRange(size, type).reshape(size/(16*8), 16, 8), true);
  		testSlicedDataset(ta);
  		testElementViews(ta);
  
  		// 3D
  		ta = DatasetUtils.createCompoundDatasetFromLastAxis(DatasetFactory.createRange(size, type).reshape(size/(16*8), 8, 8, 2), true);
  		testSlicedDataset(ta);
  		testElementViews(ta);
  	}
  
  	private void testSlicedDataset(CompoundDataset ta) {
  		int[] stop = ta.getShape();
  		stop[stop.length - 1] -= 1;
  		CompoundDataset sa = ta.getSliceView(null, stop, null);
  
  		PositionIterator it = new PositionIterator(sa.getShape());
  		int[] pos = it.getPos();
  		double[] expected = new double[ta.getElementsPerItem()];
  		double[] actual   = new double[expected.length];
  		while (it.hasNext()) {
  			ta.getDoubleArray(actual, pos);
  			sa.getDoubleArray(expected, pos);
  			assertArrayEquals(expected, actual, 1e-14);
  		}
  	}
  
  	private void testElementViews(CompoundDataset ta) {
  		int isize = ta.getElementsPerItem();
  		int e = isize > 1 ? isize - 2 : isize - 1;
  
  		Dataset va = ta.getElementsView(e);
  		assertArrayEquals(ta.getShapeRef(), va.getShapeRef());
  		IndexIterator it = ta.getIterator(true);
  		int[] pos = it.getPos();
  		while (it.hasNext()) {
  			assertEquals(ta.getElementDoubleAbs(it.index + e), va.getDouble(pos), 1e-14);
  		}
  
  		va.setSlice(0);
  		it.reset();
  		while (it.hasNext()) {
  			assertEquals(0, va.getDouble(pos), 1e-14);
  			assertEquals(0, ta.getElementDoubleAbs(it.index + e), 1e-14);
  		}
  	}
  
  	@Test
  	public void testToDoubleArray() {
  		double[] d;
  
  		d = DTypeUtils.toDoubleArray(ddata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toDoubleArray(fdata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toDoubleArray(ldata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toDoubleArray(idata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toDoubleArray(sdata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toDoubleArray(bdata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  	}
  
  	@Test
  	public void testToFloatArray() {
  		float[] d;
  
  		d = DTypeUtils.toFloatArray(ddata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toFloatArray(fdata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toFloatArray(ldata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toFloatArray(idata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toFloatArray(sdata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toFloatArray(bdata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  	}
  
  	@Test
  	public void testToLongArray() {
  		long[] d;
  
  		d = DTypeUtils.toLongArray(ddata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toLongArray(fdata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toLongArray(ldata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toLongArray(idata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toLongArray(sdata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toLongArray(bdata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  	}
  
  	@Test
  	public void testToIntegerArray() {
  		int[] d;
  
  		d = DTypeUtils.toIntegerArray(ddata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toIntegerArray(fdata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toIntegerArray(ldata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toIntegerArray(idata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toIntegerArray(sdata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toIntegerArray(bdata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  	}
  
  	@Test
  	public void testToShortArray() {
  		short[] d;
  
  		d = DTypeUtils.toShortArray(ddata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  		d = DTypeUtils.toShortArray(fdata, idata.length);
  		for (int i = 0; i < idata.length; i++) {
  			assertEquals(ddata[i], d[i], 1e-10);
  		}
  