def update(self): c = Counter() c.update('aababc') Assert(c) == Counter('aababc') c.update({'b': 1}) Assert(c['b']) == 3 c.update(c=2) Assert(c['c']) == 3
def most_common(self): c = Counter('aababc') result = [('a', 3), ('b', 2), ('c', 1)] Assert(c.most_common()) == result Assert(c.most_common(2)) == result[:-1] Assert(c.most_common(1)) == result[:-2] Assert(c.most_common(0)) == []
def __init__(self, mapping=(), maxsize=float('inf')): dict.__init__(self, mapping) self.maxsize = maxsize self.usage_counter = Counter()
def or_and(self): c1 = Counter('abc') new = c1 | c1 * 2 Assert(new.values()) == [2] * 3 new = c1 & c1 * 2 Assert(new.values()) == [1] * 3
def sub(self): c = Counter('aababc') assert not c - c
def mul(self): c = Counter('abc') Assert(c * 2) == c + c
def add(self): c = Counter('aababc') new = c + c Assert(new['a']) == 6 Assert(new['b']) == 4 Assert(new['c']) == 2
def elements(self): c = Counter('aababc') for element in c: Assert(list(c.elements()).count(element)) == c[element]
def setdefault(self): c = Counter('a') Assert(c.setdefault('a', 2)) == 1 Assert(c['a']) == 1 Assert(c.setdefault('b')) == 1 Assert(c['b']) == 1
def get(self): c = Counter('a') Assert(c.get('a')) == 1 Assert(c.get('b')) == 0
def missing(self): c = Counter() Assert(c['a']) == 0