KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > test > TestRubyHash


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2001 Benoit Cerrina <b.cerrina@wanadoo.fr>
15  * Copyright (C) 2001 Alan Moore <alan_moore@gmx.net>
16  * Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
17  * Copyright (C) 2002 Anders Bengtsson <ndrsbngtssn@yahoo.se>
18  *
19  * Alternatively, the contents of this file may be used under the terms of
20  * either of the GNU General Public License Version 2 or later (the "GPL"),
21  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
22  * in which case the provisions of the GPL or the LGPL are applicable instead
23  * of those above. If you wish to allow use of your version of this file only
24  * under the terms of either the GPL or the LGPL, and not to allow others to
25  * use your version of this file under the terms of the CPL, indicate your
26  * decision by deleting the provisions above and replace them with the notice
27  * and other provisions required by the GPL or the LGPL. If you do not delete
28  * the provisions above, a recipient may use your version of this file under
29  * the terms of any one of the CPL, the GPL or the LGPL.
30  ***** END LICENSE BLOCK *****/

31 package org.jruby.test;
32
33 import org.jruby.Ruby;
34
35 /**
36  * @author chadfowler
37  */

38 public class TestRubyHash extends TestRubyBase {
39
40     private String JavaDoc result;
41
42     public TestRubyHash(String JavaDoc name) {
43         super(name);
44     }
45
46     public void setUp() throws Exception JavaDoc {
47         if (runtime == null) {
48             runtime = Ruby.getDefaultInstance();
49         }
50         eval("$h = {'foo' => 'bar'}");
51     }
52
53     public void tearDown() {
54         super.tearDown();
55     }
56
57     /**
58      * Test literal constructor {}, Hash::[], and Hash::new with and
59      * without the optional default-value argument.
60      */

61     public void testConstructors() throws Exception JavaDoc {
62         result = eval("hash = {'a', 100}; p hash");
63         assertEquals("{\"a\"=>100}", result);
64         result = eval("hash = Hash['b', 200]; p hash");
65         assertEquals("{\"b\"=>200}", result);
66         result = eval("hash = Hash.new(); p hash['test']");
67         assertEquals("nil", result);
68         result = eval("hash = Hash.new('default'); p hash['test']");
69         assertEquals("\"default\"", result);
70     }
71
72     /**
73      * Test Hash#[]= (store) and Hash#[] (retrieve). Also test whether
74      * Object#== is properly defined for each class.
75      */

76     public void testLookups() throws Exception JavaDoc {
77         // value equality
78
result = eval("key = 'a'; hash = {key => 'one'}; hash.store('a', 'two'); puts hash[key]");
79         assertEquals("two", result);
80         result = eval("key = [1,2]; hash = {key => 'one'}; hash[[1,2]] = 'two'; puts hash[key]");
81         assertEquals("two", result);
82         result = eval("key = :a; hash = {key => 'one'}; hash[:a] = 'two'; puts hash[key]");
83         assertEquals("two", result);
84         result = eval("key = 1234; hash = {key => 'one'}; hash[1234] = 'two'; puts hash[key]");
85         assertEquals("two", result);
86         result = eval("key = 12.4; hash = {key => 'one'}; hash[12.4] = 'two'; puts hash[key]");
87         assertEquals("two", result);
88         result = eval("key = 19223372036854775807; hash = {key => 'one'}; hash[19223372036854775807] = 'two'; puts hash[key]");
89         assertEquals("two", result);
90         // identity equality
91
result = eval("key = /a/; hash = {key => 'one'}; hash[/a/] = 'two'; puts hash[key]");
92         assertEquals("two", result);
93         result = eval("key = (1..3); hash = {key => 'one'}; hash[(1..3)] = 'two'; puts hash[key]");
94         assertEquals("two", result);
95     }
96
97     /**
98      * Hash#to_s, Hash#to_a, Hash#to_hash
99      */

100     public void testConversions() throws Exception JavaDoc {
101         result = eval("p $h.to_s");
102         assertEquals("\"foobar\"", result);
103         result = eval("p $h.to_a");
104         assertEquals("[[\"foo\", \"bar\"]]", result);
105         result = eval("p $h.to_hash");
106         assertEquals("{\"foo\"=>\"bar\"}", result);
107     }
108
109     /**
110      * Hash#size, Hash#length, Hash#empty?
111      */

112     public void testSizeRelated() throws Exception JavaDoc {
113         assertEquals("1", eval("p $h.size"));
114         assertEquals("1", eval("p $h.length"));
115         assertEquals("false", eval("p $h.empty?"));
116         assertEquals("true", eval("p Hash.new().empty?"));
117     }
118
119     /**
120      * Hash#each, Hash#each_pair, Hash#each_value, Hash#each_key
121      */

122     public void testIterating() throws Exception JavaDoc {
123         assertEquals("[\"foo\", \"bar\"]", eval("$h.each {|pair| p pair}"));
124         assertEquals("{\"foo\"=>\"bar\"}", eval("p $h.each {|pair| }"));
125         assertTrue(eval("$h.each_pair {|pair| p pair}").indexOf("[\"foo\", \"bar\"]") != -1);
126         assertTrue(eval("p $h.each_pair {|pair| }").indexOf("{\"foo\"=>\"bar\"}") != -1);
127
128         assertEquals("\"foo\"", eval("$h.each_key {|k| p k}"));
129         assertEquals("{\"foo\"=>\"bar\"}", eval("p $h.each_key {|k| }"));
130
131         assertEquals("\"bar\"", eval("$h.each_value {|v| p v}"));
132         assertEquals("{\"foo\"=>\"bar\"}", eval("p $h.each_value {|v| }"));
133     }
134
135     /**
136      * Hash#delete, Hash#delete_if, Hash#reject, Hash#reject!
137      */

138     public void testDeleting() throws Exception JavaDoc {
139         eval("$delete_h = {1=>2,3=>4}");
140         assertEquals("2", eval("p $delete_h.delete(1)"));
141         assertEquals("{3=>4}", eval("p $delete_h"));
142         assertEquals("nil", eval("p $delete_h.delete(100)"));
143         assertEquals("100", eval("$delete_h.delete(100) {|x| p x }"));
144
145         eval("$delete_h = {1=>2,3=>4,5=>6}");
146         assertEquals("{1=>2}", eval("p $delete_h.delete_if {|k,v| k >= 3}"));
147         assertEquals("{1=>2}", eval("p $delete_h"));
148
149         eval("$delete_h.clear");
150         assertEquals("{}", eval("p $delete_h"));
151
152         eval("$delete_h = {1=>2,3=>4,5=>6}");
153         assertEquals("{1=>2}", eval("p $delete_h.reject {|k,v| k >= 3}"));
154         assertEquals("3", eval("p $delete_h.size"));
155
156         eval("$delete_h = {1=>2,3=>4,5=>6}");
157         eval("p $delete_h");
158
159         assertEquals("{1=>2}", eval("p $delete_h.reject! {|k,v| k >= 3}"));
160         assertEquals("1", eval("p $delete_h.size"));
161         assertEquals("nil", eval("p $delete_h.reject! {|k,v| false}"));
162     }
163
164     /**
165      * Hash#default, Hash#default=
166      */

167     public void testDefault() throws Exception JavaDoc {
168         assertEquals("nil", eval("p $h['njet']"));
169         assertEquals("nil", eval("p $h.default"));
170         eval("$h.default = 'missing'");
171         assertEquals("\"missing\"", eval("p $h['njet']"));
172         assertEquals("\"missing\"", eval("p $h.default"));
173     }
174
175     /**
176      * Hash#sort, Hash#invert
177      */

178     public void testRestructuring() throws Exception JavaDoc {
179     eval("$h_sort = {\"a\"=>20,\"b\"=>30,\"c\"=>10}");
180     assertEquals("[[\"a\", 20], [\"b\", 30], [\"c\", 10]]",
181              eval("p $h_sort.sort"));
182     assertEquals("[[\"c\", 10], [\"a\", 20], [\"b\", 30]]",
183              eval("p $h_sort.sort {|a,b| a[1]<=>b[1]}"));
184
185     eval("$h_invert = {\"n\"=>100,\"y\"=>300,\"d\"=>200,\"a\"=>0}");
186     assertEquals("[[0, \"a\"], [100, \"n\"], [200, \"d\"], [300, \"y\"]]",
187              eval("p $h_invert.invert.sort"));
188     }
189 }
190
Popular Tags