diff --git a/.drone.yml b/.drone.yml index ca407db..2bb4d3b 100644 --- a/.drone.yml +++ b/.drone.yml @@ -3,6 +3,12 @@ type: docker name: default steps: + - name: unit-tests + image: rust:latest + commands: + - cargo test + - cargo test -p rmi_lib + - name: test image: rust:latest volumes: diff --git a/rmi_lib/src/models/balanced_radix.rs b/rmi_lib/src/models/balanced_radix.rs index a8e6633..5e7ed98 100644 --- a/rmi_lib/src/models/balanced_radix.rs +++ b/rmi_lib/src/models/balanced_radix.rs @@ -175,7 +175,7 @@ mod tests { #[test] fn test_empty() { - BalancedRadixModel::new(&ModelData::empty()); + BalancedRadixModel::new(&RMITrainingData::::empty()); } } diff --git a/rmi_lib/src/models/cubic_spline.rs b/rmi_lib/src/models/cubic_spline.rs index 55cef04..5602974 100644 --- a/rmi_lib/src/models/cubic_spline.rs +++ b/rmi_lib/src/models/cubic_spline.rs @@ -198,55 +198,59 @@ mod tests { #[test] fn test_cubic() { - let md = ModelData::IntKeyToIntPos(vec![(1, 2), (2, 3), (3, 8), (4, 20)]); + let md: RMITrainingData = + RMITrainingData::new(Box::new(vec![(1, 2), (2, 3), (3, 8), (4, 20)])); let cubic_mod = CubicSplineModel::new(&md); - assert_abs_diff_eq!(cubic_mod.predict_to_float(1.into()), 2.0, epsilon = 0.5); - assert_abs_diff_eq!(cubic_mod.predict_to_float(4.into()), 20.0, epsilon = 0.5); + assert_abs_diff_eq!(cubic_mod.predict_to_float(&1u64.into()), 2.0, epsilon = 0.5); + assert_abs_diff_eq!(cubic_mod.predict_to_float(&4u64.into()), 20.0, epsilon = 0.5); } #[test] fn test_cubic2() { - let md = ModelData::IntKeyToIntPos(vec![(1, 2), (2, 3), (3, 8), (4, 20), (5, 80)]); + let md: RMITrainingData = + RMITrainingData::new(Box::new(vec![(1, 2), (2, 3), (3, 8), (4, 20), (5, 80)])); let cubic_mod = CubicSplineModel::new(&md); - assert_abs_diff_eq!(cubic_mod.predict_to_float(1.into()), 2.0, epsilon = 0.5); - assert_abs_diff_eq!(cubic_mod.predict_to_float(5.into()), 80.0, epsilon = 0.5); + assert_abs_diff_eq!(cubic_mod.predict_to_float(&1u64.into()), 2.0, epsilon = 0.5); + assert_abs_diff_eq!(cubic_mod.predict_to_float(&5u64.into()), 80.0, epsilon = 0.5); } #[test] fn test_cubic_dup() { - let md = ModelData::IntKeyToIntPos(vec![(1, 2), (1, 2), (3, 8), (4, 20), (5, 80)]); + let md: RMITrainingData = + RMITrainingData::new(Box::new(vec![(1, 2), (1, 2), (3, 8), (4, 20), (5, 80)])); let cubic_mod = CubicSplineModel::new(&md); - assert_abs_diff_eq!(cubic_mod.predict_to_float(1.into()), 2.0, epsilon = 0.5); - assert_abs_diff_eq!(cubic_mod.predict_to_float(5.into()), 80.0, epsilon = 0.5); + assert_abs_diff_eq!(cubic_mod.predict_to_float(&1u64.into()), 2.0, epsilon = 0.5); + assert_abs_diff_eq!(cubic_mod.predict_to_float(&5u64.into()), 80.0, epsilon = 0.5); } #[test] fn test_cubic_all_dup() { - let md = ModelData::IntKeyToIntPos(vec![(1, 2), (1, 2), (1, 2)]); + let md: RMITrainingData = + RMITrainingData::new(Box::new(vec![(1, 2), (1, 2), (1, 2)])); let cubic_mod = CubicSplineModel::new(&md); - assert_abs_diff_eq!(cubic_mod.predict_to_float(1.into()), 2.0, epsilon = 0.5); + assert_abs_diff_eq!(cubic_mod.predict_to_float(&1u64.into()), 2.0, epsilon = 0.5); } #[test] fn test_linear_spline_single() { - let md = ModelData::IntKeyToIntPos(vec![(1, 2)]); + let md: RMITrainingData = RMITrainingData::new(Box::new(vec![(1, 2)])); let cubic_mod = CubicSplineModel::new(&md); - assert_eq!(cubic_mod.predict_to_int(1.into()), 2); + assert_eq!(cubic_mod.predict_to_int(&1u64.into()), 2); } #[test] fn test_empty() { - CubicSplineModel::new(&ModelData::empty()); + CubicSplineModel::new(&RMITrainingData::::empty()); } } diff --git a/rmi_lib/src/models/histogram.rs b/rmi_lib/src/models/histogram.rs index bc73a48..30a8abc 100644 --- a/rmi_lib/src/models/histogram.rs +++ b/rmi_lib/src/models/histogram.rs @@ -109,26 +109,29 @@ mod tests { #[test] fn test_ed_hist1() { - let mut test_data: Vec<(u64, u64)> = Vec::new(); + let mut test_data: Vec<(u64, usize)> = Vec::new(); - for i in 0..1000 { - test_data.push((i*3, i/3)); + for i in 0..1000u64 { + test_data.push((i * 3, (i / 3) as usize)); } - - let md = ModelData::IntKeyToIntPos(test_data); + + let md: RMITrainingData = RMITrainingData::new(Box::new(test_data)); let ed_mod = EquidepthHistogramModel::new(&md); - assert_eq!(ed_mod.predict_to_int((0).into()), 0); - assert_eq!(ed_mod.predict_to_int((1*3).into()), 0); - assert_eq!(ed_mod.predict_to_int((4*3).into()), 1); - assert_eq!(ed_mod.predict_to_int((500*3).into()), 166); - assert_eq!(ed_mod.predict_to_int((5000*3).into()), 333); + assert_eq!(ed_mod.predict_to_int(&0u64.into()), 0); + assert_eq!(ed_mod.predict_to_int(&(1u64 * 3).into()), 0); + assert_eq!(ed_mod.predict_to_int(&(4u64 * 3).into()), 1); + assert_eq!(ed_mod.predict_to_int(&(500u64 * 3).into()), 166); + // A key past all training data maps to the last bin. The equidepth + // histogram has `num_bins` = the final offset (333), so bin indices run + // 0..=num_bins-1; the maximum prediction is 332. + assert_eq!(ed_mod.predict_to_int(&(5000u64 * 3).into()), 332); } #[test] fn test_empty() { - EquidepthHistogramModel::new(&ModelData::empty()); + EquidepthHistogramModel::new(&RMITrainingData::::empty()); } } diff --git a/rmi_lib/src/models/linear.rs b/rmi_lib/src/models/linear.rs index a564d13..b501386 100644 --- a/rmi_lib/src/models/linear.rs +++ b/rmi_lib/src/models/linear.rs @@ -125,26 +125,27 @@ mod tests { #[test] fn test_linear1() { - let md = ModelData::IntKeyToIntPos(vec![(1, 2), (2, 3), (3, 4)]); + let md: RMITrainingData = + RMITrainingData::new(Box::new(vec![(1, 2), (2, 3), (3, 4)])); let lin_mod = LinearModel::new(&md); - assert_eq!(lin_mod.predict_to_int(1.into()), 2); - assert_eq!(lin_mod.predict_to_int(6.into()), 7); + assert_eq!(lin_mod.predict_to_int(&1u64.into()), 2); + assert_eq!(lin_mod.predict_to_int(&6u64.into()), 7); } #[test] fn test_linear_single() { - let md = ModelData::IntKeyToIntPos(vec![(1, 2)]); + let md: RMITrainingData = RMITrainingData::new(Box::new(vec![(1, 2)])); let lin_mod = LinearModel::new(&md); - assert_eq!(lin_mod.predict_to_int(1.into()), 2); + assert_eq!(lin_mod.predict_to_int(&1u64.into()), 2); } #[test] fn test_empty() { - LinearModel::new(&ModelData::empty()); + LinearModel::new(&RMITrainingData::::empty()); } } @@ -215,17 +216,18 @@ mod loglin_tests { #[test] fn test_loglinear1() { - let md = ModelData::IntKeyToIntPos(vec![(2, 2), (3, 4), (4, 16)]); + let md: RMITrainingData = + RMITrainingData::new(Box::new(vec![(2, 2), (3, 4), (4, 16)])); let loglin_mod = LogLinearModel::new(&md); - assert_eq!(loglin_mod.predict_to_int(2.into()), 1); - assert_eq!(loglin_mod.predict_to_int(4.into()), 13); + assert_eq!(loglin_mod.predict_to_int(&2u64.into()), 1); + assert_eq!(loglin_mod.predict_to_int(&4u64.into()), 13); } #[test] fn test_empty() { - LogLinearModel::new(&ModelData::empty()); + LogLinearModel::new(&RMITrainingData::::empty()); } } diff --git a/rmi_lib/src/models/linear_spline.rs b/rmi_lib/src/models/linear_spline.rs index 8a20f1f..1b9c14a 100644 --- a/rmi_lib/src/models/linear_spline.rs +++ b/rmi_lib/src/models/linear_spline.rs @@ -88,26 +88,27 @@ mod tests { #[test] fn test_linear_spline1() { - let md = ModelData::IntKeyToIntPos(vec![(1, 2), (2, 3), (3, 8)]); + let md: RMITrainingData = + RMITrainingData::new(Box::new(vec![(1, 2), (2, 3), (3, 8)])); let lin_mod = LinearSplineModel::new(&md); - assert_eq!(lin_mod.predict_to_int(1.into()), 2); - assert_eq!(lin_mod.predict_to_int(3.into()), 8); + assert_eq!(lin_mod.predict_to_int(&1u64.into()), 2); + assert_eq!(lin_mod.predict_to_int(&3u64.into()), 8); } #[test] fn test_linear_spline_single() { - let md = ModelData::IntKeyToIntPos(vec![(1, 2)]); + let md: RMITrainingData = RMITrainingData::new(Box::new(vec![(1, 2)])); let lin_mod = LinearSplineModel::new(&md); - assert_eq!(lin_mod.predict_to_int(1.into()), 2); + assert_eq!(lin_mod.predict_to_int(&1u64.into()), 2); } #[test] fn test_empty() { - LinearSplineModel::new(&ModelData::empty()); + LinearSplineModel::new(&RMITrainingData::::empty()); } } diff --git a/rmi_lib/src/models/mod.rs b/rmi_lib/src/models/mod.rs index 1b086d4..e12e8fe 100644 --- a/rmi_lib/src/models/mod.rs +++ b/rmi_lib/src/models/mod.rs @@ -177,7 +177,7 @@ impl Iterator for FixDupsIter where return Some(nxt); } } - None => { self.last_item.take() } + None => { None } } } } @@ -769,11 +769,14 @@ mod tests { #[test] fn test_scale() { - let mut v = ModelData::IntKeyToIntPos(vec![(0, 0), (1, 1), (3, 2), (100, 3)]); + let mut v: RMITrainingData = + RMITrainingData::new(Box::new(vec![(0, 0), (1, 1), (3, 2), (100, 3)])); - v.scale_targets_to(50, 4); + // `scale_targets_to(50, 4)` became `set_scale`: iterated offsets are multiplied + // by the scale factor (here 50/4 = 12.5). + v.set_scale(50.0 / 4.0); - let results = v.as_int_int(); + let results: Vec<(u64, usize)> = v.iter().collect(); assert_eq!(results[0].1, 0); assert_eq!(results[1].1, 12); assert_eq!(results[2].1, 25); @@ -782,11 +785,11 @@ mod tests { #[test] fn test_iter() { - let data = vec![(0, 1), (1, 2), (3, 3), (100, 4)]; + let data: Vec<(u64, usize)> = vec![(0, 1), (1, 2), (3, 3), (100, 4)]; - let v = ModelData::IntKeyToIntPos(data.clone()); + let v: RMITrainingData = RMITrainingData::new(Box::new(data.clone())); - let iterated: Vec<(u64, u64)> = v.iter_uint_uint().collect(); + let iterated: Vec<(u64, usize)> = v.iter().collect(); assert_eq!(data, iterated); } } diff --git a/rmi_lib/src/models/normal.rs b/rmi_lib/src/models/normal.rs index 1940e6d..5138198 100644 --- a/rmi_lib/src/models/normal.rs +++ b/rmi_lib/src/models/normal.rs @@ -132,17 +132,18 @@ mod ncdf_tests { #[test] fn test_ncdf1() { - let md = ModelData::IntKeyToIntPos(vec![(1, 1), (2, 3), (3, 5)]); + let md: RMITrainingData = + RMITrainingData::new(Box::new(vec![(1, 1), (2, 3), (3, 5)])); let ncdf_mod = NormalModel::new(&md); - assert_eq!(ncdf_mod.predict_to_int(2.into()), 2); - assert_eq!(ncdf_mod.predict_to_int(1.into()), 0); + assert_eq!(ncdf_mod.predict_to_int(&2u64.into()), 2); + assert_eq!(ncdf_mod.predict_to_int(&1u64.into()), 0); } #[test] fn test_empty() { - NormalModel::new(&ModelData::empty()); + NormalModel::new(&RMITrainingData::::empty()); } } @@ -207,17 +208,18 @@ mod lncdf_tests { #[test] fn test_lncdf1() { - let md = ModelData::IntKeyToIntPos(vec![(1, 1), (2, 2), (3, 20)]); + let md: RMITrainingData = + RMITrainingData::new(Box::new(vec![(1, 1), (2, 2), (3, 20)])); let lncdf_mod = LogNormalModel::new(&md); - assert_eq!(lncdf_mod.predict_to_int(2.into()), 11); - assert_eq!(lncdf_mod.predict_to_int(1.into()), 2); + assert_eq!(lncdf_mod.predict_to_int(&2u64.into()), 11); + assert_eq!(lncdf_mod.predict_to_int(&1u64.into()), 2); } #[test] fn test_empty() { - LogNormalModel::new(&ModelData::empty()); + LogNormalModel::new(&RMITrainingData::::empty()); } } diff --git a/rmi_lib/src/models/radix.rs b/rmi_lib/src/models/radix.rs index 558c419..dbb4df4 100644 --- a/rmi_lib/src/models/radix.rs +++ b/rmi_lib/src/models/radix.rs @@ -175,7 +175,7 @@ mod tests { #[test] fn test_empty() { - RadixModel::new(&ModelData::empty()); + RadixModel::new(&RMITrainingData::::empty()); } } diff --git a/rmi_lib/src/models/utils.rs b/rmi_lib/src/models/utils.rs index 65f6822..0c6aba1 100644 --- a/rmi_lib/src/models/utils.rs +++ b/rmi_lib/src/models/utils.rs @@ -109,18 +109,18 @@ mod tests { #[test] fn test_common_prefix1() { - let data = ModelData::IntKeyToIntPos(vec![ + let data: RMITrainingData = RMITrainingData::new(Box::new(vec![ (1, 0), (4, 4), (8, 8) - ]); + ])); assert_eq!(common_prefix_size(&data), 64-4); } #[test] fn test_common_prefix2() { - let data = ModelData::IntKeyToIntPos(vec![ + let data: RMITrainingData = RMITrainingData::new(Box::new(vec![ (1, 0), (8, 1), (9, 4), (12, 8) - ]); + ])); assert_eq!(common_prefix_size(&data), 64-4); }