package jcublas.rng.distribution;

import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.nd4j.linalg.api.buffer.DataBuffer;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;


/**
 * RngKernelTest
 * 
 * @author bam4d
 *
 */
public class RngKernelTest {
	
	double max = 0.4;
	double min = -max;

	@Test
	public void testUniformSample() {
		Nd4j.dtype = DataBuffer.Type.FLOAT;
		for(int x = 0; x<100; x++) {
			INDArray arr = Nd4j.rand(200,200,min,max,Nd4j.getRandom());
			
			// Assert the values are within range
			assertMaxMin(arr);
		}
	}
	
	@Test
	public void testUniformSampleDouble() {
		Nd4j.dtype = DataBuffer.Type.DOUBLE;
		for(int x = 0; x<100; x++) {
			
			INDArray arr = Nd4j.rand(200,200,min,max,Nd4j.getRandom());
			
			// Assert the values are within range
			assertMaxMin(arr);
		}
	}
	
	@Test(expected=IllegalArgumentException.class)
	public void testBadMaxMin() {
		Nd4j.rand(200,200,2,1,Nd4j.getRandom());
	}
	
	private void assertMaxMin(INDArray arr) {
		
		boolean nonZero = false;
		for(Double d: arr.data().asDouble()) {
			assertTrue("Returned value is above the maximum", max > d);
			assertTrue("Returned value is below the minimum", min < d);
			
			if(d != 0.0) {
				nonZero = true;
			}
		}
		
		assertTrue("The entire array is zeros", nonZero);
		
	}
}

@@ -1155,16 +1155,17 @@
 %22, max %3E
+=
  d);%0A%09%09%09
@@ -1219,16 +1219,17 @@
 %22, min %3C
+=
  d);%0A%09%09%09

make test a bit more stable