Merge: doc: fixed some typos and other misc. corrections
[nit.git] / tests / test_ffi_c_fibonacci.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2011-2013 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 class FibonacciEngine
18 fun fibonacci( n : Int ) : Int import fibonacci `{
19 if ( n == 0 )
20 return 0;
21 if ( n == 1 )
22 return 1;
23 else
24 return FibonacciEngine_fibonacci( self, n-1 ) + FibonacciEngine_fibonacci( self, n-2 );
25 `}
26 end
27
28 redef class Int
29 fun fibonacci : Int import fibonacci `{
30 if ( self == 0 )
31 return 0;
32 else if ( self == 1 )
33 return 1;
34 else
35 return Int_fibonacci( self-1 ) + Int_fibonacci( self-2 );
36 `}
37 end
38
39 var engine = new FibonacciEngine
40 print engine.fibonacci( 0 )
41 print engine.fibonacci( 1 )
42 print engine.fibonacci( 5 )
43 print engine.fibonacci( 12 )
44 # print engine.fibonacci( 123 )
45
46 print 0.fibonacci
47 print 1.fibonacci
48 print 5.fibonacci
49 print 12.fibonacci
50 # print 123.fibonacci