[pypy-svn] r65515 - in pypy/branch/io-lang/pypy/lang/io: . test

david at codespeak.net david at codespeak.net
Sun May 31 18:06:24 CEST 2009


Author: david
Date: Sun May 31 18:06:24 2009
New Revision: 65515

Modified:
   pypy/branch/io-lang/pypy/lang/io/list.py
   pypy/branch/io-lang/pypy/lang/io/test/test_list.py
Log:
fixed List first, when called w/o a number, added last, reverseInPlace,  removeAll, atPut methods to list

Modified: pypy/branch/io-lang/pypy/lang/io/list.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/list.py	(original)
+++ pypy/branch/io-lang/pypy/lang/io/list.py	Sun May 31 18:06:24 2009
@@ -65,7 +65,7 @@
     return W_Number(space, len(w_target.items))
     
 @register_method('List', 'first')
-def list_size(space, w_target, w_message, w_context):
+def list_first(space, w_target, w_message, w_context):
     if len(w_message.arguments) != 0:
         t = w_message.arguments[0].eval(space, w_target, w_context)
         assert isinstance(t, W_Number)
@@ -75,10 +75,61 @@
     
     if len(w_target.items) == 0 and nfirst == 1:
         return space.w_nil
-
+    
+    if nfirst == 1:
+        return w_target.items[0]
     flist_w = w_target.clone()
     if nfirst < 1:
         flist_w.items = []
     else:
         flist_w.items = flist_w.items[0:nfirst]
-    return flist_w
\ No newline at end of file
+    return flist_w
+    
+ at register_method('List', 'last')
+def list_last(space, w_target, w_message, w_context):
+    if len(w_message.arguments) != 0:
+        t = w_message.arguments[0].eval(space, w_target, w_context)
+        assert isinstance(t, W_Number)
+        nlast = t.value
+    else:
+        nlast = 1
+    
+    if len(w_target.items) == 0 and nlast == 1:
+        return space.w_nil
+    
+    if nlast == 1:
+        return w_target.items[-1]
+    flist_w = w_target.clone()
+    if nlast < 1:
+        flist_w.items = []
+    else:
+        flist_w.items = flist_w.items[len(flist_w.items)-nlast:]
+    return flist_w
+
+ at register_method('List', 'reverseInPlace')
+def list_reverse_in_place(space, w_target, w_message, w_context):
+    w_target.items.reverse()
+    return w_target
+    
+ at register_method('List', 'removeAll')
+def list_remove_all(space, w_target, w_message, w_context):
+    try:
+        w_target.items = []
+    except Exception, e:
+        raise Exception, 'index out of bounds'
+
+    return w_target
+    
+ at register_method('List', 'atPut')
+def list_reverse_in_place(space, w_target, w_message, w_context):
+    w_key = w_message.arguments[0].eval(space, w_target, w_context)
+    assert isinstance(w_key, W_Number), "argument 0 to method 'atPut' must be a Number"
+    key = w_key.value
+    if len(w_message.arguments) > 1:
+        w_value = w_message.arguments[1].eval(space, w_target, w_context)
+    else:
+        w_value = space.w_nil
+        
+    w_target.items[key] = w_value
+    return w_target
+    
\ No newline at end of file

Modified: pypy/branch/io-lang/pypy/lang/io/test/test_list.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/test/test_list.py	(original)
+++ pypy/branch/io-lang/pypy/lang/io/test/test_list.py	Sun May 31 18:06:24 2009
@@ -133,13 +133,86 @@
 def test_list_first():
     inp = 'a := list(9,8,7,6,5,4,3,2,1,1); a first'
     res, space = interpret(inp)
-    assert isinstance(res, W_List)
-    assert res.items[0].value == 9
-    assert res.protos == [space.w_lobby.slots['a']]
+    assert isinstance(res, W_Number)
+    assert res.value == 9
     
 def test_list_first_n():
     inp = 'a := list(9,8,7,6,5,4,3,2,1,1); a first(3)'
     res, space = interpret(inp)
     assert isinstance(res, W_List)
     assert [x.value for x in res.items] == [9,8,7]
-    assert res.protos == [space.w_lobby.slots['a']]
\ No newline at end of file
+    assert res.protos == [space.w_lobby.slots['a']]
+    
+def test_list_last():
+    inp = 'a := list(9,8,7,6,5,4,3,2,1,100); a last'
+    res, space = interpret(inp)
+    assert isinstance(res, W_Number)
+    assert res.value == 100
+    
+def test_list_last_n():
+    inp = 'a := list(9,8,7,6,5,4,3,2,1,100); a last(3)'
+    res, space = interpret(inp)
+    assert isinstance(res, W_List)
+    assert [x.value for x in res.items] == [2, 1, 100]
+    assert res.protos == [space.w_lobby.slots['a']]
+
+def test_list_first_n_overflow():    
+    inp = 'a := list(9,8,7,6,5,4,3,2,1,100); a first(20)'
+    res, space = interpret(inp)
+    assert isinstance(res, W_List)
+    assert [x.value for x in res.items] == [9,8,7,6,5,4,3,2,1,100]
+    assert res.protos == [space.w_lobby.slots['a']]
+    
+
+def test_list_last_n_overflow():
+    inp = 'a := list(9,8,7,6,5,4,3,2,1,100); a last(20)'
+    res, space = interpret(inp)
+    assert isinstance(res, W_List)
+    assert [x.value for x in res.items] == [9,8,7,6,5,4,3,2,1,100]
+    assert res.protos == [space.w_lobby.slots['a']]
+
+
+def test_empty_list_first_n():
+    inp = 'a := list(); a first(20)'
+    res, space = interpret(inp)
+    assert isinstance(res, W_List)
+    assert [x.value for x in res.items] == []
+    assert res.protos == [space.w_lobby.slots['a']]
+
+def test_empty_list_last_n():
+    inp = 'a := list(); a last(20)'
+    res, space = interpret(inp)
+    assert isinstance(res, W_List)
+    assert [x.value for x in res.items] == []
+    assert res.protos == [space.w_lobby.slots['a']]
+
+def test_reverse_in_place():
+    inp = 'a := list(9,8,7,6,5,4,3,2,1,100); a reverseInPlace'
+    res, space = interpret(inp)
+    assert isinstance(res, W_List)
+    assert [x.value for x in res.items] == [100,1,2,3,4,5,6,7,8,9]
+    
+def test_remove_all():
+    inp = 'a := list(9,8,7,6,5,4,3,2,1,100); a removeAll; a'
+    res, space = interpret(inp)
+    assert isinstance(res, W_List)
+    assert [x.value for x in res.items] == []
+
+def test_at_put():
+    inp = 'a := list(9,8,7,6,5,4,3,2,1,100); a atPut(3, 1045)'
+    res, space = interpret(inp)
+    assert isinstance(res, W_List)
+    assert [x.value for x in res.items] == [9,8,7, 1045, 5, 4, 3, 2, 1, 100]
+    
+def test_at_put_raises():
+    inp = 'a := list(9,8,7,6,5,4,3,2,1,100); a atPut(1000, 1045)'
+    py.test.raises(Exception, 'interpret(inp)')
+
+def test_at_put_wo_value():
+    inp = 'a := list(9,8,7,6,5,4,3,2,1,100); a atPut(3)'
+    res, space = interpret(inp)
+    assert isinstance(res, W_List)
+    nums = [W_Number(space, i) for i in range(9, 0, -1)]
+    nums[3] = space.w_nil
+    nums.append(W_Number(space, 100))
+    assert [x for x in res.items] == nums



More information about the Pypy-commit mailing list