|
612 | 612 | end |
613 | 613 | end |
614 | 614 |
|
| 615 | +@testset "Test PiecewiseLinearData <-> PiecewiseStepData constructor conversions" begin |
| 616 | + # Test basic conversion: PiecewiseLinearData -> PiecewiseStepData |
| 617 | + linear_data = IS.PiecewiseLinearData([(0.0, 0.0), (1.0, 2.0), (3.0, 5.0)]) |
| 618 | + step_data = IS.PiecewiseStepData(linear_data) |
| 619 | + @test IS.get_x_coords(step_data) == [0.0, 1.0, 3.0] |
| 620 | + @test IS.get_y_coords(step_data) ≈ [2.0, 1.5] # slopes |
| 621 | + |
| 622 | + # Test basic conversion: PiecewiseStepData -> PiecewiseLinearData with default initial_y |
| 623 | + step_data = IS.PiecewiseStepData([0.0, 1.0, 3.0], [2.0, 1.5]) |
| 624 | + linear_data = IS.PiecewiseLinearData(step_data) |
| 625 | + expected_points = [(x = 0.0, y = 0.0), (x = 1.0, y = 2.0), (x = 3.0, y = 5.0)] |
| 626 | + @test isapprox(collect.(IS.get_points(linear_data)), collect.(expected_points)) |
| 627 | + |
| 628 | + # Test conversion with custom initial_y |
| 629 | + step_data = IS.PiecewiseStepData([0.0, 1.0, 3.0], [2.0, 1.5]) |
| 630 | + linear_data = IS.PiecewiseLinearData(step_data, 10.0) |
| 631 | + expected_points = [(x = 0.0, y = 10.0), (x = 1.0, y = 12.0), (x = 3.0, y = 15.0)] |
| 632 | + @test isapprox(collect.(IS.get_points(linear_data)), collect.(expected_points)) |
| 633 | + |
| 634 | + # Test Base.convert methods |
| 635 | + linear_data = IS.PiecewiseLinearData([(1.0, 1.0), (2.0, 3.0), (4.0, 7.0)]) |
| 636 | + step_data = convert(IS.PiecewiseStepData, linear_data) |
| 637 | + @test step_data isa IS.PiecewiseStepData |
| 638 | + @test IS.get_x_coords(step_data) == [1.0, 2.0, 4.0] |
| 639 | + @test IS.get_y_coords(step_data) ≈ [2.0, 2.0] |
| 640 | + |
| 641 | + step_data = IS.PiecewiseStepData([1.0, 2.0, 4.0], [2.0, 2.0]) |
| 642 | + linear_data = convert(IS.PiecewiseLinearData, step_data) |
| 643 | + @test linear_data isa IS.PiecewiseLinearData |
| 644 | + expected_points = [(x = 1.0, y = 0.0), (x = 2.0, y = 2.0), (x = 4.0, y = 6.0)] |
| 645 | + @test isapprox(collect.(IS.get_points(linear_data)), collect.(expected_points)) |
| 646 | + |
| 647 | + # Test round-trip conversion preserves data (with initial_y) |
| 648 | + rng = Random.Xoshiro(48) |
| 649 | + n_tests = 100 |
| 650 | + n_points = 10 |
| 651 | + for _ in 1:n_tests |
| 652 | + rand_x = sort(rand(rng, n_points)) |
| 653 | + rand_y = rand(rng, n_points) |
| 654 | + original = IS.PiecewiseLinearData(collect(zip(rand_x, rand_y))) |
| 655 | + initial_y = first(IS.get_points(original)).y |
| 656 | + |
| 657 | + # Convert to step and back |
| 658 | + step_data = IS.PiecewiseStepData(original) |
| 659 | + recovered = IS.PiecewiseLinearData(step_data, initial_y) |
| 660 | + |
| 661 | + @test isapprox( |
| 662 | + collect.(IS.get_points(recovered)), collect.(IS.get_points(original))) |
| 663 | + end |
| 664 | + |
| 665 | + # Test that slopes are correctly computed for non-trivial case |
| 666 | + linear_data = IS.PiecewiseLinearData([ |
| 667 | + (0.0, 0.0), (1.0, 1.0), (2.0, 4.0), (3.0, 6.0), (5.0, 10.0) |
| 668 | + ]) |
| 669 | + step_data = IS.PiecewiseStepData(linear_data) |
| 670 | + expected_slopes = [1.0, 3.0, 2.0, 2.0] |
| 671 | + @test IS.get_y_coords(step_data) ≈ expected_slopes |
| 672 | + |
| 673 | + # Test conversion preserves domain |
| 674 | + linear_data = IS.PiecewiseLinearData([(2.0, 5.0), (4.0, 10.0), (8.0, 18.0)]) |
| 675 | + step_data = IS.PiecewiseStepData(linear_data) |
| 676 | + @test IS.get_domain(linear_data) == IS.get_domain(step_data) |
| 677 | +end |
| 678 | + |
615 | 679 | @testset "Test FunctionData serialization round trip" begin |
616 | 680 | for fd in get_test_function_data() |
617 | 681 | for do_jsonify in (false, true) |
|
0 commit comments