Author: edwardyoon
Date: Mon Sep 22 08:03:31 2008
New Revision: 697864
URL: http://svn.apache.org/viewvc?rev=697864&view=rev
Log:
Shell parser JUnit test cases
Added:
incubator/hama/trunk/src/test/org/apache/hama/shell/
incubator/hama/trunk/src/test/org/apache/hama/shell/parser/
incubator/hama/trunk/src/test/org/apache/hama/shell/parser/expression/
incubator/hama/trunk/src/test/org/apache/hama/shell/parser/expression/TestHamaExpressionParser.java
Modified:
incubator/hama/trunk/CHANGES.txt
incubator/hama/trunk/src/test/org/apache/hama/HCluster.java
incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java
incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java
Modified: incubator/hama/trunk/CHANGES.txt
URL:
http://svn.apache.org/viewvc/incubator/hama/trunk/CHANGES.txt?rev=697864&r1=697863&r2=697864&view=diff
==============================================================================
--- incubator/hama/trunk/CHANGES.txt (original)
+++ incubator/hama/trunk/CHANGES.txt Mon Sep 22 08:03:31 2008
@@ -25,6 +25,7 @@
IMPROVEMENTS
+ HAMA-68: Shell parser JUnit test cases (samuel via edwardyoon)
HAMA-65: Remove the information of an inactive committers (edwardyoon)
HAMA-58: Remove duplicated code (edwardyoon)
HAMA-56: Add setRow(int row, Vector vector) method to matrix inteface
(edwardyoon)
Modified: incubator/hama/trunk/src/test/org/apache/hama/HCluster.java
URL:
http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/HCluster.java?rev=697864&r1=697863&r2=697864&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/HCluster.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/HCluster.java Mon Sep 22
08:03:31 2008
@@ -30,4 +30,8 @@
public void setUp() throws Exception {
super.setUp();
}
+
+ public HamaConfiguration getConf() {
+ return conf;
+ }
}
Modified: incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java
URL:
http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java?rev=697864&r1=697863&r2=697864&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java
(original)
+++ incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java Mon Sep
22 08:03:31 2008
@@ -1,185 +1,185 @@
-/**
- * Copyright 2007 The Apache Software Foundation
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.hama;
-
-import java.io.IOException;
-import java.util.Iterator;
-
-import junit.extensions.TestSetup;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-import org.apache.hama.io.VectorEntry;
-import org.apache.log4j.Logger;
-
-/**
- * Matrix test
- */
-public class TestDenseMatrix extends TestCase {
- static final Logger LOG = Logger.getLogger(TestDenseMatrix.class);
- private static int SIZE = 10;
- private static Matrix m1;
- private static Matrix m2;
-
- public static Test suite() {
- TestSetup setup = new TestSetup(new TestSuite(TestDenseMatrix.class)) {
- protected void setUp() throws Exception {
- HCluster hCluster = new HCluster();
- hCluster.setUp();
-
- m1 = DenseMatrix.random(hCluster.conf, SIZE, SIZE);
- m2 = DenseMatrix.random(hCluster.conf, SIZE, SIZE);
- }
-
- protected void tearDown() {
- try {
- clearTest();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- };
- return setup;
- }
-
- public static void clearTest() throws IOException {
- m1.clear();
- m2.clear();
- }
-
- /**
- * Column vector test.
- *
- * @param rand
- * @throws IOException
- */
- public void testGetColumn() throws IOException {
- Vector v = m1.getColumn(0);
- Iterator<VectorEntry> it = v.iterator();
- int x = 0;
- while (it.hasNext()) {
- assertEquals(m1.get(x, 0), it.next().getValue());
- x++;
- }
- }
-
- public void testGetSetAttribute() throws IOException {
- m1.setRowAttribute(0, "row1");
- assertEquals(m1.getRowAttribute(0), "row1");
- assertEquals(m1.getRowAttribute(1), null);
-
- m1.setColumnAttribute(0, "column1");
- assertEquals(m1.getColumnAttribute(0), "column1");
- assertEquals(m1.getColumnAttribute(1), null);
- }
-
- /**
- * Test matrices addition
- *
- * @throws IOException
- */
- public void testMatrixAdd() throws IOException {
- Matrix result = m1.add(m2);
-
- assertEquals(result.getRows(), SIZE);
- assertEquals(result.getColumns(), SIZE);
-
- for (int i = 0; i < SIZE; i++) {
- for (int j = 0; j < SIZE; j++) {
- assertEquals(result.get(i, j), m1.get(i, j) + m2.get(i, j));
- }
- }
- }
-
- /**
- * Test matrices multiplication
- *
- * @throws IOException
- */
- public void testMatrixMult() throws IOException {
- Matrix result = m1.mult(m2);
-
- assertEquals(result.getRows(), SIZE);
- assertEquals(result.getColumns(), SIZE);
-
- verifyMultResult(m1, m2, result);
- }
-
- public void testSetRow() throws IOException {
- Vector v = new DenseVector();
- double[] entries = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
-
- for (int i = 0; i < SIZE; i++) {
- v.set(i, entries[i]);
- }
-
- m1.setRow(SIZE + 1, v);
- Iterator<VectorEntry> it = m1.getRow(SIZE + 1).iterator();
-
- // We should remove the timestamp and row attribute from the vector
- int i = 0;
- while (it.hasNext()) {
- assertEquals(entries[i], it.next().getValue());
- i++;
- }
- }
-
- public void testLoadSave() throws IOException {
- m1.save("udanax");
- HCluster hCluster = new HCluster();
- DenseMatrix loadTest = new DenseMatrix(hCluster.conf);
- loadTest.load("udanax");
-
- for (int i = 0; i < loadTest.getRows(); i++) {
- for (int j = 0; j < loadTest.getColumns(); j++) {
- assertEquals(m1.get(i, j), loadTest.get(i, j));
- }
- }
- }
-
- /**
- * Verifying multiplication result
- *
- * @param m1
- * @param m2
- * @param result
- * @throws IOException
- */
- private void verifyMultResult(Matrix m1, Matrix m2, Matrix result)
- throws IOException {
- double[][] C = new double[SIZE][SIZE];
-
- for (int i = 0; i < SIZE; i++) {
- for (int j = 0; j < SIZE; j++) {
- for (int k = 0; k < SIZE; k++) {
- C[i][k] += m1.get(i, j) * m2.get(j, k);
- }
- }
- }
-
- for (int i = 0; i < SIZE; i++) {
- for (int j = 0; j < SIZE; j++) {
- assertEquals(String.valueOf(result.get(i, j)).substring(0, 14),
- String.valueOf(C[i][j]).substring(0, 14));
- }
- }
- }
-}
+/**
+ * Copyright 2007 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama;
+
+import java.io.IOException;
+import java.util.Iterator;
+
+import junit.extensions.TestSetup;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.hama.io.VectorEntry;
+import org.apache.log4j.Logger;
+
+/**
+ * Matrix test
+ */
+public class TestDenseMatrix extends TestCase {
+ static final Logger LOG = Logger.getLogger(TestDenseMatrix.class);
+ private static int SIZE = 10;
+ private static Matrix m1;
+ private static Matrix m2;
+
+ public static Test suite() {
+ TestSetup setup = new TestSetup(new TestSuite(TestDenseMatrix.class)) {
+ protected void setUp() throws Exception {
+ HCluster hCluster = new HCluster();
+ hCluster.setUp();
+
+ m1 = DenseMatrix.random(hCluster.getConf(), SIZE, SIZE);
+ m2 = DenseMatrix.random(hCluster.getConf(), SIZE, SIZE);
+ }
+
+ protected void tearDown() {
+ try {
+ clearTest();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ };
+ return setup;
+ }
+
+ public static void clearTest() throws IOException {
+ m1.clear();
+ m2.clear();
+ }
+
+ /**
+ * Column vector test.
+ *
+ * @param rand
+ * @throws IOException
+ */
+ public void testGetColumn() throws IOException {
+ Vector v = m1.getColumn(0);
+ Iterator<VectorEntry> it = v.iterator();
+ int x = 0;
+ while (it.hasNext()) {
+ assertEquals(m1.get(x, 0), it.next().getValue());
+ x++;
+ }
+ }
+
+ public void testGetSetAttribute() throws IOException {
+ m1.setRowAttribute(0, "row1");
+ assertEquals(m1.getRowAttribute(0), "row1");
+ assertEquals(m1.getRowAttribute(1), null);
+
+ m1.setColumnAttribute(0, "column1");
+ assertEquals(m1.getColumnAttribute(0), "column1");
+ assertEquals(m1.getColumnAttribute(1), null);
+ }
+
+ /**
+ * Test matrices addition
+ *
+ * @throws IOException
+ */
+ public void testMatrixAdd() throws IOException {
+ Matrix result = m1.add(m2);
+
+ assertEquals(result.getRows(), SIZE);
+ assertEquals(result.getColumns(), SIZE);
+
+ for (int i = 0; i < SIZE; i++) {
+ for (int j = 0; j < SIZE; j++) {
+ assertEquals(result.get(i, j), m1.get(i, j) + m2.get(i, j));
+ }
+ }
+ }
+
+ /**
+ * Test matrices multiplication
+ *
+ * @throws IOException
+ */
+ public void testMatrixMult() throws IOException {
+ Matrix result = m1.mult(m2);
+
+ assertEquals(result.getRows(), SIZE);
+ assertEquals(result.getColumns(), SIZE);
+
+ verifyMultResult(m1, m2, result);
+ }
+
+ public void testSetRow() throws IOException {
+ Vector v = new DenseVector();
+ double[] entries = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
+
+ for (int i = 0; i < SIZE; i++) {
+ v.set(i, entries[i]);
+ }
+
+ m1.setRow(SIZE + 1, v);
+ Iterator<VectorEntry> it = m1.getRow(SIZE + 1).iterator();
+
+ // We should remove the timestamp and row attribute from the vector
+ int i = 0;
+ while (it.hasNext()) {
+ assertEquals(entries[i], it.next().getValue());
+ i++;
+ }
+ }
+
+ public void testLoadSave() throws IOException {
+ m1.save("udanax");
+ HCluster hCluster = new HCluster();
+ DenseMatrix loadTest = new DenseMatrix(hCluster.conf);
+ loadTest.load("udanax");
+
+ for (int i = 0; i < loadTest.getRows(); i++) {
+ for (int j = 0; j < loadTest.getColumns(); j++) {
+ assertEquals(m1.get(i, j), loadTest.get(i, j));
+ }
+ }
+ }
+
+ /**
+ * Verifying multiplication result
+ *
+ * @param m1
+ * @param m2
+ * @param result
+ * @throws IOException
+ */
+ private void verifyMultResult(Matrix m1, Matrix m2, Matrix result)
+ throws IOException {
+ double[][] C = new double[SIZE][SIZE];
+
+ for (int i = 0; i < SIZE; i++) {
+ for (int j = 0; j < SIZE; j++) {
+ for (int k = 0; k < SIZE; k++) {
+ C[i][k] += m1.get(i, j) * m2.get(j, k);
+ }
+ }
+ }
+
+ for (int i = 0; i < SIZE; i++) {
+ for (int j = 0; j < SIZE; j++) {
+ assertEquals(String.valueOf(result.get(i, j)).substring(0, 14),
+ String.valueOf(C[i][j]).substring(0, 14));
+ }
+ }
+ }
+}
Modified: incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java
URL:
http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java?rev=697864&r1=697863&r2=697864&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java
(original)
+++ incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java Mon Sep
22 08:03:31 2008
@@ -1,120 +1,120 @@
-/**
- * Copyright 2007 The Apache Software Foundation
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.hama;
-
-import java.util.Iterator;
-
-import junit.extensions.TestSetup;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.hama.io.VectorEntry;
-
-public class TestDenseVector extends TestCase {
- final static Log LOG = LogFactory.getLog(TestDenseVector.class.getName());
- private static final double cosine = 0.6978227007909176;
- private static final double norm1 = 12.0;
- private static final double norm2 = 6.782329983125268;
- private static double[][] values = { { 2, 5, 1, 4 }, { 4, 1, 3, 3 } };
- private static Matrix m1;
- private static Vector v1;
- private static Vector v2;
-
- public static Test suite() {
- TestSetup setup = new TestSetup(new TestSuite(TestDenseVector.class)) {
- protected void setUp() throws Exception {
- HCluster hCluster = new HCluster();
- hCluster.setUp();
-
- m1 = new DenseMatrix(hCluster.conf, "vectorTest");
-
- for (int i = 0; i < 2; i++)
- for (int j = 0; j < 4; j++)
- m1.set(i, j, values[i][j]);
-
- v1 = m1.getRow(0);
- v2 = m1.getRow(1);
- }
-
- protected void tearDown() {
- LOG.info("tearDown()");
- }
- };
- return setup;
- }
-
- /**
- * Test |a| dot |b|
- */
- public void testDot() {
- double cos = v1.dot(v2);
- assertEquals(cos, cosine);
- }
-
- /**
- * Test norm one
- */
- public void testNom1() {
- double result = ((DenseVector) v1).getNorm1();
- assertEquals(norm1, result);
- }
-
- /**
- * Test norm two
- */
- public void testNom2() {
- double result = ((DenseVector) v1).getNorm2();
- assertEquals(norm2, result);
- }
-
- /**
- * Test scaling
- */
- public void scalingTest() {
- v2.scale(0.5);
-
- for (int i = 0; i < v2.size(); i++) {
- assertEquals(values[1][i] * 0.5, v2.get(i));
- }
- }
-
- /**
- * Test get/set methods
- */
- public void testGetSet() {
- assertEquals(v1.get(0), values[0][0]);
- }
-
- /**
- * Test iterator
- */
- public void testIterator() {
- int i = 0;
- Iterator<VectorEntry> it = v1.iterator();
- while (it.hasNext()) {
- VectorEntry c = it.next();
- assertEquals(c.getValue(), values[0][i]);
- i++;
- }
- }
-}
+/**
+ * Copyright 2007 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama;
+
+import java.util.Iterator;
+
+import junit.extensions.TestSetup;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hama.io.VectorEntry;
+
+public class TestDenseVector extends TestCase {
+ final static Log LOG = LogFactory.getLog(TestDenseVector.class.getName());
+ private static final double cosine = 0.6978227007909176;
+ private static final double norm1 = 12.0;
+ private static final double norm2 = 6.782329983125268;
+ private static double[][] values = { { 2, 5, 1, 4 }, { 4, 1, 3, 3 } };
+ private static Matrix m1;
+ private static Vector v1;
+ private static Vector v2;
+
+ public static Test suite() {
+ TestSetup setup = new TestSetup(new TestSuite(TestDenseVector.class)) {
+ protected void setUp() throws Exception {
+ HCluster hCluster = new HCluster();
+ hCluster.setUp();
+
+ m1 = new DenseMatrix(hCluster.getConf(), "vectorTest");
+
+ for (int i = 0; i < 2; i++)
+ for (int j = 0; j < 4; j++)
+ m1.set(i, j, values[i][j]);
+
+ v1 = m1.getRow(0);
+ v2 = m1.getRow(1);
+ }
+
+ protected void tearDown() {
+ LOG.info("tearDown()");
+ }
+ };
+ return setup;
+ }
+
+ /**
+ * Test |a| dot |b|
+ */
+ public void testDot() {
+ double cos = v1.dot(v2);
+ assertEquals(cos, cosine);
+ }
+
+ /**
+ * Test norm one
+ */
+ public void testNom1() {
+ double result = ((DenseVector) v1).getNorm1();
+ assertEquals(norm1, result);
+ }
+
+ /**
+ * Test norm two
+ */
+ public void testNom2() {
+ double result = ((DenseVector) v1).getNorm2();
+ assertEquals(norm2, result);
+ }
+
+ /**
+ * Test scaling
+ */
+ public void scalingTest() {
+ v2.scale(0.5);
+
+ for (int i = 0; i < v2.size(); i++) {
+ assertEquals(values[1][i] * 0.5, v2.get(i));
+ }
+ }
+
+ /**
+ * Test get/set methods
+ */
+ public void testGetSet() {
+ assertEquals(v1.get(0), values[0][0]);
+ }
+
+ /**
+ * Test iterator
+ */
+ public void testIterator() {
+ int i = 0;
+ Iterator<VectorEntry> it = v1.iterator();
+ while (it.hasNext()) {
+ VectorEntry c = it.next();
+ assertEquals(c.getValue(), values[0][i]);
+ i++;
+ }
+ }
+}
Added:
incubator/hama/trunk/src/test/org/apache/hama/shell/parser/expression/TestHamaExpressionParser.java
URL:
http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/shell/parser/expression/TestHamaExpressionParser.java?rev=697864&view=auto
==============================================================================
---
incubator/hama/trunk/src/test/org/apache/hama/shell/parser/expression/TestHamaExpressionParser.java
(added)
+++
incubator/hama/trunk/src/test/org/apache/hama/shell/parser/expression/TestHamaExpressionParser.java
Mon Sep 22 08:03:31 2008
@@ -0,0 +1,182 @@
+package org.apache.hama.shell.parser.expression;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hama.DenseMatrix;
+import org.apache.hama.HCluster;
+import org.apache.hama.HamaConfiguration;
+import org.apache.hama.Matrix;
+import org.apache.hama.shell.HamaShellEnv;
+import org.apache.hama.shell.execution.HamaExpression;
+
+import junit.extensions.TestSetup;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class TestHamaExpressionParser extends TestCase {
+
+ final static Log log = LogFactory.getLog(TestHamaExpressionParser.class);
+
+ // grammar test
+ static final String badExpr1 = "b + c;"; // we only accept evaluation
+ // expression
+ // or save expression
+ static final String badExpr2 = "a = b + c"; // missing ";"
+ static final String badExpr3 = "a = b + (c + d * b ;"; // missing ")"
+ // grammar test and aliases test
+ static final String Expr1 = "a = b;";
+ static final String Expr2 = "a = b + (c+d) * b;";
+ static final String Expr3 = "a = e;";
+ // execution test
+ // A + B : A.rows = B.rows ; A.columns = B.columns ;
+ // A * B : A.columns = B.rows ;
+ static final String Expr4 = "m1 = ( b + c ) * f ;"; // b.columns = c.columns
=
+ // f.rows
+ static final String Expr4_Aliase = "m1";
+
+ static final String Expr5 = "m2 = ( b + c ) * g ;"; // c.columns != f.rows
+ static final String Expr5_Aliase = "m2";
+
+ static final String Expr6 = "m3 = b + f ; "; // b.columns != f.columns
+ static final String Expr6_Aliase = "m3";
+
+ private static int SIZE1 = 10;
+ private static int SIZE2 = 20;
+ private static Matrix B;
+ private static Matrix C;
+ private static Matrix D;
+
+ private static Matrix F;
+ private static Matrix G;
+
+ private static HamaConfiguration hConfig;
+ private static HamaShellEnv env;
+
+ public static Test suite() {
+ TestSetup setup = new TestSetup(new TestSuite(
+ TestHamaExpressionParser.class)) {
+ protected void setUp() throws Exception {
+ HCluster hCluster = new HCluster();
+ hCluster.setUp();
+
+ B = DenseMatrix.random(hCluster.getConf(), SIZE1, SIZE1);
+ C = DenseMatrix.random(hCluster.getConf(), SIZE1, SIZE1);
+ D = DenseMatrix.random(hCluster.getConf(), SIZE1, SIZE1);
+
+ F = DenseMatrix.random(hCluster.getConf(), SIZE1, SIZE2);
+ G = DenseMatrix.random(hCluster.getConf(), SIZE2, SIZE1);
+
+ hConfig = hCluster.getConf();
+ env = new HamaShellEnv();
+
+ setUpShellEnv();
+ }
+
+ protected void tearDown() {
+ log.info("tearDown()");
+ try {
+ clear();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ };
+ return setup;
+ }
+
+ private static void clear() throws IOException {
+ B.clear();
+ C.clear();
+ D.clear();
+ F.clear();
+ G.clear();
+ }
+
+ private static void setUpShellEnv() {
+ env.setAliase("b", B);
+ env.setAliase("c", C);
+ env.setAliase("d", D);
+
+ env.setAliase("f", F);
+ env.setAliase("g", G);
+ }
+
+ private void testBadExpression(String expression) {
+ InputStream in = new ByteArrayInputStream(expression.getBytes());
+ HamaExpressionParser parser = new HamaExpressionParser(in, env, hConfig);
+ try {
+ HamaExpression hExpression = parser.Start();
+ assertNull(hExpression);
+ } catch (ParseException e) {
+ }
+ }
+
+ private void testNormalExpression(String expression) throws ParseException {
+ InputStream in = new ByteArrayInputStream(expression.getBytes());
+ HamaExpressionParser parser = new HamaExpressionParser(in, env, hConfig);
+ HamaExpression hExpression = parser.Start();
+ assertNotNull(hExpression);
+ }
+
+ /**
+ * Expression Grammar Parser Test
+ *
+ * @throws ParseException
+ */
+ public void testGrammar() throws ParseException {
+ // badExpr1
+ testBadExpression(badExpr1);
+ // badExpr2
+ testBadExpression(badExpr2);
+ // badExpr3
+ testBadExpression(badExpr3);
+ // Expr1
+ testNormalExpression(Expr1);
+ // Expr2
+ testNormalExpression(Expr2);
+ }
+
+ /**
+ * Expression Aliases Test
+ *
+ * we cannot use an aliases without initialization.
+ *
+ * @throws ParseException
+ */
+ public void testAliases() throws ParseException {
+ // Expr3
+ testBadExpression(Expr3);
+ // Expr1
+ testNormalExpression(Expr1);
+ }
+
+ private void testExprExec(String expression, String matrixAliase,
+ boolean isNormalExpr) throws ParseException {
+ InputStream in = new ByteArrayInputStream(expression.getBytes());
+ HamaExpressionParser parser = new HamaExpressionParser(in, env, hConfig);
+ HamaExpression hExpression = parser.Start();
+ assertNotNull(hExpression);
+ hExpression.execute();
+ assertEquals(env.containAliase(matrixAliase), isNormalExpr);
+ }
+
+ /**
+ * Expression Execution Test
+ *
+ * @throws ParseException
+ */
+ public void testExprExec() throws ParseException {
+ // Expr4
+ testExprExec(Expr4, Expr4_Aliase, true);
+ // Expr5
+ testExprExec(Expr5, Expr5_Aliase, false);
+ // Expr6
+ testExprExec(Expr6, Expr6_Aliase, false);
+ }
+
+}