예제 #1
0
    def test_bigint(self):
        l1 = range(1 << 100, (1 << 100) + 2)
        s = IntSet(l1)
        self.assertTrue(list(s) == list(l1))

        l1 = IntSet([1 << x for x in range(50)])
        self.assertTrue(len(l1) == 50)
예제 #2
0
def test_builder_insert_intervals_equivalent_to_successive_union(intervals):
    builder = IntSet.Builder()
    equiv = IntSet.empty()
    for ij in intervals:
        equiv |= IntSet.interval(*ij)
        builder.insert_interval(*ij)
    assert builder.build() == equiv
예제 #3
0
 def test_add(self):
     l1 = random.sample(range(10000), 2000)
     s = IntSet()
     for x in l1:
         s.add(x)
     self.assertTrue(list(s) == sorted(l1))
     self.assertRaises(TypeError, s.add, "1000000")
예제 #4
0
def test_builder_insert_intervals_equivalent_to_successive_union(intervals):
    builder = IntSet.Builder()
    equiv = IntSet.empty()
    for ij in intervals:
        equiv |= IntSet.interval(*ij)
        builder.insert_interval(*ij)
    assert builder.build() == equiv
예제 #5
0
    def test_xor(self):
        l1 = random.sample(range(10000), 2000)
        l2 = random.sample(range(10000), 2000)

        s1 = IntSet(l1)
        s2 = IntSet(l2)
        self.assertTrue(list(s1 ^ s2) == sorted(list(set(l1) ^ set(l2))))
        self.assertRaises(TypeError, lambda: s1 ^ [])
예제 #6
0
def test_sequentially_removing_intervals_yields_empty(ls):
    running = IntSet.from_intervals(ls)
    for i in ls:
        inter = IntSet.interval(*i)
        extra = inter & running
        original = running.size()
        assert (running - inter) == (running - extra)
        running -= inter
        assert running.size() == original - extra.size()
    assert running.size() == 0
예제 #7
0
    def test_remove(self):
        l1 = random.sample(range(10000), 2000)
        l2 = random.sample(l1, 300)
        s1 = IntSet(l1)
        s2 = set(l1)

        for x in l2:
            s1.remove(x)
            s2.remove(x)
        self.assertTrue(list(s1), sorted(s2))
        self.assertRaises(KeyError, s1.remove, 10001)
        self.assertRaises(TypeError, s1.remove, "1")
예제 #8
0
def test_deepcopy_collapses_reference_equality():
    x = IntSet.from_iterable([1, 2, 3])
    y = IntSet.from_iterable([1, 2, 3])
    z = IntSet.from_iterable([1, 2, 3, 4])
    assert x == y
    assert x is not y
    w, u, v = deepcopy((x, y, z))
    assert x == w
    assert y == u
    assert z == v
    assert w is u
    assert w is not v
예제 #9
0
    def test_union(self):
        l1 = random.sample(range(10000), 2000)
        l2 = random.sample(range(10000), 2000)

        s1 = IntSet(l1)
        s2 = set(l1)

        self.assertTrue(
            list(s1.union(IntSet(l2))) == sorted(list(s2.union(l2))))
        self.assertTrue(list(s1.union(l2)) == sorted(list(s2.union(l2))))
        self.assertRaises(TypeError, s1.union, 1)
        self.assertRaises(TypeError, s1.union, ["1"])
예제 #10
0
    def test_difference(self):
        l1 = random.sample(range(10000), 2000)
        l2 = random.sample(range(10000), 2000)

        s1 = IntSet(l1)
        s2 = set(l1)

        self.assertTrue(
            list(s1.difference(IntSet(l2))) == sorted(list(s2.difference(l2))))
        self.assertTrue(
            list(s1.difference(l2)) == sorted(list(s2.difference(l2))))
        self.assertRaises(TypeError, s1.difference, 1)
        self.assertRaises(TypeError, s1.difference, ["1"])
예제 #11
0
def test_is_equal_to_random_insertion(imp, rnd):
    items = list(imp)
    rnd.shuffle(items)
    equiv = IntSet.empty()
    for i in items:
        equiv = equiv.insert(i)
    assert imp == equiv
예제 #12
0
def test_insert_into_interval(bounds, ints):
    imp = IntSet.interval(*bounds)
    for i in ints:
        imp = imp.insert(i)
        assert i in imp
    for i in ints:
        assert i in imp
예제 #13
0
 def test_contains(self):
     l1 = random.sample(range(10000), 2000)
     l2 = random.sample(l1, 100)
     s = IntSet(l1)
     for x in l2:
         self.assertTrue(x in s)
     self.assertRaises(TypeError, lambda: "1" in s)
예제 #14
0
def test_validates_bounds():
    with pytest.raises(ValueError):
        IntSet.single(-1)
    with pytest.raises(ValueError):
        IntSet.single(2 ** 64)
    with pytest.raises(ValueError):
        IntSet.interval(1, 2 ** 65)
    with pytest.raises(ValueError):
        IntSet.interval(2 ** 65, 1)
예제 #15
0
 def test_issuperset(self):
     l1 = random.sample(range(10000), 2000)
     l2 = random.sample(l1, 1000)
     s1 = IntSet(l1)
     s2 = IntSet(l2)
     self.assertTrue(s1.issuperset(s2))
     s2.add(100001)
     self.assertFalse(s1.issuperset(s2))
     self.assertRaises(TypeError, s1.issuperset, "123")
예제 #16
0
 def test_init(self):
     data = random.sample(range(10000), 2000)
     self.assertTrue(list(IntSet(data)) == sorted(data))
     self.assertTrue(list(IntSet(IntSet(data))) == sorted(data))
     self.assertTrue(list(IntSet()) == [])
     self.assertTrue(list(IntSet([])) == [])
     self.assertTrue(list(IntSet(set(range(10)))) == list(range(10)))
     self.assertTrue(list(IntSet(range(10))) == list(range(10)))
     self.assertTrue(list(IntSet(tuple(range(10)))) == list(range(10)))
     self.assertTrue(list(IntSet({1: 1})) == [1])
     self.assertRaises(TypeError, IntSet, 1)
     self.assertRaises(TypeError, IntSet, object())
     self.assertRaises(TypeError, IntSet, ["1"])
예제 #17
0
    def test_cmp(self):
        l1 = random.sample(range(10000), 20)
        s1 = IntSet(l1)
        s2 = IntSet(l1)

        self.assertEqual(s1, s2)
        self.assertTrue(s1 >= s2)
        self.assertTrue(s1 <= s2)

        s2.add(10001)

        self.assertNotEqual(s1, s2)
        self.assertTrue(s2 > s1)
        self.assertTrue(s1 < s2)
        self.assertTrue(s2 >= s1)
        self.assertTrue(s1 <= s2)

        self.assertRaises(TypeError, lambda: s1 <= "1")
예제 #18
0
 def result():
     x = {}
     for i in range(sys.maxunicode + 1):
         category = unicodedata.category(unichr(i))
         if category not in x:
             x[category] = IntSet.Builder()
         x[category].insert(i)
     for k, v in list(x.items()):
         x[k] = v.build()
     return x
예제 #19
0
    def test_intersection_update(self):
        l1 = random.sample(range(10000), 2000)
        l2 = random.sample(range(10000), 2000)

        s1 = IntSet(l1)
        s2 = set(l1)
        s1.intersection_update(l2)
        s2.intersection_update(l2)
        self.assertTrue(list(s1) == sorted(list(s2)))

        s1 = IntSet(l1)
        s1.intersection_update(IntSet(l2))
        self.assertTrue(list(s1) == sorted(list(s2)))

        self.assertRaises(TypeError, s1.intersection_update, 1)
        self.assertRaises(TypeError, s1.intersection_update, ["1"])
class TestIntSet(unittest.TestCase):
    def setUp(self):
        random.seed(28)
        self.set0 = IntSet(0)  # empty
        self.set5 = IntSet(5)
        self.set4 = IntSet(4)

    def testRandomSeed(self):
        self.assertEqual(str(self.set5), '14 95 16 69 76')
        self.assertEqual(str(self.set4), '91 22 28 16')

    def testTwoEmpty(self):
        result = self.set0.merge(self.set0)
        self.assertEqual(str(result), '')

    def testOneEmpty(self):
        result = self.set0.merge(self.set5)
        self.assertEqual(str(result), '1 4 9 5 1 6 6 9 7 6')

    def testTwoDifferent(self):
        result = self.set4.merge(self.set5)
        self.assertEqual(str(result), '1 0 5 1 1 7 4 4 8 5 7 6')
예제 #21
0
 def test_get_slice(self):
     l1 = sorted(random.sample(range(10000), 2000))
     s = IntSet(l1)
     self.assertEqual(s[10:100], IntSet(l1[10:100]))
     self.assertEqual(s[10:300], IntSet(l1[10:300]))
     self.assertEqual(s[10:1000], IntSet(l1[10:1000]))
     self.assertEqual(s[10:-10], IntSet(l1[10:-10]))
     self.assertEqual(s[10:9], IntSet(l1[10:9]))
예제 #22
0
    def test_discard(self):
        l1 = random.sample(range(10000), 2000)
        l2 = random.sample(l1, 300)
        s1 = IntSet(l1)
        s2 = set(l1)

        for x in l2:
            s1.discard(x)
            s2.discard(x)

        self.assertTrue(list(s1), sorted(s2))
        s1.discard(10001)
        self.assertRaises(TypeError, s1.discard, "1")
예제 #23
0
 def single_value(self, v):
     return (IntSet.single(v), [v])
예제 #24
0
 def result():
     d = IntSet.empty()
     for v in dataset:
         d = d.insert(v)
     return d
예제 #25
0
def test_comparision_with_other_type_is_error():
    with pytest.raises(TypeError):
        IntSet([1, 2, 3]) <= [1, 2, 3]
예제 #26
0
def test_default_intset_is_empty():
    assert IntSet() == IntSet.empty()
예제 #27
0
def test_can_produce_whole_range_intset():
    assert IntSet.interval(0, 2 ** 64).size() == 2 ** 64
예제 #28
0
 def three_adjacent_values_with_hole(self, v):
     assume(v + 2 <= 2 ** 64)
     return (IntSet.single(v) | IntSet.single(v + 2), [v, v + 2])
예제 #29
0
def test_is_equal_to_reverse_insertion(imp):
    equiv = IntSet.empty()
    for i in reversed(list(imp)):
        equiv = equiv.insert(i)
    assert imp == equiv
예제 #30
0
def test_is_equal_to_sequential_insertion(imp):
    equiv = IntSet.empty()
    for i in imp:
        equiv = equiv.insert(i)
    assert imp == equiv
예제 #31
0
def test_discarding_a_solo_gives_the_empty_list(i):
    imp = IntSet.single(i)
    assert imp.discard(i).size() == 0
예제 #32
0
def test_trying_to_pass_extra_args_to_intset_errors():
    with pytest.raises(TypeError):
        IntSet.from_iterable(1, 2)
예제 #33
0
def test_concatenation_of_lists_is_union(x, y):
    assert IntSet.from_intervals(x + y) == \
        IntSet.from_intervals(x) | IntSet.from_intervals(y)
예제 #34
0
intervals = st.tuples(integers_in_range, integers_in_range).map(
    lambda x: sorted(tuple(x)))

SMALL = 100

short_intervals = st.builds(
    lambda start, length: assume(
        start + length <= 2 ** 64) and (start, start + length),
    integers_in_range, st.integers(0, SMALL))

interval_list = st.lists(intervals, average_size=10)

IntSets = st.builds(
    IntSet.from_intervals, interval_list) | integers_in_range.map(
        IntSet.single) | intervals.map(lambda x: IntSet.interval(*x))


@example(IntSet.empty())
@example(IntSet.single(1))
@example(IntSet([1, 2, 3, 6]))
@given(IntSets)
def test_pickling_works_correctly(x):
    assert pickle.loads(pickle.dumps(x)) == x


@example(IntSet.interval(0, 10))
@example(IntSet([(0, 10), (15, 20)]))
@given(IntSets)
def test_copies_as_self(x):
    assert copy(x) is x
예제 #35
0
 def __init__(self):
     self.builder = IntSet.Builder()
     self.equivalent = IntSet.empty()
예제 #36
0
 def adjacent_values(self, v):
     assume(v + 1 <= 2 ** 64)
     return (IntSet.interval(v, v + 2), [v, v + 1])
예제 #37
0
 def three_adjacent_values(self, v):
     assume(v + 2 <= 2 ** 64)
     return (IntSet.interval(v, v + 3), [v, v + 1, v + 2])
예제 #38
0
def test_union_leads_to_extension(x, y):
    z = x | y
    for u in list(x.intervals()) + list(y.intervals()):
        assert IntSet.interval(*u).issubset(z)
예제 #39
0
def test_iterable_equivalent_to_intervals_of_length_one(xs):
    assert IntSet.from_iterable(xs) == \
        IntSet.from_intervals((x, x + 1) for x in xs)
예제 #40
0
def test_union_of_two_intervals_contains_each_start(i1, i2):
    assume(i1[0] < i1[1])
    assume(i2[0] < i2[1])
    x = IntSet.interval(*i1) | IntSet.interval(*i2)
    assert i1[0] in x
    assert i2[0] in x
예제 #41
0
def test_validates_argument_types():
    with pytest.raises(TypeError):
        IntSet.single('foo')
예제 #42
0
def test_unioning_a_value_in_includes_it(intervals, i):
    mp = IntSet.from_intervals(intervals)
    assume(i not in mp)
    mp2 = mp | IntSet.interval(i, i + 1)
    assert i in mp2
예제 #43
0
def test_interval_ending_at_zero_is_zero():
    assert IntSet.interval(0, 0) == IntSet.empty()
예제 #44
0
def test_not_equal_to_other_types():
    assert IntSet.single(1) != 1
예제 #45
0
def test_extra_args_is_a_type_error():
    with pytest.raises(TypeError):
        IntSet(1, 2)
예제 #46
0
 def build_interval(self, bounds):
     return (IntSet.interval(*bounds), list(range(*bounds)))
예제 #47
0
def test_instantiating_with_a_collection_gives_that_collection(xs):
    ts = IntSet.from_iterable(xs)
    assert len(ts) == len(xs)
    assert set(ts) == xs
예제 #48
0
 def insert_interval(self, ij):
     self.builder.insert_interval(*ij)
     self.equivalent = self.equivalent | IntSet.interval(*ij)