michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: package org.mozilla.gecko.background.common; michael@0: michael@0: import java.text.SimpleDateFormat; michael@0: import java.util.Locale; michael@0: import java.util.TimeZone; michael@0: michael@0: import junit.framework.TestCase; michael@0: michael@0: import org.mozilla.gecko.background.common.DateUtils.DateFormatter; michael@0: //import android.util.SparseArray; michael@0: michael@0: public class TestDateUtils extends TestCase { michael@0: // Our old, correct implementation -- used to test the new one. michael@0: public static String getDateStringUsingFormatter(long time) { michael@0: final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.US); michael@0: format.setTimeZone(TimeZone.getTimeZone("UTC")); michael@0: return format.format(time); michael@0: } michael@0: michael@0: private void checkDateString(long time) { michael@0: assertEquals(getDateStringUsingFormatter(time), michael@0: new DateUtils.DateFormatter().getDateString(time)); michael@0: } michael@0: michael@0: public void testDateImplementations() { michael@0: checkDateString(1L); michael@0: checkDateString(System.currentTimeMillis()); michael@0: checkDateString(1379118065844L); michael@0: checkDateString(1379110000000L); michael@0: for (long i = 0L; i < (2 * GlobalConstants.MILLISECONDS_PER_DAY); i += 11000) { michael@0: checkDateString(i); michael@0: } michael@0: } michael@0: michael@0: @SuppressWarnings("static-method") michael@0: public void testReuse() { michael@0: DateFormatter formatter = new DateFormatter(); michael@0: long time = System.currentTimeMillis(); michael@0: assertEquals(formatter.getDateString(time), formatter.getDateString(time)); michael@0: } michael@0: michael@0: // Perf tests. Disabled until you need them. michael@0: /* michael@0: @SuppressWarnings("static-method") michael@0: public void testDateTiming() { michael@0: long start = 1379118000000L; michael@0: long end = 1379118045844L; michael@0: michael@0: long t0 = android.os.SystemClock.elapsedRealtime(); michael@0: for (long i = start; i < end; ++i) { michael@0: DateUtils.getDateString(i); michael@0: } michael@0: long t1 = android.os.SystemClock.elapsedRealtime(); michael@0: System.err.println("CALENDAR: " + (t1 - t0)); michael@0: michael@0: michael@0: t0 = android.os.SystemClock.elapsedRealtime(); michael@0: for (long i = start; i < end; ++i) { michael@0: getDateStringFormatter(i); michael@0: } michael@0: t1 = android.os.SystemClock.elapsedRealtime(); michael@0: System.err.println("FORMATTER: " + (t1 - t0)); michael@0: } michael@0: michael@0: @SuppressWarnings("static-method") michael@0: public void testDayTiming() { michael@0: long start = 33 * 365; michael@0: long end = start + 90; michael@0: int reps = 1; michael@0: long t0 = android.os.SystemClock.elapsedRealtime(); michael@0: for (long i = start; i < end; ++i) { michael@0: for (int j = 0; j < reps; ++j) { michael@0: DateUtils.getDateStringForDay(i); michael@0: } michael@0: } michael@0: long t1 = android.os.SystemClock.elapsedRealtime(); michael@0: System.err.println("Non-memo: " + (t1 - t0)); michael@0: } michael@0: */ michael@0: }