|
6 | 6 | import time |
7 | 7 | from numpy.testing import assert_allclose, assert_ |
8 | 8 | from quantecon.util import tic, tac, toc, loop_timer, Timer, timeit |
| 9 | +import quantecon as qe |
9 | 10 |
|
10 | 11 |
|
11 | 12 | class TestTicTacToc: |
@@ -291,3 +292,106 @@ def test_func(): |
291 | 292 | assert 'elapsed' in result2 |
292 | 293 | assert len(result2['elapsed']) == 2 |
293 | 294 |
|
| 295 | + |
| 296 | +class TestGlobalPrecision: |
| 297 | + """Test the new global precision control functionality.""" |
| 298 | + |
| 299 | + def setup_method(self): |
| 300 | + """Save original precision and restore after each test.""" |
| 301 | + self.original_precision = qe.timings.float_precision() |
| 302 | + |
| 303 | + def teardown_method(self): |
| 304 | + """Restore original precision after each test.""" |
| 305 | + qe.timings.float_precision(self.original_precision) |
| 306 | + |
| 307 | + def test_default_precision_is_4(self): |
| 308 | + """Test that default precision is now 4.""" |
| 309 | + # Reset to ensure we test the true default |
| 310 | + qe.timings.float_precision(4) |
| 311 | + assert qe.timings.float_precision() == 4 |
| 312 | + |
| 313 | + def test_float_precision_get_set(self): |
| 314 | + """Test getting and setting precision.""" |
| 315 | + # Test setting various precisions |
| 316 | + for precision in [0, 1, 2, 3, 4, 5, 6, 10]: |
| 317 | + qe.timings.float_precision(precision) |
| 318 | + assert qe.timings.float_precision() == precision |
| 319 | + |
| 320 | + def test_float_precision_validation(self): |
| 321 | + """Test that float_precision validates input.""" |
| 322 | + # Test invalid inputs |
| 323 | + try: |
| 324 | + qe.timings.float_precision(-1) |
| 325 | + assert False, "Should raise ValueError for negative precision" |
| 326 | + except ValueError as e: |
| 327 | + assert "non-negative integer" in str(e) |
| 328 | + |
| 329 | + try: |
| 330 | + qe.timings.float_precision("4") |
| 331 | + assert False, "Should raise ValueError for string input" |
| 332 | + except ValueError as e: |
| 333 | + assert "non-negative integer" in str(e) |
| 334 | + |
| 335 | + try: |
| 336 | + qe.timings.float_precision(4.5) |
| 337 | + assert False, "Should raise ValueError for float input" |
| 338 | + except ValueError as e: |
| 339 | + assert "non-negative integer" in str(e) |
| 340 | + |
| 341 | + def test_timer_uses_global_precision(self): |
| 342 | + """Test that Timer class uses global precision by default.""" |
| 343 | + # Set global precision |
| 344 | + qe.timings.float_precision(6) |
| 345 | + |
| 346 | + # Create timer without explicit precision |
| 347 | + timer = Timer(verbose=False) |
| 348 | + assert timer.precision == 6 |
| 349 | + |
| 350 | + # Test with different global precision |
| 351 | + qe.timings.float_precision(2) |
| 352 | + timer2 = Timer(verbose=False) |
| 353 | + assert timer2.precision == 2 |
| 354 | + |
| 355 | + def test_timer_explicit_precision_overrides_global(self): |
| 356 | + """Test that explicit precision overrides global setting.""" |
| 357 | + qe.timings.float_precision(6) |
| 358 | + |
| 359 | + # Explicit precision should override global |
| 360 | + timer = Timer(precision=3, verbose=False) |
| 361 | + assert timer.precision == 3 |
| 362 | + |
| 363 | + def test_tac_toc_keep_original_defaults(self): |
| 364 | + """Test that tac/toc functions maintain original default (digits=2).""" |
| 365 | + # These functions are deprecated and should maintain original behavior |
| 366 | + tic() |
| 367 | + time.sleep(0.01) |
| 368 | + |
| 369 | + # These should use digits=2 by default, not global precision |
| 370 | + result_tac = tac(verbose=False) # Uses default digits=2 |
| 371 | + result_toc = toc(verbose=False) # Uses default digits=2 |
| 372 | + |
| 373 | + # Just verify they work without error |
| 374 | + assert result_tac > 0 |
| 375 | + assert result_toc > 0 |
| 376 | + |
| 377 | + def test_loop_timer_keeps_original_default(self): |
| 378 | + """Test that loop_timer maintains original default (digits=2).""" |
| 379 | + def test_func(): |
| 380 | + time.sleep(0.001) |
| 381 | + |
| 382 | + # Should use digits=2 by default, not global precision |
| 383 | + result = loop_timer(2, test_func, verbose=False) |
| 384 | + assert len(result) == 2 # Returns (average_time, average_of_best) |
| 385 | + |
| 386 | + def test_timeit_uses_global_precision(self): |
| 387 | + """Test that timeit function uses global precision by default.""" |
| 388 | + def test_func(): |
| 389 | + time.sleep(0.001) |
| 390 | + |
| 391 | + qe.timings.float_precision(6) |
| 392 | + |
| 393 | + # Should use global precision without error |
| 394 | + result = timeit(test_func, runs=2, verbose=False, results=True) |
| 395 | + assert 'elapsed' in result |
| 396 | + assert len(result['elapsed']) == 2 |
| 397 | + |
0 commit comments