-
-
Notifications
You must be signed in to change notification settings - Fork 938
Expand file tree
/
Copy pathTestRubyArray.java
More file actions
198 lines (177 loc) · 8.49 KB
/
TestRubyArray.java
File metadata and controls
198 lines (177 loc) · 8.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
**** BEGIN LICENSE BLOCK *****
* Version: EPL 2.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* 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.eclipse.org/legal/epl-v20.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2006 Ola Bini <Ola.Bini@ki.se>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby.test;
import java.util.Arrays;
import java.util.List;
import org.jruby.RubyArray;
/**
* Test case for functionality in RubyArray
*/
public class TestRubyArray extends Base {
private String result;
public TestRubyArray(final String name) {
super(name);
}
@Override
protected void setUp() throws Exception {
super.setUp();
eval("$h = ['foo','bar']");
}
/**
* Test literal constructor [], Array[], Array::[], and Array::new with all forms of parameters.
*/
public void testConstructors() throws Exception {
result = eval("arr = ['a', 100]; p arr");
assertEquals("[\"a\", 100]", result);
result = eval("arr = Array['b', 200]; p arr");
assertEquals("[\"b\", 200]", result);
/*
result = eval("arr = Array::['c', 200]; p arr");
assertEquals("[\"c\", 200]", result);
result = eval("arr = Array.['d', 200]; p arr");
assertEquals("[\"d\", 200]", result);
*/
result = eval("arr = Array.new(); p arr");
assertEquals("[]", result);
result = eval("arr = Array.new(2); p arr");
assertEquals("[nil, nil]", result);
result = eval("arr = Array.new(3, 'a'); p arr"); // Init
assertEquals("[\"a\", \"a\", \"a\"]", result);
result = eval("arr = Array.new(5) {|i| i*i}; p arr"); // Block
assertEquals("[0, 1, 4, 9, 16]", result);
result = eval("arr = Array.new(Array.new(3, 'a')); p arr"); // Copy constructor
assertEquals("[\"a\", \"a\", \"a\"]", result);
}
/**
* Test Array#[]= (store) and Array#[] (retrieve).
*/
public void testLookups() throws Exception {
// value equality
//result = eval("key = 3; arr = []; arr[key] = 'one'; arr.store(3, 'two'); puts arr[key]");
//assertEquals("two", result);
}
/**
* Array#to_s, Array#to_a
*/
public void testConversions() throws Exception {
result = eval("p $h.to_s");
assertEquals("\"[\\\"foo\\\", \\\"bar\\\"]\"", result);
result = eval("p $h.to_a");
assertEquals("[\"foo\", \"bar\"]", result);
}
/**
* Array#size, Array#length, Array#empty?
*/
public void testSizeRelated() throws Exception {
assertEquals("2", eval("p $h.size"));
assertEquals("2", eval("p $h.length"));
assertEquals("false", eval("p $h.empty?"));
assertEquals("true", eval("p Array.new().empty?"));
}
/**
* Array#each
*/
public void testIterating() throws Exception {
//assertEquals("\"foo\"\n\"bar\"", eval("$h.each {|val| p val}"));
//assertEquals("[\"foo\", \"bar\"]", eval("p $h.each {|val| }"));
}
/**
* This tests toArray-functionality
*/
public void testToArray() throws Exception {
final RubyArray arr = (RubyArray) context.runtime.evalScriptlet("$h = ['foo','bar']");
final String val1 = "foo";
final String val2 = "bar";
final Object[] outp = arr.toArray();
assertTrue("toArray should not return null",null != outp);
assertTrue("toArray should not return empty array",0 != outp.length);
assertEquals("first element should be \"foo\"",val1,outp[0]);
assertEquals("second element should be \"bar\"",val2,outp[1]);
final String[] outp2 = (String[])arr.toArray(new String[0]);
assertTrue("toArray should not return null",null != outp2);
assertTrue("toArray should not return empty array",0 != outp2.length);
assertEquals("first element should be \"foo\"",val1,outp2[0]);
assertEquals("second element should be \"bar\"",val2,outp2[1]);
final String[] outp3 = (String[])arr.toArray(new String[arr.size()]);
assertTrue("toArray should not return null",null != outp3);
assertTrue("toArray should not return empty array",0 != outp3.length);
assertEquals("first element should be \"foo\"",val1,outp3[0]);
assertEquals("second element should be \"bar\"",val2,outp3[1]);
}
public void testSetsValuesToNullOnClearWhenNotShared() throws Exception {
final RubyArray arr = (RubyArray)context.runtime.evalScriptlet("$h = []; $h << 'foo'; $h << 'bar'");
assertNotSame("first element nil", context.nil, arr.eltInternal(0));
assertNotSame("second element nil", context.nil, arr.eltInternal(1));
arr.rb_clear(context);
assertSame("first element not nil", context.nil, arr.eltInternal(0));
assertSame("second element not nil", context.nil, arr.eltInternal(1));
}
public void testSetsLeftoverValuesToNullWhenRejectingElements() throws Exception {
final RubyArray arr = (RubyArray)context.runtime.evalScriptlet("$h = ['foo','bar']");
assertNotSame("first element nil", context.nil, arr.eltInternal(0));
assertNotSame("second element nil", context.nil, arr.eltInternal(1));
context.runtime.evalScriptlet("$h.reject! { |e| e == 'bar' }");
assertNotSame("first element nil", context.nil, arr.eltInternal(0));
assertSame("second element not nil", context.nil, arr.eltInternal(1));
}
public void testSetsLeftoverValuesToNullInDeleteAt() throws Exception {
final RubyArray arr = (RubyArray)context.runtime.evalScriptlet("$h = ['foo','bar']");
assertNotSame("first element nil", context.nil, arr.eltInternal(0));
assertNotSame("second element nil", context.nil, arr.eltInternal(1));
context.runtime.evalScriptlet("$h.delete_at(1)");
assertNotSame("first element nil", context.nil, arr.eltInternal(0));
assertSame("second element not nil", context.nil, arr.eltInternal(1));
}
public void testSetsLeftoverValuesToNullInDelete() throws Exception {
final RubyArray arr = (RubyArray)context.runtime.evalScriptlet("$h = ['foo','bar']");
assertNotSame("first element nil", context.nil, arr.eltInternal(0));
assertNotSame("second element nil", context.nil, arr.eltInternal(1));
context.runtime.evalScriptlet("$h.delete('foo')");
assertNotSame("first element nil", context.nil, arr.eltInternal(0));
assertSame("second element not nil", context.nil, arr.eltInternal(1));
}
private void assertSublistContainsCorrectSubset(final List<?> list) {
final List<?> subList = list.subList(1, 3);
assertEquals(2, subList.size());
assertEquals("bar", subList.get(0));
assertEquals("baz", subList.get(1));
}
public void testSubListContainsCorrectSubset() {
// Demonstrates that this is a fair test
final List<?> javaArray = Arrays.asList("foo", "bar", "baz", "anything");
assertSublistContainsCorrectSubset(javaArray);
// Ruby should give the same results
final RubyArray rubyArray = (RubyArray)context.runtime.evalScriptlet("$h = ['foo', 'bar', 'baz', 'anything']");
assertSublistContainsCorrectSubset(rubyArray);
}
public void testRubyCollect() {
String result = eval("a = ['a', 'b'].collect {|x| \"#{x}\"}; p a");
assertEquals("Bug: [ #502036 ]", "[\"a\", \"b\"]", result);
}
}