Skip to content

Commit

Permalink
rune: Move tests around and Send + Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Nov 5, 2024
1 parent 9f15557 commit 5d16aaf
Show file tree
Hide file tree
Showing 10 changed files with 888 additions and 898 deletions.
29 changes: 3 additions & 26 deletions crates/rune/src/compile/names.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(test)]
mod tests;

use core::mem::replace;

use crate::alloc;
Expand Down Expand Up @@ -113,29 +116,3 @@ impl TryClone for Node {
})
}
}

#[cfg(test)]
mod tests {
use super::Names;
use crate::support::Result;

#[test]
fn insert() -> Result<()> {
let mut names = Names::default();
assert!(!names.contains(["test"])?);
assert!(!names.insert(["test"]).unwrap());
assert!(names.contains(["test"])?);
assert!(names.insert(["test"]).unwrap());
Ok(())
}

#[test]
fn contains() -> Result<()> {
let mut names = Names::default();
assert!(!names.contains(["test"])?);
assert!(!names.insert(["test"]).unwrap());
assert!(names.contains(["test"])?);
assert!(names.insert(["test"]).unwrap());
Ok(())
}
}
22 changes: 22 additions & 0 deletions crates/rune/src/compile/names/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use super::Names;
use crate::support::Result;

#[test]
fn insert() -> Result<()> {
let mut names = Names::default();
assert!(!names.contains(["test"])?);
assert!(!names.insert(["test"]).unwrap());
assert!(names.contains(["test"])?);
assert!(names.insert(["test"]).unwrap());
Ok(())
}

#[test]
fn contains() -> Result<()> {
let mut names = Names::default();
assert!(!names.contains(["test"])?);
assert!(!names.insert(["test"]).unwrap());
assert!(names.contains(["test"])?);
assert!(names.insert(["test"]).unwrap());
Ok(())
}
194 changes: 3 additions & 191 deletions crates/rune/src/compile/v1/slots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#![allow(clippy::bool_assert_comparison)]

#[cfg(test)]
mod tests;

use core::fmt;
use core::slice;

Expand Down Expand Up @@ -151,194 +154,3 @@ impl Iterator for Iter<'_> {
}
}
}

#[cfg(test)]
mod tests {
use super::Slots;

macro_rules! slab_eq {
($slab:expr, $expected:expr) => {{
let expected: &[usize] = &$expected[..];

if !$slab.iter().eq(expected.iter().copied()) {
panic!("{:?} != {:?}", $slab, expected);
}
}};
}

#[test]
fn iter() {
let mut slab = Slots::new();

assert_eq!(slab.insert(), Ok(0));
assert_eq!(slab.insert(), Ok(1));
assert_eq!(slab.insert(), Ok(2));
assert_eq!(slab.insert(), Ok(3));
assert_eq!(slab.insert(), Ok(4));
slab_eq!(slab, [0, 1, 2, 3, 4]);

assert_eq!(slab.remove(2), true);
slab_eq!(slab, [0, 1, 3, 4]);

assert_eq!(slab.remove(3), true);
slab_eq!(slab, [0, 1, 4]);

assert_eq!(slab.remove(0), true);
slab_eq!(slab, [1, 4]);

assert_eq!(slab.remove(1), true);
slab_eq!(slab, [4]);

assert_eq!(slab.remove(4), true);
slab_eq!(slab, []);

assert_eq!(slab.insert(), Ok(0));
}

#[test]
fn insert() {
let mut slab = Slots::new();
assert_eq!(slab.insert(), Ok(0));
assert_eq!(slab.insert(), Ok(1));
assert_eq!(slab.insert(), Ok(2));
assert_eq!(slab.remove(1), true);
assert_eq!(slab.remove(1), false);
assert_eq!(slab.insert(), Ok(1));
assert_eq!(slab.insert(), Ok(3));
assert_eq!(slab.insert(), Ok(4));
}

#[test]
fn insert_boundary() {
let mut slab = Slots::new();

for n in 0..167 {
assert_eq!(slab.push(), Ok(n));
}

for n in 167..1024 {
assert_eq!(slab.insert(), Ok(n));
}

for n in 128..256 {
assert!(slab.remove(n));
}

assert_eq!(slab.push(), Ok(1024));
assert_eq!(slab.push(), Ok(1025));

for n in (128..256).chain(1026..2047) {
assert_eq!(slab.insert(), Ok(n));
}

for n in 2047..3000 {
assert_eq!(slab.push(), Ok(n));
}
}

#[test]
fn push() {
let mut slab = Slots::new();
assert_eq!(slab.insert(), Ok(0));
assert_eq!(slab.push(), Ok(1));
assert_eq!(slab.push(), Ok(2));
assert_eq!(slab.remove(0), true);
assert_eq!(slab.remove(0), false);
assert_eq!(slab.insert(), Ok(0));
assert_eq!(slab.insert(), Ok(3));
assert_eq!(slab.remove(2), true);
assert_eq!(slab.remove(0), true);
assert_eq!(slab.insert(), Ok(0));
assert_eq!(slab.insert(), Ok(2));
assert_eq!(slab.insert(), Ok(4));
assert_eq!(slab.push(), Ok(5));
}

#[test]
fn push_tail_hole() {
let mut slab = Slots::new();
assert_eq!(slab.insert(), Ok(0));
assert_eq!(slab.insert(), Ok(1));
assert_eq!(slab.insert(), Ok(2));

assert_eq!(slab.remove(1), true);
assert_eq!(slab.remove(2), true);
assert_eq!(slab.remove(2), false);

assert_eq!(slab.push(), Ok(1));
assert_eq!(slab.push(), Ok(2));
}

#[test]
fn push_pop() {
let mut slab = Slots::new();
assert_eq!(slab.insert(), Ok(0));
assert_eq!(slab.insert(), Ok(1));
assert_eq!(slab.insert(), Ok(2));
assert_eq!(slab.remove(1), true);

assert_eq!(slab.push(), Ok(3));
assert_eq!(slab.push(), Ok(4));
assert_eq!(slab.push(), Ok(5));
assert_eq!(slab.insert(), Ok(1));

assert_eq!(slab.remove(2), true);

assert_eq!(slab.remove(5), true);
assert_eq!(slab.remove(4), true);
assert_eq!(slab.remove(3), true);
assert_eq!(slab.remove(1), true);
assert_eq!(slab.remove(0), true);
assert_eq!(slab.remove(0), false);
}

#[test]
fn bad_test() {
let mut slab = Slots::new();
assert_eq!(slab.insert(), Ok(0));
assert_eq!(slab.insert(), Ok(1));
assert_eq!(slab.insert(), Ok(2));
assert_eq!(slab.insert(), Ok(3));

assert_eq!(slab.remove(2), true);
assert_eq!(slab.remove(3), true);

assert_eq!(slab.insert(), Ok(2));
}

#[test]
fn bug1() {
let mut slab = Slots::new();
assert_eq!(slab.insert(), Ok(0));
assert_eq!(slab.insert(), Ok(1));
assert_eq!(slab.insert(), Ok(2));

assert_eq!(slab.remove(2), true);
assert_eq!(slab.remove(1), true);

assert_eq!(slab.insert(), Ok(1));
}

#[test]
fn push_first() {
let mut slab = Slots::new();
assert_eq!(slab.push(), Ok(0));
assert_eq!(slab.insert(), Ok(1));
assert_eq!(slab.push(), Ok(2));
}

#[test]
fn test_bug() {
let mut slab = Slots::new();
assert_eq!(slab.insert(), Ok(0));
assert_eq!(slab.remove(0), true);
assert_eq!(slab.push(), Ok(0));
assert_eq!(slab.insert(), Ok(1));
assert_eq!(slab.push(), Ok(2));
assert_eq!(slab.remove(2), true);
assert_eq!(slab.insert(), Ok(2));
assert_eq!(slab.remove(2), true);
assert_eq!(slab.remove(0), true);
assert_eq!(slab.remove(0), false);
}
}
Loading

0 comments on commit 5d16aaf

Please sign in to comment.