From f0c3c99983ef1ac99d4ca42fae708a03c25c24b7 Mon Sep 17 00:00:00 2001 From: benjamc Date: Wed, 24 Jul 2024 10:17:00 +0200 Subject: [PATCH] Add test case for numpy encoder --- tests/test_utils/test_numpy_encoder.py | 39 ++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/test_utils/test_numpy_encoder.py diff --git a/tests/test_utils/test_numpy_encoder.py b/tests/test_utils/test_numpy_encoder.py new file mode 100644 index 000000000..7b9d0f20a --- /dev/null +++ b/tests/test_utils/test_numpy_encoder.py @@ -0,0 +1,39 @@ +from __future__ import annotations + +import json +import numpy as np +import pytest +from smac.utils.numpyencoder import NumpyEncoder + +# Test cases for NumpyEncoder +def test_numpy_encoder(): + data = { + "int": np.int32(1), + "float": np.float32(1.23), + "complex": np.complex64(1 + 2j), + "array": np.array([1, 2, 3]), + "bool": np.bool_(True), + "void": np.void(b'void') + } + + expected_output = { + "int": 1, + "float": 1.23, + "complex": {"real": 1.0, "imag": 2.0}, + "array": [1, 2, 3], + "bool": True, + "void": None + } + + encoded_data = json.dumps(data, cls=NumpyEncoder) + decoded_data = json.loads(encoded_data) + + assert np.isclose(decoded_data["float"], expected_output["float"]) # float ist not exactly the same + del decoded_data["float"] + del expected_output["float"] + assert decoded_data == expected_output + +# Test if default method raises TypeError for unsupported types +def test_numpy_encoder_unsupported_type(): + with pytest.raises(TypeError): + json.dumps(set([1, 2, 3]), cls=NumpyEncoder) \ No newline at end of file