/usr/share/pyshared/ZODB/tests/testfsIndex.py is in python-zodb 1:3.9.7-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | ##############################################################################
#
# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
import unittest
import random
from ZODB.fsIndex import fsIndex
from ZODB.utils import p64, z64
class Test(unittest.TestCase):
def setUp(self):
self.index = fsIndex()
for i in range(200):
self.index[p64(i * 1000)] = (i * 1000L + 1)
def test__del__(self):
index = self.index
self.assert_(p64(1000) in index)
self.assert_(p64(100*1000) in index)
del self.index[p64(1000)]
del self.index[p64(100*1000)]
self.assert_(p64(1000) not in index)
self.assert_(p64(100*1000) not in index)
for key in list(self.index):
del index[key]
self.assert_(not index)
# Whitebox. Make sure empty buckets are removed
self.assert_(not index._data)
def testInserts(self):
index = self.index
for i in range(0,200):
self.assertEqual((i,index[p64(i*1000)]), (i,(i*1000L+1)))
self.assertEqual(len(index), 200)
key=p64(2000)
self.assertEqual(index.get(key), 2001)
key=p64(2001)
self.assertEqual(index.get(key), None)
self.assertEqual(index.get(key, ''), '')
# self.failUnless(len(index._data) > 1)
def testUpdate(self):
index = self.index
d={}
for i in range(200):
d[p64(i*1000)]=(i*1000L+1)
index.update(d)
for i in range(400,600):
d[p64(i*1000)]=(i*1000L+1)
index.update(d)
for i in range(100, 500):
d[p64(i*1000)]=(i*1000L+2)
index.update(d)
self.assertEqual(index.get(p64(2000)), 2001)
self.assertEqual(index.get(p64(599000)), 599001)
self.assertEqual(index.get(p64(399000)), 399002)
self.assertEqual(len(index), 600)
def testKeys(self):
keys = list(iter(self.index))
keys.sort()
for i, k in enumerate(keys):
self.assertEqual(k, p64(i * 1000))
keys = list(self.index.iterkeys())
keys.sort()
for i, k in enumerate(keys):
self.assertEqual(k, p64(i * 1000))
keys = self.index.keys()
keys.sort()
for i, k in enumerate(keys):
self.assertEqual(k, p64(i * 1000))
def testValues(self):
values = list(self.index.itervalues())
values.sort()
for i, v in enumerate(values):
self.assertEqual(v, (i * 1000L + 1))
values = self.index.values()
values.sort()
for i, v in enumerate(values):
self.assertEqual(v, (i * 1000L + 1))
def testItems(self):
items = list(self.index.iteritems())
items.sort()
for i, item in enumerate(items):
self.assertEqual(item, (p64(i * 1000), (i * 1000L + 1)))
items = self.index.items()
items.sort()
for i, item in enumerate(items):
self.assertEqual(item, (p64(i * 1000), (i * 1000L + 1)))
def testMaxKey(self):
index = self.index
index.clear()
# An empty index should complain.
self.assertRaises(ValueError, index.maxKey)
# Now build up a tree with random values, and check maxKey at each
# step.
correct_max = "" # smaller than anything we'll add
for i in range(1000):
key = p64(random.randrange(100000000))
index[key] = i
correct_max = max(correct_max, key)
index_max = index.maxKey()
self.assertEqual(index_max, correct_max)
index.clear()
a = '\000\000\000\000\000\001\000\000'
b = '\000\000\000\000\000\002\000\000'
c = '\000\000\000\000\000\003\000\000'
d = '\000\000\000\000\000\004\000\000'
index[a] = 1
index[c] = 2
self.assertEqual(index.maxKey(b), a)
self.assertEqual(index.maxKey(d), c)
self.assertRaises(ValueError, index.maxKey, z64)
def testMinKey(self):
index = self.index
index.clear()
# An empty index should complain.
self.assertRaises(ValueError, index.minKey)
# Now build up a tree with random values, and check minKey at each
# step.
correct_min = "\xff" * 8 # bigger than anything we'll add
for i in range(1000):
key = p64(random.randrange(100000000))
index[key] = i
correct_min = min(correct_min, key)
index_min = index.minKey()
self.assertEqual(index_min, correct_min)
index.clear()
a = '\000\000\000\000\000\001\000\000'
b = '\000\000\000\000\000\002\000\000'
c = '\000\000\000\000\000\003\000\000'
d = '\000\000\000\000\000\004\000\000'
index[a] = 1
index[c] = 2
self.assertEqual(index.minKey(b), c)
self.assertRaises(ValueError, index.minKey, d)
def test_suite():
loader=unittest.TestLoader()
return loader.loadTestsFromTestCase(Test)
if __name__=='__main__':
unittest.TextTestRunner().run(test_suite())
|