OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 return true; | 195 return true; |
196 } | 196 } |
197 | 197 |
198 | 198 |
199 function deepEquals(a, b) { | 199 function deepEquals(a, b) { |
200 if (a === b) { | 200 if (a === b) { |
201 // Check for -0. | 201 // Check for -0. |
202 if (a === 0) return (1 / a) === (1 / b); | 202 if (a === 0) return (1 / a) === (1 / b); |
203 return true; | 203 return true; |
204 } | 204 } |
205 if (typeof a != typeof b) return false; | 205 if (typeof a !== typeof b) return false; |
206 if (typeof a == "number") return isNaN(a) && isNaN(b); | 206 if (typeof a === "number") return isNaN(a) && isNaN(b); |
207 if (typeof a !== "object" && typeof a !== "function") return false; | 207 if (typeof a !== "object" && typeof a !== "function") return false; |
208 // Neither a nor b is primitive. | 208 // Neither a nor b is primitive. |
209 var objectClass = classOf(a); | 209 var objectClass = classOf(a); |
210 if (objectClass !== classOf(b)) return false; | 210 if (objectClass !== classOf(b)) return false; |
211 if (objectClass === "RegExp") { | 211 if (objectClass === "RegExp") { |
212 // For RegExp, just compare pattern and flags using its toString. | 212 // For RegExp, just compare pattern and flags using its toString. |
213 return (a.toString() === b.toString()); | 213 return (a.toString() === b.toString()); |
214 } | 214 } |
215 // Functions are only identical to themselves. | 215 // Functions are only identical to themselves. |
216 if (objectClass === "Function") return false; | 216 if (objectClass === "Function") return false; |
217 if (objectClass === "Array") { | 217 if (objectClass === "Array") { |
218 var elementCount = 0; | 218 var elementCount = 0; |
219 if (a.length != b.length) { | 219 if (a.length !== b.length) { |
220 return false; | 220 return false; |
221 } | 221 } |
222 for (var i = 0; i < a.length; i++) { | 222 for (var i = 0; i < a.length; i++) { |
223 if (!deepEquals(a[i], b[i])) return false; | 223 if (!deepEquals(a[i], b[i])) return false; |
224 } | 224 } |
225 return true; | 225 return true; |
226 } | 226 } |
227 if (objectClass == "String" || objectClass == "Number" || | 227 if (objectClass === "String" || objectClass === "Number" || |
228 objectClass == "Boolean" || objectClass == "Date") { | 228 objectClass === "Boolean" || objectClass === "Date") { |
229 if (a.valueOf() !== b.valueOf()) return false; | 229 if (a.valueOf() !== b.valueOf()) return false; |
230 } | 230 } |
231 return deepObjectEquals(a, b); | 231 return deepObjectEquals(a, b); |
232 } | 232 } |
233 | 233 |
234 assertSame = function assertSame(expected, found, name_opt) { | 234 assertSame = function assertSame(expected, found, name_opt) { |
235 // TODO(mstarzinger): We should think about using Harmony's egal operator | 235 // TODO(mstarzinger): We should think about using Harmony's egal operator |
236 // or the function equivalent Object.is() here. | 236 // or the function equivalent Object.is() here. |
237 if (found === expected) { | 237 if (found === expected) { |
238 if (expected !== 0 || (1 / expected) == (1 / found)) return; | 238 if (expected !== 0 || (1 / expected) === (1 / found)) return; |
239 } else if ((expected !== expected) && (found !== found)) { | 239 } else if ((expected !== expected) && (found !== found)) { |
240 return; | 240 return; |
241 } | 241 } |
242 fail(PrettyPrint(expected), found, name_opt); | 242 fail(PrettyPrint(expected), found, name_opt); |
243 }; | 243 }; |
244 | 244 |
245 | 245 |
246 assertEquals = function assertEquals(expected, found, name_opt) { | 246 assertEquals = function assertEquals(expected, found, name_opt) { |
247 if (!deepEquals(found, expected)) { | 247 if (!deepEquals(found, expected)) { |
248 fail(PrettyPrint(expected), found, name_opt); | 248 fail(PrettyPrint(expected), found, name_opt); |
249 } | 249 } |
250 }; | 250 }; |
251 | 251 |
252 | 252 |
253 assertEqualsDelta = | 253 assertEqualsDelta = |
254 function assertEqualsDelta(expected, found, delta, name_opt) { | 254 function assertEqualsDelta(expected, found, delta, name_opt) { |
255 assertTrue(Math.abs(expected - found) <= delta, name_opt); | 255 assertTrue(Math.abs(expected - found) <= delta, name_opt); |
256 }; | 256 }; |
257 | 257 |
258 | 258 |
259 assertArrayEquals = function assertArrayEquals(expected, found, name_opt) { | 259 assertArrayEquals = function assertArrayEquals(expected, found, name_opt) { |
260 var start = ""; | 260 var start = ""; |
261 if (name_opt) { | 261 if (name_opt) { |
262 start = name_opt + " - "; | 262 start = name_opt + " - "; |
263 } | 263 } |
264 assertEquals(expected.length, found.length, start + "array length"); | 264 assertEquals(expected.length, found.length, start + "array length"); |
265 if (expected.length == found.length) { | 265 if (expected.length === found.length) { |
266 for (var i = 0; i < expected.length; ++i) { | 266 for (var i = 0; i < expected.length; ++i) { |
267 assertEquals(expected[i], found[i], | 267 assertEquals(expected[i], found[i], |
268 start + "array element at index " + i); | 268 start + "array element at index " + i); |
269 } | 269 } |
270 } | 270 } |
271 }; | 271 }; |
272 | 272 |
273 | 273 |
274 assertPropertiesEqual = function assertPropertiesEqual(expected, found, | 274 assertPropertiesEqual = function assertPropertiesEqual(expected, found, |
275 name_opt) { | 275 name_opt) { |
276 // Check properties only. | 276 // Check properties only. |
277 if (!deepObjectEquals(expected, found)) { | 277 if (!deepObjectEquals(expected, found)) { |
278 fail(expected, found, name_opt); | 278 fail(expected, found, name_opt); |
279 } | 279 } |
280 }; | 280 }; |
281 | 281 |
282 | 282 |
283 assertToStringEquals = function assertToStringEquals(expected, found, | 283 assertToStringEquals = function assertToStringEquals(expected, found, |
284 name_opt) { | 284 name_opt) { |
285 if (expected != String(found)) { | 285 if (expected !== String(found)) { |
286 fail(expected, found, name_opt); | 286 fail(expected, found, name_opt); |
287 } | 287 } |
288 }; | 288 }; |
289 | 289 |
290 | 290 |
291 assertTrue = function assertTrue(value, name_opt) { | 291 assertTrue = function assertTrue(value, name_opt) { |
292 assertEquals(true, value, name_opt); | 292 assertEquals(true, value, name_opt); |
293 }; | 293 }; |
294 | 294 |
295 | 295 |
(...skipping 12 matching lines...) Expand all Loading... |
308 assertNotNull = function assertNotNull(value, name_opt) { | 308 assertNotNull = function assertNotNull(value, name_opt) { |
309 if (value === null) { | 309 if (value === null) { |
310 fail("not null", value, name_opt); | 310 fail("not null", value, name_opt); |
311 } | 311 } |
312 }; | 312 }; |
313 | 313 |
314 | 314 |
315 assertThrows = function assertThrows(code, type_opt, cause_opt) { | 315 assertThrows = function assertThrows(code, type_opt, cause_opt) { |
316 var threwException = true; | 316 var threwException = true; |
317 try { | 317 try { |
318 if (typeof code == 'function') { | 318 if (typeof code === 'function') { |
319 code(); | 319 code(); |
320 } else { | 320 } else { |
321 eval(code); | 321 eval(code); |
322 } | 322 } |
323 threwException = false; | 323 threwException = false; |
324 } catch (e) { | 324 } catch (e) { |
325 if (typeof type_opt == 'function') { | 325 if (typeof type_opt === 'function') { |
326 assertInstanceof(e, type_opt); | 326 assertInstanceof(e, type_opt); |
327 } | 327 } |
328 if (arguments.length >= 3) { | 328 if (arguments.length >= 3) { |
329 assertEquals(e.type, cause_opt); | 329 assertEquals(e.type, cause_opt); |
330 } | 330 } |
331 // Success. | 331 // Success. |
332 return; | 332 return; |
333 } | 333 } |
334 throw new MjsUnitAssertionError("Did not throw exception"); | 334 throw new MjsUnitAssertionError("Did not throw exception"); |
335 }; | 335 }; |
336 | 336 |
337 | 337 |
338 assertInstanceof = function assertInstanceof(obj, type) { | 338 assertInstanceof = function assertInstanceof(obj, type) { |
339 if (!(obj instanceof type)) { | 339 if (!(obj instanceof type)) { |
340 var actualTypeName = null; | 340 var actualTypeName = null; |
341 var actualConstructor = Object.getPrototypeOf(obj).constructor; | 341 var actualConstructor = Object.getPrototypeOf(obj).constructor; |
342 if (typeof actualConstructor == "function") { | 342 if (typeof actualConstructor === "function") { |
343 actualTypeName = actualConstructor.name || String(actualConstructor); | 343 actualTypeName = actualConstructor.name || String(actualConstructor); |
344 } | 344 } |
345 fail("Object <" + PrettyPrint(obj) + "> is not an instance of <" + | 345 fail("Object <" + PrettyPrint(obj) + "> is not an instance of <" + |
346 (type.name || type) + ">" + | 346 (type.name || type) + ">" + |
347 (actualTypeName ? " but of < " + actualTypeName + ">" : "")); | 347 (actualTypeName ? " but of < " + actualTypeName + ">" : "")); |
348 } | 348 } |
349 }; | 349 }; |
350 | 350 |
351 | 351 |
352 assertDoesNotThrow = function assertDoesNotThrow(code, name_opt) { | 352 assertDoesNotThrow = function assertDoesNotThrow(code, name_opt) { |
353 try { | 353 try { |
354 if (typeof code == 'function') { | 354 if (typeof code === 'function') { |
355 code(); | 355 code(); |
356 } else { | 356 } else { |
357 eval(code); | 357 eval(code); |
358 } | 358 } |
359 } catch (e) { | 359 } catch (e) { |
360 fail("threw an exception: ", e.message || e, name_opt); | 360 fail("threw an exception: ", e.message || e, name_opt); |
361 } | 361 } |
362 }; | 362 }; |
363 | 363 |
364 assertUnreachable = function assertUnreachable(name_opt) { | 364 assertUnreachable = function assertUnreachable(name_opt) { |
(...skipping 14 matching lines...) Expand all Loading... |
379 "fun", "sync", "return %GetOptimizationStatus(fun, sync);"); | 379 "fun", "sync", "return %GetOptimizationStatus(fun, sync);"); |
380 } catch (e) { | 380 } catch (e) { |
381 throw new Error("natives syntax not allowed"); | 381 throw new Error("natives syntax not allowed"); |
382 } | 382 } |
383 } | 383 } |
384 return OptimizationStatusImpl(fun, sync_opt); | 384 return OptimizationStatusImpl(fun, sync_opt); |
385 } | 385 } |
386 | 386 |
387 assertUnoptimized = function assertUnoptimized(fun, sync_opt, name_opt) { | 387 assertUnoptimized = function assertUnoptimized(fun, sync_opt, name_opt) { |
388 if (sync_opt === undefined) sync_opt = ""; | 388 if (sync_opt === undefined) sync_opt = ""; |
389 assertTrue(OptimizationStatus(fun, sync_opt) != 1, name_opt); | 389 assertTrue(OptimizationStatus(fun, sync_opt) !== 1, name_opt); |
390 } | 390 } |
391 | 391 |
392 assertOptimized = function assertOptimized(fun, sync_opt, name_opt) { | 392 assertOptimized = function assertOptimized(fun, sync_opt, name_opt) { |
393 if (sync_opt === undefined) sync_opt = ""; | 393 if (sync_opt === undefined) sync_opt = ""; |
394 assertTrue(OptimizationStatus(fun, sync_opt) != 2, name_opt); | 394 assertTrue(OptimizationStatus(fun, sync_opt) !== 2, name_opt); |
395 } | 395 } |
396 | 396 |
397 })(); | 397 })(); |
OLD | NEW |