Esempio n. 1
0
    def test_underscores(self):

        for size in 2, 3, 4, 8:
            seed(size)
            tree1 = CQRTree(max_entries=size)
            for i, box in self.gen_random(10):
                tree1[box] = i
            tree2 = CQRTree(max_entries=size)
            for k, v in tree1.items():
                tree2.add(k, v)
            self.assertEqual(tree1, tree2)
            if size == 2:
                self.assertEqual(str(tree1), 'Quadratic RTree (10 leaves, 4 height, 1-2 entries)')
            tree3 = CQRTree(tree1.items(), max_entries=size)
            self.assertEqual(tree1, tree3)

        tree = CQRTree()
        tree.add([(1, 1)], '1')
        tree.add([(2, 2)], '2')
        self.assertEqual(len(tree), 2)
        self.assertTrue([(1, 1)] in tree)
        self.assertFalse([(3, 3)] in tree)
        self.assertTrue('1' in list(tree.values()))
        self.assertTrue(((1, 1),) in list(tree.keys()), list(tree.keys()))
        self.assertTrue((((1, 1),), '1') in list(tree.items()))
        del tree[[(1, 1)]]
        self.assertEqual(len(tree), 1)
        self.assertTrue(list(tree[[(2, 2)]]))
        self.assertTrue(list(tree.get([(2, 2)])))
        self.assertTrue(list(tree.get([(2, 2)], value='2')))
        self.assertFalse(list(tree.get([(2, 2)], value='1')))
        self.assertTrue(tree)
        del tree[[(2, 2)]]
        self.assertFalse(tree)
Esempio n. 2
0
    def test_docs(self):
        tree = CQRTree()
        square = ((0, 0), (0, 1), (1, 1), (1, 0))
        tree[square] = 'square'
        self.assertEqual(list(tree[square]), ['square'])
        self.assertTrue(square in tree)
        diagonal = ((0, 0), (1, 1))
        self.assertEqual(list(tree[diagonal]), [])
        self.assertFalse(diagonal in tree)
        self.assertEqual(list(tree.keys()), [((0, 0), (0, 1), (1, 1), (1, 0))])
        self.assertEqual(list(tree.values()), ['square'])
        self.assertEqual(list(tree.items()),
                         [(((0, 0), (0, 1), (1, 1), (1, 0)), 'square')])
        self.assertEqual(len(tree), 1)
        del tree[square]
        self.assertEqual(len(tree), 0)

        tree = CQRTree(default_match=MatchType.OVERLAP)
        tree[square] = 'square'
        self.assertEqual(list(tree[diagonal]), ['square'])

        tree = CQRTree(default_match=MatchType.OVERLAP)
        tree[square] = 'square'
        self.assertEqual(list(tree.get_items(diagonal)),
                         [(((0, 0), (0, 1), (1, 1), (1, 0)), 'square')])
Esempio n. 3
0
 def test_canary(self):
     tree = CQRTree()
     for i in range(5):
         tree.add([(i, i)], i)
     keys = tree.keys()
     next(keys)
     del tree[[(1, 1)]]
     with self.assertRaisesRegex(RuntimeError, 'mutated'):
         next(keys)