def test2Lists(self): """Test list/TList behavior and compatibility""" l = TList() l.Add(TObjString('a')) l.Add(TObjString('b')) l.Add(TObjString('c')) l.Add(TObjString('d')) l.Add(TObjString('e')) l.Add(TObjString('f')) l.Add(TObjString('g')) l.Add(TObjString('h')) l.Add(TObjString('i')) l.Add(TObjString('j')) self.assertEqual(len(l), 10) self.assertEqual(l[3], 'd') self.assertEqual(l[-1], 'j') self.assertRaises(IndexError, l.__getitem__, 20) self.assertRaises(IndexError, l.__getitem__, -20) self.assertEqual(list(l), ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']) l[3] = TObjString('z') self.assertEqual(list(l), ['a', 'b', 'c', 'z', 'e', 'f', 'g', 'h', 'i', 'j']) del l[2] self.assertEqual(list(l), ['a', 'b', 'z', 'e', 'f', 'g', 'h', 'i', 'j']) self.assert_(TObjString('b') in l) self.assert_(not TObjString('x') in l) self.assertEqual(list(l[2:6]), ['z', 'e', 'f', 'g']) self.assertEqual(list(l[2:6:2]), ['z', 'f']) self.assertEqual(list(l[-5:-2]), ['f', 'g', 'h']) self.assertEqual(list(l[7:]), ['i', 'j']) self.assertEqual(list(l[:3]), ['a', 'b', 'z']) del l[2:4] self.assertEqual(list(l), ['a', 'b', 'f', 'g', 'h', 'i', 'j']) l[2:5] = [TObjString('1'), TObjString('2')] self.assertEqual(list(l), ['a', 'b', '1', '2', 'i', 'j']) l[6:6] = [TObjString('3')] self.assertEqual(list(l), ['a', 'b', '1', '2', 'i', 'j', '3']) l.append(TObjString('4')) self.assertEqual(list(l), ['a', 'b', '1', '2', 'i', 'j', '3', '4']) l.extend([TObjString('5'), TObjString('j')]) self.assertEqual(list(l), ['a', 'b', '1', '2', 'i', 'j', '3', '4', '5', 'j']) self.assertEqual(l.count('b'), 1) self.assertEqual(l.count('j'), 2) self.assertEqual(l.count('x'), 0) self.assertEqual(l.index(TObjString('i')), 4) self.assertRaises(ValueError, l.index, TObjString('x')) l.insert(3, TObjString('6')) l.insert(20, TObjString('7')) l.insert(-1, TObjString('8')) if not self.legacy_pyroot: # The pythonisation of TSeqCollection in experimental PyROOT mimics the # behaviour of the Python list, in this case for insert. # The Python list insert always inserts before the specified index, so if # -1 is specified, insert will place the new element right before the last # element of the list. self.assertEqual(list(l), [ 'a', 'b', '1', '6', '2', 'i', 'j', '3', '4', '5', 'j', '8', '7' ]) # Re-synchronize with current PyROOT's list l.insert(0, TObjString('8')) self.assertEqual(list(l), [ '8', 'a', 'b', '1', '6', '2', 'i', 'j', '3', '4', '5', 'j', '8', '7' ]) l.pop(-2) self.assertEqual(list(l), [ '8', 'a', 'b', '1', '6', '2', 'i', 'j', '3', '4', '5', 'j', '7' ]) else: self.assertEqual(list(l), [ '8', 'a', 'b', '1', '6', '2', 'i', 'j', '3', '4', '5', 'j', '7' ]) self.assertEqual(l.pop(), '7') self.assertEqual(l.pop(3), '1') self.assertEqual( list(l), ['8', 'a', 'b', '6', '2', 'i', 'j', '3', '4', '5', 'j']) l.remove(TObjString('j')) l.remove(TObjString('3')) self.assertRaises(ValueError, l.remove, TObjString('x')) self.assertEqual(list(l), ['8', 'a', 'b', '6', '2', 'i', '4', '5', 'j']) l.reverse() self.assertEqual(list(l), ['j', '5', '4', 'i', '2', '6', 'b', 'a', '8']) l.sort() self.assertEqual(list(l), ['2', '4', '5', '6', '8', 'a', 'b', 'i', 'j']) if sys.hexversion >= 0x3000000: l.sort(key=TObjString.GetName) l.reverse() else: l.sort(lambda a, b: cmp(b.GetName(), a.GetName())) self.assertEqual(list(l), ['j', 'i', 'b', 'a', '8', '6', '5', '4', '2']) l2 = l[:3] self.assertEqual(list(l2 * 3), ['j', 'i', 'b', 'j', 'i', 'b', 'j', 'i', 'b']) self.assertEqual(list(3 * l2), ['j', 'i', 'b', 'j', 'i', 'b', 'j', 'i', 'b']) l2 *= 3 self.assertEqual(list(l2), ['j', 'i', 'b', 'j', 'i', 'b', 'j', 'i', 'b']) l2 = l[:3] l3 = l[6:8] self.assertEqual(list(l2 + l3), ['j', 'i', 'b', '5', '4']) if sys.hexversion >= 0x3000000: next = '__next__' else: next = 'next' i = iter(l2) self.assertEqual(getattr(i, next)(), 'j') self.assertEqual(getattr(i, next)(), 'i') self.assertEqual(getattr(i, next)(), 'b') self.assertRaises(StopIteration, getattr(i, next))
def test2Lists( self ): """Test list/TList behavior and compatibility""" l = TList() l.Add( TObjString('a') ) l.Add( TObjString('b') ) l.Add( TObjString('c') ) l.Add( TObjString('d') ) l.Add( TObjString('e') ) l.Add( TObjString('f') ) l.Add( TObjString('g') ) l.Add( TObjString('h') ) l.Add( TObjString('i') ) l.Add( TObjString('j') ) self.assertEqual( len(l), 10 ) self.assertEqual( l[3], 'd' ) self.assertEqual( l[-1], 'j' ) self.assertRaises( IndexError, l.__getitem__, 20 ) self.assertRaises( IndexError, l.__getitem__, -20 ) self.assertEqual( list(l), ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] ) l[3] = TObjString('z') self.assertEqual( list(l), ['a', 'b', 'c', 'z', 'e', 'f', 'g', 'h', 'i', 'j'] ) del l[2] self.assertEqual( list(l), ['a', 'b', 'z', 'e', 'f', 'g', 'h', 'i', 'j'] ) self.assert_( TObjString('b') in l ) self.assert_( not TObjString('x') in l ) self.assertEqual( list(l[2:6]), ['z', 'e', 'f', 'g'] ) self.assertEqual( list(l[2:6:2]), ['z', 'f'] ) self.assertEqual( list(l[-5:-2]), ['f', 'g', 'h'] ) self.assertEqual( list(l[7:]), ['i', 'j'] ) self.assertEqual( list(l[:3]), ['a', 'b', 'z'] ) del l[2:4] self.assertEqual( list(l), ['a', 'b', 'f', 'g', 'h', 'i', 'j'] ) l[2:5] = [ TObjString('1'), TObjString('2') ] self.assertEqual( list(l), ['a', 'b', '1', '2', 'i', 'j'] ) l[6:6] = [ TObjString('3') ] self.assertEqual( list(l), ['a', 'b', '1', '2', 'i', 'j', '3'] ) l.append( TObjString('4') ) self.assertEqual( list(l), ['a', 'b', '1', '2', 'i', 'j', '3', '4'] ) l.extend( [ TObjString('5'), TObjString('j') ] ) self.assertEqual( list(l), ['a', 'b', '1', '2', 'i', 'j', '3', '4', '5', 'j'] ) self.assertEqual( l.count( 'b' ), 1 ) self.assertEqual( l.count( 'j' ), 2 ) self.assertEqual( l.count( 'x' ), 0 ) self.assertEqual( l.index( TObjString( 'i' ) ), 4 ) self.assertRaises( ValueError, l.index, TObjString( 'x' ) ) l.insert( 3, TObjString('6') ) l.insert( 20, TObjString('7') ) l.insert( -1, TObjString('8') ) self.assertEqual( list(l), ['8', 'a', 'b', '1', '6', '2', 'i', 'j', '3', '4', '5', 'j', '7'] ) self.assertEqual( l.pop(), '7' ) self.assertEqual( l.pop(3), '1' ) self.assertEqual( list(l), ['8', 'a', 'b', '6', '2', 'i', 'j', '3', '4', '5', 'j'] ) l.remove( TObjString( 'j' ) ) l.remove( TObjString( '3' ) ) self.assertRaises( ValueError, l.remove, TObjString( 'x' ) ) self.assertEqual( list(l), ['8', 'a', 'b', '6', '2', 'i', '4', '5', 'j'] ) l.reverse() self.assertEqual( list(l), ['j', '5', '4', 'i', '2', '6', 'b', 'a', '8'] ) l.sort() self.assertEqual( list(l), ['2', '4', '5', '6', '8', 'a', 'b', 'i', 'j'] ) if sys.hexversion >= 0x3000000: l.sort( key=TObjString.GetName ) l.reverse() else: l.sort( lambda a, b: cmp(b.GetName(),a.GetName()) ) self.assertEqual( list(l), ['j', 'i', 'b', 'a', '8', '6', '5', '4', '2'] ) l2 = l[:3] self.assertEqual( list(l2 * 3), ['j', 'i', 'b', 'j', 'i', 'b', 'j', 'i', 'b'] ) self.assertEqual( list(3 * l2), ['j', 'i', 'b', 'j', 'i', 'b', 'j', 'i', 'b'] ) l2 *= 3 self.assertEqual( list(l2), ['j', 'i', 'b', 'j', 'i', 'b', 'j', 'i', 'b'] ) l2 = l[:3] l3 = l[6:8] self.assertEqual( list(l2+l3), ['j', 'i', 'b', '5', '4'] ) i = iter(l2) self.assertEqual( i.next(), 'j' ) self.assertEqual( i.next(), 'i' ) self.assertEqual( i.next(), 'b' ) self.assertRaises( StopIteration, i.next )
def test2Lists(self): """Test list/TList behavior and compatibility""" l = TList() l.Add(TObjString('a')) l.Add(TObjString('b')) l.Add(TObjString('c')) l.Add(TObjString('d')) l.Add(TObjString('e')) l.Add(TObjString('f')) l.Add(TObjString('g')) l.Add(TObjString('h')) l.Add(TObjString('i')) l.Add(TObjString('j')) self.assertEqual(len(l), 10) self.assertEqual(l[3], 'd') self.assertEqual(l[-1], 'j') self.assertRaises(IndexError, l.__getitem__, 20) self.assertRaises(IndexError, l.__getitem__, -20) self.assertEqual(list(l), ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']) l[3] = TObjString('z') self.assertEqual(list(l), ['a', 'b', 'c', 'z', 'e', 'f', 'g', 'h', 'i', 'j']) del l[2] self.assertEqual(list(l), ['a', 'b', 'z', 'e', 'f', 'g', 'h', 'i', 'j']) self.assert_(TObjString('b') in l) self.assert_(not TObjString('x') in l) self.assertEqual(list(l[2:6]), ['z', 'e', 'f', 'g']) self.assertEqual(list(l[2:6:2]), ['z', 'f']) self.assertEqual(list(l[-5:-2]), ['f', 'g', 'h']) self.assertEqual(list(l[7:]), ['i', 'j']) self.assertEqual(list(l[:3]), ['a', 'b', 'z']) del l[2:4] self.assertEqual(list(l), ['a', 'b', 'f', 'g', 'h', 'i', 'j']) l[2:5] = [TObjString('1'), TObjString('2')] self.assertEqual(list(l), ['a', 'b', '1', '2', 'i', 'j']) l[6:6] = [TObjString('3')] self.assertEqual(list(l), ['a', 'b', '1', '2', 'i', 'j', '3']) l.append(TObjString('4')) self.assertEqual(list(l), ['a', 'b', '1', '2', 'i', 'j', '3', '4']) l.extend([TObjString('5'), TObjString('j')]) self.assertEqual(list(l), ['a', 'b', '1', '2', 'i', 'j', '3', '4', '5', 'j']) self.assertEqual(l.count('b'), 1) self.assertEqual(l.count('j'), 2) self.assertEqual(l.count('x'), 0) self.assertEqual(l.index(TObjString('i')), 4) self.assertRaises(ValueError, l.index, TObjString('x')) l.insert(3, TObjString('6')) l.insert(20, TObjString('7')) l.insert(-1, TObjString('8')) self.assertEqual( list(l), ['8', 'a', 'b', '1', '6', '2', 'i', 'j', '3', '4', '5', 'j', '7']) self.assertEqual(l.pop(), '7') self.assertEqual(l.pop(3), '1') self.assertEqual( list(l), ['8', 'a', 'b', '6', '2', 'i', 'j', '3', '4', '5', 'j']) l.remove(TObjString('j')) l.remove(TObjString('3')) self.assertRaises(ValueError, l.remove, TObjString('x')) self.assertEqual(list(l), ['8', 'a', 'b', '6', '2', 'i', '4', '5', 'j']) l.reverse() self.assertEqual(list(l), ['j', '5', '4', 'i', '2', '6', 'b', 'a', '8']) l.sort() self.assertEqual(list(l), ['2', '4', '5', '6', '8', 'a', 'b', 'i', 'j']) if sys.hexversion >= 0x3000000: l.sort(key=TObjString.GetName) l.reverse() else: l.sort(lambda a, b: cmp(b.GetName(), a.GetName())) self.assertEqual(list(l), ['j', 'i', 'b', 'a', '8', '6', '5', '4', '2']) l2 = l[:3] self.assertEqual(list(l2 * 3), ['j', 'i', 'b', 'j', 'i', 'b', 'j', 'i', 'b']) self.assertEqual(list(3 * l2), ['j', 'i', 'b', 'j', 'i', 'b', 'j', 'i', 'b']) l2 *= 3 self.assertEqual(list(l2), ['j', 'i', 'b', 'j', 'i', 'b', 'j', 'i', 'b']) l2 = l[:3] l3 = l[6:8] self.assertEqual(list(l2 + l3), ['j', 'i', 'b', '5', '4']) if sys.hexversion >= 0x3000000: next = '__next__' else: next = 'next' i = iter(l2) self.assertEqual(getattr(i, next)(), 'j') self.assertEqual(getattr(i, next)(), 'i') self.assertEqual(getattr(i, next)(), 'b') self.assertRaises(StopIteration, getattr(i, next))