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