date-basic.js (4342B)
1 function testDateGetTime() { 2 var timeValues = [ 3 -1000, 4 +1000, 5 0, 6 NaN, 7 ]; 8 9 for (var i = 0; i < 250; ++i) { 10 var t = timeValues[i & 3]; 11 var d = new Date(t); 12 assertEq(d.getTime(), t); 13 assertEq(d.valueOf(), t); 14 } 15 } 16 testDateGetTime(); 17 18 var dateValues = [ 19 // Start of the epoch and start of the year date. 20 [1970, 1-1, 1, 4], 21 22 // End of year date. 23 [2023, 12-1, 31, 0], 24 25 // Date near maximum allowed time value (275760 September, 13). 26 [275760, 9-1, 13 - 1, 5], 27 28 // Date near minimum allowed time value (-271821 April, 20). 29 [-271821, 4-1, 20 + 1, 3], 30 31 // Invalid Date. 32 [NaN, NaN, NaN, NaN], 33 ]; 34 35 function testDateGetFullYear() { 36 for (var i = 0; i < 250; ++i) { 37 var [year, month, date] = dateValues[i % dateValues.length]; 38 39 // Create a new Date object with an uninitialized local time cache. 40 var d = new Date(year, month, date); 41 42 // First call to getFullYear initializes the cache. 43 assertEq(d.getFullYear(), year); 44 45 // Second call to getFullYear uses the cached value. 46 assertEq(d.getFullYear(), year); 47 } 48 } 49 testDateGetFullYear(); 50 51 function testDateGetMonth() { 52 for (var i = 0; i < 250; ++i) { 53 var [year, month, date] = dateValues[i % dateValues.length]; 54 55 // Create a new Date object with an uninitialized local time cache. 56 var d = new Date(year, month, date); 57 58 // First call to getMonth initializes the cache. 59 assertEq(d.getMonth(), month); 60 61 // Second call to getMonth uses the cached value. 62 assertEq(d.getMonth(), month); 63 } 64 } 65 testDateGetMonth(); 66 67 function testDateGetDate() { 68 for (var i = 0; i < 250; ++i) { 69 var [year, month, date] = dateValues[i % dateValues.length]; 70 71 // Create a new Date object with an uninitialized local time cache. 72 var d = new Date(year, month, date); 73 74 // First call to getDate initializes the cache. 75 assertEq(d.getDate(), date); 76 77 // Second call to getDate uses the cached value. 78 assertEq(d.getDate(), date); 79 } 80 } 81 testDateGetDate(); 82 83 function testDateGetDay() { 84 for (var i = 0; i < 250; ++i) { 85 var [year, month, date, day] = dateValues[i % dateValues.length]; 86 87 // Create a new Date object with an uninitialized local time cache. 88 var d = new Date(year, month, date); 89 90 // First call to getDay initializes the cache. 91 assertEq(d.getDay(), day); 92 93 // Second call to getDay uses the cached value. 94 assertEq(d.getDay(), day); 95 } 96 } 97 testDateGetDay(); 98 99 function testDateGetFullYearMonthDateDay() { 100 for (var i = 0; i < 250; ++i) { 101 var [year, month, date, day] = dateValues[i % dateValues.length]; 102 103 // Create a new Date object with an uninitialized local time cache. 104 var d = new Date(year, month, date); 105 106 // Test calling different methods, too. 107 assertEq(d.getFullYear(), year); 108 assertEq(d.getMonth(), month); 109 assertEq(d.getDate(), date); 110 assertEq(d.getDay(), day); 111 } 112 } 113 testDateGetFullYearMonthDateDay(); 114 115 function testDateGetHours() { 116 var timeValues = [ 117 0, 118 12, 119 23, 120 NaN, 121 ]; 122 123 for (var i = 0; i < 250; ++i) { 124 var t = timeValues[i & 3]; 125 126 // Create a new Date object with an uninitialized local time cache. 127 var d = new Date(2000, 0, 1, t); 128 129 // First call to getHours initializes the cache. 130 assertEq(d.getHours(), t); 131 132 // Second call to getHours uses the cached value. 133 assertEq(d.getHours(), t); 134 } 135 } 136 testDateGetHours(); 137 138 function testDateGetMinutes() { 139 var timeValues = [ 140 0, 141 30, 142 59, 143 NaN, 144 ]; 145 146 for (var i = 0; i < 250; ++i) { 147 var t = timeValues[i & 3]; 148 149 // Create a new Date object with an uninitialized local time cache. 150 var d = new Date(2000, 0, 1, 0, t); 151 152 // First call to getMinutes initializes the cache. 153 assertEq(d.getMinutes(), t); 154 155 // Second call to getMinutes uses the cached value. 156 assertEq(d.getMinutes(), t); 157 } 158 } 159 testDateGetMinutes(); 160 161 function testDateGetSeconds() { 162 var timeValues = [ 163 0, 164 30, 165 59, 166 NaN, 167 ]; 168 169 for (var i = 0; i < 250; ++i) { 170 var t = timeValues[i & 3]; 171 172 // Create a new Date object with an uninitialized local time cache. 173 var d = new Date(2000, 0, 1, 0, 0, t); 174 175 // First call to getSeconds initializes the cache. 176 assertEq(d.getSeconds(), t); 177 178 // Second call to getSeconds uses the cached value. 179 assertEq(d.getSeconds(), t); 180 } 181 } 182 testDateGetSeconds();