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
This commit is contained in:
Bobby R. Bruce
2024-09-07 06:35:09 -07:00
committed by Erin Le
parent 00f927a4e2
commit f327559ca4
2 changed files with 27 additions and 3 deletions

View File

@@ -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}."
)

View File

@@ -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.