diff --git a/tests/test_code_content_image.py b/tests/test_code_content_image.py index dc6e2e3..85bf467 100644 --- a/tests/test_code_content_image.py +++ b/tests/test_code_content_image.py @@ -91,6 +91,11 @@ def test_gen_image_code_schema_conformance(): assert iscc_obj.iscc == "ISCC:EEA4GQZQTY6J5DTH" +def test_soft_hash_image_v0_larger_256_raises(): + with pytest.raises(AssertionError): + ic.soft_hash_image_v0(IMG_SAMPLE_PIXELS, bits=288) + + IMG_WHITE_PIXELS = [255] * 1024 IMG_BLACK_PIXELS = [0] * 1024 IMG_SAMPLE_PIXELS = [ diff --git a/tests/test_iscc_id.py b/tests/test_iscc_id.py index a19adf5..a1bc16e 100644 --- a/tests/test_iscc_id.py +++ b/tests/test_iscc_id.py @@ -1,5 +1,8 @@ # -*- coding: utf-8 -*- import io + +import pytest + import iscc_core as ic @@ -62,3 +65,24 @@ def test_incr_iscc_id_v0(): assert ic.iscc_id.incr_iscc_id_v0("MAADB7WD7TC5XELQ") == "MAADB7WD7TC5XELQAE" assert ic.Code("MAADB7WD7TC5XELQAE").explain == "ID-PRIVATE-V0-64-30fec3fcc5db9170-1" assert ic.iscc_id.incr_iscc_id_v0("MAADB7WD7TC5XELQAE") == "MAADB7WD7TC5XELQAI" + + +def test_incr_iscc_id_v0_raises_wrong_mt(): + mc = ic.Code.rnd(ic.MT.META).code + with pytest.raises(AssertionError): + ic.incr_iscc_id_v0(mc) + + +def test_incr_iscc_id_v0_raises_wrong_vs(): + mc = ic.Code.rnd(ic.MT.ID) + head = list(mc._head) + head[2] = 1 + mc._head = tuple(head) + with pytest.raises(AssertionError): + ic.incr_iscc_id_v0(mc.code) + + +def test_gen_iscc_id_v0_raises_chain_id(): + code = ic.Code.rnd(ic.MT.ISCC, bits=256) + with pytest.raises(AssertionError): + ic.gen_iscc_id_v0(5, code.code) diff --git a/tests/test_minhash.py b/tests/test_minhash.py index c13f5af..9b88e38 100644 --- a/tests/test_minhash.py +++ b/tests/test_minhash.py @@ -9,7 +9,7 @@ def test_minhash_empty(): def test_minhash_single_feature(): - mh = ic.minhash([2 ** 16]) + mh = ic.minhash([2**16]) assert isinstance(mh, list) assert len(mh) == 64 assert mh[0] == 1968499307 @@ -17,14 +17,14 @@ def test_minhash_single_feature(): def test_minhash_32bit_features(): - i32 = 2 ** 32 - 1 - mh = ic.minhash([2 ** 64 - 1]) + i32 = 2**32 - 1 + mh = ic.minhash([2**64 - 1]) for n in mh: assert n <= i32 def test_minhash_compress(): - mh = ic.minhash([2 ** 16]) + mh = ic.minhash([2**16]) digest = ic.compress(mh) assert len(digest) == 32 assert digest.hex() == "a18e2fb2bd663d21db9c7dcc9ae78380253cae5bf089766d87a6b51fcb3f8f8e" @@ -35,5 +35,5 @@ def test_minhash_compress(): def test_minhash_64(): - mh = ic.minhash_64([2 ** 16]) + mh = ic.minhash_64([2**16]) assert mh.hex() == "a18e2fb2bd663d21" diff --git a/tests/test_simhash.py b/tests/test_simhash.py index 6422f33..7b59999 100644 --- a/tests/test_simhash.py +++ b/tests/test_simhash.py @@ -3,34 +3,34 @@ def test_similarity_hash(): - all_zero = 0b0 .to_bytes(8, "big") + all_zero = 0b0.to_bytes(8, "big") assert iscc_core.simhash.similarity_hash([all_zero, all_zero]) == all_zero - all_ones = 0b11111111 .to_bytes(1, "big") + all_ones = 0b11111111.to_bytes(1, "big") assert iscc_core.simhash.similarity_hash([all_ones, all_ones]) == all_ones - a = 0b0110 .to_bytes(1, "big") - b = 0b1100 .to_bytes(1, "big") - r = 0b1110 .to_bytes(1, "big") + a = 0b0110.to_bytes(1, "big") + b = 0b1100.to_bytes(1, "big") + r = 0b1110.to_bytes(1, "big") assert iscc_core.simhash.similarity_hash([a, b]) == r - a = 0b01101001 .to_bytes(1, "big") - b = 0b00111000 .to_bytes(1, "big") - c = 0b11100100 .to_bytes(1, "big") - r = 0b01101000 .to_bytes(1, "big") + a = 0b01101001.to_bytes(1, "big") + b = 0b00111000.to_bytes(1, "big") + c = 0b11100100.to_bytes(1, "big") + r = 0b01101000.to_bytes(1, "big") assert iscc_core.simhash.similarity_hash([a, b, c]) == r - a = 0b01100101 .to_bytes(1, "big") - b = 0b01011001 .to_bytes(1, "big") - c = 0b10010101 .to_bytes(1, "big") - d = 0b10101001 .to_bytes(1, "big") - r = 0b11111101 .to_bytes(1, "big") + a = 0b01100101.to_bytes(1, "big") + b = 0b01011001.to_bytes(1, "big") + c = 0b10010101.to_bytes(1, "big") + d = 0b10101001.to_bytes(1, "big") + r = 0b11111101.to_bytes(1, "big") assert iscc_core.simhash.similarity_hash([a, b, c, d]) == r - a = 0b0110100101101001 .to_bytes(2, "big") - b = 0b0011100000111000 .to_bytes(2, "big") - c = 0b1110010011100100 .to_bytes(2, "big") - r = 0b0110100001101000 .to_bytes(2, "big") + a = 0b0110100101101001.to_bytes(2, "big") + b = 0b0011100000111000.to_bytes(2, "big") + c = 0b1110010011100100.to_bytes(2, "big") + r = 0b0110100001101000.to_bytes(2, "big") assert iscc_core.simhash.similarity_hash([a, b, c]) == r diff --git a/tests/test_utils.py b/tests/test_utils.py index 588e201..db47ef7 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import io +import os import pytest import iscc_core as ic @@ -110,3 +111,14 @@ def test_ipfs_hash_raises(static_bytes): def test_canonicalize(): assert ic.canonicalize({"hello": "wörld"}) == b'{"hello":"w\xc3\xb6rld"}' + + +def test_sliding_window_raises(): + with pytest.raises(AssertionError): + ic.sliding_window([1, 2, 3, 4, 5], 1) + + +def test_hamming_distance_raises(): + a, b = os.urandom(8), os.urandom(9) + with pytest.raises(AssertionError): + ic.hamming_distance(a, b)