Skip to content

Commit 0daa9a8

Browse files
committed
Fix solution length validation and remove redundant test scaffolding
1 parent c6bfae6 commit 0daa9a8

13 files changed

Lines changed: 38 additions & 138 deletions

File tree

‎problemreductions-cli/src/commands/create/tests.rs‎

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ fn test_create_path_constrained_network_flow_rejects_invalid_paths() {
747747
}
748748

749749
#[test]
750-
fn test_create_staff_scheduling_reports_invalid_schedule_without_panic() {
750+
fn test_create_staff_scheduling_rejects_ragged_schedule() {
751751
let cli = Cli::try_parse_from([
752752
"pred",
753753
"create",
@@ -775,14 +775,7 @@ fn test_create_staff_scheduling_reports_invalid_schedule_without_panic() {
775775
auto_json: false,
776776
};
777777

778-
let result = std::panic::catch_unwind(|| create(&args, &out));
779-
assert!(result.is_ok(), "create should return an error, not panic");
780-
let err = result.unwrap().unwrap_err().to_string();
781-
// parse_bool_rows catches ragged rows before validate_staff_scheduling_args
782-
assert!(
783-
err.contains("All rows") || err.contains("schedule 1 has 6 periods, expected 7"),
784-
"expected row-length validation error, got: {err}"
785-
);
778+
assert!(create(&args, &out).is_err());
786779
}
787780

788781
#[test]
@@ -955,7 +948,7 @@ fn test_create_timetable_design_outputs_problem_json() {
955948
}
956949

957950
#[test]
958-
fn test_create_timetable_design_reports_invalid_matrix_without_panic() {
951+
fn test_create_timetable_design_rejects_invalid_matrix() {
959952
let cli = Cli::try_parse_from([
960953
"pred",
961954
"create",
@@ -987,13 +980,7 @@ fn test_create_timetable_design_reports_invalid_matrix_without_panic() {
987980
auto_json: false,
988981
};
989982

990-
let result = std::panic::catch_unwind(|| create(&args, &out));
991-
assert!(result.is_ok(), "create should return an error, not panic");
992-
let err = result.unwrap().unwrap_err().to_string();
993-
assert!(
994-
err.contains("--craftsman-avail"),
995-
"expected timetable matrix validation error, got: {err}"
996-
);
983+
assert!(create(&args, &out).is_err());
997984
}
998985

999986
#[test]

‎problemreductions-macros/src/lib.rs‎

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,38 +1340,6 @@ mod tests {
13401340
assert_eq!(error.to_string(), "duplicate `create` clause");
13411341
}
13421342

1343-
#[test]
1344-
fn declare_variants_generates_model_dispatch_without_solver_dispatch() {
1345-
let input: DeclareVariantsInput = syn::parse_quote! {
1346-
default Foo => "1",
1347-
};
1348-
let tokens = generate_declare_variants(&input).unwrap().to_string();
1349-
assert!(tokens.contains("factory :"), "expected factory field");
1350-
assert!(
1351-
tokens.contains("serialize_fn :"),
1352-
"expected serialize_fn field"
1353-
);
1354-
assert!(!tokens.contains("solve_typed_fn :"));
1355-
assert!(!tokens.contains("solve_fn :"));
1356-
assert!(
1357-
!tokens.contains("factory : None"),
1358-
"factory should not be None"
1359-
);
1360-
assert!(
1361-
!tokens.contains("serialize_fn : None"),
1362-
"serialize_fn should not be None"
1363-
);
1364-
assert!(!tokens.contains("find_cartesian"));
1365-
assert!(
1366-
!tokens.contains("find_best"),
1367-
"did not expect legacy find_best in tokens"
1368-
);
1369-
assert!(
1370-
!tokens.contains("SolutionSize :: Invalid"),
1371-
"did not expect legacy invalid fallback in tokens"
1372-
);
1373-
}
1374-
13751343
#[test]
13761344
fn reduction_rejects_unexpected_attribute() {
13771345
let extra_attr = syn::Ident::new("extra", proc_macro2::Span::call_site());

‎src/models/graph/directed_hamiltonian_path.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ pub(crate) fn decode_lehmer(code: &[usize]) -> Vec<usize> {
137137
let mut available: Vec<usize> = (0..n).collect();
138138
let mut perm = Vec::with_capacity(n);
139139
for &idx in code {
140-
let idx = idx.min(available.len().saturating_sub(1));
141140
perm.push(available.remove(idx));
142141
}
143142
perm

‎src/models/graph/maximal_is.rs‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ impl<G: Graph, W: Clone + Default> MaximalIS<G, W> {
133133

134134
/// Check if a configuration is a valid maximal independent set.
135135
pub fn is_valid_solution(&self, config: &[bool]) -> bool {
136-
self.is_maximal(config)
136+
config.len() == self.graph.num_vertices() && self.is_maximal(config)
137137
}
138138

139139
/// Check if a configuration is an independent set.
140140
fn is_independent(&self, config: &[bool]) -> bool {
141141
for (u, v) in self.graph.edges() {
142-
if config.get(u).copied().unwrap_or(false) && config.get(v).copied().unwrap_or(false) {
142+
if config[u] && config[v] {
143143
return false;
144144
}
145145
}
@@ -154,18 +154,15 @@ impl<G: Graph, W: Clone + Default> MaximalIS<G, W> {
154154

155155
let n = self.graph.num_vertices();
156156
for v in 0..n {
157-
if config.get(v).copied().unwrap_or(false) {
158-
continue; // Already in set
157+
if config[v] {
158+
continue;
159159
}
160160

161-
// Check if v can be added
162161
let neighbors = self.graph.neighbors(v);
163-
let can_add = neighbors
164-
.iter()
165-
.all(|&u| !config.get(u).copied().unwrap_or(false));
162+
let can_add = neighbors.iter().all(|&u| !config[u]);
166163

167164
if can_add {
168-
return false; // Set is not maximal
165+
return false;
169166
}
170167
}
171168

‎src/models/graph/minimum_vertex_cover.rs‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl<G: Graph, W: Clone + Default> MinimumVertexCover<G, W> {
137137

138138
/// Check if a configuration is a valid vertex cover.
139139
pub fn is_valid_solution(&self, config: &[bool]) -> bool {
140-
is_vertex_cover_config(&self.graph, config)
140+
config.len() == self.graph.num_vertices() && is_vertex_cover_config(&self.graph, config)
141141
}
142142
}
143143

@@ -207,11 +207,9 @@ where
207207
}
208208

209209
/// Check if a configuration forms a valid vertex cover.
210-
pub(crate) fn is_vertex_cover_config<G: Graph>(graph: &G, config: &[bool]) -> bool {
210+
fn is_vertex_cover_config<G: Graph>(graph: &G, config: &[bool]) -> bool {
211211
for (u, v) in graph.edges() {
212-
let u_covered = config.get(u).copied().unwrap_or(false);
213-
let v_covered = config.get(v).copied().unwrap_or(false);
214-
if !u_covered && !v_covered {
212+
if !config[u] && !config[v] {
215213
return false;
216214
}
217215
}

‎src/rules/ksatisfiability_timetabledesign.rs‎

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -826,27 +826,6 @@ impl ReduceTo<TimetableDesign> for KSatisfiability<K3> {
826826
}
827827
}
828828

829-
#[cfg(any(test, feature = "example-db"))]
830-
#[allow(dead_code)]
831-
pub(super) fn construct_timetable_from_assignment(
832-
target: &TimetableDesign,
833-
assignment: &[bool],
834-
source: &KSatisfiability<K3>,
835-
) -> Option<Vec<Vec<Vec<bool>>>> {
836-
let reduction =
837-
ReduceTo::<TimetableDesign>::reduce_to(source).expect("reduction should succeed");
838-
if reduction.target_problem().num_periods() != target.num_periods()
839-
|| reduction.target_problem().num_craftsmen() != target.num_craftsmen()
840-
|| reduction.target_problem().num_tasks() != target.num_tasks()
841-
|| reduction.target_problem().craftsman_avail() != target.craftsman_avail()
842-
|| reduction.target_problem().task_avail() != target.task_avail()
843-
|| reduction.target_problem().requirements() != target.requirements()
844-
{
845-
return None;
846-
}
847-
reduction.construct_target_solution(assignment)
848-
}
849-
850829
#[cfg(feature = "example-db")]
851830
pub(crate) fn canonical_rule_example_specs() -> Vec<crate::example_db::specs::RuleExampleSpec> {
852831
use crate::export::SolutionPair;

‎src/unit_tests/models/graph/maximal_is.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ fn test_is_valid_solution() {
192192
assert!(problem.is_valid_solution(&[true, false, true]));
193193
// Invalid: {0} is independent but not maximal (vertex 2 can be added)
194194
assert!(!problem.is_valid_solution(&[true, false, false]));
195+
assert!(!problem.is_valid_solution(&[false, true]));
196+
assert!(!problem.is_valid_solution(&[false, true, false, true]));
195197
}
196198

197199
#[test]

‎src/unit_tests/models/graph/minimum_vertex_cover.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ fn test_is_valid_solution() {
197197
assert!(problem.is_valid_solution(&[false, true, false]));
198198
// Invalid: {0} doesn't cover edge (1,2)
199199
assert!(!problem.is_valid_solution(&[true, false, false]));
200+
assert!(!problem.is_valid_solution(&[false, true]));
201+
assert!(!problem.is_valid_solution(&[false, true, false, true]));
200202
}
201203

202204
#[test]

‎src/unit_tests/models/misc/scheduling_with_individual_deadlines.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ fn test_scheduling_with_individual_deadlines_evaluate_rejects_capacity_violation
123123
fn test_scheduling_with_individual_deadlines_evaluate_handles_huge_sparse_deadline() {
124124
let problem = SchedulingWithIndividualDeadlines::new(1, 1, vec![i64::MAX], vec![]);
125125

126-
let result = std::panic::catch_unwind(|| problem.evaluate(&vec![0]).unwrap());
127-
128-
assert!(matches!(result, Ok(crate::types::Or(true))));
126+
assert_eq!(problem.evaluate(&vec![0]).unwrap(), crate::types::Or(true));
129127
}
130128

131129
#[test]

‎src/unit_tests/rules/ksatisfiability_timetabledesign.rs‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,9 @@ fn test_ksatisfiability_to_timetabledesign_extract_solution_from_constructed_tim
4949
let source = satisfiable_instance();
5050
let reduction =
5151
ReduceTo::<TimetableDesign>::reduce_to(&source).expect("reduction should succeed");
52-
let target_solution = construct_timetable_from_assignment(
53-
reduction.target_problem(),
54-
&[true, true, false],
55-
&source,
56-
)
57-
.expect("a satisfying 3SAT assignment should lift to a timetable witness");
52+
let target_solution = reduction
53+
.construct_target_solution(&[true, true, false])
54+
.expect("a satisfying 3SAT assignment should lift to a timetable witness");
5855

5956
assert!(
6057
reduction
@@ -74,12 +71,9 @@ fn test_ksatisfiability_to_timetabledesign_multi_variable_round_trip() {
7471
let reduction =
7572
ReduceTo::<TimetableDesign>::reduce_to(&source).expect("reduction should succeed");
7673

77-
let target_solution = construct_timetable_from_assignment(
78-
reduction.target_problem(),
79-
&[true, true, false],
80-
&source,
81-
)
82-
.expect("a satisfying 3SAT assignment should lift to a timetable witness");
74+
let target_solution = reduction
75+
.construct_target_solution(&[true, true, false])
76+
.expect("a satisfying 3SAT assignment should lift to a timetable witness");
8377

8478
let extracted = reduction.extract_solution(&target_solution).unwrap();
8579
assert_eq!(extracted, vec![true, true, false]);

0 commit comments

Comments
 (0)