package water.fvec;

import org.junit.*;

import water.Scope;
import water.TestUtil;

public class EnumWrappedVecTest extends TestUtil {
  @BeforeClass static public void setup() {  stall_till_cloudsize(5); }

  // Need to move test files over
  @Test public void testAdaptTo() {
    Scope.enter();
    Frame v1=null, v2=null;
    try {
      v1 = parse_test_file("smalldata/junit/mixcat_train.csv");
      v2 = parse_test_file("smalldata/junit/mixcat_test.csv");
      EnumWrappedVec vv = v2.vecs()[0].adaptTo(v1.vecs()[0].domain());
      Assert.assertArrayEquals("Mapping differs",new int[]{0,1,3},vv._map);
      Assert.assertArrayEquals("Mapping differs",new String[]{"A","B","C","D"},vv.domain());
      vv.remove();
    } finally {
      if( v1!=null ) v1.delete();
      if( v2!=null ) v2.delete();
      Scope.exit();
    }
  }

  /** Verifies that {@link EnumWrappedVec#computeMap(String[], String[])} returns
   *  correct values. */
  @Test public void testModelMappingCall() {
    Scope.enter();
    testModelMapping(ar("A", "B", "C"), ar("A", "B", "C"), ari(0,1,2));
    testModelMapping(ar("A", "B", "C"), ar(     "B", "C"), ari(  1,2));
    testModelMapping(ar("A", "B", "C"), ar(     "B"     ), ari(  1  ));

    testModelMapping(ar("A", "B", "C"), ar("A", "B", "C", "D"), ari(0,1,2,3));
    testModelMapping(ar("A", "B", "C"), ar(     "B", "C", "D"), ari(  1,2,3));
    testModelMapping(ar("A", "B", "C"), ar(     "B",      "D"), ari(  1,  3));
    Scope.exit();
  }

  private static void testModelMapping(String[] modelDomain, String[] colDomain, int[] expectedMapping) {
    int[] mapping = new EnumWrappedVec(colDomain, modelDomain)._map;
    Assert.assertArrayEquals("Mapping differs",  expectedMapping, mapping);
  }
}

@@ -77,16 +77,152 @@
 estUtil;
+%0Aimport water.util.Log;%0Aimport water.util.PrettyPrint;%0Aimport water.util.RandomUtils;%0A%0Aimport java.util.Arrays;%0Aimport java.util.Random;
 %0A%0Apublic
@@ -336,9 +336,9 @@
 ize(
-5
+1
 ); %7D
@@ -1560,44 +1560,919 @@
  ar(
-     %22B%22,      %22D%22), ari(  1,  3));%0A
+%22B%22, %22D%22), ari(1, 3));%0A    Scope.exit();%0A  %7D%0A%0A  @Ignore @Test public void testPerformance() %7B%0A    for (int N : new int%5B%5D%7B9999,100,1000,10000,20000,40000,80000%7D) %7B%0A      Scope.enter();%0A      String%5B%5D fromDomain = new String%5BN%5D;%0A      String%5B%5D toDomain = new String%5BN%5D;%0A      Random rng = RandomUtils.getRNG(0xDECAF);%0A      for (int i = 0; i %3C N; ++i) %7B%0A        byte%5B%5D b = new byte%5B(int)(Math.log10(N))%5D;%0A        rng.nextBytes(b);%0A        fromDomain%5Bi%5D = new String(b);%0A        rng.nextBytes(b);%0A        toDomain%5Bi%5D = new String(b);%0A      %7D%0A      long start = System.currentTimeMillis();%0A      new EnumWrappedVec(fromDomain, toDomain);%0A      long duration = System.currentTimeMillis() - start;%0A      if (N==9999)%0A        Log.info(%22Warming up.%22);%0A      else%0A        Log.info(%22Time for enum unification of two maps with each %22 + N + %22 factors (only partially overlapping): %22 + PrettyPrint.msecs(duration, true));%0A  
     
@@ -2481,24 +2481,30 @@
 ope.exit();%0A
+    %7D%0A
   %7D%0A%0A  priva

Add performance benchmark for enum test/train mapping, is currently using O(N^2) array copies and hence prohibitively slow for large factor counts.