|
1 | 1 | import os |
2 | 2 | import unittest |
| 3 | +from pathlib import Path |
| 4 | +from tempfile import TemporaryDirectory |
3 | 5 | from unittest.mock import Mock, patch |
4 | 6 |
|
5 | 7 | from kwave import PLATFORM |
@@ -31,6 +33,8 @@ def test_default_initialization(self): |
31 | 33 | self.assertEqual(options.verbose_level, 0) |
32 | 34 | self.assertTrue(options.auto_chunking) |
33 | 35 | self.assertTrue(options.show_sim_log) |
| 36 | + self.assertIsNone(options.checkpoint_interval) |
| 37 | + self.assertIsNone(options.checkpoint_file) |
34 | 38 |
|
35 | 39 | def test_num_threads_setter_valid(self): |
36 | 40 | """Test setting a valid number of threads.""" |
@@ -214,6 +218,113 @@ def test_list_compared_to_string(self): |
214 | 218 | options_string = options.get_options_string(self.mock_sensor) |
215 | 219 | self.assertEqual(" ".join(options_list), options_string) |
216 | 220 |
|
| 221 | + def test_as_list_with_valid_checkpoint_interval_options(self): |
| 222 | + """Test the checkpoint interval options.""" |
| 223 | + options = self.default_options |
| 224 | + options.num_threads = 1 |
| 225 | + options.checkpoint_interval = 10 |
| 226 | + options.checkpoint_file = "checkpoint.h5" |
| 227 | + |
| 228 | + options_list = options.as_list(self.mock_sensor) |
| 229 | + expected_elements = [ |
| 230 | + "--checkpoint_interval", |
| 231 | + "10", |
| 232 | + "--checkpoint_file", |
| 233 | + "checkpoint.h5", |
| 234 | + "--p_raw", |
| 235 | + "--u_max", |
| 236 | + "-s", |
| 237 | + "10", |
| 238 | + ] |
| 239 | + if not PLATFORM == "windows": |
| 240 | + expected_elements.insert(0, "-t") |
| 241 | + expected_elements.insert(1, f"1") |
| 242 | + self.assertListEqual(expected_elements, options_list) |
| 243 | + |
| 244 | + def test_as_list_with_valid_checkpoint_timesteps_options(self): |
| 245 | + """Test the checkpoint interval options.""" |
| 246 | + options = self.default_options |
| 247 | + options.num_threads = 1 |
| 248 | + options.checkpoint_timesteps = 10 |
| 249 | + options.checkpoint_file = "checkpoint.h5" |
| 250 | + |
| 251 | + options_list = options.as_list(self.mock_sensor) |
| 252 | + expected_elements = [ |
| 253 | + "--checkpoint_timesteps", |
| 254 | + "10", |
| 255 | + "--checkpoint_file", |
| 256 | + "checkpoint.h5", |
| 257 | + "--p_raw", |
| 258 | + "--u_max", |
| 259 | + "-s", |
| 260 | + "10", |
| 261 | + ] |
| 262 | + if not PLATFORM == "windows": |
| 263 | + expected_elements.insert(0, "-t") |
| 264 | + expected_elements.insert(1, f"1") |
| 265 | + self.assertListEqual(expected_elements, options_list) |
| 266 | + |
| 267 | + def test_initialization_with_invalid_checkpoint_options(self): |
| 268 | + """Test initialization with invalid checkpoint options.""" |
| 269 | + |
| 270 | + # Test with valid checkpoint_file but unset checkpoint_timesteps and checkpoint_interval |
| 271 | + with TemporaryDirectory() as temp_dir: |
| 272 | + checkpoint_file = Path(temp_dir) / "checkpoint.h5" |
| 273 | + with self.assertRaises(ValueError): |
| 274 | + SimulationExecutionOptions(checkpoint_file=checkpoint_file) |
| 275 | + |
| 276 | + # Test with invalid checkpoint_file type |
| 277 | + with self.assertRaises(ValueError): |
| 278 | + SimulationExecutionOptions(checkpoint_file=12345, checkpoint_timesteps=10) |
| 279 | + |
| 280 | + # Test with invalid checkpoint_timesteps |
| 281 | + with self.assertRaises(ValueError): |
| 282 | + SimulationExecutionOptions(checkpoint_timesteps=-1, checkpoint_file="checkpoint.h5") |
| 283 | + |
| 284 | + with self.assertRaises(ValueError): |
| 285 | + SimulationExecutionOptions(checkpoint_timesteps="not_an_integer", checkpoint_file="checkpoint.h5") |
| 286 | + |
| 287 | + # Test with invalid checkpoint_interval |
| 288 | + with self.assertRaises(ValueError): |
| 289 | + SimulationExecutionOptions(checkpoint_interval=-1, checkpoint_file="checkpoint.h5") |
| 290 | + |
| 291 | + with self.assertRaises(ValueError): |
| 292 | + SimulationExecutionOptions(checkpoint_interval="not_an_integer", checkpoint_file="checkpoint.h5") |
| 293 | + |
| 294 | + # Test with invalid checkpoint_file |
| 295 | + with self.assertRaises(ValueError): |
| 296 | + SimulationExecutionOptions( |
| 297 | + checkpoint_interval=10, |
| 298 | + checkpoint_file="checkpoint.txt", # Wrong extension |
| 299 | + ) |
| 300 | + |
| 301 | + with self.assertRaises(FileNotFoundError): |
| 302 | + SimulationExecutionOptions( |
| 303 | + checkpoint_interval=10, |
| 304 | + checkpoint_file="nonexistent_dir/checkpoint.h5", # Non-existent directory |
| 305 | + ) |
| 306 | + |
| 307 | + def test_initialization_with_valid_checkpoint_options(self): |
| 308 | + """Test initialization with valid checkpoint options.""" |
| 309 | + with TemporaryDirectory() as temp_dir: |
| 310 | + checkpoint_file = Path(temp_dir) / "checkpoint.h5" |
| 311 | + |
| 312 | + # Test with valid checkpoint_timesteps |
| 313 | + options = SimulationExecutionOptions(checkpoint_timesteps=10, checkpoint_file=checkpoint_file) |
| 314 | + self.assertEqual(options.checkpoint_timesteps, 10) |
| 315 | + self.assertEqual(options.checkpoint_file, checkpoint_file) |
| 316 | + |
| 317 | + # Test with valid checkpoint_interval |
| 318 | + options = SimulationExecutionOptions(checkpoint_interval=20, checkpoint_file=checkpoint_file) |
| 319 | + self.assertEqual(options.checkpoint_interval, 20) |
| 320 | + self.assertEqual(options.checkpoint_file, checkpoint_file) |
| 321 | + |
| 322 | + # Test with both checkpoint options - should be valid |
| 323 | + options = SimulationExecutionOptions(checkpoint_timesteps=10, checkpoint_interval=20, checkpoint_file=checkpoint_file) |
| 324 | + self.assertEqual(options.checkpoint_timesteps, 10) |
| 325 | + self.assertEqual(options.checkpoint_interval, 20) |
| 326 | + self.assertEqual(options.checkpoint_file, checkpoint_file) |
| 327 | + |
217 | 328 |
|
218 | 329 | if __name__ == "__main__": |
219 | 330 | unittest.main() |
0 commit comments