예제 #1
0
    def test_get_min(self):

        cases = (
            (((1, 2, '12'), ),                           (0, [1, 2, '12'], '12')),
            (((1, 2, '34'), (3, 4, '12')),               (1, [3, 4, '12'], '12')),
            (((1, 2, '34'), (3, 4, '12'), (5, 6, '56')), (1, [3, 4, '12'], '12')),
            (((1, 2, '12'), (3, 4, '34'), (5, 6, '12')), (0, [1, 2, '12'], '12')),
        )

        for src_rngs, expected in cases:

            dd("expected index: ", expected[0])
            dd("expected range: ", rangeset.ValueRange(*expected[1]))
            dd("expected value: ", expected[2])

            rd = rangeset.RangeDict(src_rngs)
            idx, rng, val = rd.get_min()

            dd("got index: ", idx)
            dd("got range: ", rng)
            dd("got value: ", val)

            self.assertEqual(idx, expected[0])
            self.assertEqual(rng, rangeset.ValueRange(*expected[1]))
            self.assertEqual(val, expected[2])

        rd = rangeset.RangeDict()
        self.assertRaises(ValueError, rd.get_min)
예제 #2
0
    def test_unicode(self):

        # fix: https://github.com/bsc-s2/pykit/issues/430

        # valid unicode range
        cases = (
            [u'我', None, 0],
            [None, u'我', 0],
            [u'它', u'我', 0],
        )

        for rng in cases:
            r = rangeset.ValueRange(*rng)

        # left > right
        cases = (
            [u'我a', u'我', 0],
        )

        for rng in cases:
            self.assertRaises(ValueError, rangeset.ValueRange, *rng)

        # incompatible
        cases = (
            [u'我', '我', 0],
            [u'我', 0, 0],
            [u'我', 0.0, 0],
            [u'我', (), 0],
            [u'我', [], 0],
            [u'我', {}, 0],
        )

        for l, r, v in cases:
            self.assertRaises(TypeError, rangeset.ValueRange, l, r, v)
            self.assertRaises(TypeError, rangeset.ValueRange, r, l, v)
예제 #3
0
    def test_find_overlapped(self):

        cases = (
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [None, None], [[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [None, 1],    [[None, 10, 'a']                                ]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [None, 10],   [[None, 10, 'a']                                ]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [None, 11],   [[None, 10, 'a']                                ]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [9,    11],   [[None, 10, 'a']                                ]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [9,    21],   [[None, 10, 'a'], [20, 30, 'b']                 ]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [9,    40],   [[None, 10, 'a'], [20, 30, 'b']                 ]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [9,    41],   [[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [10,   11],   [                                               ]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [11,   12],   [                                               ]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [19,   20],   [                                               ]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [19,   21],   [                 [20, 30, 'b']                 ]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [20,   21],   [                 [20, 30, 'b']                 ]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [24,   25],   [                 [20, 30, 'b']                 ]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [29,   30],   [                 [20, 30, 'b']                 ]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [29,   31],   [                 [20, 30, 'b']                 ]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [29,   41],   [                 [20, 30, 'b'], [40, None, 'c']]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [30,   31],   [                                               ]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [31,   32],   [                                               ]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [39,   40],   [                                               ]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [39,   41],   [                                [40, None, 'c']]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [40,   41],   [                                [40, None, 'c']]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [41,   42],   [                                [40, None, 'c']]),
            ([[None, 10, 'a'], [20, 30, 'b'], [40, None, 'c']], [41,   None], [                                [40, None, 'c']]),

        )

        dd()
        for a, b, expected in cases:

            dd('case:', a, b, expected)

            a = rangeset.RangeDict(a)

            self.assertEqual(expected, a.find_overlapped(b))
            self.assertEqual(expected, a.find_overlapped(rangeset.Range(*b)))
            self.assertEqual(expected, a.find_overlapped(rangeset.ValueRange(*(b+['bar']))))

            a = rangeset.RangeSet([x[:2] for x in a])
            expected = [x[:2] for x in expected]

            self.assertEqual(expected, a.find_overlapped(b))
            self.assertEqual(expected, a.find_overlapped(rangeset.Range(*b)))
            self.assertEqual(expected, a.find_overlapped(rangeset.ValueRange(*(b+['bar']))))
예제 #4
0
    def test_val(self):
        cases = (
            ([None, None, None],        None),
            ([None, None, 1],           1),
            ([None, None, ''],          ''),
            ([None, None, ['']],        ['']),
            ([None, None, ('',)],       ('',)),
            ([None, None, {'1': 1}],    {'1': 1}),
        )

        for rng, expected in cases:
            rst = rangeset.ValueRange(*rng)
            self.assertEqual(expected, rst.val())
예제 #5
0
    def test_intersect(self):

        cases = (
            ([None, None], [None, None], [None, None] ),
            ([None, None], [1,    None], [1, None]    ),
            ([None, None], [None, 1],    [None, 1]    ),
            ([None, None], [1,    3],    [1, 3]       ),
            ([None, 5],    [5,    8],    None         ),
            ([None, 5],    [4,    8],    [4, 5]       ),
            ([None, 5],    [1,    2],    [1, 2]       ),
            ([None, 5],    [None, 2],    [None, 2]    ),
            ([None, 5],    [None, 5],    [None, 5]    ),
            ([5,    None], [1,    2],    None         ),
            ([5,    None], [1,    8],    [5, 8]       ),
            ([5,    None], [5,    8],    [5, 8]       ),
            ([5,    None], [6,    8],    [6, 8]       ),
            ([5,    None], [6,    None], [6, None]    ),
            ([5,    None], [5,    None], [5, None]    ),
            ([5,    None], [4,    None], [5, None]    ),
            ([5,    10],   [5,    None], [5, 10]      ),
            ([5,    10],   [6,    None], [6, 10]      ),
            ([5,    10],   [6,    7],    [6, 7]       ),
            ([5,    10],   [6,    10],   [6, 10]      ),
        )

        for a, b, expected in cases:
            dd('case:', a, b, expected)

            a = rangeset.Range(*a)

            self.assertEqual(expected, a.intersect(rangeset.Range(*b)))
            self.assertEqual(expected, a.intersect(rangeset.ValueRange(*(b+['bar']))))

            a = rangeset.ValueRange(*(a+['ahh']))
            if expected is not None:
                expected = expected + ['ahh']

            self.assertEqual(expected, a.intersect(rangeset.Range(*b)))
            self.assertEqual(expected, a.intersect(rangeset.ValueRange(*(b+['bar']))))
예제 #6
0
    def test_get_min_is_lt(self):

        cases = (
            (((1, 2, '12'), (3, 4, '34'), (5, 6, '12')), lambda a, b: a < b, (0, [1, 2, '12'], '12')),
            (((1, 2, '12'), (3, 4, '34'), (5, 6, '12')), lambda a, b: a > b, (1, [3, 4, '34'], '34')),
        )

        for src_rngs, is_lt, expected in cases:

            dd("expected index: ", expected[0])
            dd("expected range: ", rangeset.ValueRange(*expected[1]))
            dd("expected value: ", expected[2])

            rd = rangeset.RangeDict(src_rngs)
            idx, rng, val = rd.get_min(is_lt)

            dd("got index: ", idx)
            dd("got range: ", rng)
            dd("got value: ", val)

            self.assertEqual(idx, expected[0])
            self.assertEqual(rng, rangeset.ValueRange(*expected[1]))
            self.assertEqual(val, expected[2])