(self)
| 2280 | self.assertIn(elem, c) |
| 2281 | |
| 2282 | def test_multiset_operations(self): |
| 2283 | # Verify that adding a zero counter will strip zeros and negatives |
| 2284 | c = Counter(a=10, b=-2, c=0) + Counter() |
| 2285 | self.assertEqual(dict(c), dict(a=10)) |
| 2286 | |
| 2287 | elements = 'abcd' |
| 2288 | for i in range(1000): |
| 2289 | # test random pairs of multisets |
| 2290 | p = Counter(dict((elem, randrange(-2,4)) for elem in elements)) |
| 2291 | p.update(e=1, f=-1, g=0) |
| 2292 | q = Counter(dict((elem, randrange(-2,4)) for elem in elements)) |
| 2293 | q.update(h=1, i=-1, j=0) |
| 2294 | for counterop, numberop in [ |
| 2295 | (Counter.__add__, lambda x, y: max(0, x+y)), |
| 2296 | (Counter.__sub__, lambda x, y: max(0, x-y)), |
| 2297 | (Counter.__or__, lambda x, y: max(0,x,y)), |
| 2298 | (Counter.__and__, lambda x, y: max(0, min(x,y))), |
| 2299 | (Counter.__xor__, lambda x, y: max(0, max(x,y) - min(x,y))), |
| 2300 | ]: |
| 2301 | result = counterop(p, q) |
| 2302 | for x in elements: |
| 2303 | self.assertEqual(numberop(p[x], q[x]), result[x], |
| 2304 | (counterop, x, p, q)) |
| 2305 | # verify that results exclude non-positive counts |
| 2306 | self.assertTrue(x>0 for x in result.values()) |
| 2307 | |
| 2308 | elements = 'abcdef' |
| 2309 | for i in range(100): |
| 2310 | # verify that random multisets with no repeats are exactly like sets |
| 2311 | p = Counter(dict((elem, randrange(0, 2)) for elem in elements)) |
| 2312 | q = Counter(dict((elem, randrange(0, 2)) for elem in elements)) |
| 2313 | for counterop, setop in [ |
| 2314 | (Counter.__sub__, set.__sub__), |
| 2315 | (Counter.__or__, set.__or__), |
| 2316 | (Counter.__and__, set.__and__), |
| 2317 | (Counter.__xor__, set.__xor__), |
| 2318 | ]: |
| 2319 | counter_result = counterop(p, q) |
| 2320 | set_result = setop(set(p.elements()), set(q.elements())) |
| 2321 | self.assertEqual(counter_result, dict.fromkeys(set_result, 1)) |
| 2322 | |
| 2323 | def test_inplace_operations(self): |
| 2324 | elements = 'abcd' |
nothing calls this directly
no test coverage detected