OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 | 6 |
7 // Dart test for deeply nested generic types. | 7 // Dart test for deeply nested generic types. |
8 | 8 |
9 /** A natural number aka Peano number. */ | 9 /** A natural number aka Peano number. */ |
10 abstract class N { | 10 abstract class N { |
11 N add1(); | 11 N add1(); |
12 N sub1(); | 12 N sub1(); |
13 } | 13 } |
14 | 14 |
15 /** Zero element. */ | 15 /** Zero element. */ |
16 class Z implements N { | 16 class Z implements N { |
17 Z(); | 17 Z(); |
18 N add1() { return new S<Z>(this); } | 18 N add1() { |
19 N sub1() { throw "Error: sub1(0)"; } | 19 return new S<Z>(this); |
| 20 } |
| 21 |
| 22 N sub1() { |
| 23 throw "Error: sub1(0)"; |
| 24 } |
20 } | 25 } |
21 | 26 |
22 /** Successor element. */ | 27 /** Successor element. */ |
23 class S<K> implements N { | 28 class S<K> implements N { |
24 N before; | 29 N before; |
25 S(this.before); | 30 S(this.before); |
26 N add1() { return new S<S<K>>(this); } | 31 N add1() { |
| 32 return new S<S<K>>(this); |
| 33 } |
| 34 |
27 N sub1() { | 35 N sub1() { |
28 // It would be super cool if this could be "new K()". | 36 // It would be super cool if this could be "new K()". |
29 return before; | 37 return before; |
30 } | 38 } |
31 } | 39 } |
32 | 40 |
33 N NFromInt(int x) { | 41 N NFromInt(int x) { |
34 if (x == 0) | 42 if (x == 0) |
35 return new Z(); | 43 return new Z(); |
36 else | 44 else |
37 return NFromInt(x - 1).add1(); | 45 return NFromInt(x - 1).add1(); |
38 } | 46 } |
39 | 47 |
40 int IntFromN(N x) { | 48 int IntFromN(N x) { |
41 if (x is Z) | 49 if (x is Z) return 0; |
42 return 0; | 50 if (x is S) return IntFromN(x.sub1()) + 1; |
43 if (x is S) | |
44 return IntFromN(x.sub1()) + 1; | |
45 throw "Error"; | 51 throw "Error"; |
46 } | 52 } |
47 | 53 |
48 bool IsEven(N x) { | 54 bool IsEven(N x) { |
49 if (x is Z) return true; | 55 if (x is Z) return true; |
50 if (x is S<Z>) return false; | 56 if (x is S<Z>) return false; |
51 if (x is S<S>) return IsEven(x.sub1().sub1()); | 57 if (x is S<S>) return IsEven(x.sub1().sub1()); |
52 throw "Error in IsEven"; | 58 throw "Error in IsEven"; |
53 } | 59 } |
54 | 60 |
55 main() { | 61 main() { |
56 Expect.isTrue(NFromInt(0) is Z); | 62 Expect.isTrue(NFromInt(0) is Z); |
57 Expect.isTrue(NFromInt(1) is S<Z>); | 63 Expect.isTrue(NFromInt(1) is S<Z>); |
58 Expect.isTrue(NFromInt(2) is S<S<Z>>); | 64 Expect.isTrue(NFromInt(2) is S<S<Z>>); |
59 Expect.isTrue(NFromInt(3) is S<S<S<Z>>>); | 65 Expect.isTrue(NFromInt(3) is S<S<S<Z>>>); |
60 Expect.isTrue(NFromInt(10) is S<S<S<S<S<S<S<S<S<S<Z>>>>>>>>>>); | 66 Expect.isTrue(NFromInt(10) is S<S<S<S<S<S<S<S<S<S<Z>>>>>>>>>>); |
61 | 67 |
62 // Negative tests. | 68 // Negative tests. |
63 Expect.isTrue(NFromInt(0) is !S); | 69 Expect.isTrue(NFromInt(0) is! S); |
64 Expect.isTrue(NFromInt(1) is !Z); | 70 Expect.isTrue(NFromInt(1) is! Z); |
65 Expect.isTrue(NFromInt(1) is !S<S>); | 71 Expect.isTrue(NFromInt(1) is! S<S>); |
66 Expect.isTrue(NFromInt(2) is !Z); | 72 Expect.isTrue(NFromInt(2) is! Z); |
67 Expect.isTrue(NFromInt(2) is !S<Z>); | 73 Expect.isTrue(NFromInt(2) is! S<Z>); |
68 Expect.isTrue(NFromInt(2) is !S<S<S>>); | 74 Expect.isTrue(NFromInt(2) is! S<S<S>>); |
69 | 75 |
70 // Greater-than tests | 76 // Greater-than tests |
71 Expect.isTrue(NFromInt(4) is S<S>); // 4 >= 2 | 77 Expect.isTrue(NFromInt(4) is S<S>); // 4 >= 2 |
72 Expect.isTrue(NFromInt(4) is S<S<S>>); // 4 >= 3 | 78 Expect.isTrue(NFromInt(4) is S<S<S>>); // 4 >= 3 |
73 Expect.isTrue(NFromInt(4) is S<S<S<S>>>); // 4 >= 4 | 79 Expect.isTrue(NFromInt(4) is S<S<S<S>>>); // 4 >= 4 |
74 Expect.isTrue(NFromInt(4) is !S<S<S<S<S>>>>); // 4 < 5 | 80 Expect.isTrue(NFromInt(4) is! S<S<S<S<S>>>>); // 4 < 5 |
75 | 81 |
76 Expect.isTrue(IsEven(NFromInt(0))); | 82 Expect.isTrue(IsEven(NFromInt(0))); |
77 Expect.isFalse(IsEven(NFromInt(1))); | 83 Expect.isFalse(IsEven(NFromInt(1))); |
78 Expect.isTrue(IsEven(NFromInt(2))); | 84 Expect.isTrue(IsEven(NFromInt(2))); |
79 Expect.isFalse(IsEven(NFromInt(3))); | 85 Expect.isFalse(IsEven(NFromInt(3))); |
80 Expect.isTrue(IsEven(NFromInt(4))); | 86 Expect.isTrue(IsEven(NFromInt(4))); |
81 | 87 |
82 Expect.equals(0, IntFromN(NFromInt(0))); | 88 Expect.equals(0, IntFromN(NFromInt(0))); |
83 Expect.equals(1, IntFromN(NFromInt(1))); | 89 Expect.equals(1, IntFromN(NFromInt(1))); |
84 Expect.equals(2, IntFromN(NFromInt(2))); | 90 Expect.equals(2, IntFromN(NFromInt(2))); |
85 Expect.equals(50, IntFromN(NFromInt(50))); | 91 Expect.equals(50, IntFromN(NFromInt(50))); |
86 } | 92 } |
OLD | NEW |