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