|
| 1 | +# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import hashlib |
| 16 | +import pickle |
| 17 | +import unittest |
| 18 | + |
| 19 | +import numpy as np |
| 20 | + |
| 21 | +from fastdeploy.multimodal.hasher import MultimodalHasher |
| 22 | + |
| 23 | + |
| 24 | +class TestHashFeatures(unittest.TestCase): |
| 25 | + def test_hash_features_ndarray(self): |
| 26 | + """Test hash features with numpy ndarray""" |
| 27 | + arr = np.random.randint(low=0, high=255, size=(28, 28), dtype=np.uint8) |
| 28 | + arr_hash = MultimodalHasher.hash_features(arr) |
| 29 | + target_hash = hashlib.sha256((arr.tobytes())).hexdigest() |
| 30 | + assert arr_hash == target_hash, f"Ndarray hash mismatch: {arr_hash} != {target_hash}" |
| 31 | + |
| 32 | + def test_hash_features_object(self): |
| 33 | + """Test hash features with unsupported object type""" |
| 34 | + obj = {"key": "value"} |
| 35 | + obj_hash = MultimodalHasher.hash_features(obj) |
| 36 | + target_hash = hashlib.sha256((pickle.dumps(obj))).hexdigest() |
| 37 | + assert obj_hash == target_hash, f"Dict hash mismatch: {obj_hash} != {target_hash}" |
| 38 | + |
| 39 | + obj = "test hasher str" |
| 40 | + obj_hash = MultimodalHasher.hash_features(obj) |
| 41 | + target_hash = hashlib.sha256((pickle.dumps(obj))).hexdigest() |
| 42 | + assert obj_hash == target_hash, f"Str hash mismatch: {obj_hash} != {target_hash}" |
| 43 | + |
| 44 | + |
| 45 | +if __name__ == "__main__": |
| 46 | + unittest.main() |
0 commit comments