js/src/tests/ecma/Date/shell.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1
michael@0 2 var BUGNUMBER;
michael@0 3 var summary;
michael@0 4
michael@0 5 function runDSTOffsetCachingTestsFraction(part, parts)
michael@0 6 {
michael@0 7 BUGNUMBER = 563938;
michael@0 8 summary = 'Cache DST offsets to improve SunSpider score';
michael@0 9
michael@0 10 print(BUGNUMBER + ": " + summary);
michael@0 11
michael@0 12 var MAX_UNIX_TIMET = 2145859200;
michael@0 13 var RANGE_EXPANSION_AMOUNT = 30 * 24 * 60 * 60;
michael@0 14
michael@0 15 /**
michael@0 16 * Computes the time zone offset in minutes at the given timestamp.
michael@0 17 */
michael@0 18 function tzOffsetFromUnixTimestamp(timestamp)
michael@0 19 {
michael@0 20 var d = new Date(NaN);
michael@0 21 d.setTime(timestamp); // local slot = NaN, UTC slot = timestamp
michael@0 22 return d.getTimezoneOffset(); // get UTC, calculate local => diff in minutes
michael@0 23 }
michael@0 24
michael@0 25 /**
michael@0 26 * Clear the DST offset cache, leaving it initialized to include a timestamp
michael@0 27 * completely unlike the provided one (i.e. one very, very far away in time
michael@0 28 * from it). Thus an immediately following lookup for the provided timestamp
michael@0 29 * will cache-miss and compute a clean value.
michael@0 30 */
michael@0 31 function clearDSTOffsetCache(undesiredTimestamp)
michael@0 32 {
michael@0 33 var opposite = (undesiredTimestamp + MAX_UNIX_TIMET / 2) % MAX_UNIX_TIMET;
michael@0 34
michael@0 35 // Generic purge to known, but not necessarily desired, state
michael@0 36 tzOffsetFromUnixTimestamp(0);
michael@0 37 tzOffsetFromUnixTimestamp(MAX_UNIX_TIMET);
michael@0 38
michael@0 39 // Purge to desired state. Cycle 2x in case opposite or undesiredTimestamp
michael@0 40 // is close to 0 or MAX_UNIX_TIMET.
michael@0 41 tzOffsetFromUnixTimestamp(opposite);
michael@0 42 tzOffsetFromUnixTimestamp(undesiredTimestamp);
michael@0 43 tzOffsetFromUnixTimestamp(opposite);
michael@0 44 tzOffsetFromUnixTimestamp(undesiredTimestamp);
michael@0 45 }
michael@0 46
michael@0 47 function computeCanonicalTZOffset(timestamp)
michael@0 48 {
michael@0 49 clearDSTOffsetCache(timestamp);
michael@0 50 return tzOffsetFromUnixTimestamp(timestamp);
michael@0 51 }
michael@0 52
michael@0 53 var TEST_TIMESTAMPS_SECONDS =
michael@0 54 [
michael@0 55 // Special-ish timestamps
michael@0 56 0,
michael@0 57 RANGE_EXPANSION_AMOUNT,
michael@0 58 MAX_UNIX_TIMET,
michael@0 59 ];
michael@0 60
michael@0 61 var ONE_DAY = 24 * 60 * 60;
michael@0 62 var EIGHTY_THREE_HOURS = 83 * 60 * 60;
michael@0 63 var NINETY_EIGHT_HOURS = 98 * 60 * 60;
michael@0 64 function nextIncrement(i)
michael@0 65 {
michael@0 66 return i === EIGHTY_THREE_HOURS ? NINETY_EIGHT_HOURS : EIGHTY_THREE_HOURS;
michael@0 67 }
michael@0 68
michael@0 69 // Now add a long sequence of non-special timestamps, from a fixed range, that
michael@0 70 // overlaps a DST change by "a bit" on each side. 67 days should be enough
michael@0 71 // displacement that we can occasionally exercise the implementation's
michael@0 72 // thirty-day expansion and the DST-offset-change logic. Use two different
michael@0 73 // increments just to be safe and catch something a single increment might not.
michael@0 74 var DST_CHANGE_DATE = 1268553600; // March 14, 2010
michael@0 75 for (var t = DST_CHANGE_DATE - 67 * ONE_DAY,
michael@0 76 i = nextIncrement(NINETY_EIGHT_HOURS),
michael@0 77 end = DST_CHANGE_DATE + 67 * ONE_DAY;
michael@0 78 t < end;
michael@0 79 i = nextIncrement(i), t += i)
michael@0 80 {
michael@0 81 TEST_TIMESTAMPS_SECONDS.push(t);
michael@0 82 }
michael@0 83
michael@0 84 var TEST_TIMESTAMPS =
michael@0 85 TEST_TIMESTAMPS_SECONDS.map(function(v) { return v * 1000; });
michael@0 86
michael@0 87 /**************
michael@0 88 * BEGIN TEST *
michael@0 89 **************/
michael@0 90
michael@0 91 // Compute the correct time zone offsets for all timestamps to be tested.
michael@0 92 var CORRECT_TZOFFSETS = TEST_TIMESTAMPS.map(computeCanonicalTZOffset);
michael@0 93
michael@0 94 // Intentionally and knowingly invoking every single logic path in the cache
michael@0 95 // isn't easy for a human to get right (and know he's gotten it right), so
michael@0 96 // let's do it the easy way: exhaustively try all possible four-date sequences
michael@0 97 // selecting from our array of possible timestamps.
michael@0 98
michael@0 99 var sz = TEST_TIMESTAMPS.length;
michael@0 100 var start = Math.floor((part - 1) / parts * sz);
michael@0 101 var end = Math.floor(part / parts * sz);
michael@0 102
michael@0 103 print("Exhaustively testing timestamps " +
michael@0 104 "[" + start + ", " + end + ") of " + sz + "...");
michael@0 105
michael@0 106 try
michael@0 107 {
michael@0 108 for (var i = start; i < end; i++)
michael@0 109 {
michael@0 110 print("Testing timestamp " + i + "...");
michael@0 111
michael@0 112 var t1 = TEST_TIMESTAMPS[i];
michael@0 113 for (var j = 0; j < sz; j++)
michael@0 114 {
michael@0 115 var t2 = TEST_TIMESTAMPS[j];
michael@0 116 for (var k = 0; k < sz; k++)
michael@0 117 {
michael@0 118 var t3 = TEST_TIMESTAMPS[k];
michael@0 119 for (var w = 0; w < sz; w++)
michael@0 120 {
michael@0 121 var t4 = TEST_TIMESTAMPS[w];
michael@0 122
michael@0 123 clearDSTOffsetCache(t1);
michael@0 124
michael@0 125 var tzo1 = tzOffsetFromUnixTimestamp(t1);
michael@0 126 var tzo2 = tzOffsetFromUnixTimestamp(t2);
michael@0 127 var tzo3 = tzOffsetFromUnixTimestamp(t3);
michael@0 128 var tzo4 = tzOffsetFromUnixTimestamp(t4);
michael@0 129
michael@0 130 assertEq(tzo1, CORRECT_TZOFFSETS[i]);
michael@0 131 assertEq(tzo2, CORRECT_TZOFFSETS[j]);
michael@0 132 assertEq(tzo3, CORRECT_TZOFFSETS[k]);
michael@0 133 assertEq(tzo4, CORRECT_TZOFFSETS[w]);
michael@0 134 }
michael@0 135 }
michael@0 136 }
michael@0 137 }
michael@0 138 }
michael@0 139 catch (e)
michael@0 140 {
michael@0 141 assertEq(true, false,
michael@0 142 "Error when testing with timestamps " +
michael@0 143 i + ", " + j + ", " + k + ", " + w +
michael@0 144 " (" + t1 + ", " + t2 + ", " + t3 + ", " + t4 + ")!");
michael@0 145 }
michael@0 146
michael@0 147 reportCompare(true, true);
michael@0 148 print("All tests passed!");
michael@0 149 }

mercurial