Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn example main program into rust test #46

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions solvers/chuffed/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ fn bind() {
.allowlist_function("destroy_vec_intvar")
.allowlist_function("p_addVars")
.allowlist_function("p_setcallback")
.allowlist_function("p_print")
.allowlist_function("branch_IntVar")
.clang_arg("-Ivendor/build") // generated from configure.py
.clang_arg("-Ivendor")
Expand Down
17 changes: 15 additions & 2 deletions solvers/chuffed/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ pub mod bindings {

pub mod wrappers {
use crate::bindings::{
all_different, branch_IntVar, createVar, createVars, make_vec_intvar, vec, ConLevel,
IntVar, ValBranch, VarBranch,
all_different, branch_IntVar, createVar, createVars, make_vec_intvar, output_vars1,
var_sym_break, vec, ConLevel, IntVar, ValBranch, VarBranch,
};
use core::ptr;

Expand Down Expand Up @@ -50,4 +50,17 @@ pub mod wrappers {
branch_IntVar(x, var_branch, val_branch);
}
}

pub unsafe fn output_vars_wrapper(x: *mut vec<*mut IntVar>) {
unsafe {
// output_vars1 takes in an vec<IntVar*> instead of branching
output_vars1(x);
}
}

pub unsafe fn var_sym_break_wrapper(x: *mut vec<*mut IntVar>) {
unsafe {
var_sym_break(x);
}
}
}
19 changes: 16 additions & 3 deletions solvers/chuffed/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use chuffed_rs::bindings::{
get_idx, new_dummy_problem, p_addVars, p_setcallback, vec, ConLevel_CL_DEF, IntVar,
get_idx, new_dummy_problem, p_addVars, p_print, p_setcallback, vec, ConLevel_CL_DEF, IntVar,
VarBranch_VAR_INORDER, VarBranch_VAR_MIN_MIN,
};
use chuffed_rs::wrappers::{all_different_wrapper, branch_wrapper, create_vars};
use chuffed_rs::wrappers::{
all_different_wrapper, branch_wrapper, create_vars, output_vars_wrapper, var_sym_break_wrapper,
};

unsafe fn post_constraints(_n: i32) -> *mut vec<*mut IntVar> {
// Create constant
Expand All @@ -16,15 +18,23 @@ unsafe fn post_constraints(_n: i32) -> *mut vec<*mut IntVar> {
// Post some branchings
branch_wrapper(x as _, VarBranch_VAR_INORDER, VarBranch_VAR_MIN_MIN);

// Declare output variables (optional)
output_vars_wrapper(x);

// Declare symmetries (optional)
var_sym_break_wrapper(x);

// Return the variable
x
}

// Custom printing function for this problem
#[no_mangle]
pub unsafe extern "C" fn callback(x: *mut vec<*mut IntVar>) {
print!("First output is: {:?}", get_idx(x, 0));
print!("First output is: {}", get_idx(x, 0));
}

// Entry point for running this problem
fn main() {
let args: Vec<String> = std::env::args().collect();

Expand All @@ -43,5 +53,8 @@ fn main() {
p_addVars(p, x);
// Call problem.setcallback()
p_setcallback(p, Some(callback));
// Commented out currently as trying to print causes the assertion of
// isFixed() in IntVar::getVal() to fail.
// p_print(p);
}
}
67 changes: 67 additions & 0 deletions solvers/chuffed/tests/chuffed_basic_run.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use chuffed_rs::bindings::{
get_idx, new_dummy_problem, p_addVars, p_print, p_setcallback, vec, ConLevel_CL_DEF, IntVar,
VarBranch_VAR_INORDER, VarBranch_VAR_MIN_MIN,
};
use chuffed_rs::wrappers::{
all_different_wrapper, branch_wrapper, create_vars, output_vars_wrapper, var_sym_break_wrapper,
};

/// Creates the variable for the test problem and posts some constraints and
/// branchings on it.
unsafe fn post_constraints(_n: i32) -> *mut vec<*mut IntVar> {
// Create constant
let n: i32 = _n;
// Create some variables
let x: *mut vec<*mut IntVar> = create_vars(n, 0, n, false);

// Post some constraints
all_different_wrapper(x, ConLevel_CL_DEF);

// Post some branchings
branch_wrapper(x as _, VarBranch_VAR_INORDER, VarBranch_VAR_MIN_MIN);

// Declare output variables (optional)
output_vars_wrapper(x);

// Declare symmetries (optional)
var_sym_break_wrapper(x);

// Return the variable
x
}

/// Custom printing function for this test problem
#[no_mangle]
pub unsafe extern "C" fn callback(x: *mut vec<*mut IntVar>) {
print!("First output is: {}", get_idx(x, 0));
}

/// Basic test to make sure that running the ffi bindings and wrappers does not
/// crash
#[test]
fn run_basic_problem() {
let args: Vec<String> = std::env::args().collect();

if args.len() != 2 {
println!("Invalid number of arguments");
return;
}

let n: i32 = args[1].parse().expect("Invalid input");

unsafe {
let x = post_constraints(n);
// make new dummy problem
let p = new_dummy_problem();
// Call problem.addvars()
p_addVars(p, x);
// Call problem.setcallback()
p_setcallback(p, Some(callback));
// Commented out currently as trying to print causes the assertion of
// isFixed() in IntVar::getVal() to fail.
// p_print(p);

// Pass test if no crash occurs
assert!(true);
}
}
14 changes: 8 additions & 6 deletions solvers/chuffed/wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ void p_addVars(DummyProblem *p, vec<IntVar *> *_searchVars) {
void p_setcallback(DummyProblem *p, void (*_callback)(vec<IntVar *> *)) {
p->setcallback(_callback);
}
IntVar* get_idx(vec<IntVar *> *x, int i) { return *x[i]; }
void p_print(DummyProblem *p) { p->print(); }

vec<IntVar*>* make_vec_intvar() {
return new vec<IntVar*>();
int get_idx(vec<IntVar *> *x, int i) {
IntVar *var = *x[i];
int t = var->getVal();
return t;
}

void destroy_vec_intvar(vec<IntVar*>* v) {
delete v;
}
vec<IntVar *> *make_vec_intvar() { return new vec<IntVar *>(); }

void destroy_vec_intvar(vec<IntVar *> *v) { delete v; }

void branch_IntVar(vec<IntVar *> *x, VarBranch var_branch,
ValBranch val_branch) {
Expand Down
7 changes: 4 additions & 3 deletions solvers/chuffed/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class DummyProblem {
DummyProblem *new_dummy_problem();
void p_addVars(DummyProblem *p, vec<IntVar *> *_searchVars);
void p_setcallback(DummyProblem *p, void (*_callback)(vec<IntVar *> *));
IntVar* get_idx(vec<IntVar *> *x, int i);
void p_print(DummyProblem *p);
int get_idx(vec<IntVar *> *x, int i);

vec<IntVar*>* make_vec_intvar();
void destroy_vec_intvar(vec<IntVar*>* v);
vec<IntVar *> *make_vec_intvar();
void destroy_vec_intvar(vec<IntVar *> *v);

void branch_IntVar(vec<IntVar *> *x, VarBranch var_branch,
ValBranch val_branch);
Loading