Download tests/utils/test_encoding_utils.py from paulagb/hackathon-dataset_caramelos: direct link, hf CLI and curl.
- Browser
- Download file 4.57 kB
-
https://huggingface.co/datasets/paulagb/hackathon-dataset_caramelos/resolve/main/tests/utils/test_encoding_utils.py
- Command line
-
hf download hf://datasets/paulagb/hackathon-dataset_caramelos/tests/utils/test_encoding_utils.py
-
curl -L -o test_encoding_utils.py https://huggingface.co/datasets/paulagb/hackathon-dataset_caramelos/resolve/main/tests/utils/test_encoding_utils.py
4.57 kB
| #!/usr/bin/env python | |
| # Copyright 2025 The HuggingFace Inc. team. All rights reserved. | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| import pytest | |
| from lerobot.motors.encoding_utils import ( | |
| decode_sign_magnitude, | |
| decode_twos_complement, | |
| encode_sign_magnitude, | |
| encode_twos_complement, | |
| ) | |
| def test_encode_sign_magnitude(value, sign_bit_index, expected): | |
| assert encode_sign_magnitude(value, sign_bit_index) == expected | |
| def test_decode_sign_magnitude(encoded, sign_bit_index, expected): | |
| assert decode_sign_magnitude(encoded, sign_bit_index) == expected | |
| def test_encode_raises_on_overflow(encoded, sign_bit_index): | |
| with pytest.raises(ValueError): | |
| encode_sign_magnitude(encoded, sign_bit_index) | |
| def test_encode_decode_sign_magnitude(): | |
| for sign_bit_index in range(2, 6): | |
| max_val = (1 << sign_bit_index) - 1 | |
| for value in range(-max_val, max_val + 1): | |
| encoded = encode_sign_magnitude(value, sign_bit_index) | |
| decoded = decode_sign_magnitude(encoded, sign_bit_index) | |
| assert decoded == value, f"Failed at value={value}, index={sign_bit_index}" | |
| def test_encode_twos_complement(value, n_bytes, expected): | |
| assert encode_twos_complement(value, n_bytes) == expected | |
| def test_decode_twos_complement(value, n_bytes, expected): | |
| assert decode_twos_complement(value, n_bytes) == expected | |
| def test_encode_twos_complement_out_of_range(value, n_bytes): | |
| with pytest.raises(ValueError): | |
| encode_twos_complement(value, n_bytes) | |
| def test_encode_decode_twos_complement(value, n_bytes): | |
| encoded = encode_twos_complement(value, n_bytes) | |
| decoded = decode_twos_complement(encoded, n_bytes) | |
| assert decoded == value, f"Failed at value={value}, n_bytes={n_bytes}" | |