Skip to content

Commit a3ac25a

Browse files
committed
constraints with f32 are better tested
1 parent 1964e0d commit a3ac25a

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

src/constraints/tests.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ fn t_zero_set() {
2020
);
2121
}
2222

23+
#[test]
24+
fn t_zero_set_f32() {
25+
let zero = Zero::new();
26+
let mut x = [1.0_f32, -2.0, 3.5];
27+
zero.project(&mut x);
28+
assert_eq!([0.0_f32, 0.0, 0.0], x);
29+
}
30+
2331
#[test]
2432
fn t_hyperplane() {
2533
let normal_vector = [1.0, 2.0, 3.0];
@@ -149,6 +157,15 @@ fn t_finite_set() {
149157
);
150158
}
151159

160+
#[test]
161+
fn t_finite_set_f32() {
162+
let data: &[&[f32]] = &[&[0.0_f32, 0.0], &[1.0, 1.0], &[0.0, 1.0], &[1.0, 0.0]];
163+
let finite_set = FiniteSet::new(data);
164+
let mut x = [0.7_f32, 0.2];
165+
finite_set.project(&mut x);
166+
assert_eq!([1.0_f32, 0.0], x);
167+
}
168+
152169
#[test]
153170
#[should_panic]
154171
fn t_finite_set_project_wrong_dimension() {
@@ -176,6 +193,18 @@ fn t_rectangle_bounded() {
176193
);
177194
}
178195

196+
#[test]
197+
fn t_rectangle_bounded_f32() {
198+
let xmin = vec![2.0_f32; 3];
199+
let xmax = vec![4.5_f32; 3];
200+
let rectangle = Rectangle::new(Some(&xmin[..]), Some(&xmax[..]));
201+
let mut x = [1.0_f32, 3.0, 5.0];
202+
203+
rectangle.project(&mut x);
204+
205+
assert_eq!([2.0_f32, 3.0, 4.5], x);
206+
}
207+
179208
#[test]
180209
fn t_rectangle_infinite_bounds() {
181210
let xmin = [-1.0, 2.0, f64::NEG_INFINITY];
@@ -282,6 +311,19 @@ fn t_ball2_at_origin() {
282311
);
283312
}
284313

314+
#[test]
315+
fn t_ball2_at_origin_f32() {
316+
let radius = 1.0_f32;
317+
let mut x = [1.0_f32, 1.0];
318+
let ball = Ball2::new(None, radius);
319+
320+
ball.project(&mut x);
321+
322+
let expected = std::f32::consts::FRAC_1_SQRT_2;
323+
assert!((x[0] - expected).abs() < 1e-6);
324+
assert!((x[1] - expected).abs() < 1e-6);
325+
}
326+
285327
#[test]
286328
fn t_ball2_at_origin_different_radius_outside() {
287329
let radius = 0.8;
@@ -352,6 +394,16 @@ fn t_no_constraints() {
352394
unit_test_utils::assert_nearly_equal_array(&[1., 2., 3.], &x, 1e-10, 1e-15, "x is wrong");
353395
}
354396

397+
#[test]
398+
fn t_no_constraints_f32() {
399+
let mut x = [1.0_f32, 2.0, 3.0];
400+
let whole_space = NoConstraints::new();
401+
402+
whole_space.project(&mut x);
403+
404+
assert_eq!([1.0_f32, 2.0, 3.0], x);
405+
}
406+
355407
#[test]
356408
#[should_panic]
357409
fn t_cartesian_product_constraints_incoherent_indices() {
@@ -701,6 +753,18 @@ fn t_simplex_projection() {
701753
);
702754
}
703755

756+
#[test]
757+
fn t_simplex_projection_f32() {
758+
let mut x = [1.0_f32, 2.0, 3.0];
759+
let alpha = 3.0_f32;
760+
let simplex = Simplex::new(alpha);
761+
simplex.project(&mut x);
762+
763+
let sum = x[0] + x[1] + x[2];
764+
assert!((sum - alpha).abs() < 1e-5);
765+
assert!(x.iter().all(|&xi| xi >= -1e-6));
766+
}
767+
704768
#[test]
705769
fn t_simplex_projection_random_spam() {
706770
let n = 10;
@@ -995,6 +1059,15 @@ fn t_epigraph_squared_norm_correctness() {
9951059
);
9961060
}
9971061

1062+
#[test]
1063+
fn t_epigraph_squared_norm_f32() {
1064+
let epi = EpigraphSquaredNorm::new();
1065+
let mut x = [1.0_f32, 0.0, 0.0];
1066+
epi.project(&mut x);
1067+
let err = (matrix_operations::norm2_squared(&x[..2]) - x[2]).abs();
1068+
assert!(err < 1e-4);
1069+
}
1070+
9981071
#[test]
9991072
fn t_affine_space() {
10001073
let a = vec![

0 commit comments

Comments
 (0)