python/nondist/sandbox/parrotbench b5.py, NONE, 1.1 out5, NONE, 1.1 b.py, 1.3, 1.4

Update of /cvsroot/python/python/nondist/sandbox/parrotbench In directory sc8-pr-cvs1:/tmp/cvs-serv15475 Modified Files: b.py Added Files: b5.py out5 Log Message: Add rudimentary checks of builtins. --- NEW FILE: b5.py --- # Test existence and performance of builtins that aren't used elsewhere show = True def check(a, b): if __debug__: if show: print `a`, "==", `b` if not a == b: raise AssertionError("%.30r != %.30r" % (a, b)) def exception(exc, f, *args): try: f(*args) except exc: pass else: raise AssertionError("%s not raised by %s%r", exc.__name, f.__name__, args) def check_functions(): check(abs(42), 42) check(abs(-42), 42) check(abs(-12345678910), 12345678910) check(abs(-3.14), 3.14) check(abs(3j+4), 5) check(bool(1), True) check(bool(100), True) check(bool(0), False) check(bool([1,2,3]), True) check(bool([]), False) check(bool({1: 2}), True) check(bool({}), False) check(complex(3, 4), 3+4j) check(dict([(1,2), (3,4)]), {1: 2, 3: 4}) check(dict.fromkeys("abc"), {'a': None, 'b': None, 'c': None}) check(divmod(7, 4), (1, 3)) check(list(enumerate("abc")), [(0, 'a'), (1, 'b'), (2, 'c')]) check(filter(None, range(10)), range(1, 10)) check(filter(lambda x: x < 5, range(10)), range(5)) check(float("1.5"), 1.5) check(float(15), 15.0) check(float(10**100), 1e100) check(hash(42), hash(42L)) check(hash(42), hash(42.0)) check(hash(42+0j), hash(42.0)) check(hash("abc"), hash(u"abc")) check(hex(42).lower(), "0x2a") check(hex(42L).lower(), "0x2al") check(int("42"), 42) check(int("12345678910"), 12345678910) check(int("42", 0), 42) check(int("042", 0), 34) check(int("0x42", 0), 66) check(int("42", 8), 34) check(int("42", 16), 66) check(list((1, 2, 3)), [1, 2, 3]) check(list("abc"), ['a', 'b', 'c']) check(list(u"abc"), ['a', 'b', 'c']) check(long("42"), 42) check(long("12345678910"), 12345678910) check(long("42", 0), 42) check(long("042", 0), 34) check(long("0x42", 0), 66) check(long("42", 8), 34) check(long("42", 16), 66) check(map(None, range(5)), range(5)) check(map(None, range(5), range(5)), [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]) check(map(lambda x: x+1, range(5)), range(1, 6)) check(map(len, ("", "a", "ab", "abc")), range(4)) check(min(1, 5), 1) check(min([3, 1, 2]), 1) check(min("Bac"), "B") check(min(u"aBc"), u"B") a = object() b = object() if id(a) == id(b): raise AssertionError("objects not unique") check(oct(42), '052') check(oct(42L), '052L') check(ord("a"), 97) check(ord(u"a"), 97) check(ord(u"\u1234"), 0x1234) check(pow(2, 10), 1024) check(pow(2, 100), 1L<<100) check(pow(2, 10, 100), 24) check(reduce(lambda a, b: a+b, ("a", "b", "c", "d", "e")), "abcde") check(repr(42), "42") check(repr(42L), "42L") check(repr(3.5), "3.5") check(repr(4.5j), "4.5j") check(repr(4j+3), "(3+4j)") check(repr(4j-3), "(-3+4j)") check(repr(-4j), "-4j") check(repr(3.5-0j), "(3.5+0j)") check(repr("abc"), "'abc'") check(repr("abc\012"), "'abc\\n'") check(repr(u"abc"), "u'abc'") check(repr(u"abc\u1234"), "u'abc\u1234'") check(repr(range(5)), "[0, 1, 2, 3, 4]") check(repr(('a', 'b', 'c')), "('a', 'b', 'c')") check(repr({1: 42}), "{1: 42}") for x in 42, 42L, 3.5, 4.5j, 4j+3, "abc", range(3), (1, 2, 'c'), {}: check(repr(x), `x`) check(str(42), "42") check(str(42L), "42") check(str(3.5), "3.5") check(str(4.5j), "4.5j") check(str(4j+3), "(3+4j)") check(str(4j-3), "(-3+4j)") check(str(-4j), "-4j") check(str(3.5-0j), "(3.5+0j)") check(str("abc"), "abc") check(str(range(5)), "[0, 1, 2, 3, 4]") check(str(('a', 'b', 'c')), "('a', 'b', 'c')") check(str({1: 42}), "{1: 42}") check(sum(range(1, 11)), 55) check(sum((3.5, 2, 4.5)), 10) check(tuple("abc"), ('a', 'b', 'c')) check(tuple(range(5)), (0, 1, 2, 3, 4)) check(tuple({1: 2}), (1,)) check(tuple(u"abc\u1234"), (u'a', u'b', u'c', u'\u1234')) check(type(1 == 1), bool) check(type(42), int) check(type(42L), long) check(type(3.14), float) check(type(0j), complex) check(type(''), str) check(type(u''), unicode) check(type(()), tuple) check(type(range(10)), list) check(type({}), dict) check(type(type), type) check(type(object), type) check(type(lambda: None), type(check_functions)) check(unicode("abc"), u"abc") check(unicode("abc", "ascii"), u"abc") check(unicode("abc\xff", "Latin-1"), u"abc\u00ff") check(unicode("abc\xc3\xbf", "utf8"), u"abc\xff") exception(UnicodeError, unicode, "abc\xff", "ascii") exception(UnicodeError, unicode, "abc\xff", "utf-8") check(list(xrange(10)), range(10)) check(zip("abc", "def"), [('a', 'd'), ('b', 'e'), ('c', 'f')]) check(zip("abc", "def", "ghi"), [('a', 'd', 'g'), ('b', 'e', 'h'), ('c', 'f', 'i')]) def check_descriptors(): class C(object): def getx(self): return self._x def setx(self, x): self._x = x def delx(self): del self._x x = property(getx, setx, delx) xx = property(getx) def f(*args): return list(args) fc = classmethod(f) fs = staticmethod(f) def __repr__(self): s = super(C, self).__repr__() s = s.replace("<" + __name__ + ".", "<") i = s.index(" at ") s = s[:i] + ">" return s c1 = C() exception(AttributeError, getattr, c1, "x") exception(AttributeError, getattr, c1, "xx") exception(AttributeError, setattr, c1, "xx", 42) setattr(c1, "x", 42) check(c1.x, 42) check(c1._x, 42) check(c1.xx, 42) exception(AttributeError, delattr, c1, "xx") del c1.x exception(AttributeError, getattr, c1, "x") exception(AttributeError, getattr, c1, "xx") exception(AttributeError, delattr, c1, "x") check(c1.f(42), [c1, 42]) check(c1.fc(42), [C, 42]) check(c1.fs(42), [42]) check(repr(c1), "<C object>") def main(): global show show = True for i in range(500): check_functions() check_descriptors() show = False print "OK." if __name__ == '__main__': main() --- NEW FILE: out5 --- 42 == 42 42 == 42 12345678910L == 12345678910L 3.1400000000000001 == 3.1400000000000001 5.0 == 5 True == True True == True False == False True == True False == False True == True False == False (3+4j) == (3+4j) {1: 2, 3: 4} == {1: 2, 3: 4} {'a': None, 'c': None, 'b': None} == {'a': None, 'c': None, 'b': None} (1, 3) == (1, 3) [(0, 'a'), (1, 'b'), (2, 'c')] == [(0, 'a'), (1, 'b'), (2, 'c')] [1, 2, 3, 4, 5, 6, 7, 8, 9] == [1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4] == [0, 1, 2, 3, 4] 1.5 == 1.5 15.0 == 15.0 1e+100 == 1e+100 42 == 42 42 == 42 42 == 42 -1600925533 == -1600925533 '0x2a' == '0x2a' '0x2al' == '0x2al' 42 == 42 12345678910L == 12345678910L 42 == 42 34 == 34 66 == 66 34 == 34 66 == 66 [1, 2, 3] == [1, 2, 3] ['a', 'b', 'c'] == ['a', 'b', 'c'] [u'a', u'b', u'c'] == ['a', 'b', 'c'] 42L == 42 12345678910L == 12345678910L 42L == 42 34L == 34 66L == 66 34L == 34 66L == 66 [0, 1, 2, 3, 4] == [0, 1, 2, 3, 4] [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)] == [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)] [1, 2, 3, 4, 5] == [1, 2, 3, 4, 5] [0, 1, 2, 3] == [0, 1, 2, 3] 1 == 1 1 == 1 'B' == 'B' u'B' == u'B' '052' == '052' '052L' == '052L' 97 == 97 97 == 97 4660 == 4660 1024 == 1024 1267650600228229401496703205376L == 1267650600228229401496703205376L 24 == 24 'abcde' == 'abcde' '42' == '42' '42L' == '42L' '3.5' == '3.5' '4.5j' == '4.5j' '(3+4j)' == '(3+4j)' '(-3+4j)' == '(-3+4j)' '-4j' == '-4j' '(3.5+0j)' == '(3.5+0j)' "'abc'" == "'abc'" "'abc\\n'" == "'abc\\n'" "u'abc'" == "u'abc'" "u'abc\\u1234'" == "u'abc\\u1234'" '[0, 1, 2, 3, 4]' == '[0, 1, 2, 3, 4]' "('a', 'b', 'c')" == "('a', 'b', 'c')" '{1: 42}' == '{1: 42}' '42' == '42' '42L' == '42L' '3.5' == '3.5' '4.5j' == '4.5j' '(3+4j)' == '(3+4j)' "'abc'" == "'abc'" '[0, 1, 2]' == '[0, 1, 2]' "(1, 2, 'c')" == "(1, 2, 'c')" '{}' == '{}' '42' == '42' '42' == '42' '3.5' == '3.5' '4.5j' == '4.5j' '(3+4j)' == '(3+4j)' '(-3+4j)' == '(-3+4j)' '-4j' == '-4j' '(3.5+0j)' == '(3.5+0j)' 'abc' == 'abc' '[0, 1, 2, 3, 4]' == '[0, 1, 2, 3, 4]' "('a', 'b', 'c')" == "('a', 'b', 'c')" '{1: 42}' == '{1: 42}' 55 == 55 10.0 == 10 ('a', 'b', 'c') == ('a', 'b', 'c') (0, 1, 2, 3, 4) == (0, 1, 2, 3, 4) (1,) == (1,) (u'a', u'b', u'c', u'\u1234') == (u'a', u'b', u'c', u'\u1234') <type 'bool'> == <type 'bool'> <type 'int'> == <type 'int'> <type 'long'> == <type 'long'> <type 'float'> == <type 'float'> <type 'complex'> == <type 'complex'> <type 'str'> == <type 'str'> <type 'unicode'> == <type 'unicode'> <type 'tuple'> == <type 'tuple'> <type 'list'> == <type 'list'> <type 'dict'> == <type 'dict'> <type 'type'> == <type 'type'> <type 'type'> == <type 'type'> <type 'function'> == <type 'function'> u'abc' == u'abc' u'abc' == u'abc' u'abc\xff' == u'abc\xff' u'abc\xff' == u'abc\xff' [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [('a', 'd'), ('b', 'e'), ('c', 'f')] == [('a', 'd'), ('b', 'e'), ('c', 'f')] [('a', 'd', 'g'), ('b', 'e', 'h'), ('c', 'f', 'i')] == [('a', 'd', 'g'), ('b', 'e', 'h'), ('c', 'f', 'i')] 42 == 42 42 == 42 42 == 42 [<C object>, 42] == [<C object>, 42] [<class '__main__.C'>, 42] == [<class '__main__.C'>, 42] [42] == [42] '<C object>' == '<C object>' OK. Index: b.py =================================================================== RCS file: /cvsroot/python/python/nondist/sandbox/parrotbench/b.py,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** b.py 31 Dec 2003 02:04:20 -0000 1.3 --- b.py 31 Dec 2003 05:39:55 -0000 1.4 *************** *** 4,5 **** --- 4,6 ---- import b3 import b4 + import b5; b5.main()
participants (1)
-
gvanrossumļ¼ users.sourceforge.net