Changeset 3f2f7df in trunk
- Timestamp:
- 2017-08-10T17:27:02Z (8 years ago)
- Branches:
- master
- Children:
- aaf167b, d91516a5
- Parents:
- 95ac5494
- git-author:
- Brian Warner <warner@…> (2017-08-10 01:53:29)
- git-committer:
- Brian Warner <warner@…> (2017-08-10 17:27:02)
- Location:
- src/allmydata
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified src/allmydata/test/test_util.py ¶
r95ac5494 r3f2f7df 2 2 def foo(): pass # keep the line number constant 3 3 4 import os, time, sys, yaml 4 import os, time, sys, itertools, random 5 import yaml 5 6 from StringIO import StringIO 6 7 from datetime import timedelta … … 1465 1466 self.failUnlessEqual(d3, {1: "a"}) 1466 1467 1467 d1 = {1: "a", 2: "b" }1468 d2 = {2: "c" }1468 d1 = {1: "a", 2: "b", 3: "c"} 1469 d2 = {2: "c", 4: "d"} 1469 1470 d3 = dictutil.subtract(d1, d2) 1470 self.failUnlessEqual(d3, {1: "a" })1471 self.failUnlessEqual(d3, {1: "a", 3: "c"}) 1471 1472 1472 1473 def test_utildict(self): … … 1557 1558 self.failUnlessEqual(d.item_with_largest_value(), ("b", 6)) 1558 1559 1560 # to get full coverage of item_with_largest_value(), we need to 1561 # exercise two situations: the first value (in iteritems() order) is 1562 # larger than the second, and vice versa. Since iteration is not 1563 # deterministic, we need to try a bunch of random dictionaries to 1564 # exercise this 1565 r = random.Random(0) # consistent seed 1566 count = itertools.count() 1567 found = set() 1568 while count.next() < 1000: 1569 a = r.randrange(100) 1570 b = r.randrange(100) 1571 larger = ("a",a) if a > b else ("b",b) 1572 if a == b: 1573 continue 1574 d0 = dictutil.NumDict() 1575 d0.add_num("a", a) 1576 d0.add_num("b", b) 1577 self.failUnlessEqual(d0, {"a": a, "b": b}) 1578 items = list(d0.d.iteritems()) 1579 if items[0][1] > items[1][1]: 1580 found.add("first-larger") 1581 else: 1582 found.add("first-smaller") 1583 self.failUnlessEqual(d0.item_with_largest_value(), larger) 1584 if found == set(["first-larger", "first-smaller"]): 1585 break 1586 else: 1587 self.fail("unable to exercise all cases of item_with_largest_value") 1588 1559 1589 d = dictutil.NumDict({"a": 1, "b": 2}) 1560 1590 self.failUnlessIn(repr(d), ("{'a': 1, 'b': 2}", … … 1628 1658 self.failUnlessEqual(d.keys(), ["c", "b", "a"]) 1629 1659 self.failUnlessEqual(repr(d), "<ValueOrderedDict {c: 1, b: 2, a: 3}>") 1660 self.failUnlessEqual(str(d), "<ValueOrderedDict {c: 1, b: 2, a: 3}>") 1661 # str() is supposed to only show the first 16 entries 1662 large_d = dictutil.ValueOrderedDict() 1663 for i in range(20): 1664 large_d["k%d" % i] = i 1665 large_d_repr = ("<ValueOrderedDict {%s, ...}>" % 1666 ", ".join(["k%d: %d" % (i, i) for i in range(16)])) 1667 self.failUnlessEqual(str(large_d), large_d_repr) 1668 1630 1669 def eq(a, b): 1631 1670 return a == b -
TabularUnified src/allmydata/util/dictutil.py ¶
r95ac5494 r3f2f7df 437 437 i = 1 438 438 while (n is None) or (i < n): 439 i += 1 439 440 x = iter.next() 440 s.append(", "); s.append(str(x[0])); s.append(": "); s.append(str(x[1])) 441 s.append(", "); 442 s.append(str(x[0])); s.append(": "); s.append(str(x[1])) 443 # if we get here, we're truncating the repr, so make that clear 444 s.append(", ...") 441 445 except StopIteration: 442 446 pass
Note: See TracChangeset
for help on using the changeset viewer.