def test_basics(self):
     c = Counter("abcaba")
     self.assertEqual(c, Counter({"a": 3, "b": 2, "c": 1}))
     self.assertEqual(c, Counter(a=3, b=2, c=1))
     self.assertIsInstance(c, dict)
     self.assertIsInstance(c, Mapping)
     self.assertTrue(issubclass(Counter, dict))
     self.assertTrue(issubclass(Counter, Mapping))
     self.assertEqual(len(c), 3)
     self.assertEqual(sum(c.values()), 6)
     self.assertEqual(sorted(c.values()), [1, 2, 3])
     self.assertEqual(sorted(c.keys()), ["a", "b", "c"])
     self.assertEqual(sorted(c), ["a", "b", "c"])
     self.assertEqual(sorted(c.items()), [("a", 3), ("b", 2), ("c", 1)])
     self.assertEqual(c["b"], 2)
     self.assertEqual(c["z"], 0)
     with test_support.check_py3k_warnings():
         self.assertEqual(c.has_key("c"), True)
         self.assertEqual(c.has_key("z"), False)
     self.assertEqual(c.__contains__("c"), True)
     self.assertEqual(c.__contains__("z"), False)
     self.assertEqual(c.get("b", 10), 2)
     self.assertEqual(c.get("z", 10), 10)
     self.assertEqual(c, dict(a=3, b=2, c=1))
     self.assertEqual(repr(c), "Counter({'a': 3, 'b': 2, 'c': 1})")
     self.assertEqual(c.most_common(), [("a", 3), ("b", 2), ("c", 1)])
     for i in range(5):
         self.assertEqual(c.most_common(i), [("a", 3), ("b", 2), ("c", 1)][:i])
     self.assertEqual("".join(sorted(c.elements())), "aaabbc")
     c["a"] += 1  # increment an existing value
     c["b"] -= 2  # sub existing value to zero
     del c["c"]  # remove an entry
     del c["c"]  # make sure that del doesn't raise KeyError
     c["d"] -= 2  # sub from a missing value
     c["e"] = -5  # directly assign a missing value
     c["f"] += 4  # add to a missing value
     self.assertEqual(c, dict(a=4, b=0, d=-2, e=-5, f=4))
     self.assertEqual("".join(sorted(c.elements())), "aaaaffff")
     self.assertEqual(c.pop("f"), 4)
     self.assertNotIn("f", c)
     for i in range(3):
         elem, cnt = c.popitem()
         self.assertNotIn(elem, c)
     c.clear()
     self.assertEqual(c, {})
     self.assertEqual(repr(c), "Counter()")
     self.assertRaises(NotImplementedError, Counter.fromkeys, "abc")
     self.assertRaises(TypeError, hash, c)
     c.update(dict(a=5, b=3))
     c.update(c=1)
     c.update(Counter("a" * 50 + "b" * 30))
     c.update()  # test case with no args
     c.__init__("a" * 500 + "b" * 300)
     c.__init__("cdc")
     c.__init__()
     self.assertEqual(c, dict(a=555, b=333, c=3, d=1))
     self.assertEqual(c.setdefault("d", 5), 1)
     self.assertEqual(c["d"], 1)
     self.assertEqual(c.setdefault("e", 5), 5)
     self.assertEqual(c["e"], 5)
Пример #2
0
    def test_read(self):
        # Test for read only operations on mapping
        p = self._empty_mapping()
        p1 = dict(p)  # workaround for singleton objects
        d = self._full_mapping(self.reference)
        if d is p:
            p = p1
        # Indexing
        for key, value in self.reference.items():
            self.assertEqual(d[key], value)
        knownkey = self.other.keys()[0]
        self.assertRaises(KeyError, lambda: d[knownkey])
        # len
        self.assertEqual(len(p), 0)
        self.assertEqual(len(d), len(self.reference))
        # in
        for k in self.reference:
            self.assertIn(k, d)
        for k in self.other:
            self.assertNotIn(k, d)
        # has_key
        with test_support.check_py3k_warnings(quiet=True):
            for k in self.reference:
                self.assertTrue(d.has_key(k))
            for k in self.other:
                self.assertFalse(d.has_key(k))
        # cmp
        self.assertEqual(cmp(p, p), 0)
        self.assertEqual(cmp(d, d), 0)
        self.assertEqual(cmp(p, d), -1)
        self.assertEqual(cmp(d, p), 1)
        # __non__zero__
        if p:
            self.fail("Empty mapping must compare to False")
        if not d:
            self.fail("Full mapping must compare to True")
        # keys(), items(), iterkeys() ...
        def check_iterandlist(iter, lst, ref):
            self.assertTrue(hasattr(iter, "next"))
            self.assertTrue(hasattr(iter, "__iter__"))
            x = list(iter)
            self.assertTrue(set(x) == set(lst) == set(ref))

        check_iterandlist(d.iterkeys(), d.keys(), self.reference.keys())
        check_iterandlist(iter(d), d.keys(), self.reference.keys())
        check_iterandlist(d.itervalues(), d.values(), self.reference.values())
        check_iterandlist(d.iteritems(), d.items(), self.reference.items())
        # get
        key, value = d.iteritems().next()
        knownkey, knownvalue = self.other.iteritems().next()
        self.assertEqual(d.get(key, knownvalue), value)
        self.assertEqual(d.get(knownkey, knownvalue), knownvalue)
        self.assertNotIn(knownkey, d)
Пример #3
0
    def test_read(self):
        # Test for read only operations on mapping
        p = self._empty_mapping()
        p1 = dict(p)  #workaround for singleton objects
        d = self._full_mapping(self.reference)
        if d is p:
            p = p1
        #Indexing
        for key, value in self.reference.items():
            self.assertEqual(d[key], value)
        knownkey = self.other.keys()[0]
        self.assertRaises(KeyError, lambda: d[knownkey])
        #len
        self.assertEqual(len(p), 0)
        self.assertEqual(len(d), len(self.reference))
        #in
        for k in self.reference:
            self.assertIn(k, d)
        for k in self.other:
            self.assertNotIn(k, d)
        #has_key
        with test_support.check_py3k_warnings(quiet=True):
            for k in self.reference:
                self.assertTrue(d.has_key(k))
            for k in self.other:
                self.assertFalse(d.has_key(k))
        #cmp
        self.assertEqual(cmp(p, p), 0)
        self.assertEqual(cmp(d, d), 0)
        # Pyston change: we don't handle dictionary comparisons correctly yet:
        # self.assertEqual(cmp(p,d), -1)
        # self.assertEqual(cmp(d,p), 1)
        #__non__zero__
        if p: self.fail("Empty mapping must compare to False")
        if not d: self.fail("Full mapping must compare to True")

        # keys(), items(), iterkeys() ...
        def check_iterandlist(iter, lst, ref):
            self.assertTrue(hasattr(iter, 'next'))
            self.assertTrue(hasattr(iter, '__iter__'))
            x = list(iter)
            self.assertTrue(set(x) == set(lst) == set(ref))

        check_iterandlist(d.iterkeys(), d.keys(), self.reference.keys())
        check_iterandlist(iter(d), d.keys(), self.reference.keys())
        check_iterandlist(d.itervalues(), d.values(), self.reference.values())
        check_iterandlist(d.iteritems(), d.items(), self.reference.items())
        #get
        key, value = d.iteritems().next()
        knownkey, knownvalue = self.other.iteritems().next()
        self.assertEqual(d.get(key, knownvalue), value)
        self.assertEqual(d.get(knownkey, knownvalue), knownvalue)
        self.assertNotIn(knownkey, d)
Пример #4
0
 def test_basics(self):
     c = Counter('abcaba')
     self.assertEqual(c, Counter({'a': 3, 'b': 2, 'c': 1}))
     self.assertEqual(c, Counter(a=3, b=2, c=1))
     self.assertIsInstance(c, dict)
     self.assertIsInstance(c, Mapping)
     self.assertTrue(issubclass(Counter, dict))
     self.assertTrue(issubclass(Counter, Mapping))
     self.assertEqual(len(c), 3)
     self.assertEqual(sum(c.values()), 6)
     self.assertEqual(sorted(c.values()), [1, 2, 3])
     self.assertEqual(sorted(c.keys()), ['a', 'b', 'c'])
     self.assertEqual(sorted(c), ['a', 'b', 'c'])
     self.assertEqual(sorted(c.items()), [('a', 3), ('b', 2), ('c', 1)])
     self.assertEqual(c['b'], 2)
     self.assertEqual(c['z'], 0)
     with test_support.check_py3k_warnings():
         self.assertEqual(c.has_key('c'), True)
         self.assertEqual(c.has_key('z'), False)
     self.assertEqual(c.__contains__('c'), True)
     self.assertEqual(c.__contains__('z'), False)
     self.assertEqual(c.get('b', 10), 2)
     self.assertEqual(c.get('z', 10), 10)
     self.assertEqual(c, dict(a=3, b=2, c=1))
     self.assertEqual(repr(c), "Counter({'a': 3, 'b': 2, 'c': 1})")
     self.assertEqual(c.most_common(), [('a', 3), ('b', 2), ('c', 1)])
     for i in range(5):
         self.assertEqual(c.most_common(i), [('a', 3), ('b', 2),
                                             ('c', 1)][:i])
     self.assertEqual(''.join(sorted(c.elements())), 'aaabbc')
     c['a'] += 1  # increment an existing value
     c['b'] -= 2  # sub existing value to zero
     del c['c']  # remove an entry
     del c['c']  # make sure that del doesn't raise KeyError
     c['d'] -= 2  # sub from a missing value
     c['e'] = -5  # directly assign a missing value
     c['f'] += 4  # add to a missing value
     self.assertEqual(c, dict(a=4, b=0, d=-2, e=-5, f=4))
     self.assertEqual(''.join(sorted(c.elements())), 'aaaaffff')
     self.assertEqual(c.pop('f'), 4)
     self.assertNotIn('f', c)
     for i in range(3):
         elem, cnt = c.popitem()
         self.assertNotIn(elem, c)
     c.clear()
     self.assertEqual(c, {})
     self.assertEqual(repr(c), 'Counter()')
     self.assertRaises(NotImplementedError, Counter.fromkeys, 'abc')
     self.assertRaises(TypeError, hash, c)
     c.update(dict(a=5, b=3))
     c.update(c=1)
     c.update(Counter('a' * 50 + 'b' * 30))
     c.update()  # test case with no args
     c.__init__('a' * 500 + 'b' * 300)
     c.__init__('cdc')
     c.__init__()
     self.assertEqual(c, dict(a=555, b=333, c=3, d=1))
     self.assertEqual(c.setdefault('d', 5), 1)
     self.assertEqual(c['d'], 1)
     self.assertEqual(c.setdefault('e', 5), 5)
     self.assertEqual(c['e'], 5)