Perlin { public static double makeNoise(int x, int y, int n, long seed, double min, double max) throws IllegalArgumentException { if (min >= max) throw new IllegalArgumentException(MIN_MAX_EXCEPTION); if (n < 1) throw new IllegalArgumentException(NUM_LAYERS_EXCEPTION); setUpArray(seed); double result = 0.0; for (int i = 0; i < n; i++) { result += (noise((double) x / (100 - i * 100 / n), (double) y / (100 - i * 100 / n)) + 1) * (max - min) / 2; } result = result / n; double multiplier = 0.484689 * Math.pow(n, 0.45) * (max - min) * 2.592 * Math.pow(max - min, -1.003); result = (result - (max - min) / 2) * multiplier + (max - min) / 2 + min; if (result < min) { return min; } if (result > max) { return max; } return result; } private  Perlin(); static double makeNoise(int x, int y, int n, long seed, double min,
            double max);  }
@Test public void makeNoiseTest() { try { Assert.assertTrue("Error in output range.", (0 <= (int) Perlin.makeNoise(1, 2, 3, 4, 0, 255)) && ((int) Perlin.makeNoise(1, 2, 3, 4, 0, 255) <= 255)); Assert.assertTrue("Error in output range.", (64 <= (int) Perlin.makeNoise(1, 2, 3, 4, 64, 128)) && ((int) Perlin.makeNoise(1, 2, 3, 4, 64, 128) <= 128)); Assert.assertTrue("Not repeatable", Perlin.makeNoise(1, 2, 3, 4, 0, 255) == (Perlin.makeNoise(1, 2, 3, 4, 0, 255))); ArrayList<Double> data = new ArrayList<Double>(); for (int i = 0; i < 1000; i++) { data.add(Perlin.makeNoise(i, i + 1000, 5, 1, 0, 255)); } double sum = 0; for (double value : data) { sum += value; } double mean = sum / 1000; Assert.assertTrue("Average not centred", 10 > Math.abs(mean - 128)); sum = 0; for (Double value : data) sum += Math.pow((value - mean), 2); double sd = Math.sqrt(sum / (data.size() - 1)); System.out.println(sd); Assert.assertTrue("Standard deviation too large or small", 15 > Math.abs(sd - (255 - 0) * 0.15)); } catch (Exception e) { Assert.assertFalse("Exception using makeNoise", e instanceof Exception); } }
@Test(expected = Exception.class) public void minMaxErrorTest() throws Exception { Perlin.makeNoise(1, 1, 1, 1, 1, 1); Perlin.makeNoise(1, 1, 1, 1, 2, 1); }
@Test(expected = Exception.class) public void numLayerErrorTest() throws Exception { Perlin.makeNoise(1, 1, 0, 1, 1, 2); Perlin.makeNoise(1, 1, -1, 1, 1, 2); }