lp_parser_rs 3.4.1

A Rust parser for the LP file format.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
use super::state::{extract_mps_name, parse_mps};
use super::writer::write_mps_string;
use crate::lexer::RawConstraint;
use crate::model::{ComparisonOp, Sense, VariableType};
use crate::problem::LpProblem;

#[test]
fn test_minimal_mps() {
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        2
RHS
    RHS_V     c1        10
ENDATA
";
    let result = parse_mps(input).unwrap();
    assert_eq!(result.sense, Sense::Minimize);
    assert_eq!(result.objectives.len(), 1);
    assert_eq!(result.objectives[0].coefficients.len(), 1);
    assert_eq!(result.objectives[0].coefficients[0].name, "x1");
    assert_eq!(result.objectives[0].coefficients[0].value, 1.0);
    assert_eq!(result.constraints.len(), 1);
    if let RawConstraint::Standard { name, operator, rhs, .. } = &result.constraints[0] {
        assert_eq!(name.as_ref(), "c1");
        assert_eq!(*operator, ComparisonOp::LTE);
        assert_eq!(*rhs, 10.0);
    } else {
        panic!("Expected Standard constraint");
    }
}

#[test]
fn test_objsense_max() {
    let input = "\
NAME        test
OBJSENSE
  MAX
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        1
RHS
    RHS_V     c1        5
ENDATA
";
    let result = parse_mps(input).unwrap();
    assert_eq!(result.sense, Sense::Maximize);
}

#[test]
fn test_integer_markers() {
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
COLUMNS
    MARK0000  'MARKER'                 'INTORG'
    x1        obj       1
    x1        c1        2
    MARK0001  'MARKER'                 'INTEND'
    x2        obj       3
    x2        c1        4
ENDATA
";
    let result = parse_mps(input).unwrap();
    assert!(result.integers.contains(&"x1"));
    assert!(!result.integers.contains(&"x2"));
}

#[test]
fn test_bound_types() {
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        1
    x2        obj       1
    x2        c1        1
    x3        obj       1
    x3        c1        1
    x4        obj       1
    x4        c1        1
RHS
    RHS_V     c1        10
BOUNDS
 FR BOUND     x1
 LO BOUND     x2        5
 UP BOUND     x2        15
 BV BOUND     x3
 FX BOUND     x4        7
ENDATA
";
    let result = parse_mps(input).unwrap();

    // x1 = Free
    assert!(result.bounds.iter().any(|(n, t)| *n == "x1" && *t == VariableType::Free));
    // x2 = DoubleBound(5, 15)
    assert!(result.bounds.iter().any(|(n, t)| *n == "x2" && *t == VariableType::DoubleBound(5.0, 15.0)));
    // x3 = Binary
    assert!(result.bounds.iter().any(|(n, t)| *n == "x3" && *t == VariableType::Binary));
    assert!(result.binaries.contains(&"x3"));
    // x4 = Fixed = DoubleBound(7, 7)
    assert!(result.bounds.iter().any(|(n, t)| *n == "x4" && *t == VariableType::DoubleBound(7.0, 7.0)));
}

#[test]
fn test_multiple_constraint_types() {
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
 G  c2
 E  c3
COLUMNS
    x1        obj       1
    x1        c1        1
    x1        c2        2
    x1        c3        3
RHS
    RHS_V     c1        10
    RHS_V     c2        5
    RHS_V     c3        7
ENDATA
";
    let result = parse_mps(input).unwrap();
    assert_eq!(result.constraints.len(), 3);

    let ops: Vec<ComparisonOp> = result
        .constraints
        .iter()
        .filter_map(|c| if let RawConstraint::Standard { operator, .. } = c { Some(*operator) } else { None })
        .collect();
    assert_eq!(ops, vec![ComparisonOp::LTE, ComparisonOp::GTE, ComparisonOp::EQ]);
}

#[test]
fn test_missing_rows_section() {
    let input = "\
NAME        test
COLUMNS
    x1        obj       1
ENDATA
";
    let result = parse_mps(input);
    assert!(result.is_err());
}

#[test]
fn test_missing_columns_section() {
    let input = "\
NAME        test
ROWS
 N  obj
ENDATA
";
    let result = parse_mps(input);
    assert!(result.is_err());
}

#[test]
fn test_two_entries_per_line() {
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
 L  c2
COLUMNS
    x1        c1        1          c2        2
RHS
    RHS_V     c1        10         c2        20
ENDATA
";
    let result = parse_mps(input).unwrap();
    assert_eq!(result.constraints.len(), 2);

    if let RawConstraint::Standard { rhs, .. } = &result.constraints[0] {
        assert_eq!(*rhs, 10.0);
    }
    if let RawConstraint::Standard { rhs, .. } = &result.constraints[1] {
        assert_eq!(*rhs, 20.0);
    }
}

#[test]
fn test_extract_mps_name() {
    let input = "NAME        my_problem\nROWS\n N  obj\n";
    assert_eq!(extract_mps_name(input), Some("my_problem".to_string()));
}

#[test]
fn test_comment_lines_skipped() {
    let input = "\
* This is a comment
NAME        test
* Another comment
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        1
ENDATA
";
    let result = parse_mps(input).unwrap();
    assert_eq!(result.objectives.len(), 1);
}

#[test]
fn test_blank_lines_skipped() {
    let input = "\
NAME        test

ROWS
 N  obj
 L  c1

COLUMNS
    x1        obj       1
    x1        c1        1

ENDATA
";
    let result = parse_mps(input).unwrap();
    assert_eq!(result.constraints.len(), 1);
}

#[test]
fn test_semi_continuous_bounds() {
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        1
    x2        obj       1
    x2        c1        1
BOUNDS
 SC BOUND     x1        100
 SI BOUND     x2        200
ENDATA
";
    let result = parse_mps(input).unwrap();
    assert!(result.semi_continuous.contains(&"x1"));
    assert!(result.semi_continuous.contains(&"x2"));
    assert!(!result.integers.contains(&"x1"));
    assert!(result.integers.contains(&"x2"));
}

#[test]
fn test_default_rhs_zero() {
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        1
ENDATA
";
    let result = parse_mps(input).unwrap();
    if let RawConstraint::Standard { rhs, .. } = &result.constraints[0] {
        assert_eq!(*rhs, 0.0);
    }
}

// --- New spec-compliance tests ---

#[test]
fn test_default_bounds_zero_to_inf() {
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        1
    x2        obj       2
    x2        c1        3
ENDATA
";
    let result = parse_mps(input).unwrap();

    // Variables with no BOUNDS entry should default to LowerBound(0.0)
    assert!(
        result.bounds.iter().any(|(n, t)| *n == "x1" && *t == VariableType::LowerBound(0.0)),
        "x1 should have default LowerBound(0.0), got: {:?}",
        result.bounds.iter().find(|(n, _)| *n == "x1")
    );
    assert!(
        result.bounds.iter().any(|(n, t)| *n == "x2" && *t == VariableType::LowerBound(0.0)),
        "x2 should have default LowerBound(0.0), got: {:?}",
        result.bounds.iter().find(|(n, _)| *n == "x2")
    );
}

#[test]
fn test_integer_default_bounds_zero_to_one() {
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
COLUMNS
    MARK0000  'MARKER'                 'INTORG'
    y1        obj       1
    y1        c1        2
    MARK0001  'MARKER'                 'INTEND'
ENDATA
";
    let result = parse_mps(input).unwrap();

    // INTORG/INTEND variable with no BOUNDS should default to [0, 1]
    assert!(
        result.bounds.iter().any(|(n, t)| *n == "y1" && *t == VariableType::DoubleBound(0.0, 1.0)),
        "y1 should have default DoubleBound(0.0, 1.0), got: {:?}",
        result.bounds.iter().find(|(n, _)| *n == "y1")
    );
}

#[test]
fn test_negative_upper_implies_mi() {
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        1
BOUNDS
 UP BOUND     x1        -5
ENDATA
";
    let result = parse_mps(input).unwrap();

    // UP < 0 with no LO should produce DoubleBound(-inf, -5)
    assert!(
        result.bounds.iter().any(|(n, t)| *n == "x1" && *t == VariableType::DoubleBound(f64::NEG_INFINITY, -5.0)),
        "x1 should have DoubleBound(-inf, -5.0), got: {:?}",
        result.bounds.iter().find(|(n, _)| *n == "x1")
    );
}

#[test]
fn test_dollar_inline_comment() {
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1          $ this is a comment
    x1        c1        2
RHS
    RHS_V     c1        10         $ comment here too
ENDATA
";
    let result = parse_mps(input).unwrap();
    assert_eq!(result.objectives[0].coefficients.len(), 1);
    assert_eq!(result.objectives[0].coefficients[0].value, 1.0);
    assert_eq!(result.constraints.len(), 1);
    if let RawConstraint::Standard { rhs, .. } = &result.constraints[0] {
        assert_eq!(*rhs, 10.0);
    }
}

#[test]
fn test_multiple_n_rows() {
    let input = "\
NAME        test
ROWS
 N  obj1
 N  obj2
 L  c1
COLUMNS
    x1        obj1      1
    x1        obj2      2
    x1        c1        3
RHS
    RHS_V     c1        10
ENDATA
";
    let result = parse_mps(input).unwrap();

    // Two N-rows should produce two objectives
    assert_eq!(result.objectives.len(), 2);
    assert_eq!(result.objectives[0].name.as_ref(), "obj1");
    assert_eq!(result.objectives[0].coefficients[0].value, 1.0);
    assert_eq!(result.objectives[1].name.as_ref(), "obj2");
    assert_eq!(result.objectives[1].coefficients[0].value, 2.0);
}

#[test]
fn test_ranges_section_g_row() {
    // G row with range r: lower = rhs, upper = rhs + |r|
    let input = "\
NAME        test
ROWS
 N  obj
 G  c1
COLUMNS
    x1        obj       1
    x1        c1        1
RHS
    RHS_V     c1        5
RANGES
    RNG_V     c1        10
ENDATA
";
    let result = parse_mps(input).unwrap();

    // Should expand into two constraints
    assert_eq!(result.constraints.len(), 2);

    // First: c1 >= 5 (the lower bound)
    if let RawConstraint::Standard { name, operator, rhs, .. } = &result.constraints[0] {
        assert_eq!(name.as_ref(), "c1");
        assert_eq!(*operator, ComparisonOp::GTE);
        assert_eq!(*rhs, 5.0);
    } else {
        panic!("Expected Standard constraint");
    }

    // Second: c1_rng <= 15 (the upper bound = rhs + |range|)
    if let RawConstraint::Standard { name, operator, rhs, .. } = &result.constraints[1] {
        assert_eq!(name.as_ref(), "c1_rng");
        assert_eq!(*operator, ComparisonOp::LTE);
        assert_eq!(*rhs, 15.0);
    } else {
        panic!("Expected Standard constraint");
    }
}

#[test]
fn test_ranges_section_l_row() {
    // L row with range r: lower = rhs - |r|, upper = rhs
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        1
RHS
    RHS_V     c1        20
RANGES
    RNG_V     c1        8
ENDATA
";
    let result = parse_mps(input).unwrap();

    assert_eq!(result.constraints.len(), 2);

    // First: c1 >= 12 (lower = rhs - |range|)
    if let RawConstraint::Standard { name, operator, rhs, .. } = &result.constraints[0] {
        assert_eq!(name.as_ref(), "c1");
        assert_eq!(*operator, ComparisonOp::GTE);
        assert_eq!(*rhs, 12.0);
    } else {
        panic!("Expected Standard constraint");
    }

    // Second: c1_rng <= 20 (upper = rhs)
    if let RawConstraint::Standard { name, operator, rhs, .. } = &result.constraints[1] {
        assert_eq!(name.as_ref(), "c1_rng");
        assert_eq!(*operator, ComparisonOp::LTE);
        assert_eq!(*rhs, 20.0);
    } else {
        panic!("Expected Standard constraint");
    }
}

#[test]
fn test_ranges_section_e_row() {
    // E row, positive range: lower = rhs, upper = rhs + range
    let input = "\
NAME        test
ROWS
 N  obj
 E  c1
 E  c2
COLUMNS
    x1        obj       1
    x1        c1        1
    x1        c2        1
RHS
    RHS_V     c1        10
    RHS_V     c2        10
RANGES
    RNG_V     c1        5
    RNG_V     c2        -3
ENDATA
";
    let result = parse_mps(input).unwrap();

    // 2 rows * 2 constraints each = 4 constraints
    assert_eq!(result.constraints.len(), 4);

    // c1 (positive range 5): lower=10, upper=15
    if let RawConstraint::Standard { name, operator, rhs, .. } = &result.constraints[0] {
        assert_eq!(name.as_ref(), "c1");
        assert_eq!(*operator, ComparisonOp::GTE);
        assert_eq!(*rhs, 10.0);
    }
    if let RawConstraint::Standard { name, operator, rhs, .. } = &result.constraints[1] {
        assert_eq!(name.as_ref(), "c1_rng");
        assert_eq!(*operator, ComparisonOp::LTE);
        assert_eq!(*rhs, 15.0);
    }

    // c2 (negative range -3): lower=7 (rhs+range), upper=10 (rhs)
    if let RawConstraint::Standard { name, operator, rhs, .. } = &result.constraints[2] {
        assert_eq!(name.as_ref(), "c2");
        assert_eq!(*operator, ComparisonOp::GTE);
        assert_eq!(*rhs, 7.0);
    }
    if let RawConstraint::Standard { name, operator, rhs, .. } = &result.constraints[3] {
        assert_eq!(name.as_ref(), "c2_rng");
        assert_eq!(*operator, ComparisonOp::LTE);
        assert_eq!(*rhs, 10.0);
    }
}

#[test]
fn test_objective_rhs_no_crash() {
    // RHS on N-row should not crash -- it logs a warning but is otherwise ignored
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        1
RHS
    RHS_V     obj       42
    RHS_V     c1        10
ENDATA
";
    let result = parse_mps(input).unwrap();
    assert_eq!(result.objectives.len(), 1);
    assert_eq!(result.constraints.len(), 1);
}

#[test]
fn test_explicit_bounds_override_default() {
    // Variables with explicit bounds should not get default [0, +inf]
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        1
    x2        obj       2
    x2        c1        3
BOUNDS
 FR BOUND     x1
ENDATA
";
    let result = parse_mps(input).unwrap();

    // x1 has explicit FR bound
    assert!(result.bounds.iter().any(|(n, t)| *n == "x1" && *t == VariableType::Free));
    // x2 has no explicit bounds -- should get default LowerBound(0.0)
    assert!(result.bounds.iter().any(|(n, t)| *n == "x2" && *t == VariableType::LowerBound(0.0)));
}

#[test]
fn test_negative_upper_with_explicit_lower() {
    // UP < 0 WITH explicit LO should NOT override to -inf
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        1
BOUNDS
 LO BOUND     x1        -10
 UP BOUND     x1        -5
ENDATA
";
    let result = parse_mps(input).unwrap();

    assert!(
        result.bounds.iter().any(|(n, t)| *n == "x1" && *t == VariableType::DoubleBound(-10.0, -5.0)),
        "x1 should have DoubleBound(-10.0, -5.0), got: {:?}",
        result.bounds.iter().find(|(n, _)| *n == "x1")
    );
}

#[test]
fn test_dollar_comment_in_bounds() {
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        1
BOUNDS
 LO BOUND     x1        5    $ lower bound comment
 UP BOUND     x1        15   $ upper bound comment
ENDATA
";
    let result = parse_mps(input).unwrap();
    assert!(result.bounds.iter().any(|(n, t)| *n == "x1" && *t == VariableType::DoubleBound(5.0, 15.0)));
}

#[test]
fn test_first_rhs_vector_only() {
    // Two RHS vectors -- only the first (RHS1) should be used
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
 L  c2
COLUMNS
    x1        obj       1
    x1        c1        1
    x1        c2        1
RHS
    RHS1      c1        10
    RHS1      c2        20
    RHS2      c1        99
    RHS2      c2        99
ENDATA
";
    let result = parse_mps(input).unwrap();

    // c1 should have RHS=10 (from RHS1), not 99 (from RHS2)
    if let RawConstraint::Standard { name, rhs, .. } = &result.constraints[0] {
        assert_eq!(name.as_ref(), "c1");
        assert_eq!(*rhs, 10.0);
    } else {
        panic!("Expected Standard constraint");
    }

    // c2 should have RHS=20 (from RHS1), not 99 (from RHS2)
    if let RawConstraint::Standard { name, rhs, .. } = &result.constraints[1] {
        assert_eq!(name.as_ref(), "c2");
        assert_eq!(*rhs, 20.0);
    } else {
        panic!("Expected Standard constraint");
    }
}

#[test]
fn test_first_bounds_vector_only() {
    // Two BOUNDS vectors -- only the first (BND1) should be used
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        1
BOUNDS
 UP BND1      x1        10
 UP BND2      x1        99
ENDATA
";
    let result = parse_mps(input).unwrap();

    // x1 should have UP=10 from BND1, not 99 from BND2
    assert!(
        result.bounds.iter().any(|(n, t)| *n == "x1" && *t == VariableType::UpperBound(10.0)),
        "x1 should have UpperBound(10.0), got: {:?}",
        result.bounds.iter().find(|(n, _)| *n == "x1")
    );
}

#[test]
fn test_first_ranges_vector_only() {
    // Two RANGES vectors -- only the first (RNG1) should be used
    let input = "\
NAME        test
ROWS
 N  obj
 G  c1
COLUMNS
    x1        obj       1
    x1        c1        1
RHS
    RHS_V     c1        5
RANGES
    RNG1      c1        10
    RNG2      c1        99
ENDATA
";
    let result = parse_mps(input).unwrap();

    // Range should expand using RNG1 value (10), not RNG2 (99)
    assert_eq!(result.constraints.len(), 2);
    if let RawConstraint::Standard { rhs, .. } = &result.constraints[1] {
        // upper = rhs + |range| = 5 + 10 = 15
        assert_eq!(*rhs, 15.0);
    } else {
        panic!("Expected Standard constraint");
    }
}

#[test]
fn test_duplicate_lower_bound_rejected() {
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        1
BOUNDS
 LO BOUND     x1        5
 LO BOUND     x1        10
ENDATA
";
    let result = parse_mps(input);
    assert!(result.is_err(), "Duplicate LO bound should be rejected");
    let err = result.unwrap_err().to_string();
    assert!(err.contains("duplicate lower bound"), "Error should mention duplicate: {err}");
}

#[test]
fn test_duplicate_upper_bound_rejected() {
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        1
BOUNDS
 UP BOUND     x1        10
 UP BOUND     x1        20
ENDATA
";
    let result = parse_mps(input);
    assert!(result.is_err(), "Duplicate UP bound should be rejected");
    let err = result.unwrap_err().to_string();
    assert!(err.contains("duplicate upper bound"), "Error should mention duplicate: {err}");
}

#[test]
fn test_duplicate_fixed_bound_rejected() {
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        1
BOUNDS
 FX BOUND     x1        5
 FX BOUND     x1        10
ENDATA
";
    let result = parse_mps(input);
    assert!(result.is_err(), "Duplicate FX bound should be rejected");
}

#[test]
fn test_n_row_extra_fields_accepted() {
    // Gurobi-style N-row with priority/weight/tolerance fields
    let input = "\
NAME        test
ROWS
 N  OBJ0 2 1 0 0
 L  c1
COLUMNS
    x1        OBJ0      1
    x1        c1        1
ENDATA
";
    let result = parse_mps(input).unwrap();
    assert_eq!(result.objectives.len(), 1);
    // The extra fields are ignored but parsing succeeds
    assert_eq!(result.objectives[0].name.as_ref(), "OBJ0");
}

#[test]
fn test_unsupported_section_skipped() {
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        1
QUADOBJ
    x1  x1  2.0
RHS
    RHS_V     c1        10
ENDATA
";
    let result = parse_mps(input).unwrap();
    assert_eq!(result.objectives.len(), 1);
    assert_eq!(result.constraints.len(), 1);
}

#[test]
fn test_enlight4_all_variables_integer() {
    use std::path::PathBuf;

    let mut file_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    file_path.push("resources/enlight4.mps");
    let input = std::fs::read_to_string(&file_path).expect("failed to read enlight4.mps");

    let result = parse_mps(&input).unwrap();

    // All variables in enlight4 are between INTORG/INTEND markers
    debug_assert!(!result.integers.is_empty(), "enlight4 should have integer variables");
    let all_column_names: Vec<&str> = result
        .objectives
        .iter()
        .flat_map(|o| o.coefficients.iter().map(|c| c.name))
        .collect::<std::collections::HashSet<_>>()
        .into_iter()
        .collect();

    for var_name in &all_column_names {
        assert!(result.integers.contains(var_name), "Variable '{var_name}' should be integer (between INTORG/INTEND markers)");
    }
}

#[test]
fn test_multiple_unsupported_sections_skipped() {
    let input = "\
NAME        test
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        1
INDICATORS
    IF c1 x1 1
PWLOBJ
    x1 3 0.0 0.0 1.0 1.0 2.0 3.0
GENCONS
    gc0: MIN x1 x1 x1
SCENARIOS
    scenario1
RHS
    RHS_V     c1        10
ENDATA
";
    let result = parse_mps(input).unwrap();
    assert_eq!(result.constraints.len(), 1);
}

// --- Round-trip tests: parse MPS → build LpProblem → write MPS → re-parse → compare ---

/// Helper: parse MPS input, write back, re-parse, and assert structural parity.
fn assert_mps_round_trip(input: &str) {
    let original = LpProblem::parse_mps(input).unwrap_or_else(|e| panic!("Failed to parse original MPS: {e}"));

    let written = write_mps_string(&original).unwrap_or_else(|e| panic!("Failed to write MPS: {e}"));

    let round_tripped =
        LpProblem::parse_mps(&written).unwrap_or_else(|e| panic!("Failed to re-parse written MPS: {e}\n\nWritten MPS:\n{written}"));

    assert_eq!(
        original.sense, round_tripped.sense,
        "Sense mismatch: original={:?}, round-tripped={:?}",
        original.sense, round_tripped.sense
    );
    assert_eq!(
        original.objective_count(),
        round_tripped.objective_count(),
        "Objective count mismatch: original={}, round-tripped={}",
        original.objective_count(),
        round_tripped.objective_count()
    );
    assert_eq!(
        original.constraint_count(),
        round_tripped.constraint_count(),
        "Constraint count mismatch: original={}, round-tripped={}",
        original.constraint_count(),
        round_tripped.constraint_count()
    );
    assert_eq!(
        original.variable_count(),
        round_tripped.variable_count(),
        "Variable count mismatch: original={}, round-tripped={}",
        original.variable_count(),
        round_tripped.variable_count()
    );
}

#[test]
fn test_mps_round_trip_basic() {
    let input = "\
NAME        basic
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        2
RHS
    RHS_V     c1        10
ENDATA
";
    assert_mps_round_trip(input);
}

#[test]
fn test_mps_round_trip_maximize() {
    let input = "\
NAME        maxtest
OBJSENSE
  MAX
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       3
    x1        c1        1
RHS
    RHS_V     c1        5
ENDATA
";
    assert_mps_round_trip(input);
}

#[test]
fn test_mps_round_trip_integer_markers() {
    let input = "\
NAME        inttest
ROWS
 N  obj
 L  c1
COLUMNS
    MARK0000  'MARKER'                 'INTORG'
    x1        obj       1
    x1        c1        2
    x2        obj       3
    x2        c1        4
    MARK0001  'MARKER'                 'INTEND'
ENDATA
";
    assert_mps_round_trip(input);
}

#[test]
fn test_mps_round_trip_bound_types() {
    let input = "\
NAME        bounds
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        1
    x2        obj       1
    x2        c1        1
    x3        obj       1
    x3        c1        1
    x4        obj       1
    x4        c1        1
RHS
    RHS_V     c1        10
BOUNDS
 FR BOUND     x1
 LO BOUND     x2        5
 UP BOUND     x2        15
 BV BOUND     x3
 FX BOUND     x4        7
ENDATA
";
    assert_mps_round_trip(input);
}

#[test]
fn test_mps_round_trip_semi_continuous() {
    let input = "\
NAME        sc_test
ROWS
 N  obj
 L  c1
COLUMNS
    x1        obj       1
    x1        c1        1
    x2        obj       1
    x2        c1        1
BOUNDS
 SC BOUND     x1        100
ENDATA
";
    assert_mps_round_trip(input);
}

#[test]
fn test_mps_round_trip_multiple_constraints() {
    let input = "\
NAME        multi
ROWS
 N  obj
 L  c1
 G  c2
 E  c3
COLUMNS
    x1        obj       1
    x1        c1        1
    x1        c2        2
    x1        c3        3
    x2        obj       4
    x2        c1        5
    x2        c2        6
    x2        c3        7
RHS
    RHS_V     c1        10
    RHS_V     c2        5
    RHS_V     c3        7
ENDATA
";
    assert_mps_round_trip(input);
}

#[test]
fn test_mps_round_trip_multiple_objectives() {
    let input = "\
NAME        multiobj
ROWS
 N  obj1
 N  obj2
 L  c1
COLUMNS
    x1        obj1      1
    x1        obj2      2
    x1        c1        3
RHS
    RHS_V     c1        10
ENDATA
";
    assert_mps_round_trip(input);
}

#[test]
fn test_mps_round_trip_enlight4() {
    use std::path::PathBuf;

    let mut file_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    file_path.push("resources/enlight4.mps");
    let input = std::fs::read_to_string(&file_path).expect("failed to read enlight4.mps");

    assert_mps_round_trip(&input);
}