예제 #1
0
 def test_index(self):
     ss = SortedSet({
         'one': 1,
         'two': 2,
         })
     self.assertEqual(ss.index('two'), 1)
     self.assertEqual(ss.index('one'), 0)
예제 #2
0
 def test_index(self):
     ss = SortedSet({
         'one': 1,
         'two': 2,
     })
     self.assertEqual(ss.index('two'), 1)
     self.assertEqual(ss.index('one'), 0)
예제 #3
0
 def test_negative(self):
     ss = SortedSet()
     ss['one'] = 1
     ss['two'] = -2
     self.assertEqual(ss['one'], 1)
     self.assertEqual(ss['two'], -2)
     self.assertEqual(list(ss), ['two', 'one'])
     self.assertEqual(ss.index('two'), 0)
     self.assertEqual(ss.index('one'), 1)
예제 #4
0
 def test_negative(self):
     ss = SortedSet()
     ss['one'] = 1
     ss['two'] = -2
     self.assertEqual(ss['one'], 1)
     self.assertEqual(ss['two'], -2)
     self.assertEqual(list(ss), ['two', 'one'])
     self.assertEqual(ss.index('two'), 0)
     self.assertEqual(ss.index('one'), 1)
예제 #5
0
    def test_insert_integers(self):
        items = [  # fifty random values
             ('Xe2W0QxllGdCW251l7U9Dg', 150),
             ('3HT/SVSdCwM+4ZjtSqHCew', 476),
             ('Q2BKuEOFwIojkPsnjmPNFg', 2390),
             ('Qjq1fGnHVc5nXvWnbEyXjQ', 3773),
             ('wamjIdfm+ajk81fR7gKcAA', 4729),
             ('uDUHFv5CtiY/Gm5LOCfGUg', 6143),
             ('78l934GXETN68sql2vjP5w', 6487),
             ('1LAwgvIO0tEikYySkaSaXw', 7449),
             ('Y4mDqz7LfIV4L8h2aUwAkA', 10234),
             ('AidPceym19y/lmIdi6JxQQ', 10779),
             ('hHqwNSMusq7O895yFkr+rQ', 10789),
             ('dg16QiUDC2rgE39FWTSOxg', 11873),
             ('sgAdgtQ5wRFGSOZ3xZYHyA', 12273),
             ('OACKY0A1ftBbyLvTzyf8lQ', 12776),
             ('f+dLA1jK8EFEAHxm1FKUkA', 13079),
             ('1uDN4mSmsEQF/o6VNiBl3g', 13147),
             ('nNwOvGfk9AH2tIzK8uNdzQ', 16636),
             ('tMUZ6A1e/1SKd3ko0FhhBQ', 17933),
             ('M77ZQiFlYeU4ySUtVa6XYg', 18570),
             ('fY7RKQu8toBxoug4CMmznA', 18711),
             ('UaZorA+/GnCL4nmgLs3c/g', 19968),
             ('VbXaOsRHqH2CAoNJiYsrqg', 20064),
             ('dAr84/axpItIAjjNcVPzHA', 20250),
             ('HjzS0QlpofFhDO2iU4mXAw', 20756),
             ('ipksmQaeYErYwjZ6ia46TA', 21084),
             ('XemDsutAYPpCZ6WY4M//ig', 21609),
             ('6U6fbOs8jYVfqWeArQ5HHQ', 22410),
             ('QFblGefWYZqFbuSK0SDPRQ', 23267),
             ('J13bR75czCiLfeBcIy4XTg', 26556),
             ('e6XlDT9h6GVPdfvBOrXW5Q', 26608),
             ('/eLYo+GKgAt7I2PrOrFTzQ', 28597),
             ('48W/xF+VIQZoyKlhktifMw', 31364),
             ('NTUtbi4YOHiNIV6SVrpwyg', 33709),
             ('364+KUYYuwlmezv1EvaE0g', 33945),
             ('YaD6Ktqw1iIWcFGgMEvMxg', 38248),
             ('cJSZfsidFuaMK9jY15g44A', 38668),
             ('UeP/HvscsnQXUK37Dyo8/w', 40861),
             ('xon2bN9ZToI4LpN4o7M2nQ', 41836),
             ('MQKXJCNNtWsRqoGbSaDorw', 47171),
             ('LCcqUwfmOFq+VXI2kGBQow', 49311),
             ('gMXF4DMHCWBjbgucOqWKQg', 50725),
             ('JKHDvGMcLQrR4G3zC2g9ug', 50875),
             ('Mp1feZZmnmMPJk8bGv0NaA', 51017),
             ('rhZyspOoakQBO9Ses3jl+A', 53781),
             ('JB9bMHKHoT+hMVjuBrbqlg', 56409),
             ('/DsgGH+7F6Fh2/81SzyXYA', 56512),
             ('InjjAuUMGHYUIRdRnkUw2w', 56903),
             ('otVFi6DLAO+v7XUAcmKttA', 57114),
             ('mVTvHObgjfzvZLOzl/xo2Q', 58550),
             ('uU1yLoXCgPtifROhCST0sA', 60267)]
        keys = list(map(itemgetter(0), items))
        values = list(map(itemgetter(1), items))

        # simple checks
        set_n = SortedSet(items)
        set_r = SortedSet(reversed(items))
        for cur in (set_n, set_r):
            self.assertEqual(list(cur), keys)
            self.assertEqual(list(cur.keys()), keys)
            self.assertEqual(list(cur.values()), values)
            self.assertEqual(list(cur.items()), items)
        for idx in range(len(items)):
            key, score = items[idx]
            self.assertEqual(set_n.index(key), idx)
            self.assertEqual(set_r.index(key), idx)
            self.assertEqual(set_n.by_index[idx], key)
            self.assertEqual(set_r.by_index[idx], key)

        # slicing by index should be same as slicing list
        ends = (None, 0, 2, 5, 10, 49, 50, 51, -1, -2, -5, -10, -49, 50, -51)
        steps = (1, 2, 3)
        for start, stop, step in product(ends, ends, steps):
            self.assertEqual(list(set_n.by_index[start:stop:step].items()),
                             items[start:stop:step])
            self.assertEqual(list(set_r.by_index[start:stop:step].items()),
                             items[start:stop:step])
        # let's try to delete and reinsert slice
        for start, stop in product(ends, ends):
            slc = set_r.by_index[start:stop]
            slclist = items[start:stop]
            self.assertEqual(list(slc.items()), slclist)
            # delete a slice from set and list, then compare
            del set_r.by_index[start:stop]
            tmp = items[:]
            del tmp[start:stop]
            self.assertEqual(list(set_r.items()), tmp)
            # let's recreate set and check
            set_r.update(slc)
            self.assertEqual(list(set_r.items()), items)

            # try score slice on reverted set
            if slclist:
                # can do this only if set is not empty
                scorestart = slclist[0][1]
                scorestop = slclist[-1][1] + 1
                slc2 = set_r.by_score[scorestart:scorestop]
                self.assertEqual(list(slc2.items()), slclist)
                del set_r.by_score[scorestart:scorestop]
                self.assertEqual(list(set_r.items()), tmp)
                # and revert this change back again
                set_r.update(slc2)
                self.assertEqual(list(set_r.items()), items)


        # Lets test 7 random insertion orders
        for i in (10, 20, 30, 40, 50, 60, 70):
            rnd = random.Random(i)
            to_insert = copy.copy(items)
            rnd.shuffle(to_insert)

            # Let's check several sets created with different methods
            set1 = SortedSet()
            for k, v in to_insert:
                set1[k] = v
            set2 = SortedSet(to_insert)
            set3 = SortedSet(set1)
            set4 = SortedSet(set2)

            # Check all of them
            cursets = (set1, set2, set3, set4)
            for cur in cursets:
                self.assertEqual(list(cur), keys)
                self.assertEqual(list(cur.keys()), keys)
                self.assertEqual(list(cur.values()), values)
                self.assertEqual(list(cur.items()), items)

            # Check equality of all combinations
            all_sets = (set_n, set_r, set1, set2, set3, set4)
            for s1, s2 in combinations(all_sets, 2):
                self.assertEqual(s1, s2)

            # Let's pick up items to delete
            left = copy.copy(items)
            to_delete = []
            for i in range(rnd.randrange(10, 30)):
                idx = random.randrange(len(left))
                to_delete.append(left[idx])
                del left[idx]

            # Let's test deletion
            for cur in cursets:
                rnd.shuffle(to_delete)
                for key, value in to_delete:
                    del cur[key]
                self.assertEqual(list(cur.items()), left)
                self.assertNotEqual(cur, set_n)
                self.assertNotEqual(cur, set_r)

            # Let's reinsert keys in random order, and check if it's still ok
            for cur in cursets:
                rnd.shuffle(to_delete)
                for key, value in to_delete:
                    cur[key] = value
                self.assertEqual(cur, set_n)
                self.assertEqual(cur, set_r)
                self.assertEqual(list(cur.items()), items)
                for idx in range(len(items)):
                    key, score = items[idx]
                    self.assertEqual(cur.index(key), idx)
                    self.assertEqual(cur.by_index[idx], key)
예제 #6
0
    def test_insert_integers(self):
        items = [  # fifty random values
            ('Xe2W0QxllGdCW251l7U9Dg', 150), ('3HT/SVSdCwM+4ZjtSqHCew', 476),
            ('Q2BKuEOFwIojkPsnjmPNFg', 2390), ('Qjq1fGnHVc5nXvWnbEyXjQ', 3773),
            ('wamjIdfm+ajk81fR7gKcAA', 4729), ('uDUHFv5CtiY/Gm5LOCfGUg', 6143),
            ('78l934GXETN68sql2vjP5w', 6487), ('1LAwgvIO0tEikYySkaSaXw', 7449),
            ('Y4mDqz7LfIV4L8h2aUwAkA', 10234), ('AidPceym19y/lmIdi6JxQQ',
                                                10779),
            ('hHqwNSMusq7O895yFkr+rQ', 10789), ('dg16QiUDC2rgE39FWTSOxg',
                                                11873),
            ('sgAdgtQ5wRFGSOZ3xZYHyA', 12273), ('OACKY0A1ftBbyLvTzyf8lQ',
                                                12776),
            ('f+dLA1jK8EFEAHxm1FKUkA', 13079), ('1uDN4mSmsEQF/o6VNiBl3g',
                                                13147),
            ('nNwOvGfk9AH2tIzK8uNdzQ', 16636), ('tMUZ6A1e/1SKd3ko0FhhBQ',
                                                17933),
            ('M77ZQiFlYeU4ySUtVa6XYg', 18570), ('fY7RKQu8toBxoug4CMmznA',
                                                18711),
            ('UaZorA+/GnCL4nmgLs3c/g', 19968), ('VbXaOsRHqH2CAoNJiYsrqg',
                                                20064),
            ('dAr84/axpItIAjjNcVPzHA', 20250), ('HjzS0QlpofFhDO2iU4mXAw',
                                                20756),
            ('ipksmQaeYErYwjZ6ia46TA', 21084), ('XemDsutAYPpCZ6WY4M//ig',
                                                21609),
            ('6U6fbOs8jYVfqWeArQ5HHQ', 22410),
            ('QFblGefWYZqFbuSK0SDPRQ', 23267), ('J13bR75czCiLfeBcIy4XTg',
                                                26556),
            ('e6XlDT9h6GVPdfvBOrXW5Q', 26608), ('/eLYo+GKgAt7I2PrOrFTzQ',
                                                28597),
            ('48W/xF+VIQZoyKlhktifMw', 31364), ('NTUtbi4YOHiNIV6SVrpwyg',
                                                33709),
            ('364+KUYYuwlmezv1EvaE0g', 33945), ('YaD6Ktqw1iIWcFGgMEvMxg',
                                                38248),
            ('cJSZfsidFuaMK9jY15g44A', 38668), ('UeP/HvscsnQXUK37Dyo8/w',
                                                40861),
            ('xon2bN9ZToI4LpN4o7M2nQ', 41836), ('MQKXJCNNtWsRqoGbSaDorw',
                                                47171),
            ('LCcqUwfmOFq+VXI2kGBQow', 49311), ('gMXF4DMHCWBjbgucOqWKQg',
                                                50725),
            ('JKHDvGMcLQrR4G3zC2g9ug', 50875), ('Mp1feZZmnmMPJk8bGv0NaA',
                                                51017),
            ('rhZyspOoakQBO9Ses3jl+A', 53781), ('JB9bMHKHoT+hMVjuBrbqlg',
                                                56409),
            ('/DsgGH+7F6Fh2/81SzyXYA', 56512), ('InjjAuUMGHYUIRdRnkUw2w',
                                                56903),
            ('otVFi6DLAO+v7XUAcmKttA', 57114), ('mVTvHObgjfzvZLOzl/xo2Q',
                                                58550),
            ('uU1yLoXCgPtifROhCST0sA', 60267)
        ]
        keys = list(map(itemgetter(0), items))
        values = list(map(itemgetter(1), items))

        # simple checks
        set_n = SortedSet(items)
        set_r = SortedSet(reversed(items))
        for cur in (set_n, set_r):
            self.assertEqual(list(cur), keys)
            self.assertEqual(list(cur.keys()), keys)
            self.assertEqual(list(cur.values()), values)
            self.assertEqual(list(cur.items()), items)
        for idx in range(len(items)):
            key, score = items[idx]
            self.assertEqual(set_n.index(key), idx)
            self.assertEqual(set_r.index(key), idx)
            self.assertEqual(set_n.by_index[idx], key)
            self.assertEqual(set_r.by_index[idx], key)

        # slicing by index should be same as slicing list
        ends = (None, 0, 2, 5, 10, 49, 50, 51, -1, -2, -5, -10, -49, 50, -51)
        steps = (1, 2, 3)
        for start, stop, step in product(ends, ends, steps):
            self.assertEqual(list(set_n.by_index[start:stop:step].items()),
                             items[start:stop:step])
            self.assertEqual(list(set_r.by_index[start:stop:step].items()),
                             items[start:stop:step])
        # let's try to delete and reinsert slice
        for start, stop in product(ends, ends):
            slc = set_r.by_index[start:stop]
            slclist = items[start:stop]
            self.assertEqual(list(slc.items()), slclist)
            # delete a slice from set and list, then compare
            del set_r.by_index[start:stop]
            tmp = items[:]
            del tmp[start:stop]
            self.assertEqual(list(set_r.items()), tmp)
            # let's recreate set and check
            set_r.update(slc)
            self.assertEqual(list(set_r.items()), items)

            # try score slice on reverted set
            if slclist:
                # can do this only if set is not empty
                scorestart = slclist[0][1]
                scorestop = slclist[-1][1] + 1
                slc2 = set_r.by_score[scorestart:scorestop]
                self.assertEqual(list(slc2.items()), slclist)
                del set_r.by_score[scorestart:scorestop]
                self.assertEqual(list(set_r.items()), tmp)
                # and revert this change back again
                set_r.update(slc2)
                self.assertEqual(list(set_r.items()), items)

        # Lets test 7 random insertion orders
        for i in (10, 20, 30, 40, 50, 60, 70):
            rnd = random.Random(i)
            to_insert = copy.copy(items)
            rnd.shuffle(to_insert)

            # Let's check several sets created with different methods
            set1 = SortedSet()
            for k, v in to_insert:
                set1[k] = v
            set2 = SortedSet(to_insert)
            set3 = SortedSet(set1)
            set4 = SortedSet(set2)

            # Check all of them
            cursets = (set1, set2, set3, set4)
            for cur in cursets:
                self.assertEqual(list(cur), keys)
                self.assertEqual(list(cur.keys()), keys)
                self.assertEqual(list(cur.values()), values)
                self.assertEqual(list(cur.items()), items)

            # Check equality of all combinations
            all_sets = (set_n, set_r, set1, set2, set3, set4)
            for s1, s2 in combinations(all_sets, 2):
                self.assertEqual(s1, s2)

            # Let's pick up items to delete
            left = copy.copy(items)
            to_delete = []
            for i in range(rnd.randrange(10, 30)):
                idx = random.randrange(len(left))
                to_delete.append(left[idx])
                del left[idx]

            # Let's test deletion
            for cur in cursets:
                rnd.shuffle(to_delete)
                for key, value in to_delete:
                    del cur[key]
                self.assertEqual(list(cur.items()), left)
                self.assertNotEqual(cur, set_n)
                self.assertNotEqual(cur, set_r)

            # Let's reinsert keys in random order, and check if it's still ok
            for cur in cursets:
                rnd.shuffle(to_delete)
                for key, value in to_delete:
                    cur[key] = value
                self.assertEqual(cur, set_n)
                self.assertEqual(cur, set_r)
                self.assertEqual(list(cur.items()), items)
                for idx in range(len(items)):
                    key, score = items[idx]
                    self.assertEqual(cur.index(key), idx)
                    self.assertEqual(cur.by_index[idx], key)