test_b1.py

来自「mallet是自然语言处理、机器学习领域的一个开源项目。」· Python 代码 · 共 633 行 · 第 1/2 页

PY
633
字号
hash(None)if not hash(1) == hash(1L) == hash(1.0): raise TestFailed, 'numeric hash()'hash('spam')hash((0,1,2,3))def f(): passtry: hash([])except TypeError: passelse: raise TestFailed, "hash([]) should raise an exception"try: hash({})except TypeError: passelse: raise TestFailed, "hash({}) should raise an exception"print 'hex'if hex(16) != '0x10': raise TestFailed, 'hex(16)'if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)'if len(hex(-1)) != len(hex(sys.maxint)): raise TestFailed, 'len(hex(-1))'if hex(-16) not in ('0xfffffff0', '0xfffffffffffffff0'):    raise TestFailed, 'hex(-16)'if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)'print 'id'id(None)id(1)id(1L)id(1.0)id('spam')id((0,1,2,3))id([0,1,2,3])id({'spam': 1, 'eggs': 2, 'ham': 3})# Test input() later, together with raw_inputprint 'int'if int(314) != 314: raise TestFailed, 'int(314)'if int(3.14) != 3: raise TestFailed, 'int(3.14)'if int(314L) != 314: raise TestFailed, 'int(314L)'# Check that conversion from float truncates towards zeroif int(-3.14) != -3: raise TestFailed, 'int(-3.14)'if int(3.9) != 3: raise TestFailed, 'int(3.9)'if int(-3.9) != -3: raise TestFailed, 'int(-3.9)'if int(3.5) != 3: raise TestFailed, 'int(3.5)'if int(-3.5) != -3: raise TestFailed, 'int(-3.5)'# Different base:if int("10",16) != 16L: raise TestFailed, 'int("10",16)'if have_unicode:    if int(unicode("10"),16) != 16L:        raise TestFailed, 'int(u"10",16)'# Test conversion from strings and various anomaliesL = [        ('0', 0),        ('1', 1),        ('9', 9),        ('10', 10),        ('99', 99),        ('100', 100),        ('314', 314),        (' 314', 314),        ('314 ', 314),        ('  \t\t  314  \t\t  ', 314),        (`sys.maxint`, sys.maxint),        ('  1x', ValueError),        ('  1  ', 1),        ('  1\02  ', ValueError),        ('', ValueError),        (' ', ValueError),        ('  \t\t  ', ValueError)]if have_unicode:    L += [        (unicode('0'), 0),        (unicode('1'), 1),        (unicode('9'), 9),        (unicode('10'), 10),        (unicode('99'), 99),        (unicode('100'), 100),        (unicode('314'), 314),        (unicode(' 314'), 314),        (unicode('\u0663\u0661\u0664 ','raw-unicode-escape'), 314),        (unicode('  \t\t  314  \t\t  '), 314),        (unicode('  1x'), ValueError),        (unicode('  1  '), 1),        (unicode('  1\02  '), ValueError),        (unicode(''), ValueError),        (unicode(' '), ValueError),        (unicode('  \t\t  '), ValueError),]for s, v in L:    for sign in "", "+", "-":        for prefix in "", " ", "\t", "  \t\t  ":            ss = prefix + sign + s            vv = v            if sign == "-" and v is not ValueError:                vv = -v            try:                if int(ss) != vv:                    raise TestFailed, "int(%s)" % `ss`            except v:                pass            except ValueError, e:                raise TestFailed, "int(%s) raised ValueError: %s" % (`ss`, e)s = `-1-sys.maxint`if int(s)+1 != -sys.maxint:    raise TestFailed, "int(%s)" % `s`try:    int(s[1:])except ValueError:    passelse:    raise TestFailed, "int(%s)" % `s[1:]` + " should raise ValueError"try:    int(1e100)except OverflowError:    passelse:    raise TestFailed("int(1e100) expected OverflowError")try:    int(-1e100)except OverflowError:    passelse:    raise TestFailed("int(-1e100) expected OverflowError")# SF bug 434186:  0x80000000/2 != 0x80000000>>1.# Worked by accident in Windows release build, but failed in debug build.# Failed in all Linux builds.x = -1-sys.maxintif x >> 1 != x//2:    raise TestFailed("x >> 1 != x/2 when x == -1-sys.maxint")try: int('123\0')except ValueError: passelse: raise TestFailed("int('123\0') didn't raise exception")print 'isinstance'class C:    passclass D(C):    passclass E:    passc = C()d = D()e = E()if not isinstance(c, C): raise TestFailed, 'isinstance(c, C)'if not isinstance(d, C): raise TestFailed, 'isinstance(d, C)'if isinstance(e, C): raise TestFailed, 'isinstance(e, C)'if isinstance(c, D): raise TestFailed, 'isinstance(c, D)'if isinstance('foo', E): raise TestFailed, 'isinstance("Foo", E)'try:    isinstance(E, 'foo')    raise TestFailed, 'isinstance(E, "foo")'except TypeError:    passprint 'issubclass'if not issubclass(D, C): raise TestFailed, 'issubclass(D, C)'if not issubclass(C, C): raise TestFailed, 'issubclass(C, C)'if issubclass(C, D): raise TestFailed, 'issubclass(C, D)'try:    issubclass('foo', E)    raise TestFailed, 'issubclass("foo", E)'except TypeError:    passtry:    issubclass(E, 'foo')    raise TestFailed, 'issubclass(E, "foo")'except TypeError:    passprint 'len'if len('123') != 3: raise TestFailed, 'len(\'123\')'if len(()) != 0: raise TestFailed, 'len(())'if len((1, 2, 3, 4)) != 4: raise TestFailed, 'len((1, 2, 3, 4))'if len([1, 2, 3, 4]) != 4: raise TestFailed, 'len([1, 2, 3, 4])'if len({}) != 0: raise TestFailed, 'len({})'if len({'a':1, 'b': 2}) != 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})'print 'list'if list([]) != []: raise TestFailed, 'list([])'l0_3 = [0, 1, 2, 3]l0_3_bis = list(l0_3)if l0_3 != l0_3_bis or l0_3 is l0_3_bis: raise TestFailed, 'list([0, 1, 2, 3])'if list(()) != []: raise TestFailed, 'list(())'if list((0, 1, 2, 3)) != [0, 1, 2, 3]: raise TestFailed, 'list((0, 1, 2, 3))'if list('') != []: raise TestFailed, 'list('')'if list('spam') != ['s', 'p', 'a', 'm']: raise TestFailed, "list('spam')"if sys.maxint == 0x7fffffff:    # This test can currently only work on 32-bit machines.    # XXX If/when PySequence_Length() returns a ssize_t, it should be    # XXX re-enabled.    try:        # Verify clearing of bug #556025.        # This assumes that the max data size (sys.maxint) == max        # address size this also assumes that the address size is at        # least 4 bytes with 8 byte addresses, the bug is not well        # tested        #        # Note: This test is expected to SEGV under Cygwin 1.3.12 or        # earlier due to a newlib bug.  See the following mailing list        # thread for the details:        #     http://sources.redhat.com/ml/newlib/2002/msg00369.html        list(xrange(sys.maxint // 2))    except MemoryError:        pass    else:        raise TestFailed, 'list(xrange(sys.maxint / 4))'print 'long'if long(314) != 314L: raise TestFailed, 'long(314)'if long(3.14) != 3L: raise TestFailed, 'long(3.14)'if long(314L) != 314L: raise TestFailed, 'long(314L)'# Check that conversion from float truncates towards zeroif long(-3.14) != -3L: raise TestFailed, 'long(-3.14)'if long(3.9) != 3L: raise TestFailed, 'long(3.9)'if long(-3.9) != -3L: raise TestFailed, 'long(-3.9)'if long(3.5) != 3L: raise TestFailed, 'long(3.5)'if long(-3.5) != -3L: raise TestFailed, 'long(-3.5)'if long("-3") != -3L: raise TestFailed, 'long("-3")'if have_unicode:    if long(unicode("-3")) != -3L:        raise TestFailed, 'long(u"-3")'# Different base:if long("10",16) != 16L: raise TestFailed, 'long("10",16)'if have_unicode:    if long(unicode("10"),16) != 16L:        raise TestFailed, 'long(u"10",16)'# Check conversions from string (same test set as for int(), and then some)LL = [        ('1' + '0'*20, 10L**20),        ('1' + '0'*100, 10L**100)]if have_unicode:    L+=[        (unicode('1') + unicode('0')*20, 10L**20),        (unicode('1') + unicode('0')*100, 10L**100),]for s, v in L + LL:    for sign in "", "+", "-":        for prefix in "", " ", "\t", "  \t\t  ":            ss = prefix + sign + s            vv = v            if sign == "-" and v is not ValueError:                vv = -v            try:                if long(ss) != long(vv):                    raise TestFailed, "long(%s)" % `ss`            except v:                pass            except ValueError, e:                raise TestFailed, "long(%s) raised ValueError: %s" % (`ss`, e)try: long('123\0')except ValueError: passelse: raise TestFailed("long('123\0') didn't raise exception")print 'map'if map(None, 'hello world') != ['h','e','l','l','o',' ','w','o','r','l','d']:    raise TestFailed, 'map(None, \'hello world\')'if map(None, 'abcd', 'efg') != \   [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]:    raise TestFailed, 'map(None, \'abcd\', \'efg\')'if map(None, range(10)) != [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:    raise TestFailed, 'map(None, range(10))'if map(lambda x: x*x, range(1,4)) != [1, 4, 9]:    raise TestFailed, 'map(lambda x: x*x, range(1,4))'try:    from math import sqrtexcept ImportError:    def sqrt(x):        return pow(x, 0.5)if map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]) != [[4.0, 2.0], [9.0, 3.0]]:    raise TestFailed, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])'if map(lambda x, y: x+y, [1,3,2], [9,1,4]) != [10, 4, 6]:    raise TestFailed, 'map(lambda x,y: x+y, [1,3,2], [9,1,4])'def plus(*v):    accu = 0    for i in v: accu = accu + i    return accuif map(plus, [1, 3, 7]) != [1, 3, 7]:    raise TestFailed, 'map(plus, [1, 3, 7])'if map(plus, [1, 3, 7], [4, 9, 2]) != [1+4, 3+9, 7+2]:    raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2])'if map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]) != [1+4+1, 3+9+1, 7+2+0]:    raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])'if map(None, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:    raise TestFailed, 'map(None, Squares(10))'if map(int, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:    raise TestFailed, 'map(int, Squares(10))'if map(None, Squares(3), Squares(2)) != [(0,0), (1,1), (4,None)]:    raise TestFailed, 'map(None, Squares(3), Squares(2))'if map(max, Squares(3), Squares(2)) != [0, 1, 4]:    raise TestFailed, 'map(max, Squares(3), Squares(2))'print 'max'if max('123123') != '3': raise TestFailed, 'max(\'123123\')'if max(1, 2, 3) != 3: raise TestFailed, 'max(1, 2, 3)'if max((1, 2, 3, 1, 2, 3)) != 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))'if max([1, 2, 3, 1, 2, 3]) != 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])'#if max(1, 2L, 3.0) != 3.0: raise TestFailed, 'max(1, 2L, 3.0)'if max(1L, 2.0, 3) != 3: raise TestFailed, 'max(1L, 2.0, 3)'if max(1.0, 2, 3L) != 3L: raise TestFailed, 'max(1.0, 2, 3L)'print 'min'if min('123123') != '1': raise TestFailed, 'min(\'123123\')'if min(1, 2, 3) != 1: raise TestFailed, 'min(1, 2, 3)'if min((1, 2, 3, 1, 2, 3)) != 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))'if min([1, 2, 3, 1, 2, 3]) != 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])'#if min(1, 2L, 3.0) != 1: raise TestFailed, 'min(1, 2L, 3.0)'if min(1L, 2.0, 3) != 1L: raise TestFailed, 'min(1L, 2.0, 3)'if min(1.0, 2, 3L) != 1.0: raise TestFailed, 'min(1.0, 2, 3L)'

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?