]>
Commit | Line | Data |
---|---|---|
ed1e77d3 A |
1 | /* |
2 | * Copyright (C) 2015 Apple Inc. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * 1. Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * | |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 | */ | |
25 | ||
26 | var funcArgAndBodyStr = | |
27 | "(arr) {" + "\n" + | |
28 | " var sum = 0;" + "\n" + | |
29 | " for (var i in arr)" + "\n" + | |
30 | " sum += arr[i];" + "\n" + | |
31 | " return sum;" + "\n" + | |
32 | "}"; | |
33 | ||
34 | var testData = { | |
35 | "ArrayWithUndecided": { in: [], out: 0 }, | |
36 | "ArrayWithInt32": { in: [ 1, 2, 3 ], out: 6 }, | |
37 | "ArrayWithContiguous": { in: [ "a", "b", "c" ], out: "0abc" }, | |
38 | "ArrayWithDouble": { in: [10.25, 20.25, 30.25 ], out: 60.75 }, | |
39 | "ArrayWithArrayStorage": { in: [ "a", "b", "c" ], out: "0abc1000" }, // The in array will be augmented below. | |
40 | "ArrayWithSlowPutArrayStorage": { in: [ "a", "b", "c" ], out: "0abc10" }, // the in array will be augmented below. | |
41 | ||
42 | "NonArrayWithUndecided": { in: {}, out: 0 }, | |
43 | "NonArrayWithInt32": { in: { "0":1, "1":2, "2":3 }, out: 6 }, | |
44 | "NonArrayWithContiguous": { in: { "0":"a", "1":"b", "2":"c" }, out: "0abc" }, | |
45 | "NonArrayWithDouble": { in: { "0":10.25, "1":20.25, "2":30.25 }, out: 60.75 }, | |
46 | "NonArrayWithArrayStorage": { in: { "0":"a", "1":"b", "2":"c" }, out: "0abc1000" }, // The in obj will be augmented below. | |
47 | "NonArrayWithSlowPutArrayStorage": { in: { "0":"a", "1":"b", "2":"c" }, out: "0abc10" }, // the in obj will be augmented below. | |
48 | }; | |
49 | ||
50 | ||
51 | var o = { a: 10 }; | |
52 | Object.defineProperties(o, { | |
53 | "0": { | |
54 | get: function() { return this.a; }, | |
55 | set: function(x) { this.a = x; }, | |
56 | }, | |
57 | }); | |
58 | ||
59 | testData["ArrayWithArrayStorage"].in[1000] = 1000; | |
60 | testData["ArrayWithSlowPutArrayStorage"].in.__proto__ = o; | |
61 | testData["NonArrayWithArrayStorage"].in["1000"] = 1000; | |
62 | testData["NonArrayWithSlowPutArrayStorage"].in.__proto__ = o; | |
63 | ||
64 | var numberOfFailures = 0; | |
65 | ||
66 | function test(name, data) { | |
67 | eval("function " + name + funcArgAndBodyStr); | |
68 | noInline(name); | |
69 | ||
70 | var failed = false; | |
71 | var previousResult; | |
72 | for (var i = 0; i < 10000; ++i) { | |
73 | var expected = data.out; | |
74 | var actual = eval(name + "(data.in)"); | |
75 | ||
76 | if ((actual != expected) && (actual != previousResult)) { | |
77 | print("FAIL: " + name + ": expected: " + expected + ", actual: " + actual + ", starting @ loop iteration " + i); | |
78 | previousResult = actual; | |
79 | failed = true; | |
80 | numberOfFailures++; | |
81 | } | |
82 | } | |
83 | } | |
84 | ||
85 | for (name in testData) | |
86 | test(name, testData[name]); | |
87 | ||
88 | if (numberOfFailures) | |
89 | throw "Error: number of failures found: " + numberOfFailures; |