def test_popitem(self): # dict.popitem() for copymode in -1, +1: # -1: b has same structure as a # +1: b is a.copy() for log2size in range(12): size = 2**log2size a = ordereddict() b = ordereddict() for i in range(size): a[repr(i)] = i if copymode < 0: b[repr(i)] = i if copymode > 0: b = a.copy() for i in range(size): ka, va = ta = a.popitem() self.assertEqual(va, int(ka)) kb, vb = tb = b.popitem() self.assertEqual(vb, int(kb)) self.assert_(not (copymode < 0 and ta != tb)) self.assert_(not a) self.assert_(not b) d = ordereddict() self.assertRaises(KeyError, d.popitem)
def test_popitem(self): # dict.popitem() for copymode in -1, +1: # -1: b has same structure as a # +1: b is a.copy() for log2size in range(12): size = 2**log2size a = ordereddict() b = ordereddict() for i in range(size): a[repr(i)] = i if copymode < 0: b[repr(i)] = i if copymode > 0: b = a.copy() for i in range(size): ka, va = ta = a.popitem() self.assertEqual(va, int(ka)) kb, vb = tb = b.popitem() self.assertEqual(vb, int(kb)) self.assert_(not(copymode < 0 and ta != tb)) self.assert_(not a) self.assert_(not b) d = ordereddict() self.assertRaises(KeyError, d.popitem)
def _get_info_dict(self, info_text): """ Helper function to parse the INFO field of a VCF into a dict. VEP-like entries are included as a list of nested dictionaries. Args: info_text (str): text from the INFO column of the VCF Returns: dict: dict containing parsed data from info_text. """ tags = [x.split('=') for x in info_text.split(';')] info_dict = ordereddict(tags) for tag in info_dict: if 'format' in self.infos[tag]: result = [] for entry in info_dict[tag].split(','): result.append( ordereddict( zip(self.infos[tag]['format'], entry.split(FORMAT_DELIMITER)))) info_dict[tag] = result return info_dict
def test_fromkeys(self): od1 = ordereddict([ ('a', None), ('b', None), ('c', None), ]) self.assertEqual(ordereddict.fromkeys('abc'), od1) d = ordereddict() self.assert_(not (d.fromkeys('abc') is d)) self.assertEqual(d.fromkeys('abc'), od1) self.assertEqual(d.fromkeys((4, 5), 0), ordereddict(((4, 0), (5, 0)))) self.assertEqual(d.fromkeys([]), {}) def g(): yield 1 self.assertEqual(d.fromkeys(g()), ordereddict(((1, None), ))) self.assertRaises(TypeError, {}.fromkeys, 3) class dictlike(ordereddict): pass self.assertEqual(dictlike.fromkeys('a'), {'a': None}) self.assertEqual(dictlike().fromkeys('a'), {'a': None}) self.assert_(type(dictlike.fromkeys('a')) is dictlike) self.assert_(type(dictlike().fromkeys('a')) is dictlike) class mydict(ordereddict): def __new__(cls): return UserDict.UserDict() ud = mydict.fromkeys('ab') self.assertEqual(ud, {'a': None, 'b': None}) self.assert_(isinstance(ud, UserDict.UserDict)) self.assertRaises(TypeError, dict.fromkeys) class Exc(Exception): pass class baddict1(dict): def __init__(self): raise Exc() self.assertRaises(Exc, baddict1.fromkeys, [1]) class BadSeq(object): def __iter__(self): return self def next(self): raise Exc() self.assertRaises(Exc, ordereddict.fromkeys, BadSeq()) class baddict2(ordereddict): def __setitem__(self, key, value): raise Exc() self.assertRaises(Exc, baddict2.fromkeys, [1])
def test_ass_non_consequitive_slice(self): r = self.z r[10:15:2] = ordereddict([(1, 0), (2, 1), (3, 2),]) #print r[9:16] #print ordereddict([('j', 9), (1, 0), ('l', 11), (2, 1), # ('n', 13), (3, 2), ('p', 15)]) assert r[9:16] == ordereddict([('j', 9), (1, 0), ('l', 11), (2, 1), ('n', 13), (3, 2), ('p', 15)])
def test_ass_reverse_non_consequitive_slice(self): r = self.z r[14:9:-2] = ordereddict([(3, 2), (2, 1), (1, 0),]) assert len(r) == 26 #print r[9:16] #print ordereddict([('j', 9), (1, 0), ('l', 11), (2, 1), # ('n', 13), (3, 2), ('p', 15)]) assert r[9:16] == ordereddict([('j', 9), (1, 0), ('l', 11), (2, 1), ('n', 13), (3, 2), ('p', 15)])
def test_keys(self): d = ordereddict() self.assertEqual(d.keys(), []) d = ordereddict([('a', 1), ('b', 2)]) k = d.keys() self.assert_(d.has_key('a')) self.assert_(d.has_key('b')) self.assertRaises(TypeError, d.keys, None)
def test_ass_non_consequitive_slice(self): r = self.z r[10:15:2] = ordereddict([ (1, 0), (2, 1), (3, 2), ]) #print r[9:16] #print ordereddict([('j', 9), (1, 0), ('l', 11), (2, 1), # ('n', 13), (3, 2), ('p', 15)]) assert r[9:16] == ordereddict([('j', 9), (1, 0), ('l', 11), (2, 1), ('n', 13), (3, 2), ('p', 15)])
def test_ass_reverse_non_consequitive_slice(self): r = self.z r[14:9:-2] = ordereddict([ (3, 2), (2, 1), (1, 0), ]) assert len(r) == 26 #print r[9:16] #print ordereddict([('j', 9), (1, 0), ('l', 11), (2, 1), # ('n', 13), (3, 2), ('p', 15)]) assert r[9:16] == ordereddict([('j', 9), (1, 0), ('l', 11), (2, 1), ('n', 13), (3, 2), ('p', 15)])
def setup_method(self, method): self.x = ordereddict() self.x['a'] = 1 self.x['b'] = 2 self.x['c'] = 3 self.x['d'] = 4 self.z = ordereddict() for index, ch in enumerate(string.lowercase): self.z[ch] = index self.e = ordereddict() self.part = ordereddict((('f', 5),('g', 6),('h', 7),('i', 8), ('j', 9))) #self.revpart = ordereddict((('j', 9),('g', 6),('i', 8),('h', 7), ('f', 5))) self.upperlower = ordereddict([('A', 1), ('a', 2), ('B', 4), ('b', 4)])
def setup_method(self, method): self.x = ordereddict() self.x['a'] = 1 self.x['b'] = 2 self.x['c'] = 3 self.x['d'] = 4 self.z = ordereddict() for index, ch in enumerate(string.lowercase): self.z[ch] = index self.e = ordereddict() self.part = ordereddict( (('f', 5), ('g', 6), ('h', 7), ('i', 8), ('j', 9))) #self.revpart = ordereddict((('j', 9),('g', 6),('i', 8),('h', 7), ('f', 5))) self.upperlower = ordereddict([('A', 1), ('a', 2), ('B', 4), ('b', 4)])
def test_setdefault(self): # ordereddict.setdefault() d = ordereddict() self.assert_(d.setdefault('key0') is None) d.setdefault('key0', []) self.assert_(d.setdefault('key0') is None) d.setdefault('key', []).append(3) self.assertEqual(d['key'][0], 3) d.setdefault('key', []).append(4) self.assertEqual(len(d['key']), 2) self.assertRaises(TypeError, d.setdefault) class Exc(Exception): pass class BadHash(object): fail = False def __hash__(self): if self.fail: raise Exc() else: return 42 x = BadHash() d[x] = 42 x.fail = True self.assertRaises(Exc, d.setdefault, x, [])
def test_tuple_keyerror(self): # SF #1576657 d = ordereddict() try: d[(1, )] except KeyError, e: self.assertEqual(e.args, ((1, ), ))
def test_repr(self): d = ordereddict() assert repr(d) == 'ordereddict([])' d['a'] = 1 assert repr(d) == "ordereddict([('a', 1)])" d[2] = 'b' assert repr(d) == "ordereddict([('a', 1), (2, 'b')])"
def test_tuple_keyerror(self): # SF #1576657 d = ordereddict() try: d[(1,)] except KeyError, e: self.assertEqual(e.args, ((1,),))
def test_values(self): d = ordereddict() self.assertEqual(d.values(), []) d[1] = 2 self.assertEqual(d.values(), [2]) self.assertRaises(TypeError, d.values, None)
def test_slice(self): r = self.z rp = self.part.copy() rp.reverse() assert r[9:4:-1] == rp assert r[5:25:5] == ordereddict([('f', 5), ('k', 10), ('p', 15), ('u', 20)])
def test_relax(self): nd = dict(z=1, y=2, w=3, v=4, x=5) if not self.nopytest: py.test.raises(TypeError, "r = ordereddict(nd)") r = ordereddict(nd, relax=True) assert nd.keys() == r.keys() assert nd.values() == r.values()
def test_relax_update(self): d = ordereddict() nd = dict(z=1, y=2, w=3, v=4, x=5) if not self.nopytest: py.test.raises(TypeError, "d.update(nd)") d.update(nd, relax=True) assert len(d) == 5
def test_relax(self): nd = dict(z=1,y=2,w=3,v=4,x=5) if not self.nopytest: py.test.raises(TypeError, "r = ordereddict(nd)") r = ordereddict(nd, relax=True) assert nd.keys() == r.keys() assert nd.values() == r.values()
def test_relax_update(self): d = ordereddict() nd = dict(z=1,y=2,w=3,v=4,x=5) if not self.nopytest: py.test.raises(TypeError, "d.update(nd)") d.update(nd, relax=True) assert len(d) == 5
def test_kvio2(self): r = ordereddict(kvio=True) r.update(self.x) r['b']=2 assert r != self.x r.update([('c', 3), ('d', 4)]) assert r == self.x
def test_kvio2(self): r = ordereddict(kvio=True) r.update(self.x) r['b'] = 2 assert r != self.x r.update([('c', 3), ('d', 4)]) assert r == self.x
def test_kvio_copy(self): r = ordereddict(kvio=True) r.update(self.x) r['b']=2 r1 = r.copy() assert r1 != self.x r1.update([('c', 3), ('d', 4)]) assert r1 == self.x
def test_repr(self): d = ordereddict() self.assertEqual(repr(d), 'ordereddict([])') d[1] = 2 self.assertEqual(repr(d), 'ordereddict([(1, 2)])') d = ordereddict() d[1] = d self.assertEqual(repr(d), 'ordereddict([(1, {...})])') class Exc(Exception): pass class BadRepr(object): def __repr__(self): raise Exc() d = {1: BadRepr()} self.assertRaises(Exc, repr, d)
def test_items(self): d = ordereddict() self.assertEqual(d.items(), []) d[1] = 2 self.assertEqual(d.items(), [(1, 2)]) self.assertRaises(TypeError, d.items, None)
def test_copy(self): d = ordereddict() d[1] = 1 d[2] = 2 d[3] = 3 self.assertEqual(d.copy(), {1: 1, 2: 2, 3: 3}) self.assertEqual({}.copy(), {}) self.assertRaises(TypeError, d.copy, None)
def test_kvio_copy(self): r = ordereddict(kvio=True) r.update(self.x) r['b'] = 2 r1 = r.copy() assert r1 != self.x r1.update([('c', 3), ('d', 4)]) assert r1 == self.x
def do_one(): d = ordereddict() index = 0 for ch1 in string.lowercase: for ch2 in string.lowercase: for ch3 in string.lowercase: d[ch1 + ch2] = index index += 1
def test_fromkeys(self): od1 = ordereddict([('a', None),('b', None),('c', None),]) self.assertEqual(ordereddict.fromkeys('abc'), od1) d = ordereddict() self.assert_(not(d.fromkeys('abc') is d)) self.assertEqual(d.fromkeys('abc'), od1) self.assertEqual(d.fromkeys((4,5),0), ordereddict(((4,0), (5,0)))) self.assertEqual(d.fromkeys([]), {}) def g(): yield 1 self.assertEqual(d.fromkeys(g()), ordereddict(((1,None),))) self.assertRaises(TypeError, {}.fromkeys, 3) class dictlike(ordereddict): pass self.assertEqual(dictlike.fromkeys('a'), {'a':None}) self.assertEqual(dictlike().fromkeys('a'), {'a':None}) self.assert_(type(dictlike.fromkeys('a')) is dictlike) self.assert_(type(dictlike().fromkeys('a')) is dictlike) class mydict(ordereddict): def __new__(cls): return UserDict.UserDict() ud = mydict.fromkeys('ab') self.assertEqual(ud, {'a':None, 'b':None}) self.assert_(isinstance(ud, UserDict.UserDict)) self.assertRaises(TypeError, dict.fromkeys) class Exc(Exception): pass class baddict1(dict): def __init__(self): raise Exc() self.assertRaises(Exc, baddict1.fromkeys, [1]) class BadSeq(object): def __iter__(self): return self def next(self): raise Exc() self.assertRaises(Exc, ordereddict.fromkeys, BadSeq()) class baddict2(ordereddict): def __setitem__(self, key, value): raise Exc() self.assertRaises(Exc, baddict2.fromkeys, [1])
def test_copy(self): d = ordereddict() d[1] = 1 d[2] = 2 d[3] = 3 self.assertEqual(d.copy(), {1:1, 2:2, 3:3}) self.assertEqual({}.copy(), {}) self.assertRaises(TypeError, d.copy, None)
def test_insert_range(self): r = ordereddict() r['c'] = 3 r.insert(0, 'b', 2) r.insert(-10, 'a', 1) r.insert(3, 'd', 4) r.insert(10, 'e', 5) self.x['e'] = 5 assert r == self.x
def test_clear(self): x = ordereddict() x['a'] = 1 x['b'] = 2 x['c'] = 3 x['d'] = 4 assert len(x) == 4 x.clear() assert len(x) == 0
def test_clear(self): d = ordereddict() d[1] = 1 d[2] = 2 d[3] = 3 d.clear() self.assertEqual(d, {}) self.assertRaises(TypeError, d.clear, None)
def test_pop(self): # Tests for pop with specified key d = ordereddict() k, v = 'abc', 'def' d[k] = v self.assertRaises(KeyError, d.pop, 'ghi') self.assertEqual(d.pop(k), v) self.assertEqual(len(d), 0) self.assertRaises(KeyError, d.pop, k) # verify longs/ints get same value when key > 32 bits (for 64-bit archs) # see SF bug #689659 x = 4503599627370496L y = 4503599627370496 h = ordereddict(( (x, 'anything'), (y, 'something else'), )) self.assertEqual(h[x], h[y]) self.assertEqual(d.pop(k, v), v) d[k] = v self.assertEqual(d.pop(k, 1), v) self.assertRaises(TypeError, d.pop) class Exc(Exception): pass class BadHash(object): fail = False def __hash__(self): if self.fail: raise Exc() else: return 42 x = BadHash() d[x] = 42 x.fail = True self.assertRaises(Exc, d.pop, x)
def test_has_key(self): d = ordereddict() self.assert_(not d.has_key('a')) d['a'] = 1 d['b'] = 2 k = d.keys() k.sort() self.assertEqual(k, ['a', 'b']) self.assertRaises(TypeError, d.has_key)
def test_multiple_inserts_then_deletes(self): d = ordereddict() for i in range(1,6): d.insert(i-1, i, 'a') for i in range(1,6): del d[i] d.insert(0, 0, 'will this crash?')
def test_deepcopy(self): # found thanks to Alexandre Andrade import copy import gc d = ordereddict([(1, 2)]) gc.collect() none_refcount = sys.getrefcount(None) for i in xrange(10): copy.deepcopy(d) gc.collect() assert none_refcount == sys.getrefcount(None)
def test_mutatingiteration(self): d = ordereddict() d[1] = 1 try: for i in d: d[i+1] = 1 except RuntimeError: pass else: self.fail("changing dict size during iteration doesn't raise Error")
def test_multiple_inserts_then_deletes(self): d = ordereddict() for i in range(1, 6): d.insert(i - 1, i, 'a') for i in range(1, 6): del d[i] d.insert(0, 0, 'will this crash?')
def test_contains(self): d = ordereddict() self.assert_(not ('a' in d)) self.assert_('a' not in d) d['a'] = 1 d['b'] = 2 self.assert_('a' in d) self.assert_('b' in d) self.assert_('c' not in d) self.assertRaises(TypeError, d.__contains__)
def test_update(self): y = ordereddict() y[1] = 'x' y['b'] = 'abc' y[3] = 'xx' y.update(self.x) assert len(y) == 6 assert y.values()[1] == self.x.values()[1] assert y.values()[3] == self.x.values()[0] assert y.values()[4] == self.x.values()[2] assert y.values()[5] == self.x.values()[3]
def test_del_rev_non_consequitive_slice(self): r = self.z del r[22:3:-2] t = ordereddict() for index, ch in enumerate(string.lowercase): if ch not in 'abcdfhjlnprtvxyz': continue t[ch] = index #print r #print t assert r == t
def test_setkeys(self): self.x[42] = 'abc' r1 = self.x.copy() r2 = ordereddict([('d', 4), ('c', 3), ('a', 1), (42, 'abc'), ('b', 2),]) r1.setkeys(('d', 'c', 'a', 42, 'b',)) assert r1 == r2 if not self.nopytest: py.test.raises(KeyError, "r1.setkeys(('d', 'e', 'a', 42, 'b',))") py.test.raises(KeyError, "r1.setkeys(('d', 42, 'a', 42, 'b',))") py.test.raises(ValueError, "r1.setkeys(('d', 'c', 'a', 42, 'b', 'a',))") py.test.raises(ValueError, "r1.setkeys(('g', 'c', 'a', 42,))")
def test0(): x = _ordereddict.ordereddict() x['abcd'] = 0 x['bcde'] = 1 x['cdef'] = 2 x['defg'] = 3 x['efgh'] = 4 x['fghi'] = 5 x['ghij'] = 6 x['hijk'] = 7 x['ijkl'] = 8 x['jklm'] = 9