13514
|
1 /*
|
|
2 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. This
|
|
3 * code is released under a tri EPL/GPL/LGPL license. You can use it,
|
|
4 * redistribute it and/or modify it under the terms of the:
|
|
5 *
|
|
6 * Eclipse Public License version 1.0
|
|
7 * GNU General Public License version 2
|
|
8 * GNU Lesser General Public License version 2.1
|
|
9 */
|
|
10 package com.oracle.truffle.ruby.test.core;
|
|
11
|
|
12 import org.junit.*;
|
|
13
|
|
14 import com.oracle.truffle.ruby.test.*;
|
|
15
|
|
16 /**
|
|
17 * Test the {@code Array} class.
|
|
18 */
|
|
19 public class ArrayTests extends RubyTests {
|
|
20
|
|
21 @Test
|
|
22 public void testLiteral() {
|
|
23 assertPrints("", "puts []");
|
|
24 assertPrints("1\n", "puts [1]");
|
|
25 assertPrints("1\n2\n", "puts [1, 2]");
|
|
26 assertPrints("1\n2\n3\n", "puts [1, 2, 3]");
|
|
27 assertPrints("1\n2\n3\n", "puts [(1), 2, ((3))]");
|
|
28 }
|
|
29
|
|
30 @Test
|
|
31 public void testReadIndexSimple() {
|
|
32 assertPrints("2\n", "puts [1, 2, 3][1]");
|
|
33 assertPrints("2\n", "x = [1, 2, 3]; puts x[1]");
|
|
34 assertPrints("\n", "puts [1, 2, 3][10]");
|
|
35 }
|
|
36
|
|
37 @Test
|
|
38 public void testReadIndexRange() {
|
|
39 assertPrints("2\n3\n", "puts [1, 2, 3][1..2]");
|
|
40 assertPrints("2\n3\n", "puts [1, 2, 3][1..-1]");
|
|
41 assertPrints("1\n2\n3\n", "puts [1, 2, 3][0..-1]");
|
|
42 }
|
|
43
|
|
44 @Test
|
|
45 public void testWriteIndexSimple() {
|
|
46 assertPrints("1\n0\n3\n", "x = [1, 2, 3]; x[1] = 0; puts x");
|
|
47 assertPrints("0\n", "x = [1, 2, 3]; puts x[1] = 0");
|
|
48 assertPrints("1\n\n3\n", "x = [1]; x[2] = 3; puts x");
|
|
49 }
|
|
50
|
|
51 @Test
|
|
52 public void testWriteIndexRangeSingle() {
|
|
53 assertPrints("1\n0\n", "x = [1, 2, 3]; x[1..2] = 0; puts x");
|
|
54 assertPrints("1\n0\n", "x = [1, 2, 3]; x[1..3] = 0; puts x");
|
|
55 assertPrints("1\n0\n", "x = [1, 2, 3]; x[1..-1] = 0; puts x");
|
|
56 assertPrints("0\n", "x = [1, 2, 3]; x[0..-1] = 0; puts x");
|
|
57 }
|
|
58
|
|
59 @Test
|
|
60 public void testWriteIndexRangeArray() {
|
|
61 assertPrints("1\n4\n5\n", "x = [1, 2, 3]; x[1..2] = [4, 5]; puts x");
|
|
62 assertPrints("1\n4\n5\n", "x = [1, 2, 3]; x[1..-1] = [4, 5]; puts x");
|
|
63 assertPrints("4\n5\n6\n", "x = [1, 2, 3]; x[0..-1] = [4, 5, 6]; puts x");
|
|
64 }
|
|
65
|
|
66 @Test
|
|
67 public void testInsert() {
|
|
68 assertPrints("1\n2\n3\n", "x = [1, 2]; x.insert(2, 3); puts x");
|
|
69 assertPrints("1\n2\n3\n", "x = [1, 3]; x.insert(1, 2); puts x");
|
|
70 assertPrints("1\n\n3\n", "x = [1]; x.insert(2, 3); puts x");
|
|
71 }
|
|
72
|
|
73 @Test
|
|
74 public void testPush() {
|
|
75 assertPrints("1\n2\n3\n", "x = [1, 2]; x.push(3); puts x");
|
|
76 assertPrints("1\n2\n3\n", "x = [1, 2]; x << 3; puts x");
|
|
77 assertPrints("1\n2\n3\n4\n", "x = [1, 2]; x << 3 << 4; puts x");
|
|
78 }
|
|
79
|
|
80 @Test
|
|
81 public void testDeleteAt() {
|
|
82 assertPrints("1\n3\n", "x = [1, 2, 3]; x.delete_at(1); puts x");
|
|
83 }
|
|
84
|
|
85 @Test
|
|
86 public void testDup() {
|
|
87 assertPrints("1\n2\n3\n", "x = [1, 2, 3]; y = x.dup; x.delete_at(1); puts y;");
|
|
88 }
|
|
89
|
|
90 @Test
|
|
91 public void testOpAssign() {
|
|
92 assertPrints("1\n", "x = [0]; x[0] += 1; puts x");
|
|
93 }
|
|
94
|
|
95 @Test
|
|
96 public void testSize() {
|
|
97 assertPrints("0\n", "puts [].size");
|
|
98 assertPrints("1\n", "puts [1].size");
|
|
99 assertPrints("2\n", "puts [1, 2].size");
|
|
100 }
|
|
101
|
|
102 @Test
|
|
103 public void testEach() {
|
|
104 assertPrints("", "[].each { |n| puts n }");
|
|
105 assertPrints("1\n", "[1].each { |n| puts n }");
|
|
106 assertPrints("1\n2\n3\n", "[1, 2, 3].each { |n| puts n }");
|
|
107 }
|
|
108
|
|
109 @Test
|
|
110 public void testMap() {
|
|
111 assertPrints("2\n4\n6\n", "puts [1, 2, 3].map { |n| n*2 }");
|
|
112 }
|
|
113
|
|
114 @Test
|
|
115 public void testZip() {
|
|
116 assertPrints("1\n4\n2\n5\n3\n6\n", "puts [1, 2, 3].zip([4, 5, 6])");
|
|
117 assertPrints("1\n4\n2\n5\n", "puts [1, 2].zip([4, 5, 6])");
|
|
118 assertPrints("1\n4\n2\n5\n3\n\n", "puts [1, 2, 3].zip([4, 5])");
|
|
119 assertPrints("1\n3\n5\n2\n4\n6\n", "puts [1, 2].zip([3, 4], [5, 6])");
|
|
120 }
|
|
121
|
|
122 @Test
|
|
123 public void testProduct() {
|
|
124 assertPrints("[[1, 3], [1, 4], [2, 3], [2, 4]]\n", "puts [1, 2].product([3, 4]).to_s");
|
|
125 }
|
|
126
|
|
127 @Test
|
|
128 public void testInject() {
|
|
129 assertPrints("10\n", "puts [2, 3, 4].inject(1) { |a, n| a + n }");
|
|
130 }
|
|
131
|
|
132 @Test
|
|
133 public void testLiteralSplat() {
|
|
134 assertPrints("[1, 2, 3, 4]\n", "a = [3, 4]; puts [1, 2, *a].to_s");
|
|
135 assertPrints("[1, 2, 3, 4, 5, 6]\n", "a = [3, 4]; puts [1, 2, *a, 5, 6].to_s");
|
|
136 }
|
|
137
|
|
138 @Test
|
|
139 public void testSplatCast() {
|
|
140 assertPrints("[14]\n", "def foo; value = 14; return *value; end; puts foo.to_s");
|
|
141 }
|
|
142
|
|
143 @Test
|
|
144 public void testSelect() {
|
|
145 assertPrints("[3, 4]\n", "foo = [1, 2, 3, 4]; bar = foo.select { |x| x > 2 }; puts bar.to_s");
|
|
146 }
|
|
147
|
|
148 @Test
|
|
149 public void testInclude() {
|
|
150 assertPrints("true\nfalse\n", "foo = [1, 2, 3, 4]; puts foo.include? 2; puts foo.include? 5");
|
|
151 }
|
|
152
|
|
153 @Test
|
|
154 public void testSub() {
|
|
155 assertPrints("[1, 4]\n", "puts ([1, 2, 2, 3, 4] - [2, 3]).to_s");
|
|
156 }
|
|
157
|
|
158 @Test
|
|
159 public void testJoin() {
|
|
160 assertPrints("1.2.3\n", "puts [1, 2, 3].join('.')");
|
|
161 }
|
|
162
|
|
163 @Test
|
|
164 public void testShift() {
|
|
165 assertPrints("1\n2\n3\n", "a = [1, 2, 3]; while b = a.shift; puts b; end");
|
|
166 }
|
|
167
|
|
168 }
|