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

branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
equal deleted inserted replaced
-1:000000000000 0:1257327ed617
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 package org.mozilla.gecko.background.common;
5
6 import java.text.SimpleDateFormat;
7 import java.util.Locale;
8 import java.util.TimeZone;
9
10 import junit.framework.TestCase;
11
12 import org.mozilla.gecko.background.common.DateUtils.DateFormatter;
13 //import android.util.SparseArray;
14
15 public class TestDateUtils extends TestCase {
16 // Our old, correct implementation -- used to test the new one.
17 public static String getDateStringUsingFormatter(long time) {
18 final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
19 format.setTimeZone(TimeZone.getTimeZone("UTC"));
20 return format.format(time);
21 }
22
23 private void checkDateString(long time) {
24 assertEquals(getDateStringUsingFormatter(time),
25 new DateUtils.DateFormatter().getDateString(time));
26 }
27
28 public void testDateImplementations() {
29 checkDateString(1L);
30 checkDateString(System.currentTimeMillis());
31 checkDateString(1379118065844L);
32 checkDateString(1379110000000L);
33 for (long i = 0L; i < (2 * GlobalConstants.MILLISECONDS_PER_DAY); i += 11000) {
34 checkDateString(i);
35 }
36 }
37
38 @SuppressWarnings("static-method")
39 public void testReuse() {
40 DateFormatter formatter = new DateFormatter();
41 long time = System.currentTimeMillis();
42 assertEquals(formatter.getDateString(time), formatter.getDateString(time));
43 }
44
45 // Perf tests. Disabled until you need them.
46 /*
47 @SuppressWarnings("static-method")
48 public void testDateTiming() {
49 long start = 1379118000000L;
50 long end = 1379118045844L;
51
52 long t0 = android.os.SystemClock.elapsedRealtime();
53 for (long i = start; i < end; ++i) {
54 DateUtils.getDateString(i);
55 }
56 long t1 = android.os.SystemClock.elapsedRealtime();
57 System.err.println("CALENDAR: " + (t1 - t0));
58
59
60 t0 = android.os.SystemClock.elapsedRealtime();
61 for (long i = start; i < end; ++i) {
62 getDateStringFormatter(i);
63 }
64 t1 = android.os.SystemClock.elapsedRealtime();
65 System.err.println("FORMATTER: " + (t1 - t0));
66 }
67
68 @SuppressWarnings("static-method")
69 public void testDayTiming() {
70 long start = 33 * 365;
71 long end = start + 90;
72 int reps = 1;
73 long t0 = android.os.SystemClock.elapsedRealtime();
74 for (long i = start; i < end; ++i) {
75 for (int j = 0; j < reps; ++j) {
76 DateUtils.getDateStringForDay(i);
77 }
78 }
79 long t1 = android.os.SystemClock.elapsedRealtime();
80 System.err.println("Non-memo: " + (t1 - t0));
81 }
82 */
83 }

mercurial