Skip to content

Commit

Permalink
test list equality
Browse files Browse the repository at this point in the history
  • Loading branch information
waltsims committed Dec 15, 2024
1 parent 493021c commit 0205e08
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
2 changes: 1 addition & 1 deletion kwave/options/simulation_execution_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def as_list(self, sensor: kSensor) -> list[str]:
}

if sensor.record is not None:
matching_keys = set(sensor.record).intersection(record_options_map.keys())
matching_keys = sorted(set(sensor.record).intersection(record_options_map.keys()))
options_list.extend([f"--{record_options_map[key]}" for key in matching_keys])

if "u_non_staggered" in sensor.record or "I_avg" in sensor.record or "I" in sensor.record:
Expand Down
59 changes: 28 additions & 31 deletions tests/test_simulation_execution_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,98 +96,94 @@ def test_get_options_string_darwin(self):
"""Test the get_options_string method with a mock sensor."""
options = self.default_options
options.device_num = 1
options.num_threads = os.cpu_count()
options.num_threads = 1
options.verbose_level = 2

options_list = options.as_list(self.mock_sensor)
expected_elements = [
"-g",
f"{options.device_num}",
"1",
"-t",
f"{os.cpu_count()}",
"1",
"--verbose",
"2",
"--p_raw",
"--u_max",
"-s",
f"{self.mock_sensor.record_start_index}" # Updated to use self.mock_sensor
]
for element in expected_elements:
self.assertIn(element, options_list)
self.assertListEqual(expected_elements, options_list)

@patch("kwave.options.simulation_execution_options.PLATFORM", "windows")
def test_as_list_windows(self):
"""Test the list representation of options on Windows."""
options = self.default_options
options.device_num = 1
options.num_threads = os.cpu_count()
options.num_threads = 1
options.verbose_level = 2

options_list = options.as_list(self.mock_sensor)
expected_elements = [
"-g",
f"{options.device_num}",
"1",
"--verbose",
"2",
"--p_raw",
"--u_max",
"-s",
f"{self.mock_sensor.record_start_index}"
]
for element in expected_elements:
self.assertIn(element, options_list)
self.assertNotIn("-t", options_list)
self.assertNotIn(f"{os.cpu_count()}", options_list)
self.assertListEqual(expected_elements, options_list)

@patch("kwave.options.simulation_execution_options.PLATFORM", "darwin")
def test_as_list_darwin(self):
"""Test the list representation of options on macOS."""
options = self.default_options
options.device_num = 1
options.num_threads = os.cpu_count()
options.num_threads = 1
options.verbose_level = 2

options_list = options.as_list(self.mock_sensor)
expected_elements = [
"-g",
f"{options.device_num}",
"-t",
f"{os.cpu_count()}",
f"1",
"--verbose",
"2",
"--p_raw",
"--u_max",
"-s",
f"{self.mock_sensor.record_start_index}" # Updated to use self.mock_sensor
]
for element in expected_elements:
self.assertIn(element, options_list)

self.assertListEqual(expected_elements, options_list)

def test_as_list_custom_record(self):
"""Test the list representation with a custom record configuration."""
options = self.default_options
self.mock_sensor.record = ["p_max", "u_min", "I_avg"]
options.device_num = 2
options.num_threads = os.cpu_count()
options.device_num = 1
options.num_threads = 1
options.verbose_level = 1

options_list = options.as_list(self.mock_sensor)
expected_elements = [
"-g",
f"{options.device_num}",

"1",
"--verbose",
"1",
"--p_max",
"--u_min",
"--p_raw" # Default if no specific 'p' or 'u' options are given
'--u_non_staggered_raw',
"--p_raw",
'-s',
'10',
]
if not PLATFORM == "windows":
expected_elements.append("-t")
expected_elements.append(f"{os.cpu_count()}")

for element in expected_elements:
self.assertIn(element, options_list)
expected_elements.insert(2,"-t")
expected_elements.insert(3,f"1")
self.assertListEqual(expected_elements, options_list)

def test_as_list_with_invalid_values(self):
"""Test the behavior of as_list when there are invalid values."""
Expand All @@ -201,22 +197,23 @@ def test_as_list_no_record(self):
options = self.default_options
self.mock_sensor.record = None
options.device_num = 1
options.num_threads = os.cpu_count()
options.num_threads = 1
options.verbose_level = 0

options_list = options.as_list(self.mock_sensor)
expected_elements = [
"-g",
f"{options.device_num}",

"--p_raw", # Default value
"-s", # start timestep index
"10"
]

if not PLATFORM == "windows":
expected_elements.append("-t")
expected_elements.append(f"{os.cpu_count()}")
for element in expected_elements:
self.assertIn(element, options_list)
expected_elements.insert(2, "-t")
expected_elements.insert(3, "1")
self.assertListEqual(expected_elements, options_list)


def test_list_compared_to_string(self):
"""Test the list representation compared to the string representation."""
Expand Down

0 comments on commit 0205e08

Please sign in to comment.