mobile/android/tests/background/junit3/src/common/TestDateUtils.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/tests/background/junit3/src/common/TestDateUtils.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,83 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +package org.mozilla.gecko.background.common;
     1.8 +
     1.9 +import java.text.SimpleDateFormat;
    1.10 +import java.util.Locale;
    1.11 +import java.util.TimeZone;
    1.12 +
    1.13 +import junit.framework.TestCase;
    1.14 +
    1.15 +import org.mozilla.gecko.background.common.DateUtils.DateFormatter;
    1.16 +//import android.util.SparseArray;
    1.17 +
    1.18 +public class TestDateUtils extends TestCase {
    1.19 +  // Our old, correct implementation -- used to test the new one.
    1.20 +  public static String getDateStringUsingFormatter(long time) {
    1.21 +    final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
    1.22 +    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    1.23 +    return format.format(time);
    1.24 +  }
    1.25 +
    1.26 +  private void checkDateString(long time) {
    1.27 +    assertEquals(getDateStringUsingFormatter(time),
    1.28 +                 new DateUtils.DateFormatter().getDateString(time));
    1.29 +  }
    1.30 +
    1.31 +  public void testDateImplementations() {
    1.32 +    checkDateString(1L);
    1.33 +    checkDateString(System.currentTimeMillis());
    1.34 +    checkDateString(1379118065844L);
    1.35 +    checkDateString(1379110000000L);
    1.36 +    for (long i = 0L; i < (2 * GlobalConstants.MILLISECONDS_PER_DAY); i += 11000) {
    1.37 +      checkDateString(i);
    1.38 +    }
    1.39 +  }
    1.40 +
    1.41 +  @SuppressWarnings("static-method")
    1.42 +  public void testReuse() {
    1.43 +    DateFormatter formatter = new DateFormatter();
    1.44 +    long time = System.currentTimeMillis();
    1.45 +    assertEquals(formatter.getDateString(time), formatter.getDateString(time));
    1.46 +  }
    1.47 +
    1.48 +  // Perf tests. Disabled until you need them.
    1.49 +  /*
    1.50 +  @SuppressWarnings("static-method")
    1.51 +  public void testDateTiming() {
    1.52 +    long start = 1379118000000L;
    1.53 +    long end   = 1379118045844L;
    1.54 +
    1.55 +    long t0 = android.os.SystemClock.elapsedRealtime();
    1.56 +    for (long i = start; i < end; ++i) {
    1.57 +      DateUtils.getDateString(i);
    1.58 +    }
    1.59 +    long t1 = android.os.SystemClock.elapsedRealtime();
    1.60 +    System.err.println("CALENDAR: " + (t1 - t0));
    1.61 +
    1.62 +
    1.63 +    t0 = android.os.SystemClock.elapsedRealtime();
    1.64 +    for (long i = start; i < end; ++i) {
    1.65 +      getDateStringFormatter(i);
    1.66 +    }
    1.67 +    t1 = android.os.SystemClock.elapsedRealtime();
    1.68 +    System.err.println("FORMATTER: " + (t1 - t0));
    1.69 +  }
    1.70 +
    1.71 +  @SuppressWarnings("static-method")
    1.72 +  public void testDayTiming() {
    1.73 +    long start = 33 * 365;
    1.74 +    long end   = start + 90;
    1.75 +    int reps   = 1;
    1.76 +    long t0 = android.os.SystemClock.elapsedRealtime();
    1.77 +    for (long i = start; i < end; ++i) {
    1.78 +      for (int j = 0; j < reps; ++j) {
    1.79 +        DateUtils.getDateStringForDay(i);
    1.80 +      }
    1.81 +    }
    1.82 +    long t1 = android.os.SystemClock.elapsedRealtime();
    1.83 +    System.err.println("Non-memo: " + (t1 - t0));
    1.84 +  }
    1.85 +  */
    1.86 +}

mercurial