package water.cascade;

import org.junit.BeforeClass;
import org.junit.Test;
import water.DKV;
import water.Key;
import water.TestUtil;
import water.fvec.Frame;

public class CascadeTest extends TestUtil {
  @BeforeClass public static void setup() { stall_till_cloudsize(1); }

  @Test public void test1() {
    // Checking `hex + 5`
    String tree = "(+ $a.hex #5)";
    checkTree(tree);
  }

  @Test public void test2() {
    // Checking `hex + 5 + 10`
    String tree = "(+ $a.hex (+ #5 #10))";
    checkTree(tree);
  }

  @Test public void test3() {
    // Checking `hex + 5 - 1 * hex + 15 * (23 / hex)`
    String tree = "(+ (- (+ $a.hex #5) (* #1 $a.hex)) (* #15 (/ #23 $a.hex)))";
    checkTree(tree);
  }

  @Test public void test4() {
    //Checking `hex == 5`, <=, >=, <, >, !=
    String tree = "(n $a.hex #5)";
    checkTree(tree);
    tree = "(L $a.hex #5)";
    checkTree(tree);
    tree = "(G $a.hex #1.25132)";
    checkTree(tree);
    tree = "(g $a.hex #112.341e-5)";
    checkTree(tree);
    tree = "(l $a.hex #0.0123)";
    checkTree(tree);
    tree = "(N $a.hex #0)";
    checkTree(tree);
//    tree = "(n $a.hex \"hello\")";
    checkTree(tree);
  }

  @Test public void test5() {
    // Checking `hex && hex`, ||, &, |
    String tree = "(&& $a.hex $a.hex)";
    checkTree(tree);
    tree = "(|| $a.hex $a.hex)";
    checkTree(tree);
    tree = "(& $a.hex $a.hex)";
    checkTree(tree);
    tree = "(| $a.hex $a.hex)";
    checkTree(tree);
  }

  private static void checkTree(String tree) {
    Frame r = frame(new double[]{-1,1,2,3,4,5,6,254});
    Frame fr = new Frame(Key.make("a.hex"), null, r.vecs());
    DKV.put(fr._key, fr);
    Env env = Exec.exec(tree);
    Object result = env.pop();
    if (result instanceof ASTFrame) {
      Frame f2 = ((ASTFrame)result)._fr;
      for (int i = 0; i < f2.numCols(); ++i) System.out.println(f2.vecs()[i].at(0));
      f2.delete();
    }
    if (result instanceof ASTNum) {
      double d = ((ASTNum)result)._d;
      System.out.println(d);
    }
    fr.delete();
    r.delete();
    DKV.remove(Key.make("a.hex"));
  }
}
@@ -1103,18 +1103,16 @@
 (tree);%0A
-//
     tree
@@ -1459,16 +1459,351 @@
 );%0A  %7D%0A%0A
+  @Test public void test6() %7B%0A    // Checking %60hex%5B,1%5D%60%0A    String tree = %22(%5B $a.hex %5C%22null%5C%22 #1)%22;%0A    checkTree(tree);%0A    // Checking %60hex%5B1,5%5D%60%0A    tree = %22(%5B $a.hex #0 #5)%22;%0A    checkTree(tree);%0A    tree = %22(%5B $a.hex %7B(: #0 #4);6;8%7D #5)%22;%0A    checkTree(tree);%0A    tree = %22(%5B $a.hex #0 %7B(: #0 #4);6;7%7D)%22;%0A    checkTree(tree);%0A  %7D%0A%0A
   privat
@@ -1953,24 +1953,56 @@
  r.vecs());%0A
+    fr.vecs()%5B0%5D.rollupStats();%0A
     DKV.put(
@@ -2042,24 +2042,64 @@
 exec(tree);%0A
+    System.out.println(env.toString());%0A
     Object r

update CascadeTest