Skip to content

Commit

Permalink
test: Add a test for system that has infinite solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
pnevyk committed Apr 28, 2023
1 parent 33ae76a commit 266a1b1
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/solver/cuckoo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,16 @@ mod tests {
);
}
}

#[test]
fn infinite_solutions() {
let f = InfiniteSolutions::default();
let dom = f.domain();
let eps = convert(1e-12);

for x in f.initials() {
let solver = Cuckoo::new(&f, &dom, thread_rng());
assert!(f.is_root(&solve(&f, &dom, solver, x, 25, eps).unwrap(), eps));
}
}
}
12 changes: 12 additions & 0 deletions src/solver/steffensen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,16 @@ mod tests {
assert!(f.is_root(&solve(&f, &dom, solver, x, 15, eps).unwrap(), eps));
}
}

#[test]
fn infinite_solutions() {
let f = InfiniteSolutions::default();
let dom = f.domain();
let eps = convert(1e-12);

for x in f.initials() {
let solver = Steffensen::new(&f, &dom);
assert!(f.is_root(&solve(&f, &dom, solver, x, 25, eps).unwrap(), eps));
}
}
}
16 changes: 16 additions & 0 deletions src/solver/trust_region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,4 +708,20 @@ mod tests {
Err(SolveError::Solver(TrustRegionError::NoProgress)) | Err(SolveError::Termination)
));
}

#[test]
fn infinite_solutions() {
let f = InfiniteSolutions::default();
let dom = f.domain();
let eps = convert(1e-12);

for x in f.initials() {
let solver = TrustRegion::new(&f, &dom);
assert!(match solve(&f, &dom, solver, x, 25, eps) {
Ok(root) => f.is_root(&root, eps),
Err(SolveError::Solver(TrustRegionError::NoValidStep)) => true,
Err(error) => panic!("{:?}", error),
});
}
}
}
54 changes: 54 additions & 0 deletions src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,60 @@ impl TestSystem for Exponential {
}
}

/// This system is true for any assignment of x.
#[derive(Debug, Clone, Copy)]
pub struct InfiniteSolutions {
n: usize,
}

impl InfiniteSolutions {
/// Initializes the system with given dimension.
pub fn new(n: usize) -> Self {
assert!(n > 0, "n must be greater than zero");
Self { n }
}
}

impl Default for InfiniteSolutions {
fn default() -> Self {
Self { n: 1 }
}
}

impl Problem for InfiniteSolutions {
type Scalar = f64;
type Dim = Dynamic;

fn dim(&self) -> Self::Dim {
Dynamic::from_usize(self.n)
}
}

impl System for InfiniteSolutions {
fn eval<Sx, Sfx>(
&self,
_x: &Vector<Self::Scalar, Self::Dim, Sx>,
fx: &mut Vector<Self::Scalar, Self::Dim, Sfx>,
) -> Result<(), ProblemError>
where
Sx: Storage<Self::Scalar, Self::Dim> + IsContiguous,
Sfx: StorageMut<Self::Scalar, Self::Dim>,
{
for i in 0..self.n {
fx[i] = 0.0;
}

Ok(())
}
}

impl TestSystem for InfiniteSolutions {
fn initials(&self) -> Vec<OVector<Self::Scalar, Self::Dim>> {
let init = DVector::zeros_generic(Dynamic::from_usize(self.n), U1::name());
vec![init]
}
}

/// Solving error of the testing solver driver (see [`solve`]).
#[derive(Debug, Error)]
pub enum SolveError<E: StdError + 'static> {
Expand Down

0 comments on commit 266a1b1

Please sign in to comment.