|
| 1 | +#!/usr/bin/env python |
| 2 | +# --coding:utf-8-- |
| 3 | + |
| 4 | +# Copyright (c) 2020 vesoft inc. All rights reserved. |
| 5 | +# |
| 6 | +# This source code is licensed under Apache 2.0 License. |
| 7 | + |
| 8 | +import pytest |
| 9 | +from nebula3.utils.hash import hash as murmur_hash |
| 10 | + |
| 11 | +TEST_VECTORS = [ |
| 12 | + (b"", 6142509188972423790), |
| 13 | + (b"a", 4993892634952068459), |
| 14 | + (b"abcdefgh", 8664279048047335611), # length-8 bytes cases |
| 15 | + (b"abcdefghi", -5409788147785758033), |
| 16 | + ("to_be_hashed", -1098333533029391540), |
| 17 | + ("中文", -8591787916246384322), |
| 18 | +] |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.parametrize("data, expected", TEST_VECTORS) |
| 22 | +def test_known_vectors(data, expected): |
| 23 | + assert murmur_hash(data) == expected |
| 24 | + |
| 25 | + |
| 26 | +def test_str_bytes_equiv(): |
| 27 | + """ |
| 28 | + Ensure str and bytes inputs produce the same hash. |
| 29 | + """ |
| 30 | + s = "pytest" |
| 31 | + assert murmur_hash(s) == murmur_hash(s.encode("utf-8")) |
| 32 | + |
| 33 | + |
| 34 | +def test_type_error(): |
| 35 | + """ |
| 36 | + TypeError |
| 37 | + """ |
| 38 | + with pytest.raises(TypeError): |
| 39 | + murmur_hash(12345) |
| 40 | + |
| 41 | + |
| 42 | +def test_seed_variation(): |
| 43 | + """Different seed values should produce different hashes.""" |
| 44 | + data = b"seed_test" |
| 45 | + hash1 = murmur_hash(data, seed=0) |
| 46 | + hash2 = murmur_hash(data, seed=1) |
| 47 | + assert hash1 != hash2 |
| 48 | + |
| 49 | + |
| 50 | +def test_idempotent(): |
| 51 | + """Repeated calls with same input must yield the same result.""" |
| 52 | + data = b"consistent" |
| 53 | + assert murmur_hash(data) == murmur_hash(data) |
| 54 | + |
| 55 | + |
| 56 | +def test_large_input_performance(): |
| 57 | + """Large inputs should be processed without error and return an int.""" |
| 58 | + data = b"x" * 10_000 |
| 59 | + result = murmur_hash(data) |
| 60 | + assert isinstance(result, int) |
0 commit comments