Skip to content

Commit

Permalink
added boolean to int casting
Browse files Browse the repository at this point in the history
  • Loading branch information
orzikhd committed Aug 11, 2017
1 parent cf649aa commit a0c4fa0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ private enum CastType {
* int to boolean.
*/
INT_TO_BOOLEAN,
/**
* boolean to int.
*/
BOOLEAN_TO_INT,
/**
* from double to float.
*/
Expand Down Expand Up @@ -161,6 +165,8 @@ private CastType getCastType(final Type castFrom, final Type castTo) {
return CastType.INT_TO_BOOLEAN;
case "LONG_TYPE|BOOLEAN_TYPE":
return CastType.LONG_TO_BOOLEAN;
case "BOOLEAN_TYPE|INT_TYPE":
return CastType.BOOLEAN_TO_INT;
default:
break;
}
Expand Down Expand Up @@ -212,6 +218,9 @@ public String getJavaString(final ExpressionOperatorParameter parameters) {
case INT_TO_BOOLEAN:
return getLeftFunctionCallString(
"edu.washington.escience.myria.util.MathUtils.castIntToBoolean", parameters);
case BOOLEAN_TO_INT:
return getLeftFunctionCallString(
"edu.washington.escience.myria.util.MathUtils.castBooleanToInt", parameters);
case DOUBLE_TO_FLOAT:
return getLeftFunctionCallString(
"edu.washington.escience.myria.util.MathUtils.castDoubleToFloat", parameters);
Expand Down
13 changes: 13 additions & 0 deletions src/edu/washington/escience/myria/util/MathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ public static boolean castIntToBoolean(final int v) {
return v != 0;
}

/**
* Cast a boolean to an int
*
* @param v the boolean
* @returns 1 if v was True, 0 if v was False
*/
public static int castBooleanToInt(final boolean v) {
if (v) {
return 1;
}
return 0;
}

/**
* util classes are not instantiable.
* */
Expand Down
11 changes: 11 additions & 0 deletions test/edu/washington/escience/myria/operator/apply/CastTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ public void testLongToBoolean() throws Throwable {
assertEquals(true, ((Boolean) ans).booleanValue());
}

@Test
public void testBooleanToInt() throws Throwable {
ConstantExpression valF = new ConstantExpression(false);
Object ans = evaluateCastAndUnrollException(valF, Type.INT_TYPE);
assertEquals(0, ((Integer) ans).intValue());

ConstantExpression valT = new ConstantExpression(true);
ans = evaluateCastAndUnrollException(valT, Type.INT_TYPE);
assertEquals(1, ((Integer) ans).intValue());
}

@Test
public void testDoubleToFloat() throws Throwable {
float tolerance = (float) 1e-6;
Expand Down

0 comments on commit a0c4fa0

Please sign in to comment.