[pypy-svn] r48007 - in pypy/dist/pypy/lang/smalltalk: . test
akuhn at codespeak.net
akuhn at codespeak.net
Fri Oct 26 00:34:25 CEST 2007
Author: akuhn
Date: Fri Oct 26 00:34:25 2007
New Revision: 48007
Modified:
pypy/dist/pypy/lang/smalltalk/interpreter.py
pypy/dist/pypy/lang/smalltalk/model.py
pypy/dist/pypy/lang/smalltalk/primitives.py
pypy/dist/pypy/lang/smalltalk/test/test_primitives.py
Log:
(akuhn, tverwaes)
fixed all those mind numbing off by one errors in the test
We strongly suggest to use n1 and n0 as identifier namesto discriminate 1 and 0 based indices
Also we would like to recommend to use *speaking* names whenever possible, a method named common_at_put that just asserst the range but does not set anything is certainly NOT speaking (lying if you want)
Modified: pypy/dist/pypy/lang/smalltalk/interpreter.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/interpreter.py (original)
+++ pypy/dist/pypy/lang/smalltalk/interpreter.py Fri Oct 26 00:34:25 2007
@@ -116,15 +116,16 @@
def _sendSelfSelector(self, selector, argcount, interp):
receiver = self.peek(argcount)
self._sendSelector(selector, argcount, interp,
- receiver, receiver.shadow_of_my_class())
+ receiver, receiver.shadow_of_my_class(), 1)
def _sendSuperSelector(self, selector, argcount, interp):
s_compiledin = self.w_method().compiledin().as_class_get_shadow()
self._sendSelector(selector, argcount, interp, self.w_receiver,
- s_compiledin.s_superclass)
+ s_compiledin.s_superclass, 0)
def _sendSelector(self, selector, argcount, interp,
- receiver, receiverclassshadow):
+ receiver, receiverclassshadow, popreceiver):
+ assert argcount >= 0
method = receiverclassshadow.lookup(selector)
# XXX catch MethodNotFound here and send doesNotUnderstand:
if method.primitive:
@@ -140,8 +141,8 @@
start = len(self.stack) - argcount
assert start >= 0 # XXX check in the Blue Book what to do in this case
arguments = self.stack[start:]
- interp.w_active_context = method.create_frame(receiver, arguments, self)
- self.pop_n(argcount + 1)
+ interp.w_active_context = method.create_frame(receiver, arguments, self)
+ self.pop_n(argcount + popreceiver)
def _return(self, object, interp):
if self.w_sender is None: # for tests, when returning from the top-level context
Modified: pypy/dist/pypy/lang/smalltalk/model.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/model.py (original)
+++ pypy/dist/pypy/lang/smalltalk/model.py Fri Oct 26 00:34:25 2007
@@ -116,13 +116,13 @@
W_AbstractObjectWithClassReference.__init__(self, w_class)
self._vars = [None] * size
- def fetch(self, index):
- return self._vars[index]
+ def fetch(self, n0):
+ return self._vars[n0]
- def store(self, index, w_value):
+ def store(self, n0, w_value):
if self._shadow is not None:
self._shadow.invalidate()
- self._vars[index] = w_value
+ self._vars[n0] = w_value
def size(self):
return len(self._vars)
Modified: pypy/dist/pypy/lang/smalltalk/primitives.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/primitives.py (original)
+++ pypy/dist/pypy/lang/smalltalk/primitives.py Fri Oct 26 00:34:25 2007
@@ -25,12 +25,12 @@
return objtable.wrap_int(w_obj.getbyte(idx))
raise PrimitiveFailedError()
-def assert_bounds(idx, minb, maxb):
- if idx < minb or idx >= maxb:
+def assert_bounds(n0, minimum, maximum):
+ if not minimum <= n0 < maximum:
raise PrimitiveFailedError()
-def assert_valid_index(idx, w_obj):
- assert_bounds(idx, 0, w_obj.size())
+def assert_valid_index(n0, w_obj):
+ assert_bounds(n0, 0, w_obj.size())
# ___________________________________________________________________________
# Primitive table: it is filled in at initialization time with the
@@ -300,15 +300,13 @@
STRING_AT = 63
STRING_AT_PUT = 64
-def common_at((w_obj, w_idx)):
- idx = unwrap_int(w_idx)
- # XXX should be idx-1, probably
- assert_valid_index(idx-1, w_obj)
- return w_obj, idx-1
+def common_at((w_obj, w_index1)):
+ index1 = unwrap_int(w_index1)
+ assert_valid_index(index1-1, w_obj)
+ return w_obj, index1-1
def common_at_put((w_obj, w_idx, w_val)):
idx = unwrap_int(w_idx)
- # XXX should be idx-1, probably
assert_valid_index(idx-1, w_obj)
return w_obj, idx-1, w_val
@@ -366,19 +364,17 @@
@primitive(OBJECT_AT)
@stack(2)
-def func(args, (w_rcvr, w_idx)):
- idx = unwrap_int(w_idx)
- # XXX should be idx-1, probably
- assert_bounds(idx, 0, w_rcvr.shadow_of_my_class().instance_size)
- return w_rcvr.fetch(idx)
+def func(args, (w_rcvr, w_n1)):
+ n0 = unwrap_int(w_n1) - 1
+ assert_bounds(n0, 0, w_rcvr.shadow_of_my_class().instance_size)
+ return w_rcvr.fetch(n0)
@primitive(OBJECT_AT_PUT)
@stack(3)
-def func(args, (w_rcvr, w_idx, w_val)):
- idx = unwrap_int(w_idx)
- # XXX should be idx-1, probably
- assert_bounds(idx, 0, w_rcvr.shadow_of_my_class().instance_size)
- w_rcvr.store(idx, w_val)
+def func(args, (w_rcvr, w_n1, w_val)):
+ n0 = unwrap_int(w_n1) - 1
+ assert_bounds(n0, 0, w_rcvr.shadow_of_my_class().instance_size)
+ w_rcvr.store(n0, w_val)
return w_val
@primitive(NEW)
Modified: pypy/dist/pypy/lang/smalltalk/test/test_primitives.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/test/test_primitives.py (original)
+++ pypy/dist/pypy/lang/smalltalk/test/test_primitives.py Fri Oct 26 00:34:25 2007
@@ -169,53 +169,53 @@
def test_at():
w_obj = mockclass(0, varsized=True).as_class_get_shadow().new(1)
w_obj.store(0, "foo")
- assert prim(p.AT, [w_obj, 0]) == "foo"
+ assert prim(p.AT, [w_obj, 1]) == "foo"
def test_invalid_at():
w_obj = mockclass(0).as_class_get_shadow().new()
- prim_fails(p.AT, [w_obj, 0])
+ prim_fails(p.AT, [w_obj, 1])
def test_at_put():
w_obj = mockclass(0, varsized=1).as_class_get_shadow().new(1)
- assert prim(p.AT_PUT, [w_obj, 0, 22]).value == 22
- assert prim(p.AT, [w_obj, 0]).value == 22
+ assert prim(p.AT_PUT, [w_obj, 1, 22]).value == 22
+ assert prim(p.AT, [w_obj, 1]).value == 22
def test_invalid_at_put():
w_obj = mockclass(0).as_class_get_shadow().new()
- prim_fails(p.AT_PUT, [w_obj, 0, 22])
+ prim_fails(p.AT_PUT, [w_obj, 1, 22])
def test_string_at():
- assert prim(p.STRING_AT, ["foobar", 3]) == wrap("b")
+ assert prim(p.STRING_AT, ["foobar", 4]) == wrap("b")
def test_string_at_put():
test_str = wrap("foobar")
- assert prim(p.STRING_AT_PUT, [test_str, 3, "c"]) == wrap("c")
+ assert prim(p.STRING_AT_PUT, [test_str, 4, "c"]) == wrap("c")
exp = "foocar"
for i in range(len(exp)):
assert prim(p.STRING_AT, [test_str, i]) == wrap(exp[i])
def test_object_at():
- w_v = prim(p.OBJECT_AT, ["q", objtable.CHARACTER_VALUE_INDEX])
+ w_v = prim(p.OBJECT_AT, ["q", objtable.CHARACTER_VALUE_INDEX+1])
assert w_v.value == ord("q")
def test_invalid_object_at():
- prim_fails(p.OBJECT_AT, ["q", objtable.CHARACTER_VALUE_INDEX+1])
+ prim_fails(p.OBJECT_AT, ["q", objtable.CHARACTER_VALUE_INDEX+2])
def test_object_at_put():
w_obj = mockclass(1).as_class_get_shadow().new()
- assert prim(p.OBJECT_AT_PUT, [w_obj, 0, "q"]) is wrap("q")
- assert prim(p.OBJECT_AT, [w_obj, 0]) is wrap("q")
+ assert prim(p.OBJECT_AT_PUT, [w_obj, 1, "q"]) is wrap("q")
+ assert prim(p.OBJECT_AT, [w_obj, 1]) is wrap("q")
def test_invalid_object_at_put():
w_obj = mockclass(1).as_class_get_shadow().new()
- prim_fails(p.OBJECT_AT, [w_obj, 1, 1])
+ prim_fails(p.OBJECT_AT, [w_obj, 2, 42])
def test_string_at_put():
test_str = wrap("foobar")
- assert prim(p.STRING_AT_PUT, [test_str, 3, "c"]) == wrap("c")
+ assert prim(p.STRING_AT_PUT, [test_str, 4, "c"]) == wrap("c")
exp = "foocar"
- for i in range(len(exp)):
- assert prim(p.STRING_AT, [test_str, i]) == wrap(exp[i])
+ for i in range(1,len(exp)+1):
+ assert prim(p.STRING_AT, [test_str, i]) == wrap(exp[i-1])
def test_new():
w_Object = classtable.classtable['w_Object']
@@ -243,6 +243,7 @@
assert w_v.value == ord("b")
def test_as_oop():
+ py.test.skip("not yet clear what AS_OOP returns: hash or header?")
w_obj = mockclass(0).as_class_get_shadow().new()
w_obj.w_hash = wrap(22)
assert prim(p.AS_OOP, [w_obj]).value == 22
More information about the Pypy-commit
mailing list