Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test fails because of locale #257

Closed
AlexP11223 opened this issue Mar 8, 2018 · 1 comment
Closed

Test fails because of locale #257

AlexP11223 opened this issue Mar 8, 2018 · 1 comment

Comments

@AlexP11223
Copy link

AlexP11223 commented Mar 8, 2018

RowTest testSimple fails on my Windows PC because it tries to compare "5,0" (comma) from row.toString() and "5.0" (dot) here:

assertTrue(row.toString().contains("5.0"));
testSimple(edu.stanford.futuredata.macrobase.datamodel.RowTest)  Time elapsed: 0.001 sec  <<< FAILURE!
java.lang.AssertionError
	at org.junit.Assert.fail(Assert.java:86)
	at org.junit.Assert.assertTrue(Assert.java:41)
	at org.junit.Assert.assertTrue(Assert.java:52)
	at edu.stanford.futuredata.macrobase.datamodel.RowTest.testSimple(RowTest.java:16)
............
	at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

It happens because Row uses DecimalFormat("#.0#####") to convert doubles to string, which uses the current locale. (By the way, seems like it just takes default format for locale ignoring additional Windows format settings: I have ru-RU locale on this PC but decimal separator is specified as dot in additional settings)

So I think either DecimalFormat in Row should use some fixed locale with dot decimal separator (such as en-US):

--- a/lib/src/main/java/edu/stanford/futuredata/macrobase/datamodel/Row.java
+++ b/lib/src/main/java/edu/stanford/futuredata/macrobase/datamodel/Row.java
@@ -5,8 +5,11 @@ import static java.util.stream.Collectors.toList;
 import com.google.common.base.Joiner;
 import java.io.PrintStream;
 import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
 import java.util.Collections;
 import java.util.List;
+import java.util.Locale;
+
 import org.apache.commons.lang3.StringUtils;

 /**
@@ -15,7 +18,7 @@ import org.apache.commons.lang3.StringUtils;
 public class Row {

     // Formatter for printing out doubles; print at least 1 and no more than 6 decimal places
-    private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("#.0#####");
+    private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("#.0#####", new DecimalFormatSymbols(Locale.US));

     private Schema schema; // not set by user
     private List<Object> vals;

or the test should use the same format for current locale:

--- a/lib/src/test/java/edu/stanford/futuredata/macrobase/datamodel/RowTest.java
+++ b/lib/src/test/java/edu/stanford/futuredata/macrobase/datamodel/RowTest.java
@@ -2,6 +2,7 @@ package edu.stanford.futuredata.macrobase.datamodel;

 import org.junit.Test;

+import java.text.DecimalFormat;
 import java.util.Arrays;

 import static org.junit.Assert.assertEquals;
@@ -13,7 +14,7 @@ public class RowTest {
         Row row = new Row(Arrays.asList(5.0, "java"));
         assertEquals(5.0, row.getAs(0), 1e-10);
         assertEquals("java", row.getAs(1));
-        assertTrue(row.toString().contains("5.0"));
+        assertTrue(row.toString().contains(new DecimalFormat("#.0#####").format(5.0)));

         Schema schema = new Schema();
         schema.addColumn(Schema.ColType.DOUBLE, "metric");
@fabuzaid21
Copy link
Contributor

Thanks for filing this issue! We just pushed a fix to master—let us know if the bug still persists (or if there are other bugs you find)

fabuzaid21 added a commit that referenced this issue Mar 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants