summaryrefslogtreecommitdiff
path: root/test-backwards-compatibility
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2012-05-23 15:32:04 +0200
committerLars Wirzenius <liw@liw.fi>2012-05-23 15:32:04 +0200
commita10b3c09f5eb772a21d6b10f307f285ad159e90d (patch)
tree8ba7d399a8823d3c34b64ea420d38b075aa7d1d9 /test-backwards-compatibility
parent0f4379081f62370c360f6a792a844cbc982a229c (diff)
downloadlarch-a10b3c09f5eb772a21d6b10f307f285ad159e90d.tar.gz
Add program to generate test data for backwards compatbility
Diffstat (limited to 'test-backwards-compatibility')
-rwxr-xr-xtest-backwards-compatibility113
1 files changed, 113 insertions, 0 deletions
diff --git a/test-backwards-compatibility b/test-backwards-compatibility
new file mode 100755
index 0000000..a10c6bb
--- /dev/null
+++ b/test-backwards-compatibility
@@ -0,0 +1,113 @@
+#!/usr/bin/python
+# Copyright 2012 Lars Wirzenius
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+'''Test backwards compatibility of an on-disk B-tree.
+
+This program tests that a Larch on-disk B-tree is backwards compatible
+with previous versions, at least to the extent that it can be read from.
+This program operates in one of two modes:
+
+* it can generate a new B-tree to be stored as test data for the future
+* it can read an existing tree and verify that it can read it right
+
+The generated B-tree is actually a forest, and contains four trees.
+The first tree has the following keys:
+
+* key size is 4 bytes
+* keys are 0, 1, 2, ..., 1023, converted into binary strings with struct
+* values are 0, 1, 2, ..., 1023, converted into text strings with '%d' % i
+* node size is 128 bytes
+
+The second tree is a clone of the first one, but with all odd-numbered
+keys removed.
+
+The third tree is a clone of the second one, but with all odd-numbered
+keys and values added back.
+
+The fourth tree is a clone of the third one, but with all even-numbered
+keys removed.
+
+'''
+
+
+import cliapp
+import shutil
+import struct
+import tarfile
+import tempfile
+
+import larch
+
+
+class BackwardsCompatibilityTester(cliapp.Application):
+
+ key_size = 4
+ node_size = 128
+ num_keys = 1024
+
+ def setup(self):
+ self.dirname = tempfile.mkdtemp()
+
+ def teardown(self):
+ shutil.rmtree(self.dirname)
+
+ def key(self, i):
+ return struct.pack('!L', i)
+
+ def value(self, i):
+ return '%d' % i
+
+ def cmd_generate(self, args):
+ '''Generate a Larch B-tree forest'''
+
+ forest = larch.open_forest(key_size=self.key_size,
+ node_size=self.node_size,
+ dirname=self.dirname,
+ allow_writes=True)
+
+ # First tree.
+ t = forest.new_tree()
+ for i in range(self.num_keys):
+ t.insert(self.key(i), self.value(i))
+
+ # Second tree.
+ t = forest.new_tree(t)
+ for i in range(self.num_keys):
+ if (i % 2) == 1:
+ t.remove(self.key(i))
+
+ # Third tree.
+ t = forest.new_tree(t)
+ for i in range(self.num_keys):
+ if (i % 2) == 1:
+ t.insert(self.key(i), self.value(i))
+
+ # Fourth tree.
+ t = forest.new_tree(t)
+ for i in range(self.num_keys):
+ if (i % 2) == 0:
+ t.remove(self.key(i))
+
+ # Commit and make into a tarball.
+ forest.commit()
+ tf = tarfile.open(fileobj=self.output, mode='w:gz')
+ tf.add(self.dirname, arcname='.')
+ tf.close()
+
+
+BackwardsCompatibilityTester().run()
+