tests: add test_comprehension.nit
authorJean Privat <jean@pryen.org>
Wed, 17 Dec 2014 18:56:31 +0000 (13:56 -0500)
committerJean Privat <jean@pryen.org>
Wed, 17 Dec 2014 20:29:28 +0000 (15:29 -0500)
Signed-off-by: Jean Privat <jean@pryen.org>

tests/sav/test_comprehension.res [new file with mode: 0644]
tests/test_comprehension.nit [new file with mode: 0644]

diff --git a/tests/sav/test_comprehension.res b/tests/sav/test_comprehension.res
new file mode 100644 (file)
index 0000000..cdb7bd8
--- /dev/null
@@ -0,0 +1,5 @@
+2,4,6,8
+11,12,13,14,21,22,23,24,31,32,33,34,41,42,43,44
+12,14,32,34
+3,5,7,9
+11,12,13,14;21,22,23,24;31,32,33,34;41,42,43,44
diff --git a/tests/test_comprehension.nit b/tests/test_comprehension.nit
new file mode 100644 (file)
index 0000000..2d65d97
--- /dev/null
@@ -0,0 +1,35 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+var c1 = [1..4]
+
+# Simple comprehension
+var a1 = [for i in c1 do i*2]
+print a1.join(",")
+
+# Cartesian product
+var a2 = [for i in c1 do for j in c1 do i * 10 + j]
+print a2.join(",")
+
+# Cartesian product and filters
+var a3 = [for i in c1 do if i.is_odd then for j in c1 do if j.is_even then i * 10 + j]
+print a3.join(",")
+
+# Nesting (useless)
+var a4 = [for i in [for i in c1 do i*2] do i + 1]
+print a4.join(",")
+
+# Multi-dimensional
+var a5 = [for i in c1 do [for j in c1 do i*10+j]]
+print([for i in a5 do i.join(",")].join(";"))