MCPcopy Create free account
hub / github.com/ashvardanian/StringZilla / test_sha256

Function test_sha256

scripts/test_stringzilla.py:1060–1100  ·  view source on GitHub ↗
(length: int, seed_value: int)

Source from the content-addressed store, hash-verified

1058@pytest.mark.parametrize("length", [0, 1, 3, 7, 15, 31, 63, 64, 65, 127, 128, 129, 255, 256, 1000, 4096, 10000])
1059@pytest.mark.parametrize("seed_value", SEED_VALUES)
1060def test_sha256(length: int, seed_value: int):
1061
1062 seed_random_generators(seed_value)
1063 text = get_random_string(length=length)
1064 expected = hashlib.sha256(text.encode()).digest()
1065
1066 # One-shot: standalone function
1067 assert sz.sha256(text) == expected
1068
1069 # One-shot: Str method
1070 assert Str(text).sha256() == expected
1071
1072 # One-shot: bytes input
1073 assert sz.sha256(text.encode()) == expected
1074
1075 # Progressive: single update
1076 h = sz.Sha256()
1077 h.update(text)
1078 assert h.digest() == expected
1079 assert h.hexdigest() == expected.hex()
1080
1081 # Progressive: chunked updates
1082 if length > 0:
1083 h = sz.Sha256()
1084 chunk_size = max(1, length // 3)
1085 for i in range(0, length, chunk_size):
1086 h.update(text[i : i + chunk_size])
1087 assert h.digest() == expected
1088 assert h.hexdigest() == expected.hex()
1089
1090 # Reset
1091 h.reset().update(text)
1092 assert h.digest() == expected
1093
1094 # Copy
1095 mid = length // 2
1096 h1 = sz.Sha256().update(text[:mid])
1097 h2 = h1.copy()
1098 h1.update(text[mid:])
1099 h2.update(text[mid:])
1100 assert h1.digest() == h2.digest() == expected
1101
1102
1103@pytest.mark.parametrize("key_length", [0, 1, 16, 32, 64, 65, 128])

Callers

nothing calls this directly

Calls 6

updateMethod · 0.80
seed_random_generatorsFunction · 0.70
get_random_stringFunction · 0.70
digestMethod · 0.45
resetMethod · 0.45
copyMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…