Example #1
0
  @Test
  public void testRepresentations() {
    Range a = Range.make((double) 0, (double) 4, null);
    assertEquals("0\u20264", a.toString());
    assertEquals(2, Data.asNumeric(a), 0.01);

    double low = -200.5;
    double high = -100;
    Range b = Range.make(low, high, null);
    assertEquals("-200.5\u2026-100", b.toString());
    assertEquals(-150.25, Data.asNumeric(b), 0.01);

    assertNotEquals(a.hashCode(), b.hashCode());
  }
Example #2
0
 @Test
 public void testConversions() {
   assertEquals(1.0, Data.asNumeric(1), 0.01);
   assertEquals(1.0, Data.asNumeric(1.0), 0.01);
   assertEquals(1.0, Data.asNumeric("1"), 0.01);
   assertEquals(null, Data.asNumeric("foo"));
   assertEquals(null, Data.asNumeric(null));
   assertEquals(null, Data.asNumeric("NaN"));
   assertEquals(null, Data.asNumeric("Inf"));
   assertEquals(null, Data.asNumeric("1/8/70"));
   assertEquals(null, Data.asNumeric("abc -1 ddd"));
 }
Example #3
0
 public String format(Object v) {
   if (v == null) return "?";
   if (v instanceof Range) return v.toString();
   if (isDate()) return ((DateFormat) property("dateFormat")).format(Data.asDate(v));
   if ("percent".equals(property("summary"))) {
     // Show one decimal place at most
     Double d = Data.asNumeric(v);
     if (d == null) return null;
     return Data.formatNumeric(Math.round(d * 10) / 10.0, false) + "%";
   }
   if (isNumeric()) {
     Double d = Data.asNumeric(v);
     return d == null ? "?" : Data.formatNumeric(d, true);
   }
   return v.toString();
 }
Example #4
0
 @Test
 public void testDates() {
   assertEquals(0.0, Data.asNumeric(Data.asDate("1970-01-01")), 0.001);
   assertEquals(0.0, Data.asNumeric(Data.asDate("1/1/70")), 0.001);
   assertEquals(1.0, Data.asNumeric(Data.asDate("1/2/70")), 0.001);
   assertEquals(1.0, Data.asNumeric(Data.asDate("Jan 2 1970")), 0.001);
   assertEquals(0.25, Data.asNumeric(Data.asDate("January 1, 1970 6:00:00")), 0.001);
   assertEquals(0.125, Data.asNumeric(Data.asDate("January 1, 1970 3:00:00")), 0.001);
   assertEquals(1.0 / 3600 / 24, Data.asNumeric(Data.asDate("1970-01-01T00:00:01")), 0.000001);
   assertEquals(1.0 / 3600 / 24, Data.asNumeric(Data.asDate("00:00:01")), 0.000001);
   assertEquals(
       (7 + 45 / 60.0 + 22 / 3600.0) / 24.0, Data.asNumeric(Data.asDate("07:45:22")), 0.000001);
   assertEquals(
       (10 + 45 / 60.0 + 22 / 3600.0) / 24.0, Data.asNumeric(Data.asDate("10:45:22")), 0.000001);
   assertEquals(null, Data.asDate(null));
   assertEquals(null, Data.asDate("a"));
   assertEquals(null, Data.asDate("Not a date"));
   assertEquals(null, Data.asDate("WURGH 53, 2000"));
 }