syntax: 'meth' -> 'fun', 'attr' -> 'var'
[nit.git] / tests / test_map.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-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 fun test1(h: Map[Int, Int])
19 do
20 print("* test 1 *")
21 h[1] = 2
22 h[2] = 4
23 printn(h[1], " - ", h[2], "\n")
24 h[1] = 20
25 printn(h[1], " - ", h[2], "\n")
26 print(h.has_key(1))
27 print(h.has_key(2))
28 print(not h.has_key(3))
29 print(not h.has(2))
30 print(h.has(4))
31 print(h.has(20))
32 print(h.length)
33 var s = 24
34 for x in h do
35 s = s - x
36 end
37 print(s)
38 end
39
40 fun test2(h: Map[Int, Int])
41 do
42 print("* test 2 *")
43 var nb = 999
44
45 var i = 0
46 while i <= nb do
47 h[i*31+13] = i * 2
48 i = i + 1
49 end
50
51 print(h.length)
52
53 i = nb
54 while i >= 0 do
55 if (h[i*31+13] != i * 2) then
56 print("{i}: {i*31+13} != {h[i]}")
57 end
58 i = i - 1
59 end
60
61 i = nb * 2
62 while i >= 0 do
63 if (i % 3 != 0) then
64 h.remove(i)
65 end
66 i = i - 1
67 end
68
69 print(h.length)
70
71 i = nb
72 while i >= 0 do
73 var j = i * 31 + 13
74 if (i * 2) % 3 != 0 then
75 if h.has_key(j) then
76 print("{i}: {j} should be removed")
77 end
78 else if (h[j] != i * 2) then
79 print("{i}: {j} != {h[i]}")
80 end
81 i = i - 1
82 end
83 end
84
85
86 fun test3(m: Map[String, String])
87 do
88 print("* test 3 *")
89 print("* start:")
90 print(m.is_empty)
91 print(m.length == 0)
92 print(m.has_only("vert"))
93 print(m.count("vert") == 0)
94 m["blue"] = "vert"
95 print(not m.is_empty)
96 print(m.has_only("vert"))
97 print(m.count("vert") == 1)
98 m["red"] = "rouge"
99 print(not m.has_only("vert"))
100 m["blue"] = "bleu"
101 print(m.length == 2)
102
103 print("* add some:")
104 m["pink"] = "rose"
105 m["yellow"] = "jaune"
106 m["orange"] = "orange"
107 m["black"] = "noir"
108 m["gray"] = "gris"
109 m["grey"] = "gris"
110 m["white"] = "blanc"
111 print(m.length == 9)
112 print(m.count("vert") == 0)
113 print(m.count("gris") == 2)
114 print(m["blue"] == "bleu")
115 print(not m.has("vert"))
116 print(m.has("bleu"))
117 print(not m.has("blue"))
118 print(m.has_key("blue"))
119 print(not m.has_key("green"))
120 print(not m.has_key("vert"))
121 print(m.join(", "))
122
123 print("* remove:")
124 print(m.count("rose") == 1)
125 m.remove("rose")
126 print(m.length == 8)
127 print(m.count("rose") == 0)
128 m.remove_all("gris")
129 print(m.length == 6)
130 print(m.count("gris") == 0)
131 m.remove_at("blue")
132 print(m.length == 5)
133 print(m.count("bleu") == 0)
134 print(m.join(", "))
135 m.clear
136 print(m.is_empty)
137 end
138
139 test1(new ArrayMap[Int, Int])
140 test2(new ArrayMap[Int, Int])
141 test3(new ArrayMap[String, String])