MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_multiset_operations

Method test_multiset_operations

Lib/test/test_collections.py:2262–2299  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2260 self.assertIn(elem, c)
2261
2262 def test_multiset_operations(self):
2263 # Verify that adding a zero counter will strip zeros and negatives
2264 c = Counter(a=10, b=-2, c=0) + Counter()
2265 self.assertEqual(dict(c), dict(a=10))
2266
2267 elements = 'abcd'
2268 for i in range(1000):
2269 # test random pairs of multisets
2270 p = Counter(dict((elem, randrange(-2,4)) for elem in elements))
2271 p.update(e=1, f=-1, g=0)
2272 q = Counter(dict((elem, randrange(-2,4)) for elem in elements))
2273 q.update(h=1, i=-1, j=0)
2274 for counterop, numberop in [
2275 (Counter.__add__, lambda x, y: max(0, x+y)),
2276 (Counter.__sub__, lambda x, y: max(0, x-y)),
2277 (Counter.__or__, lambda x, y: max(0,x,y)),
2278 (Counter.__and__, lambda x, y: max(0, min(x,y))),
2279 ]:
2280 result = counterop(p, q)
2281 for x in elements:
2282 self.assertEqual(numberop(p[x], q[x]), result[x],
2283 (counterop, x, p, q))
2284 # verify that results exclude non-positive counts
2285 self.assertTrue(x>0 for x in result.values())
2286
2287 elements = 'abcdef'
2288 for i in range(100):
2289 # verify that random multisets with no repeats are exactly like sets
2290 p = Counter(dict((elem, randrange(0, 2)) for elem in elements))
2291 q = Counter(dict((elem, randrange(0, 2)) for elem in elements))
2292 for counterop, setop in [
2293 (Counter.__sub__, set.__sub__),
2294 (Counter.__or__, set.__or__),
2295 (Counter.__and__, set.__and__),
2296 ]:
2297 counter_result = counterop(p, q)
2298 set_result = setop(set(p.elements()), set(q.elements()))
2299 self.assertEqual(counter_result, dict.fromkeys(set_result, 1))
2300
2301 def test_inplace_operations(self):
2302 elements = 'abcd'

Callers

nothing calls this directly

Calls 10

updateMethod · 0.95
elementsMethod · 0.95
CounterClass · 0.90
maxFunction · 0.85
minFunction · 0.85
setFunction · 0.85
assertTrueMethod · 0.80
assertEqualMethod · 0.45
valuesMethod · 0.45
fromkeysMethod · 0.45

Tested by

no test coverage detected