File size: 3,777 Bytes
a0ea4e3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | # Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md).
# All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
import random
import pytest
import isaaclab.utils.dict as dict_utils
import isaaclab.utils.string as string_utils
def _test_function(x):
"""Test function for string <-> callable conversion."""
return x**2
def _test_lambda_function(x):
"""Test function for string <-> callable conversion."""
return x**2
def test_print_dict():
"""Test printing of dictionary."""
# create a complex nested dictionary
test_dict = {
"a": 1,
"b": 2,
"c": {"d": 3, "e": 4, "f": {"g": 5, "h": 6}},
"i": 7,
"j": lambda x: x**2, # noqa: E731
"k": dict_utils.class_to_dict,
}
# print the dictionary
dict_utils.print_dict(test_dict)
def test_string_callable_function_conversion():
"""Test string <-> callable conversion for function."""
# convert function to string
test_string = dict_utils.callable_to_string(_test_function)
# convert string to function
test_function_2 = string_utils.string_to_callable(test_string)
# check that functions are the same
assert _test_function(2) == test_function_2(2)
def test_string_callable_function_with_lambda_in_name_conversion():
"""Test string <-> callable conversion for function which has lambda in its name."""
# convert function to string
test_string = dict_utils.callable_to_string(_test_lambda_function)
# convert string to function
test_function_2 = string_utils.string_to_callable(test_string)
# check that functions are the same
assert _test_function(2) == test_function_2(2)
def test_string_callable_lambda_conversion():
"""Test string <-> callable conversion for lambda expression."""
# create lambda function
func = lambda x: x**2 # noqa: E731
# convert function to string
test_string = dict_utils.callable_to_string(func)
# convert string to function
func_2 = string_utils.string_to_callable(test_string)
# check that functions are the same
assert test_string == "lambda x: x**2"
assert func(2) == func_2(2)
def test_dict_to_md5():
"""Test MD5 hash generation for dictionary."""
# create a complex nested dictionary
test_dict = {
"a": 1,
"b": 2,
"c": {"d": 3, "e": 4, "f": {"g": 5, "h": 6}},
"i": random.random(),
"k": dict_utils.callable_to_string(dict_utils.class_to_dict),
}
# generate the MD5 hash
md5_hash_1 = dict_utils.dict_to_md5_hash(test_dict)
# check that the hash is correct even after multiple calls
for _ in range(200):
md5_hash_2 = dict_utils.dict_to_md5_hash(test_dict)
assert md5_hash_1 == md5_hash_2
class _CallableCfg:
class_type = _test_function
def test_update_class_from_dict_keeps_callable_string_lazy():
"""Callable-string updates should remain lazy via ResolvableString."""
cfg = _CallableCfg()
dict_utils.update_class_from_dict(cfg, {"class_type": "math:sin"})
assert isinstance(cfg.class_type, string_utils.ResolvableString)
# Dunder probing should not force resolution/import side effects.
assert hasattr(cfg.class_type, "__dataclass_fields__") is False
# Runtime use still resolves correctly.
assert pytest.approx(cfg.class_type(0.0), rel=0.0, abs=1e-9) == 0.0
def test_update_class_from_dict_does_not_rewrap_resolvable_string():
"""Existing ResolvableString should be preserved, not re-wrapped."""
cfg = _CallableCfg()
existing = string_utils.ResolvableString("math:sin")
dict_utils.update_class_from_dict(cfg, {"class_type": existing})
assert cfg.class_type is existing
|