def test_random_hash(): from string import ascii_letters as domain times = 1000 minlen = 1 maxlen = len(domain) def randstring(): s = "".join(random.sample(domain, random.randint(minlen, maxlen))) return b(s) with TempStorage("randomhash") as st: samp = dict((randstring(), randstring()) for _ in xrange(times)) hw = HashWriter(st.create_file("test.hsh")) for k, v in iteritems(samp): hw.add(k, v) hw.close() keys = list(samp.keys()) random.shuffle(keys) hr = HashReader.open(st, "test.hsh") for k in keys: assert hr[k] == samp[k] hr.close()
def test_hash_contents(): samp = [('alfa', 'bravo'), ('charlie', 'delta'), ('echo', 'foxtrot'), ('golf', 'hotel'), ('india', 'juliet'), ('kilo', 'lima'), ('mike', 'november'), ('oskar', 'papa'), ('quebec', 'romeo'), ('sierra', 'tango'), ('ultra', 'victor'), ('whiskey', 'xray'), ] # Convert to bytes samp = set((b(k), b(v)) for k, v in samp) with TempStorage("hashcontents") as st: hw = HashWriter(st.create_file("test.hsh")) hw.add_all(samp) hw.close() hr = HashReader.open(st, "test.hsh") probes = list(samp) random.shuffle(probes) for key, value in probes: assert hr[key] == value assert set(hr.keys()) == set([k for k, v in samp]) assert set(hr.values()) == set([v for k, v in samp]) assert set(hr.items()) == samp hr.close()
def test_hash_contents(): samp = [ ('alfa', 'bravo'), ('charlie', 'delta'), ('echo', 'foxtrot'), ('golf', 'hotel'), ('india', 'juliet'), ('kilo', 'lima'), ('mike', 'november'), ('oskar', 'papa'), ('quebec', 'romeo'), ('sierra', 'tango'), ('ultra', 'victor'), ('whiskey', 'xray'), ] # Convert to bytes samp = set((b(k), b(v)) for k, v in samp) with TempStorage("hashcontents") as st: hw = HashWriter(st.create_file("test.hsh")) hw.add_all(samp) hw.close() hr = HashReader.open(st, "test.hsh") probes = list(samp) random.shuffle(probes) for key, value in probes: assert hr[key] == value assert set(hr.keys()) == set([k for k, v in samp]) assert set(hr.values()) == set([v for k, v in samp]) assert set(hr.items()) == samp hr.close()
def test_hash_single(): st = RamStorage() hw = HashWriter(st.create_file("test.hsh")) hw.add(b("alfa"), b("bravo")) hw.close() hr = HashReader.open(st, "test.hsh") assert hr.get(b("alfa")) == b("bravo") assert hr.get(b("foo")) is None
def test_hash(): with TempStorage("hash") as st: hwf = st.create_file("test.hsh") hw = HashWriter(hwf) hw.add(b("foo"), b("bar")) hw.add(b("glonk"), b("baz")) hw.close() hr = HashReader.open(st, "test.hsh") assert hr.get(b("foo")) == b("bar") assert hr.get(b("baz")) is None hr.close()
def test_hash_extras(): st = RamStorage() hw = HashWriter(st.create_file("test.hsh")) hw.extras["test"] = 100 hw.add(b("foo"), b("bar")) hw.add(b("glonk"), b("baz")) hw.close() hr = HashReader.open(st, "test.hsh") assert hr.extras["test"] == 100 assert hr.get(b("foo")) == b("bar") assert hr.get(b("baz")) is None hr.close()
def test_random_access(): times = 1000 with TempStorage("orderedhash") as st: hw = HashWriter(st.create_file("test.hsh")) hw.add_all((b("%08x" % x), b(str(x))) for x in xrange(times)) hw.close() keys = list(range(times)) random.shuffle(keys) hr = HashReader.open(st, "test.hsh") for x in keys: assert hr[b("%08x" % x)] == b(str(x)) hr.close()