Skip to content

Commit

Permalink
test(rad): add and rename tests for RADON reducers
Browse files Browse the repository at this point in the history
  • Loading branch information
lrubiorod committed Sep 1, 2021
1 parent 1904aa1 commit 294e070
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 85 deletions.
58 changes: 22 additions & 36 deletions rad/src/reducers/average.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,32 +127,25 @@ pub fn mean(input: &RadonArray, return_policy: MeanReturnPolicy) -> Result<Radon

#[cfg(test)]
mod tests {
use serde_cbor::Value;

use crate::{
operators::array::reduce,
types::{float::RadonFloat, integer::RadonInteger, string::RadonString},
};
use crate::types::{float::RadonFloat, integer::RadonInteger, string::RadonString};

use super::*;
use witnet_data_structures::radon_report::ReportContext;

#[test]
fn test_reduce_average_mean_float() {
let input = &RadonArray::from(vec![
fn test_average_mean_float() {
let input = RadonArray::from(vec![
RadonFloat::from(1f64).into(),
RadonFloat::from(2f64).into(),
]);
let args = &[Value::Integer(RadonReducers::AverageMean as i128)];
let expected = RadonTypes::from(RadonFloat::from(1.5f64));

let output = reduce(input, args, &mut ReportContext::default()).unwrap();
let output = mean(&input, MeanReturnPolicy::RoundToInteger).unwrap();

assert_eq!(output, expected);
}

#[test]
fn test_reduce_average_mean_float_arrays() {
fn test_average_mean_float_arrays() {
let array_1 = RadonTypes::from(RadonArray::from(vec![
RadonFloat::from(1f64).into(),
RadonFloat::from(2f64).into(),
Expand All @@ -168,14 +161,13 @@ mod tests {
RadonFloat::from(6f64).into(),
]));

let args = &[Value::Integer(0x03)]; // This is RadonReducers::AverageMean
let output = reduce(&input, args, &mut ReportContext::default()).unwrap();
let output = mean(&input, MeanReturnPolicy::RoundToInteger).unwrap();

assert_eq!(output, expected);
}

#[test]
fn test_reduce_average_mean_float_arrays_different_size() {
fn test_average_mean_float_arrays_different_size() {
let array_1 = RadonTypes::from(RadonArray::from(vec![
RadonFloat::from(1f64).into(),
RadonFloat::from(2f64).into(),
Expand All @@ -193,14 +185,13 @@ mod tests {
second: 2,
};

let args = &[Value::Integer(0x03)]; // This is RadonReducers::AverageMean
let output = reduce(&input, args, &mut ReportContext::default()).unwrap_err();
let output = mean(&input, MeanReturnPolicy::RoundToInteger).unwrap_err();

assert_eq!(output, expected);
}

#[test]
fn test_reduce_average_mean_float_array_of_arrays() {
fn test_average_mean_float_array_of_arrays() {
let array_11 = RadonTypes::from(RadonArray::from(vec![
RadonFloat::from(1f64).into(),
RadonFloat::from(2f64).into(),
Expand Down Expand Up @@ -244,35 +235,33 @@ mod tests {
]));
let expected = RadonTypes::from(RadonArray::from(vec![array_e1, array_e2, array_e3]));

let args = &[Value::Integer(0x03)]; // This is RadonReducers::AverageMean
let output = reduce(&input, args, &mut ReportContext::default()).unwrap();
let output = mean(&input, MeanReturnPolicy::RoundToInteger).unwrap();

assert_eq!(output, expected);
}

#[test]
fn test_operate_reduce_mean_empty() {
fn test_average_mean_empty() {
let input = RadonArray::from(vec![]);
let output = mean(&input, MeanReturnPolicy::ReturnFloat).unwrap();
assert_eq!(output, RadonTypes::from(RadonFloat::from(std::f64::NAN)));
}

#[test]
fn test_reduce_average_mean_integer() {
let input = &RadonArray::from(vec![
fn test_average_mean_integer() {
let input = RadonArray::from(vec![
RadonInteger::from(1i128).into(),
RadonInteger::from(2i128).into(),
]);
let args = &[Value::Integer(RadonReducers::AverageMean as i128)];
let expected = RadonTypes::Integer(RadonInteger::from(2));

let output = reduce(input, args, &mut ReportContext::default()).unwrap();
let output = mean(&input, MeanReturnPolicy::RoundToInteger).unwrap();

assert_eq!(output, expected);
}

#[test]
fn test_reduce_average_mean_integer_arrays() {
fn test_average_mean_integer_arrays() {
let array_1 = RadonTypes::from(RadonArray::from(vec![
RadonInteger::from(1).into(),
RadonInteger::from(2).into(),
Expand All @@ -288,31 +277,29 @@ mod tests {
RadonInteger::from(6).into(),
]));

let args = &[Value::Integer(RadonReducers::AverageMean as i128)];
let output = reduce(&input, args, &mut ReportContext::default()).unwrap();
let output = mean(&input, MeanReturnPolicy::RoundToInteger).unwrap();

assert_eq!(output, expected);
}

#[test]
fn test_reduce_average_mean_string_unsupported() {
let input = &RadonArray::from(vec![
fn test_average_mean_string_unsupported() {
let input = RadonArray::from(vec![
RadonString::from("Hello").into(),
RadonString::from("world").into(),
]);
let args = &[Value::Integer(0x03)]; // This is RadonReducers::AverageMean
let output = reduce(input, args, &mut ReportContext::default()).unwrap_err();
let output = mean(&input, MeanReturnPolicy::RoundToInteger).unwrap_err();

let expected = RadError::UnsupportedReducer {
array: input.clone(),
array: input,
reducer: "RadonReducers::AverageMean".to_string(),
};

assert_eq!(output, expected);
}

#[test]
fn test_reduce_average_mean_float_int_arrays() {
fn test_average_mean_float_int_arrays() {
let array_1 = RadonTypes::from(RadonArray::from(vec![
RadonFloat::from(1f64).into(),
RadonFloat::from(2f64).into(),
Expand All @@ -329,8 +316,7 @@ mod tests {
found: RadonInteger::radon_type_name(),
};

let args = &[Value::Integer(0x03)]; // This is RadonReducers::AverageMean
let output = reduce(&input, args, &mut ReportContext::default()).unwrap_err();
let output = mean(&input, MeanReturnPolicy::RoundToInteger).unwrap_err();

assert_eq!(output, expected);
}
Expand Down
22 changes: 11 additions & 11 deletions rad/src/reducers/deviation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ mod tests {
use std::convert::TryFrom;

#[test]
fn test_reduce_deviation_standard_float() {
fn test_deviation_standard_float() {
let input = &RadonArray::from(vec![
RadonFloat::from(1f64).into(),
RadonFloat::from(2f64).into(),
Expand All @@ -117,7 +117,7 @@ mod tests {
// FIXME: Allow for now, wait for https://github.com/rust-lang/rust/issues/67058 to reach stable
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
#[test]
fn test_reduce_deviation_standard_float2() {
fn test_deviation_standard_float2() {
let input = &RadonArray::from(vec![
RadonFloat::from(100f64).into(),
RadonFloat::from(256f64).into(),
Expand All @@ -136,7 +136,7 @@ mod tests {
}

#[test]
fn test_reduce_deviation_standard_float_arrays() {
fn test_deviation_standard_float_arrays() {
let array_1 = RadonTypes::from(RadonArray::from(vec![
RadonFloat::from(1f64).into(),
RadonFloat::from(2f64).into(),
Expand All @@ -158,7 +158,7 @@ mod tests {
}

#[test]
fn test_reduce_deviation_standard_float_arrays_different_size() {
fn test_deviation_standard_float_arrays_different_size() {
let array_1 = RadonTypes::from(RadonArray::from(vec![
RadonFloat::from(1f64).into(),
RadonFloat::from(2f64).into(),
Expand All @@ -182,7 +182,7 @@ mod tests {
}

#[test]
fn test_reduce_deviation_standard_float_array_of_arrays() {
fn test_deviation_standard_float_array_of_arrays() {
let array_11 = RadonTypes::from(RadonArray::from(vec![
RadonFloat::from(1f64).into(),
RadonFloat::from(7f64).into(),
Expand Down Expand Up @@ -232,21 +232,21 @@ mod tests {
}

#[test]
fn test_operate_reduce_deviation_standard_empty() {
fn test_operate_deviation_standard_empty() {
let input = RadonArray::from(vec![]);
let output = standard(&input).unwrap();
assert_eq!(output, RadonTypes::from(RadonFloat::from(std::f64::NAN)));
}

#[test]
fn test_operate_reduce_deviation_standard_one_element() {
fn test_operate_deviation_standard_one_element() {
let input = RadonArray::from(vec![RadonTypes::Float(RadonFloat::from(4f64))]);
let output = standard(&input).unwrap();
assert_eq!(output, RadonTypes::from(RadonFloat::from(0f64)));
}

#[test]
fn test_reduce_deviation_standard_integer() {
fn test_deviation_standard_integer() {
let input = &RadonArray::from(vec![
RadonInteger::from(1i128).into(),
RadonInteger::from(2i128).into(),
Expand All @@ -259,7 +259,7 @@ mod tests {
}

#[test]
fn test_reduce_deviation_standard_integer_arrays() {
fn test_deviation_standard_integer_arrays() {
let array_1 = RadonTypes::from(RadonArray::from(vec![
RadonInteger::from(1i128).into(),
RadonInteger::from(6i128).into(),
Expand All @@ -281,7 +281,7 @@ mod tests {
}

#[test]
fn test_reduce_deviation_standard_string_unsupported() {
fn test_deviation_standard_string_unsupported() {
let input = &RadonArray::from(vec![
RadonString::from("Hello").into(),
RadonString::from("world").into(),
Expand All @@ -297,7 +297,7 @@ mod tests {
}

#[test]
fn test_reduce_deviation_standard_float_int_arrays() {
fn test_deviation_standard_float_int_arrays() {
let array_1 = RadonTypes::from(RadonArray::from(vec![
RadonFloat::from(1f64).into(),
RadonFloat::from(2f64).into(),
Expand Down
30 changes: 0 additions & 30 deletions rad/src/reducers/median.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,9 @@ mod tests {
use super::*;

use crate::{
current_active_wips,
operators::array::reduce,
types::{float::RadonFloat, integer::RadonInteger, string::RadonString},
RadError::ModeEmpty,
};
use serde_cbor::Value;

#[test]
fn test_operate_reduce_median_empty() {
Expand Down Expand Up @@ -288,31 +285,4 @@ mod tests {
let output = median(&input).unwrap();
assert_eq!(output, expected);
}

#[test]
fn test_reduce_average_median_tapi_activation() {
let mut active_wips = current_active_wips();
let mut context = ReportContext::default();
context.active_wips = Some(active_wips.clone());
let input = &RadonArray::from(vec![
RadonFloat::from(1f64).into(),
RadonFloat::from(2f64).into(),
RadonFloat::from(2f64).into(),
]);
let args = &[Value::Integer(RadonReducers::AverageMedian as i128)];

let expected_err = RadError::UnsupportedReducer {
array: input.clone(),
reducer: "RadonReducers::AverageMedian".to_string(),
};
let output = reduce(input, args, &mut context).unwrap_err();
assert_eq!(output, expected_err);

// Activate WIP-0017
active_wips.active_wips.insert("WIP0017".to_string(), 0);
context.active_wips = Some(active_wips);
let expected = RadonTypes::from(RadonFloat::from(2f64));
let output = reduce(input, args, &mut context).unwrap();
assert_eq!(output, expected);
}
}
87 changes: 87 additions & 0 deletions rad/src/reducers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,90 @@ pub fn reduce(
})
}
}

#[cfg(test)]
mod tests {
use crate::{
current_active_wips,
error::RadError,
reducers::{reduce, RadonReducers},
types::{array::RadonArray, float::RadonFloat, RadonTypes},
};
use witnet_data_structures::radon_report::ReportContext;

#[test]
fn test_reduce_average_mean_float() {
let input = &RadonArray::from(vec![
RadonFloat::from(1f64).into(),
RadonFloat::from(2f64).into(),
]);
let expected = RadonTypes::from(RadonFloat::from(1.5f64));

let output = reduce(
input,
RadonReducers::AverageMean,
&mut ReportContext::default(),
)
.unwrap();

assert_eq!(output, expected);
}

#[test]
fn test_reduce_deviation_standard_float() {
let input = &RadonArray::from(vec![
RadonFloat::from(1f64).into(),
RadonFloat::from(2f64).into(),
]);
let expected = RadonTypes::from(RadonFloat::from(0.5));

let output = reduce(
input,
RadonReducers::DeviationStandard,
&mut ReportContext::default(),
)
.unwrap();

assert_eq!(output, expected);
}

#[test]
fn test_reduce_average_median_tapi_activation() {
let mut active_wips = current_active_wips();
let mut context = ReportContext::default();
context.active_wips = Some(active_wips.clone());
let input = &RadonArray::from(vec![
RadonFloat::from(1f64).into(),
RadonFloat::from(2f64).into(),
RadonFloat::from(2f64).into(),
]);

let expected_err = RadError::UnsupportedReducer {
array: input.clone(),
reducer: "RadonReducers::AverageMedian".to_string(),
};
let output = reduce(input, RadonReducers::AverageMedian, &mut context).unwrap_err();

assert_eq!(output, expected_err);

// Activate WIP-0017
active_wips.active_wips.insert("WIP0017".to_string(), 0);
context.active_wips = Some(active_wips);
let expected = RadonTypes::from(RadonFloat::from(2f64));
let output = reduce(input, RadonReducers::AverageMedian, &mut context).unwrap();

assert_eq!(output, expected);
}

#[test]
fn test_reduce_mode_float() {
let input = &RadonArray::from(vec![
RadonFloat::from(1f64).into(),
RadonFloat::from(2f64).into(),
RadonFloat::from(2f64).into(),
]);
let expected = RadonTypes::from(RadonFloat::from(2f64));
let output = reduce(input, RadonReducers::Mode, &mut ReportContext::default()).unwrap();
assert_eq!(output, expected);
}
}
Loading

0 comments on commit 294e070

Please sign in to comment.