Skip to content

Commit

Permalink
New tests for sum_with_threads_rel function in Rust. Corresponding …
Browse files Browse the repository at this point in the history
…tests in R pass.
  • Loading branch information
jibarozzo committed Dec 15, 2024
1 parent 15aac7a commit 93ab57f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 24 deletions.
100 changes: 79 additions & 21 deletions src/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use savvy::{savvy, NumericSexp, NumericTypedSexp, IntegerSexp, RealSexp, Sexp};
use std::thread;

/// Calculate the sum of a vector of integers using multiple threads.
/// Calculate the sum of a vector of integers/doubles using multiple threads.
///
/// @param x A vector of integers to sum over.
/// @param x A vector of integers/doubles to sum over.
/// @param n The number of threads used to compute this calculation (int).
///
/// @return The sum of all elements of the input vector.
Expand Down Expand Up @@ -83,65 +83,123 @@ fn sum_with_threads_rel_impl(x: Vec<f64>, n: usize) -> f64 {
mod tests {
use crate::{sum_with_threads_int_impl, sum_with_threads_rel_impl};

// Integers
/// Integers
#[test]
fn test_single_thread() {
fn test_single_thread_int() {
let numbers = vec![1, 2, 3, 4, 5];
let n = 1;
assert_eq!(sum_with_threads_impl(numbers, n), 15);
assert_eq!(sum_with_threads_int_impl(numbers, n), 15);
}

#[test]
fn test_multiple_threads() {
fn test_multiple_threads_int() {
let x = vec![1, 2, 3, 4];
let num_threads = 2;

let result = sum_with_threads_impl(x, num_threads);
let result = sum_with_threads_int_impl(x, num_threads);
assert_eq!(result, 10);
}

#[test]
fn test_more_threads_than_elements() {
fn test_more_threads_than_elements_int() {
let numbers = vec![1, 2, 3, 4, 5];
let n = 10;
assert_eq!(sum_with_threads_impl(numbers, n), 15);
assert_eq!(sum_with_threads_int_impl(numbers, n), 15);
}

#[test]
fn test_empty_vector() {
fn test_empty_vector_int() {
let numbers: Vec<i32> = vec![];
let n = 4;
assert_eq!(sum_with_threads_impl(numbers, n), 0);
assert_eq!(sum_with_threads_int_impl(numbers, n), 0);
}

#[test]
fn test_large_numbers() {
fn test_large_numbers_int() {
let numbers = vec![1_000_000, 2_000_000, 3_000_000];
let n = 3;
assert_eq!(sum_with_threads_impl(numbers, n), 6_000_000);
assert_eq!(sum_with_threads_int_impl(numbers, n), 6_000_000);
}

#[test]
fn test_negative_numbers() {
fn test_negative_numbers_int() {
let numbers = vec![-1, -2, -3, -4, -5];
let n = 2;
assert_eq!(sum_with_threads_impl(numbers, n), -15);
assert_eq!(sum_with_threads_int_impl(numbers, n), -15);
}

#[test]
fn test_mixed_numbers() {
fn test_mixed_numbers_int() {
let numbers = vec![-1, 2, -3, 4, -5, 6];
let n = 3;
assert_eq!(sum_with_threads_impl(numbers, n), 3);
}

assert_eq!(sum_with_threads_int_impl(numbers, n), 3);
}

#[test]
fn test_large_vector() {
fn test_large_vector_int() {
let numbers: Vec<i32> = (1..=1_000).collect();
let n = 4;
let expected_sum: i32 = (1..=1_000).sum();
assert_eq!(sum_with_threads_impl(numbers, n), expected_sum);
assert_eq!(sum_with_threads_int_impl(numbers, n), expected_sum);
}
}

/// Real/doubles
#[test]
fn test_single_thread_rel() {
let numbers = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let n = 1;
assert_eq!(sum_with_threads_rel_impl(numbers, n), 15.0);
}

#[test]
fn test_multiple_threads_rel() {
let x = vec![1.0, 2.0, 3.0, 4.0];
let num_threads = 2;

let result = sum_with_threads_rel_impl(x, num_threads);
assert_eq!(result, 10.0);
}

#[test]
fn test_more_threads_than_elements_rel() {
let numbers = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let n = 10;
assert_eq!(sum_with_threads_rel_impl(numbers, n), 15.0);
}

#[test]
fn test_empty_vector_rel() {
let numbers: Vec<f64> = vec![];
let n = 4;
assert_eq!(sum_with_threads_rel_impl(numbers, n), 0);
}

#[test]
fn test_large_numbers_rel() {
let numbers = vec![1_000_000, 2_000_000, 3_000_000];
let n = 3;
assert_eq!(sum_with_threads_rel_impl(numbers, n), 6_000_000);
}

#[test]
fn test_negative_numbers_rel() {
let numbers = vec![-1.0, -2.0, -3.0, -4.0, -5.0];
let n = 2;
assert_eq!(sum_with_threads_rel_impl(numbers, n), -15.0);
}

#[test]
fn test_mixed_numbers_rel() {
let numbers = vec![-1.0, 2.0, -3.0, 4.0, -5.0, 6.0];
let n = 3;
assert_eq!(sum_with_threads_rel_impl(numbers, n), 3.0);
}

#[test]
fn test_large_vector_rel() {
let numbers: Vec<f64> = (1..=1_000).collect();
let n = 4;
let expected_sum: i32 = (1..=1_000).sum();
assert_eq!(sum_with_threads_rel_impl(numbers, n), expected_sum);
}
13 changes: 10 additions & 3 deletions tests/testthat/test-sum_with_threads.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
test_that("sums as expected", {
vector <- 1:10
test_that("sums as integers expected", {
vector_int <- 1:10

out <- blazr::sum_with_threads(1:10, 5L)
expect_equal(out, sum(vector))
expect_equal(out, sum(vector_int))
})

test_that("sums as doubles expected", {
vector_dbl <- c(1.5, 2.7, 4.25)

out <- blazr::sum_with_threads(vector_dbl, 5L)
expect_equal(out, sum(vector_dbl))
})

0 comments on commit 93ab57f

Please sign in to comment.