Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1815)

Unified Diff: test/mjsunit/harmony/typesystem/tuple-types.js

Issue 1810943002: Add parsing for tuple types (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@types
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: test/mjsunit/harmony/typesystem/tuple-types.js
diff --git a/test/mjsunit/harmony/typesystem/simple-types.js b/test/mjsunit/harmony/typesystem/tuple-types.js
similarity index 51%
copy from test/mjsunit/harmony/typesystem/simple-types.js
copy to test/mjsunit/harmony/typesystem/tuple-types.js
index 5c09802050235d94bf14a41506a61e8676ae6349..8d6cc66affbae5a812479d767207f191d5821ed8 100644
--- a/test/mjsunit/harmony/typesystem/simple-types.js
+++ b/test/mjsunit/harmony/typesystem/tuple-types.js
@@ -5,128 +5,14 @@
// Flags: --harmony-types
+load("test/mjsunit/harmony/typesystem/testgen.js");
+
+
// In all the following functions, the size parameter (positive integer)
// denotes how many test cases will be tried. The variable test_size
// controls execution time of this test. It should not be too high.
let test_size = 1000;
-function CheckValid(type) {
- // print("V:", type);
- assertDoesNotThrow("'use types'; var x: " + type + ";");
-}
-
-function CheckInvalid(type) {
- // print("I:", type);
- assertThrows("'use types'; var x: " + type + ";", SyntaxError);
-}
-
-// Parameters:
-// multiplicity: positive integer
-// generator: (size: positive integer, ...params: any[]) => Iterator<SomeType>
-// transformations: [false | ((t: SomeType) => OtherType)]
-// params: any[]
-//
-// A "test generator" object will have its "generator" generate tests,
-// will transform each generated test with all of its "transformations"
-// and will yield the results. false transformations will be ignored.
-class TestGen {
- constructor(multiplicity, generator, transformations, ...params) {
- // Determines how often this generator will run, compared to other ones.
- this.multiplicity = multiplicity;
- // The generator function. It will be called with (size, ...params),
- // for an appropriate size provided externally to method initialize.
- this.generator = generator;
- // The list of transformation functions, excluding false ones.
- this.transformations = transformations.filter(f => f !== false);
- // The optional parameters to be passed to the generator function.
- this.params = params;
- }
- // Returns how many tests this test generator is expected to yield
- // in a row.
- weight() {
- return this.multiplicity * this.transformations.length;
- }
- // Initialize the generator function.
- initialize(size) {
- this.factory = this.generator(size, ...this.params);
- }
- // Is the test generator exhausted?
- exhausted() {
- return this.factory === null;
- }
- // Return a generator that will yield up to weight tests.
- // It returns an Iterator<OtherType>.
- * [Symbol.iterator]() {
- for (let i = 0; i < this.multiplicity; i++) {
- let element = this.factory.next();
- if (element.done) {
- this.factory = null;
- return;
- }
- for (let f of this.transformations) yield f(element.value);
- }
- }
-}
-
-// Parameters:
-// size: positive integer
-// generators: [false | string | TestGen]
-//
-// This generator function will yield up to size tests. It will iterate
-// cyclically through the list of test generators. A string in this list
-// behaves as a simple generator yielding just one test; a false value
-// behaves as a generator yielding nothing. For every test generator in
-// the list, this function will generate as many tests as its weight
-// before proceeding to the next test generator. Once a generator is
-// exhausted, it is ignored in subsequent iterations.
-function* Generate(size, generators) {
- let fixed = 0, flexible = 0;
- for (let gen of generators) {
- if (gen === false) continue;
- if (typeof gen === "string") fixed++;
- else flexible += gen.weight();
- }
- if (fixed + flexible == 0) throw "Empty list of test generators";
- let remaining = 0;
- for (let gen of generators) {
- if (gen === false) continue;
- if (typeof gen !== "string") {
- let weight = 1 + gen.weight();
- gen.initialize(Math.ceil(size * weight / flexible));
- remaining++;
- }
- }
- for (let once = true; true; once = false) {
- if (remaining == 0) return;
- for (let gen of generators) {
- if (gen === false) continue;
- if (typeof gen === "string") {
- if (once) {
- if (size-- <= 0) return; else yield gen;
- }
- continue;
- }
- if (gen.exhausted()) continue;
- for (let test of gen) {
- if (size-- <= 0) return; else yield test;
- }
- if (gen.exhausted()) remaining--;
- }
- }
-}
-
-// Parameters:
-// size: positive integer
-// generators: [string | TestGen]
-//
-// This function will generate all tests yielded by Generate and will
-// discard the results. It will normally be called with test generators
-// whose transformation functions test for validity (e.g. CheckValid or
-// CheckInvalid) and do not return anything interesting.
-function Test(size, generators) {
- for (let attempt of Generate(size, generators)) continue;
-}
-
// In the rest, for each NonTerminal symbol in the parser grammar that we
// care to test, there are two generator functions (ValidNonTerminal and
@@ -181,13 +67,6 @@ function InvalidPrimaryTypes(size, proper=false) {
]);
}
-(function TestPrimaryTypes(size) {
- Test(size, [
- new TestGen(4, ValidPrimaryTypes, [CheckValid], true),
- new TestGen(1, InvalidPrimaryTypes, [CheckInvalid], true)
- ]);
-})(test_size);
-
// Intersection types.
@@ -219,13 +98,6 @@ function InvalidIntersectionTypes(size, proper=false) {
]);
}
-(function TestIntersectionTypes(size) {
- Test(size, [
- new TestGen(4, ValidIntersectionTypes, [CheckValid], true),
- new TestGen(1, InvalidIntersectionTypes, [CheckInvalid], true)
- ]);
-})(test_size);
-
// Union types.
@@ -257,13 +129,6 @@ function InvalidUnionTypes(size, proper=false) {
]);
}
-(function TestUnionTypes(size) {
- Test(size, [
- new TestGen(4, ValidUnionTypes, [CheckValid], true),
- new TestGen(1, InvalidUnionTypes, [CheckInvalid], true)
- ]);
-})(test_size);
-
// Function and constructor types.
@@ -300,17 +165,50 @@ function InvalidFunctionTypes(size, constr) {
]);
}
-(function TestFunctionAndConstructorTypes(size) {
+
+// Tuple types.
+
+function ValidTupleTypes(size, proper=false) {
+ return Generate(size, [
+ new TestGen(1, ValidTypes, [
+ !proper && (t => t),
+ t => "[" + t + "]",
+ t => "[" + t + ", " + t + "]",
+ t => "[" + t + ", " + t + ", " + t + "]"
+ ])
+ ]);
+}
+
+function InvalidTupleTypes(size, proper=false) {
+ return Generate(size, [
+ // Illegal types in legal places.
+ new TestGen(1, InvalidTypes, [
+ !proper && (t => t),
+ t => "[" + t + "]",
+ t => "[" + t + ", " + t + "]",
+ t => "[" + t + ", " + t + ", " + t + "]"
+ ]),
+ // Valid binding patterns that are not tuple types.
+ "[]",
+ new TestGen(1, ValidTypes, [t => "[" + t + ",]"]),
+ new TestGen(1, ValidTypes, [t => "[" + t + ",,]"]),
+ new TestGen(1, ValidTypes, [t => "[," + t + "]"]),
+ new TestGen(1, ValidTypes, [t => "[,," + t + "]"]),
+ new TestGen(1, ValidTypes, [t => "[" + t + ",," + t + "]"]),
+ new TestGen(1, ValidTypes, [t => "[..." + t + "]"]),
+ new TestGen(1, ValidTypes, [t => "[" + t + ", ..." + t + "]"]),
+ ]);
+}
+
+(function TestTupleTypes(size) {
Test(size, [
- new TestGen(4, ValidFunctionTypes, [CheckValid], false),
- new TestGen(1, InvalidFunctionTypes, [CheckInvalid], false),
- new TestGen(4, ValidFunctionTypes, [CheckValid], true),
- new TestGen(1, InvalidFunctionTypes, [CheckInvalid], true)
+ new TestGen(4, ValidTupleTypes, [CheckValid], true),
+ new TestGen(1, InvalidTupleTypes, [CheckInvalid], true)
]);
})(test_size);
-// All simple types.
+// All types.
function ValidTypes(size) {
return Generate(size, [
« test/mjsunit/harmony/typesystem/testgen.js ('K') | « test/mjsunit/harmony/typesystem/testgen.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698