From f327559ca449ef10ad03df52b7fab288585894b2 Mon Sep 17 00:00:00 2001 From: "Bobby R. Bruce" Date: Sat, 7 Sep 2024 06:35:09 -0700 Subject: [PATCH] tests,stdlib,python: Add tests for base 10 to 2 SI unit check **Note**: Erin needs to complete the commit by expanding this test to properly test the behavior of this change. To run the pyunit tests: ```sh scons build/ALL/gem5.opt -j`nproc` ./build/ALL/gem5.opt tests/run_pyunit.py ``` Change-Id: I8cea0fe8b088e03e84072a000444953768bc3151 --- src/python/m5/util/convert.py | 22 +++++++++++++++++++--- tests/pyunit/util/pyunit_convert_check.py | 8 ++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/python/m5/util/convert.py b/src/python/m5/util/convert.py index a4bf7ae95e..4b7a047904 100644 --- a/src/python/m5/util/convert.py +++ b/src/python/m5/util/convert.py @@ -44,6 +44,8 @@ # from . import warn # metric prefixes +from typing import Optional + atto = 1.0e-18 femto = 1.0e-15 pico = 1.0e-12 @@ -299,15 +301,29 @@ def toMemoryBandwidth(value): return toBinaryFloat(value, "memory bandwidth", "B/s") +def _base_10_to_2(value: str) -> Optional[str]: + """Convert a base 10 memory/cache size SI prefix strings to base 2. Used + in `checkBaseConversion` to provide a warning message to the user. Will + return None if no conversion is required. + + + This function is intentionally separate from `checkBaseConversion` to aid + in testing.""" + size, prefix = _split_suffix_by_prefix(value) + if prefix in base_10_to_2.keys(): + return f"{size}{base_10_to_2[prefix]}" + return None + + def checkBaseConversion(value, unit): if type(value) is str: - size, prefix = _split_suffix_by_prefix(value) - if prefix in base_10_to_2.keys(): + new_value = _base_10_to_2(value) + if new_value: from m5.util import warn warn( f"Base 10 memory/cache size {value} will be cast to base 2" - + f" size {size}{base_10_to_2[prefix]}{unit}." + + f" size {new_value}{unit}." ) diff --git a/tests/pyunit/util/pyunit_convert_check.py b/tests/pyunit/util/pyunit_convert_check.py index 91b89e64ad..c01022f470 100644 --- a/tests/pyunit/util/pyunit_convert_check.py +++ b/tests/pyunit/util/pyunit_convert_check.py @@ -277,3 +277,11 @@ class ConvertTestSuite(unittest.TestCase): self.assertRaises(ValueError, conv, "-1K") self.assertEqual(conv("32F"), 273.15) + + def test_base_10_to_2(self): + conv = convert._base_10_to_2 + + self.assertEqual(conv("1k"), "1Ki") + self.assertIsNone(conv("1Ki")) + + # Leaving the rest of this test for Erin to finish.