package cs437.som.neighborhood;

import org.testng.annotations.Test;

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

public class MexicanHatNeighborhoodWidthFunctionTest {
    private static final int ITERATIONS = 1000;
    static final double STANDARD_DEVIATION = 100.0;
    private static final int[] SAMPLES = {0, 1, 10, 100, 200, 500, 1000};
    private static final double[] SAMPLE_RESULTS =
            {0.0867325, 0.0867195, 0.0854369, 0, -0.0352139, -7.75733e-6,
                    -1.65612e-21};
    private static final double[] SAMPLE_ACCURACY =
            {1.0e-7, 1.0e-7, 1.0e-7, 1.0e-8, 1.0e-7, 1.0e-11, 1.0e-26};
    private static final int derivZero = 173; /* .2050807568877293527446341 */

    @Test
    public void testNeighborhoodWidth() throws Exception {
        MexicanHatNeighborhoodWidthFunction gnwf =
                new MexicanHatNeighborhoodWidthFunction(STANDARD_DEVIATION);
        gnwf.setExpectedIterations(ITERATIONS);

        // always decreasing before minimum
        {
            double last = gnwf.neighborhoodWidth(0);
            for (int i = 1; i < derivZero; i++) {
                double current = gnwf.neighborhoodWidth(i);
                assertTrue(current <= last,
                        "Must consistently decrease before minimum.");
                last = current;
            }
        }

        // always increasing after minimum
        {
            double last = gnwf.neighborhoodWidth(derivZero);
            for (int i = derivZero + 1; i < ITERATIONS; i++) {
                double current = gnwf.neighborhoodWidth(i);
                assertTrue(current >= last,
                        "Must consistently increase after minimum. " + i);
                last = current;
            }
        }

        // Verify the function fits known samples
        for (int j = 0; j < SAMPLES.length; j++) {
            assertEquals(gnwf.neighborhoodWidth(SAMPLES[j]), SAMPLE_RESULTS[j],
                    SAMPLE_ACCURACY[j]);
        }
    }
}

@@ -26,16 +26,58 @@
 rhood;%0A%0A
+import org.testng.annotations.BeforeTest;%0A
 import o
@@ -747,14 +747,37 @@
 eriv
-Zero =
+ativeEqualsZero =%0A           
  173
@@ -815,144 +815,118 @@
  */%0A
-%0A
     
-@Test
 %0A    p
-ublic void testNeighborhoodWidth() throws Exception %7B%0A        MexicanHatNeighborhoodWidthFunction gnwf =%0A       
+rivate MexicanHatNeighborhoodWidthFunction gnwf;%0A%0A    @BeforeTest%0A    public void setUp() %7B%0A
         
  new
@@ -921,16 +921,22 @@
         
+gnwf =
  new Mex
@@ -1041,28 +1041,52 @@
 S);%0A
-%0A        // a
+    %7D%0A%0A    @Test%0A    public void testA
 lways
- d
+D
 ecre
@@ -1094,38 +1094,43 @@
 sing
- b
+B
 efore
- m
+M
 inimum
-%0A        %7B%0A    
+() throws Exception %7B%0A
     
@@ -1174,36 +1174,32 @@
 dth(0);%0A        
-    
 for (int i = 1; 
@@ -1203,24 +1203,35 @@
 1; i %3C deriv
+ativeEquals
 Zero; i++) %7B
@@ -1223,36 +1223,32 @@
 alsZero; i++) %7B%0A
-    
             doub
@@ -1291,36 +1291,32 @@
 i);%0A            
-    
 assertTrue(curre
@@ -1339,36 +1339,32 @@
                 
-  
-  
 %22Must consistent
@@ -1398,36 +1398,32 @@
 %22);%0A            
-    
 last = current;%0A
@@ -1434,44 +1434,288 @@
     
+%7D%0A
     %7D%0A
+%0A
     
-    %7D%0A%0A        // always i
+@Test%0A    public void testBySampling() %7B%0A        for (int j = 0; j %3C SAMPLES.length; j++) %7B%0A            assertEquals(gnwf.neighborhoodWidth(SAMPLES%5Bj%5D), SAMPLE_RESULTS%5Bj%5D,%0A                    SAMPLE_ACCURACY%5Bj%5D);%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void testAlwaysI
 ncre
@@ -1723,37 +1723,25 @@
 sing
- a
+A
 fter
- m
+M
 inimum
-%0A        %7B%0A    
+() %7B%0A
     
@@ -1790,19 +1790,26 @@
 eriv
+ativeEquals
 Zero);%0A
-    
     
@@ -1830,16 +1830,27 @@
  = deriv
+ativeEquals
 Zero + 1
@@ -1866,36 +1866,32 @@
 RATIONS; i++) %7B%0A
-    
             doub
@@ -1934,36 +1934,32 @@
 i);%0A            
-    
 assertTrue(curre
@@ -1962,36 +1962,32 @@
 urrent %3E= last,%0A
-    
                 
@@ -2037,28 +2037,24 @@
 um. %22 + i);%0A
-    
             
@@ -2073,255 +2073,8 @@
 nt;%0A
-            %7D%0A        %7D%0A%0A        // Verify the function fits known samples%0A        for (int j = 0; j %3C SAMPLES.length; j++) %7B%0A            assertEquals(gnwf.neighborhoodWidth(SAMPLES%5Bj%5D), SAMPLE_RESULTS%5Bj%5D,%0A                    SAMPLE_ACCURACY%5Bj%5D);%0A
     

Split tests into seperate methods in MexicanHatNeighborhoodWidthFunctionTest.