nitc: fix calling extern constructors from extern code in separate compiler
[nit.git] / tests / bench_complex_sort.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2006-2008 Jean Privat <jean@pryen.org>
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
18 interface Elt
19 fun val1: Int is abstract
20 fun val2: Int do return val1
21 end
22
23 class A
24 super Elt
25 var a: Int
26 redef fun val1: Int do return _a
27
28 init(i: Int) do _a = i
29 end
30
31 class Elt2
32 super Elt
33 var b: Int
34 redef fun val1: Int do return _b/2
35 redef fun val2: Int do return _b
36 init initelt2(i: Int) do _b = i
37 end
38
39 class B
40 super Elt2
41 init(i: Int) do initelt2(i)
42 end
43
44 class C
45 super Elt
46 var c: Int
47 var d: Int
48 redef fun val1: Int do return _c end
49 redef fun val2: Int do return _d end
50
51 init init2(i: Int, j: Int) do
52 _c = i
53 _d = j
54 end
55 end
56
57 class D
58 super A
59 super Elt2
60 redef fun val1: Int do return _a end
61 redef fun val2: Int do return _b end
62
63 init init2(i: Int, j: Int) do
64 init(i)
65 initelt2(j)
66 end
67 end
68
69 class E
70 super Elt2
71 redef fun val1: Int do return 5 end
72
73 init(i: Int) do initelt2(i)
74 end
75
76 class EltComparator
77 super Comparator
78 redef type COMPARED: Elt
79 redef fun compare(a, b)
80 do
81 if _is_val1 then
82 return a.val1 <=> b.val1
83 else
84 return a.val2 <=> b.val2
85 end
86 end
87
88 fun toggle
89 do
90 _is_val1 = not _is_val1
91 end
92
93 var is_val1: Bool = false
94
95 init do end
96 end
97
98 fun generator: Elt
99 do
100 var r = 5.rand
101 if r == 0 then
102 return new A(10.rand)
103 else if r == 1 then
104 return new B(10.rand)
105 else if r == 2 then
106 return new C.init2(10.rand, 10.rand)
107 else if r == 3 then
108 return new D.init2(10.rand, 10.rand)
109 else
110 return new E(10.rand)
111 end
112 end
113
114 var n = 100
115
116 if not args.is_empty then
117 n = args.first.to_i
118 end
119
120 var array = new Array[Elt].with_capacity(n)
121 for i in [0..n[ do
122 array.push(generator)
123 end
124
125 var comparator = new EltComparator
126 for i in [0..n[ do
127 comparator.sort(array)
128 comparator.toggle
129 end
130