mirror of
https://github.com/dart-lang/sdk
synced 2024-09-15 21:10:22 +00:00
Test for deeply nested parameterized types.
BUG= TEST= Review URL: https://chromereviews.googleplex.com/3543014 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@171 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
parent
8dda0078b8
commit
56cc8ce66f
84
tests/language/src/GenericDeepTest.dart
Normal file
84
tests/language/src/GenericDeepTest.dart
Normal file
|
@ -0,0 +1,84 @@
|
|||
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
// Dart test for deeply nested generic types.
|
||||
|
||||
/** A natural number aka Peano number. */
|
||||
interface N {
|
||||
N add1();
|
||||
N sub1();
|
||||
}
|
||||
|
||||
/** Zero element. */
|
||||
class Z implements N {
|
||||
Z();
|
||||
N add1() { return new S<Z>(this); }
|
||||
N sub1() { throw "Error: sub1(0)"; }
|
||||
}
|
||||
|
||||
/** Successor element. */
|
||||
class S<K> implements N {
|
||||
N before;
|
||||
S(this.before);
|
||||
N add1() { return new S<S<K>>(this); }
|
||||
N sub1() {
|
||||
// It would be super cool if this could be "new K()".
|
||||
return before;
|
||||
}
|
||||
}
|
||||
|
||||
N NFromInt(int x) {
|
||||
if (x == 0)
|
||||
return new Z();
|
||||
else
|
||||
return NFromInt(x - 1).add1();
|
||||
}
|
||||
|
||||
int IntFromN(N x) {
|
||||
if (x is Z)
|
||||
return 0;
|
||||
if (x is S)
|
||||
return IntFromN(x.sub1()) + 1;
|
||||
throw "Error";
|
||||
}
|
||||
|
||||
bool IsEven(N x) {
|
||||
if (x is Z) return true;
|
||||
if (x is S<Z>) return false;
|
||||
if (x is S<S>) return IsEven(x.sub1().sub1());
|
||||
throw "Error in IsEven";
|
||||
}
|
||||
|
||||
main() {
|
||||
Expect.isTrue(NFromInt(0) is Z);
|
||||
Expect.isTrue(NFromInt(1) is S<Z>);
|
||||
Expect.isTrue(NFromInt(2) is S<S<Z>>);
|
||||
Expect.isTrue(NFromInt(3) is S<S<S<Z>>>);
|
||||
Expect.isTrue(NFromInt(10) is S<S<S<S<S<S<S<S<S<S<Z>>>>>>>>>>);
|
||||
|
||||
// Negative tests.
|
||||
Expect.isTrue(NFromInt(0) is !S);
|
||||
Expect.isTrue(NFromInt(1) is !Z);
|
||||
Expect.isTrue(NFromInt(1) is !S<S>);
|
||||
Expect.isTrue(NFromInt(2) is !Z);
|
||||
Expect.isTrue(NFromInt(2) is !S<Z>);
|
||||
Expect.isTrue(NFromInt(2) is !S<S<S>>);
|
||||
|
||||
// Greater-than tests
|
||||
Expect.isTrue(NFromInt(4) is S<S>); // 4 >= 2
|
||||
Expect.isTrue(NFromInt(4) is S<S<S>>); // 4 >= 3
|
||||
Expect.isTrue(NFromInt(4) is S<S<S<S>>>); // 4 >= 4
|
||||
Expect.isTrue(NFromInt(4) is !S<S<S<S<S>>>>); // 4 < 5
|
||||
|
||||
Expect.isTrue(IsEven(NFromInt(0)));
|
||||
Expect.isFalse(IsEven(NFromInt(1)));
|
||||
Expect.isTrue(IsEven(NFromInt(2)));
|
||||
Expect.isFalse(IsEven(NFromInt(3)));
|
||||
Expect.isTrue(IsEven(NFromInt(4)));
|
||||
|
||||
Expect.equals(0, IntFromN(NFromInt(0)));
|
||||
Expect.equals(1, IntFromN(NFromInt(1)));
|
||||
Expect.equals(2, IntFromN(NFromInt(2)));
|
||||
Expect.equals(50, IntFromN(NFromInt(50)));
|
||||
}
|
Loading…
Reference in a new issue