update NOTICE and LICENSE
[nit.git] / tests / base_closure_forms.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 import kernel
18
19 class Array[E]
20 fun sort
21 !cmp(a, b: E): Int
22 do
23 var e1 = _e1
24 var e2 = _e2
25 if cmp(e1, e2) > 0 then
26 _e1 = e2
27 _e2 = e1
28 end
29 end
30
31 fun iterate
32 !each(e: E)
33 do
34 each(_e1)
35 each(_e2)
36 end
37
38 var _e1: E
39 var _e2: E
40
41 init(e1, e2: E)
42 do
43 _e1 = e1
44 _e2 = e2
45 end
46
47 redef fun output
48 do
49 '['.output
50 _e1.output
51 ','.output
52 _e2.output
53 ']'.output
54 '\n'.output
55 end
56 end
57
58 class Map[K, V]
59 fun [](k: K): V
60 !def: V do abort
61 do
62 if _k == k then return _v
63 var n = _next
64 if n != null then return n[k] !def do continue def
65 var v = def
66 _next = new Map[K, V](k, v)
67 return v
68 end
69 fun has_key(k: K): Bool
70 do
71 if _k == k then return true
72 var n = _next
73 if n != null then return n.has_key(k) else return false
74 end
75 fun iterate
76 !each2(k: K, v: V)
77 do
78 var n: nullable Map[K, V] = self
79 while n != null do
80 each2(n._k, n._v)
81 n = n._next
82 end
83 end
84 var _k: K
85 var _v: V
86 var _next: nullable Map[K, V]
87 init(k: K, v: V)
88 do
89 _k = k
90 _v = v
91 end
92 redef fun output
93 do
94 '{'.output
95 output_inner
96 '}'.output
97 '\n'.output
98 end
99 fun output_inner
100 do
101 _k.output
102 ':'.output
103 _v.output
104 var n = _next
105 if n != null then
106 ','.output
107 n.output_inner
108 end
109 end
110 end
111
112 class File
113 readable var _id: Int
114 readable var _is_open: Bool
115 fun close
116 do
117 if _is_open then
118 'C'.output
119 _id.output
120 _is_open = false
121 end
122 end
123 init(id: Int)
124 do
125 _id = id
126 _is_open = id > 0
127 end
128 end
129
130 fun file_open(i: Int)
131 !work(f: File)
132 break !error(j: Int) do abort
133 do
134 var f = new File(i)
135 if not f.is_open then error(404)
136 work(f) !break do f.close
137 f.close
138 end
139
140 ##################
141
142 fun test_sort
143 do
144 var a = new Array[Char]('2', '1')
145 a.output
146 a.sort !cmp(x, y) = x <=> y
147 a.output
148 a.sort !cmp(x, y) = y <=> x
149 a.output
150 a.iterate !each i do i.output
151 '\n'.output
152 end
153
154 fun test_map
155 do
156 var m = new Map[Char, Char]('I', '1')
157
158 m.output
159
160 if not m.has_key('I') then (-1).output
161 'I'.output
162 '='.output
163 var i = m['I']
164 i.output
165 '\n'.output
166
167 m.output
168
169 if m.has_key('V') then (-2).output
170 'V'.output
171 '='.output
172 i = m['V'] !def = '5'
173 i.output
174 '\n'.output
175
176 m.output
177
178 if not m.has_key('V') then (-3).output
179 'V'.output
180 '='.output
181 i = m['V'] !def = '6'
182 i.output
183 '\n'.output
184
185 m.output
186
187 if m.has_key('X') then (-4).output
188 'X'.output
189 '='.output
190 i = m['X'] !def do break '0'
191 i.output
192 '\n'.output
193
194 m.output
195
196 if m.has_key('X') then (-5).output
197
198 m.iterate !each2(k,v) do
199 k.output
200 '='.output
201 v.output
202 ';'.output
203 end
204 '\n'.output
205 end
206
207 fun test_file(i: Int)
208 do
209 var f_escape: nullable File = null
210 file_open(i) !work(f) do
211 'O'.output
212 '?'.output
213 f.is_open.output
214 f_escape = f
215 !error(e) do
216 'E'.output
217 e.output
218 end
219 if f_escape != null then
220 'O'.output
221 '?'.output
222 f_escape.is_open.output
223 end
224 end
225
226 test_sort
227 '\n'.output
228 test_map
229 '\n'.output
230 test_file(1)
231 '\n'.output
232 test_file(-1)
233