Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#![no_std]
#![allow(incomplete_features)]
#![feature(
adt_const_params,
const_fn_floating_point_arithmetic,
generic_const_exprs
)]

#[cfg(test)]
mod tests;

#[doc(hidden)]
pub struct __StaticSorter<T: Copy + PartialOrd, const N: usize> {
Expand Down
44 changes: 44 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::staticsort;

const X: [usize; 12] = [1, 6, 2, 5, 3, 4, 7, 12, 8, 11, 9, 10];

const Y: [f64; 12] = [
1.0, 6.0, 2.0, 5.0,
3.0, 4.0, 7.0, 12.0,
8.0, 11.0, 9.0, 10.0,
];

// The macro takes the following parameters in the order they're
// listed: type to sort, index to start at, index to end at, and
// either the name of an existing const array variable or just
// a directly-passed "anonymous" array.

// Sort all of X:
static XX: [usize; 12] = staticsort!(usize, 0, 11, X);
// Just sort half of Y:
static YY: [f64; 12] = staticsort!(f64, 0, 6, Y);
// Sort all of an array that's the same as X, but passed
// directly as a parameter:
static ZZ: [usize; 12] = staticsort!(
usize,
0,
11,
[1, 6, 2, 5, 3, 4, 7, 12, 8, 11, 9, 10]
);

#[test]
fn assert_sort_int() {
assert!(!X.is_sorted(), "Original array was already sorted!");
assert!(XX.is_sorted());
}

#[test]
fn assert_sort_float() {
assert!(!Y.is_sorted(), "Original array was already sorted!");
assert!(YY[0..YY.len()/2].is_sorted());
}

#[test]
fn assert_sort_direct() {
assert!(ZZ.is_sorted());
}