2013-06-29 12:22:05 +00:00
|
|
|
import hashlib
|
2013-10-02 18:42:26 +00:00
|
|
|
import os
|
2013-06-29 12:22:05 +00:00
|
|
|
import tempfile
|
2013-07-08 21:38:27 +00:00
|
|
|
from attic.hashindex import NSIndex, ChunkIndex
|
|
|
|
from attic.testsuite import AtticTestCase
|
2013-06-29 12:22:05 +00:00
|
|
|
|
|
|
|
|
2013-07-08 21:38:27 +00:00
|
|
|
class HashIndexTestCase(AtticTestCase):
|
2013-06-29 12:22:05 +00:00
|
|
|
|
|
|
|
def _generic_test(self, cls, make_value, sha):
|
2014-07-10 13:32:12 +00:00
|
|
|
idx = cls()
|
2013-06-29 12:22:05 +00:00
|
|
|
self.assert_equal(len(idx), 0)
|
|
|
|
# Test set
|
|
|
|
for x in range(100):
|
|
|
|
idx[bytes('%-32d' % x, 'ascii')] = make_value(x)
|
|
|
|
self.assert_equal(len(idx), 100)
|
|
|
|
for x in range(100):
|
|
|
|
self.assert_equal(idx[bytes('%-32d' % x, 'ascii')], make_value(x))
|
|
|
|
# Test update
|
|
|
|
for x in range(100):
|
|
|
|
idx[bytes('%-32d' % x, 'ascii')] = make_value(x * 2)
|
|
|
|
self.assert_equal(len(idx), 100)
|
|
|
|
for x in range(100):
|
|
|
|
self.assert_equal(idx[bytes('%-32d' % x, 'ascii')], make_value(x * 2))
|
|
|
|
# Test delete
|
|
|
|
for x in range(50):
|
|
|
|
del idx[bytes('%-32d' % x, 'ascii')]
|
|
|
|
self.assert_equal(len(idx), 50)
|
2014-07-10 13:32:12 +00:00
|
|
|
idx_name = tempfile.NamedTemporaryFile()
|
|
|
|
idx.write(idx_name.name)
|
2013-06-29 12:22:05 +00:00
|
|
|
del idx
|
|
|
|
# Verify file contents
|
|
|
|
with open(idx_name.name, 'rb') as fd:
|
|
|
|
self.assert_equal(hashlib.sha256(fd.read()).hexdigest(), sha)
|
|
|
|
# Make sure we can open the file
|
2014-07-10 13:32:12 +00:00
|
|
|
idx = cls.read(idx_name.name)
|
2013-06-29 12:22:05 +00:00
|
|
|
self.assert_equal(len(idx), 50)
|
|
|
|
for x in range(50, 100):
|
|
|
|
self.assert_equal(idx[bytes('%-32d' % x, 'ascii')], make_value(x * 2))
|
|
|
|
idx.clear()
|
|
|
|
self.assert_equal(len(idx), 0)
|
2014-07-10 13:32:12 +00:00
|
|
|
idx.write(idx_name.name)
|
2013-06-29 12:22:05 +00:00
|
|
|
del idx
|
2014-07-10 13:32:12 +00:00
|
|
|
self.assert_equal(len(cls.read(idx_name.name)), 0)
|
2013-06-29 12:22:05 +00:00
|
|
|
|
|
|
|
def test_nsindex(self):
|
2013-07-08 21:38:27 +00:00
|
|
|
self._generic_test(NSIndex, lambda x: (x, x), '369a18ae6a52524eb2884a3c0fdc2824947edd017a2688c5d4d7b3510c245ab9')
|
2013-06-29 12:22:05 +00:00
|
|
|
|
|
|
|
def test_chunkindex(self):
|
2013-07-08 21:38:27 +00:00
|
|
|
self._generic_test(ChunkIndex, lambda x: (x, x, x), 'ed22e8a883400453c0ee79a06c54df72c994a54eeefdc6c0989efdc5ee6d07b7')
|
2013-06-29 12:22:05 +00:00
|
|
|
|
2013-10-02 18:42:26 +00:00
|
|
|
def test_resize(self):
|
|
|
|
n = 2000 # Must be >= MIN_BUCKETS
|
|
|
|
idx_name = tempfile.NamedTemporaryFile()
|
2014-07-10 13:32:12 +00:00
|
|
|
idx = NSIndex()
|
|
|
|
idx.write(idx_name.name)
|
2013-10-02 18:42:26 +00:00
|
|
|
initial_size = os.path.getsize(idx_name.name)
|
|
|
|
self.assert_equal(len(idx), 0)
|
|
|
|
for x in range(n):
|
|
|
|
idx[bytes('%-32d' % x, 'ascii')] = x, x
|
2014-07-10 13:32:12 +00:00
|
|
|
idx.write(idx_name.name)
|
2013-10-02 18:42:26 +00:00
|
|
|
self.assert_true(initial_size < os.path.getsize(idx_name.name))
|
|
|
|
for x in range(n):
|
|
|
|
del idx[bytes('%-32d' % x, 'ascii')]
|
|
|
|
self.assert_equal(len(idx), 0)
|
2014-07-10 13:32:12 +00:00
|
|
|
idx.write(idx_name.name)
|
2013-10-02 18:42:26 +00:00
|
|
|
self.assert_equal(initial_size, os.path.getsize(idx_name.name))
|
|
|
|
|
2014-02-09 21:05:33 +00:00
|
|
|
def test_iteritems(self):
|
2014-07-10 13:32:12 +00:00
|
|
|
idx = NSIndex()
|
2014-02-09 21:05:33 +00:00
|
|
|
for x in range(100):
|
|
|
|
idx[bytes('%-0.32d' % x, 'ascii')] = x, x
|
|
|
|
all = list(idx.iteritems())
|
|
|
|
self.assert_equal(len(all), 100)
|
|
|
|
second_half = list(idx.iteritems(marker=all[49][0]))
|
|
|
|
self.assert_equal(len(second_half), 50)
|
|
|
|
self.assert_equal(second_half, all[50:])
|