OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --harmony-types | 5 // Flags: --harmony-types |
6 | 6 |
7 | 7 |
| 8 load("test/mjsunit/harmony/typesystem/testgen.js"); |
| 9 |
| 10 |
8 // In all the following functions, the size parameter (positive integer) | 11 // In all the following functions, the size parameter (positive integer) |
9 // denotes how many test cases will be tried. The variable test_size | 12 // denotes how many test cases will be tried. The variable test_size |
10 // controls execution time of this test. It should not be too high. | 13 // controls execution time of this test. It should not be too high. |
11 let test_size = 1000; | 14 let test_size = 1000; |
12 | 15 |
13 function CheckValid(type) { | |
14 // print("V:", type); | |
15 assertDoesNotThrow("'use types'; var x: " + type + ";"); | |
16 } | |
17 | |
18 function CheckInvalid(type) { | |
19 // print("I:", type); | |
20 assertThrows("'use types'; var x: " + type + ";", SyntaxError); | |
21 } | |
22 | |
23 // Parameters: | |
24 // multiplicity: positive integer | |
25 // generator: (size: positive integer, ...params: any[]) => Iterator<SomeType> | |
26 // transformations: [false | ((t: SomeType) => OtherType)] | |
27 // params: any[] | |
28 // | |
29 // A "test generator" object will have its "generator" generate tests, | |
30 // will transform each generated test with all of its "transformations" | |
31 // and will yield the results. false transformations will be ignored. | |
32 class TestGen { | |
33 constructor(multiplicity, generator, transformations, ...params) { | |
34 // Determines how often this generator will run, compared to other ones. | |
35 this.multiplicity = multiplicity; | |
36 // The generator function. It will be called with (size, ...params), | |
37 // for an appropriate size provided externally to method initialize. | |
38 this.generator = generator; | |
39 // The list of transformation functions, excluding false ones. | |
40 this.transformations = transformations.filter(f => f !== false); | |
41 // The optional parameters to be passed to the generator function. | |
42 this.params = params; | |
43 } | |
44 // Returns how many tests this test generator is expected to yield | |
45 // in a row. | |
46 weight() { | |
47 return this.multiplicity * this.transformations.length; | |
48 } | |
49 // Initialize the generator function. | |
50 initialize(size) { | |
51 this.factory = this.generator(size, ...this.params); | |
52 } | |
53 // Is the test generator exhausted? | |
54 exhausted() { | |
55 return this.factory === null; | |
56 } | |
57 // Return a generator that will yield up to weight tests. | |
58 // It returns an Iterator<OtherType>. | |
59 * [Symbol.iterator]() { | |
60 for (let i = 0; i < this.multiplicity; i++) { | |
61 let element = this.factory.next(); | |
62 if (element.done) { | |
63 this.factory = null; | |
64 return; | |
65 } | |
66 for (let f of this.transformations) yield f(element.value); | |
67 } | |
68 } | |
69 } | |
70 | |
71 // Parameters: | |
72 // size: positive integer | |
73 // generators: [false | string | TestGen] | |
74 // | |
75 // This generator function will yield up to size tests. It will iterate | |
76 // cyclically through the list of test generators. A string in this list | |
77 // behaves as a simple generator yielding just one test; a false value | |
78 // behaves as a generator yielding nothing. For every test generator in | |
79 // the list, this function will generate as many tests as its weight | |
80 // before proceeding to the next test generator. Once a generator is | |
81 // exhausted, it is ignored in subsequent iterations. | |
82 function* Generate(size, generators) { | |
83 let fixed = 0, flexible = 0; | |
84 for (let gen of generators) { | |
85 if (gen === false) continue; | |
86 if (typeof gen === "string") fixed++; | |
87 else flexible += gen.weight(); | |
88 } | |
89 if (fixed + flexible == 0) throw "Empty list of test generators"; | |
90 let remaining = 0; | |
91 for (let gen of generators) { | |
92 if (gen === false) continue; | |
93 if (typeof gen !== "string") { | |
94 let weight = 1 + gen.weight(); | |
95 gen.initialize(Math.ceil(size * weight / flexible)); | |
96 remaining++; | |
97 } | |
98 } | |
99 for (let once = true; true; once = false) { | |
100 if (remaining == 0) return; | |
101 for (let gen of generators) { | |
102 if (gen === false) continue; | |
103 if (typeof gen === "string") { | |
104 if (once) { | |
105 if (size-- <= 0) return; else yield gen; | |
106 } | |
107 continue; | |
108 } | |
109 if (gen.exhausted()) continue; | |
110 for (let test of gen) { | |
111 if (size-- <= 0) return; else yield test; | |
112 } | |
113 if (gen.exhausted()) remaining--; | |
114 } | |
115 } | |
116 } | |
117 | |
118 // Parameters: | |
119 // size: positive integer | |
120 // generators: [string | TestGen] | |
121 // | |
122 // This function will generate all tests yielded by Generate and will | |
123 // discard the results. It will normally be called with test generators | |
124 // whose transformation functions test for validity (e.g. CheckValid or | |
125 // CheckInvalid) and do not return anything interesting. | |
126 function Test(size, generators) { | |
127 for (let attempt of Generate(size, generators)) continue; | |
128 } | |
129 | |
130 | 16 |
131 // In the rest, for each NonTerminal symbol in the parser grammar that we | 17 // In the rest, for each NonTerminal symbol in the parser grammar that we |
132 // care to test, there are two generator functions (ValidNonTerminal and | 18 // care to test, there are two generator functions (ValidNonTerminal and |
133 // InvalidNonTerminal) yielding valid and non valid terms for this symbol. | 19 // InvalidNonTerminal) yielding valid and non valid terms for this symbol. |
134 // These functions are of the form to be passed to Generate. | 20 // These functions are of the form to be passed to Generate. |
135 // There is also a test (using the TestNonTerminal function). | 21 // There is also a test (using the TestNonTerminal function). |
136 | 22 |
137 | 23 |
138 // Primary types. | 24 // Primary types. |
139 | 25 |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 new TestGen(1, ValidFunctionTypes, [t => t], false), | 204 new TestGen(1, ValidFunctionTypes, [t => t], false), |
319 ]); | 205 ]); |
320 } | 206 } |
321 | 207 |
322 function InvalidTypes(size) { | 208 function InvalidTypes(size) { |
323 return Generate(size, [ | 209 return Generate(size, [ |
324 new TestGen(3, InvalidUnionTypes, [t => t]), | 210 new TestGen(3, InvalidUnionTypes, [t => t]), |
325 new TestGen(1, InvalidFunctionTypes, [t => t], false), | 211 new TestGen(1, InvalidFunctionTypes, [t => t], false), |
326 ]); | 212 ]); |
327 } | 213 } |
OLD | NEW |