Skip to content

Commit

Permalink
tests(rust): Add test for arithmetic & aggregate methods on sorted Ch…
Browse files Browse the repository at this point in the history
…unkedArray (#5189)

Co-authored-by: Danny van Kooten <dannyvankooten@users.noreply.github.com>
  • Loading branch information
dannyvankooten and dannyvankooten committed Oct 13, 2022
1 parent 2e6709c commit c84611d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
25 changes: 14 additions & 11 deletions polars/polars-core/src/chunked_array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,17 +669,20 @@ pub(crate) mod test {
let a = a.sort(false);
let b = a.into_iter().collect::<Vec<_>>();
assert_eq!(b, [Some("a"), Some("b"), Some("c")]);
assert_eq!(a.is_sorted(), true);
}

#[test]
fn arithmetic() {
let s1 = get_chunked_array();
println!("{:?}", s1.chunks);
let s2 = &s1;
let s1 = &s1;
println!("{:?}", s1 + s2);
println!("{:?}", s1 - s2);
println!("{:?}", s1 * s2);
let a = &Int32Chunked::new("a", &[1, 100, 6, 40]);
let b = &Int32Chunked::new("b", &[-1, 2, 3, 4]);

// Not really asserting anything here but shill making sure the code is exercised
// This (and more) is properly tested from the integration test suite and Python bindings.
println!("{:?}", a + b);
println!("{:?}", a - b);
println!("{:?}", a * b);
println!("{:?}", a / b);
}

#[test]
Expand Down Expand Up @@ -708,11 +711,11 @@ pub(crate) mod test {
}

#[test]
fn aggregates_numeric() {
let a = get_chunked_array();
assert_eq!(a.max(), Some(3));
fn aggregates() {
let a = &Int32Chunked::new("a", &[1, 100, 10, 9]);
assert_eq!(a.max(), Some(100));
assert_eq!(a.min(), Some(1));
assert_eq!(a.sum(), Some(6))
assert_eq!(a.sum(), Some(120))
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions polars/tests/it/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod list;
mod pivot;
#[cfg(feature = "rolling_window")]
mod rolling_window;
mod series;
mod utils;

use polars::prelude::*;
38 changes: 38 additions & 0 deletions polars/tests/it/core/series.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use polars::prelude::*;
use polars::series::*;

#[test]
fn test_series_arithmetic() {
let a = &Series::new("a", &[1, 100, 6, 40]);
let b = &Series::new("b", &[-1, 2, 3, 4]);
assert_eq!(a + b, Series::new("a", &[0, 102, 9, 44]));
assert_eq!(a - b, Series::new("a", &[2, 98, 3, 36]));
assert_eq!(a * b, Series::new("a", &[-1, 200, 18, 160]));
assert_eq!(a / b, Series::new("a", &[-1, 50, 2, 10]));
}

#[test]
fn test_aggregates() {
let a = &Series::new("a", &[9, 11, 10]);
assert_eq!(a.max(), Some(11));
assert_eq!(a.min(), Some(9));
assert_eq!(a.sum(), Some(30));
assert_eq!(a.mean(), Some(10.0));
assert_eq!(a.median(), Some(10.0));
}

#[test]
fn test_min_max_sorted_asc() {
let a = &mut Series::new("a", &[1, 2, 3, 4]);
a.set_sorted(IsSorted::Ascending);
assert_eq!(a.max(), Some(4));
assert_eq!(a.min(), Some(1));
}

#[test]
fn test_min_max_sorted_desc() {
let mut a = &mut Series::new("a", &[4, 3, 2, 1]);
a.set_sorted(IsSorted::Descending);
assert_eq!(a.max(), Some(4));
assert_eq!(a.min(), Some(1));
}

0 comments on commit c84611d

Please sign in to comment.