OLD | NEW |
| (Empty) |
1 // Copyright (c) 2016, 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 // Tests of loops. | |
6 | |
7 import 'expect.dart'; | |
8 | |
9 fact(n) { | |
10 var f = 1; | |
11 while (n > 0) { | |
12 f *= n; | |
13 --n; | |
14 } | |
15 return f; | |
16 } | |
17 | |
18 fib(n) { | |
19 if (n == 0) return 0; | |
20 var previous = 0, current = 1; | |
21 while (n > 1) { | |
22 var temp = current; | |
23 current += previous; | |
24 previous = temp; | |
25 --n; | |
26 } | |
27 return current; | |
28 } | |
29 | |
30 mkTrue() => true; | |
31 mkFalse() => false; | |
32 | |
33 check(b) { | |
34 Expect.isTrue(b); | |
35 return b; | |
36 } | |
37 | |
38 test0() { | |
39 while (mkTrue()) { | |
40 Expect.isTrue(true); | |
41 return; | |
42 } | |
43 Expect.isTrue(false); | |
44 } | |
45 | |
46 test1() { | |
47 while (mkFalse()) { | |
48 Expect.isTrue(false); | |
49 } | |
50 Expect.isTrue(true); | |
51 } | |
52 | |
53 test2() { | |
54 do { | |
55 Expect.isTrue(true); | |
56 } while (mkFalse()); | |
57 Expect.isTrue(true); | |
58 } | |
59 | |
60 test3() { | |
61 do { | |
62 Expect.isTrue(true); | |
63 return; | |
64 } while (check(false)); | |
65 Expect.isTrue(false); | |
66 } | |
67 | |
68 main() { | |
69 Expect.isTrue(fact(0) == 1); | |
70 Expect.isTrue(fact(1) == 1); | |
71 Expect.isTrue(fact(5) == 120); | |
72 Expect.isTrue(fact(42) == | |
73 1405006117752879898543142606244511569936384000000000); | |
74 Expect.isTrue(fact(3.14159) == 1.0874982674320444); | |
75 | |
76 Expect.isTrue(fib(0) == 0); | |
77 Expect.isTrue(fib(1) == 1); | |
78 Expect.isTrue(fib(2) == 1); | |
79 Expect.isTrue(fib(3) == 2); | |
80 Expect.isTrue(fib(4) == 3); | |
81 Expect.isTrue(fib(5) == 5); | |
82 Expect.isTrue(fib(6) == 8); | |
83 Expect.isTrue(fib(7) == 13); | |
84 Expect.isTrue(fib(42) == 267914296); | |
85 Expect.isTrue(fib(3.14159) == 3); | |
86 | |
87 test0(); | |
88 test1(); | |
89 test2(); | |
90 test3(); | |
91 } | |
OLD | NEW |