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