Changeset 3f2f7df in trunk


Ignore:
Timestamp:
2017-08-10T17:27:02Z (8 years ago)
Author:
Brian Warner <warner@…>
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)
Message:

dictutil: fix bug in str(ValueOrderedDict?), and improve test coverage

It looks like str() was meant to truncate the dict, but a missing i+=1 meant
that it never actually did. I also changed the format to include a clear
"..." in case we truncate it, to avoid confusion with a non-truncated dict of
the same size.

This also improves test coverage in subtract() and
NumDict?.item_with_largest_value().

refs ticket:2891

Location:
src/allmydata
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/allmydata/test/test_util.py

    r95ac5494 r3f2f7df  
    22def foo(): pass # keep the line number constant
    33
    4 import os, time, sys, yaml
     4import os, time, sys, itertools, random
     5import yaml
    56from StringIO import StringIO
    67from datetime import timedelta
     
    14651466        self.failUnlessEqual(d3, {1: "a"})
    14661467
    1467         d1 = {1: "a", 2: "b"}
    1468         d2 = {2: "c"}
     1468        d1 = {1: "a", 2: "b", 3: "c"}
     1469        d2 = {2: "c", 4: "d"}
    14691470        d3 = dictutil.subtract(d1, d2)
    1470         self.failUnlessEqual(d3, {1: "a"})
     1471        self.failUnlessEqual(d3, {1: "a", 3: "c"})
    14711472
    14721473    def test_utildict(self):
     
    15571558        self.failUnlessEqual(d.item_with_largest_value(), ("b", 6))
    15581559
     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
    15591589        d = dictutil.NumDict({"a": 1, "b": 2})
    15601590        self.failUnlessIn(repr(d), ("{'a': 1, 'b': 2}",
     
    16281658        self.failUnlessEqual(d.keys(), ["c", "b", "a"])
    16291659        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
    16301669        def eq(a, b):
    16311670            return a == b
  • TabularUnified src/allmydata/util/dictutil.py

    r95ac5494 r3f2f7df  
    437437            i = 1
    438438            while (n is None) or (i < n):
     439                i += 1
    439440                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(", ...")
    441445        except StopIteration:
    442446            pass
Note: See TracChangeset for help on using the changeset viewer.