package com.airbnb.aerosolve.core.function;

import com.airbnb.aerosolve.core.models.NDTreeModel;
import com.airbnb.aerosolve.core.models.NDTreeModelTest;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;

@Slf4j
public class MultiDimensionSplineTest {
  public MultiDimensionSpline getMultiDimensionSpline() {
    NDTreeModel tree = NDTreeModelTest.getNDTreeModel();
    return new MultiDimensionSpline(tree);
  }

  @Test
  public void evaluate() throws Exception {
    MultiDimensionSpline spline = getMultiDimensionSpline();
    eval(spline);
  }

  private static void eval(Function spline) {
    spline.update((float)0.5, (float)0.5, (float)2.0);
    assertEquals(0.125, spline.evaluate((float) 0.5, (float)2.0), 0.0001);

    spline.update((float)0.8, (float)0.6, (float)2.0);
    assertEquals(0.325, spline.evaluate((float)0.5, (float)2.0), 0.0001);

    spline.update((float) 0.8, (float)2.0, (float)1.0);
    assertEquals(0.28868586584843375, spline.evaluate((float)3.0, (float)1.0), 0.0001);

    spline.update((float)0.8, (float)3.0, (float)3.0);
    assertEquals(0.40389338302461303, spline.evaluate((float)3.0, (float)3.0), 0.0001);

  }

  @Test
  public void aggregate() throws Exception {
    List<Function> splineList = new ArrayList<>();
    for (int i = 0; i < 10; ++i) {
      MultiDimensionSpline spline = getMultiDimensionSpline();
      splineList.add(spline);
    }
    Function r = splineList.get(0).aggregate(splineList, 0.1f, 0);
    eval(r);
  }
}
@@ -38,16 +38,62 @@
 ction;%0A%0A
+import com.airbnb.aerosolve.core.ModelRecord;%0A
 import c
@@ -1495,24 +1495,43 @@
 onSpline();%0A
+      set(spline);%0A
       spline
@@ -1544,24 +1544,24 @@
 dd(spline);%0A
-
     %7D%0A    Fu
@@ -1629,14 +1629,632 @@
     
-eval(r
+assertEquals(0.40389338302461303, r.evaluate(3.0f, 3.0f), 0.0001);%0A  %7D%0A%0A  private static void set(Function spline) %7B%0A    spline.update(0.5f, 0.5f, 2.0f);%0A    spline.update(0.8f, 0.6f, 2.0f);%0A    spline.update(0.8f, 2.0f, 1.0f);%0A    spline.update(0.8f, 3.0f, 3.0f);%0A  %7D%0A%0A  @Test%0A  public void modelRecord() %7B%0A    MultiDimensionSpline a = getMultiDimensionSpline();%0A    set(a);%0A    ModelRecord record = a.toModelRecord(%22%22,%22%22);%0A%0A    MultiDimensionSpline b = new MultiDimensionSpline(%0A        new NDTreeModel(record.getNdtreeModel()), record.getWeightMap());%0A%0A    assertEquals(0.40389338302461303, b.evaluate(3.0f, 3.0f), 0.0001
 );%0A 

update unit test for toModelRecord