Beispiel #1
0
    def setUp(self):
        Node = node_factory('<i')
        left = Node(True,
                    [2, 7, 11],
                    [32, 87, 43, 2])

        right = Node(True,
                     [15, 24, 31],
                     [45, 67, 89, 0])

        with open('foobar', 'wb') as file:
            pass

        manager = BufferManager()

        block = manager.get_file_block('foobar', 0)
        with pin(block):
            block.write(b'\0' * BufferManager.block_size)

        block = manager.get_file_block('foobar', 1)
        with pin(block):
            block.write(bytes(left))

        block = manager.get_file_block('foobar', 2)
        with pin(block):
            block.write(bytes(right))

        self.Node = Node
        self.left = left
Beispiel #2
0
 def test_bytes(self):
     Node = node_factory('<ii5s')
     node = Node(True, [(42, 666, 'spam'), (233, 987, 'foo')], [518, 2, 42])
     octets = bytes(node)
     node2 = Node.frombytes(octets.ljust(BufferManager.block_size, b'\0'))
     self.assertEqual(node2.is_leaf, True)
     self.assertEqual(node2.keys, [(42, 666, 'spam'), (233, 987, 'foo')])
     self.assertEqual(node2.children, [518, 2, 42])
Beispiel #3
0
 def test_single_iter(self):
     Node = node_factory('<i')
     node = Node(True,
                 [1, 14, 35],
                 [54, 21, 518, 0])
     iterator = LeafIterator(Node, 'foobar', node, 1)
     self.assertEqual(next(iterator), ((14,), 21))
     self.assertEqual(next(iterator), ((35,), 518))
     with self.assertRaises(StopIteration):
         next(iterator)
Beispiel #4
0
    def test_internal_split(self):
        Node = node_factory('<i')
        node = Node(False, [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 42])
        Node.n = 4
        new_node, key, value = node.split(50)

        self.assertEqual(key, (2,))
        self.assertEqual(value, 50)
        self.assertEqual(node.is_leaf, False)
        self.assertEqual(node.keys, [(0,), (1,)])
        self.assertEqual(node.children, [0, 1, 2])
        self.assertEqual(new_node.is_leaf, False)
        self.assertEqual(new_node.keys, [(3,), (4,)])
        self.assertEqual(new_node.children, [3, 4, 42])
Beispiel #5
0
    def test_mixed_fuse(self):
        Node = node_factory('<i')
        parent = Node(False,
                      [1, 14, 35],
                      [54, 21, 518, 42])

        left = Node(True,
                    [2, 7, 11],
                    [32, 87, 43, 518])

        right = Node(False,
                     [15, 24, 31],
                     [45, 67, 89, 42])
        with self.assertRaises(ValueError):
            left.fuse_with(right, parent, 1)
Beispiel #6
0
    def test_fuse_internal(self):
        Node = node_factory('<i')
        parent = Node(False,
                      [1, 14, 35],
                      [54, 21, 518, 42])

        left = Node(False,
                    [2, 7, 11],
                    [32, 87, 43, 518])

        right = Node(False,
                     [15, 24, 31],
                     [45, 67, 89, 42])

        left.fuse_with(right, parent, 1)

        self.assertEqual(parent.keys, _convert_to_tuple_list([1, 35]))
        self.assertEqual(parent.children, [54, 21, 42])

        self.assertEqual(left.keys, _convert_to_tuple_list([2, 7, 11, 14, 15, 24, 31]))
        self.assertEqual(left.children, [32, 87, 43, 518, 45, 67, 89, 42])
Beispiel #7
0
    def test_transfer_from_right_leaf(self):
        Node = node_factory('<i')
        parent = Node(False,
                      [1, 14, 35],
                      [54, 21, 518, 42])

        left = Node(True,
                    [2, 7, 11],
                    [32, 87, 43, 518])

        right = Node(True,
                     [15, 24, 31],
                     [45, 67, 89, 42])

        left.transfer_from_right(right, parent, 1)

        self.assertEqual(parent.keys, _convert_to_tuple_list([1, 24, 35]))
        self.assertEqual(parent.children, [54, 21, 518, 42])

        self.assertEqual(left.keys, _convert_to_tuple_list([2, 7, 11, 15]))
        self.assertEqual(left.children, [32, 87, 43, 45, 518])

        self.assertEqual(right.keys, _convert_to_tuple_list([24, 31]))
        self.assertEqual(right.children, [67, 89, 42])
Beispiel #8
0
 def test_insert_into_internal(self):
     Node = node_factory('<i')
     node = Node(False, [0, 2, 4, 6, 8], [0, 1, 2, 3, 4, 5])
     node.insert(3, 518)
     self.assertEqual(node.keys, [(0,), (2,), (3,), (4,), (6,), (8,)])
     self.assertEqual(node.children, [0, 1, 518, 2, 3, 4, 5])