Files
dioxus/packages/generational-box/tests/basic.rs
T
Jonathan Kelley 0cbcef735f Pre-release 0.6.0-alpha.0 (#2755)
* CI: add docsrs to ci

* add env vars

* no need for tokio things

* whoops, wrong rust nightly

* add swc

* bump crate versions to 0.6.0-alpha.0

* hoist up warnings and shuffle cargo.toml

* update cargos and disable warnings from ci

* attempt fix of aws

* attempt disabling aws_lc_sys globally

* pull in lc-sys

* add fips feature to fix docs

* turn on features for aws-lc-sys on cargo-doc-all

* pass along more args

* fix cfg docs

* fix: flakey gen box test

* fix: doc_cfg in static gen

* dont include readme

* fix error on static gen

* remove random line

* temporarily disable warnings

* simplify axum desktop so it doesnt count as a lib

* fix binstall link

* bump locks

* lock fixes

* revert axum desktop

* dont publish some crates
2024-07-31 22:37:39 -05:00

152 lines
3.9 KiB
Rust

use generational_box::{GenerationalBox, Storage, SyncStorage, UnsyncStorage};
/// # Example
///
/// ```compile_fail
/// let data = String::from("hello world");
/// let owner = UnsyncStorage::owner();
/// let key = owner.insert(&data);
/// drop(data);
/// assert_eq!(*key.read(), "hello world");
/// ```
#[allow(unused)]
fn compile_fail() {}
#[test]
fn leaking_is_ok() {
fn leaking_is_ok_test<S: Storage<String>>() {
let data = String::from("hello world");
let key;
{
// create an owner
let owner = S::owner();
// insert data into the store
key = owner.insert(data);
// don't drop the owner
std::mem::forget(owner);
}
assert_eq!(
key.try_read().as_deref().unwrap(),
&"hello world".to_string()
);
}
leaking_is_ok_test::<UnsyncStorage>();
leaking_is_ok_test::<SyncStorage>();
}
#[test]
fn drops() {
fn drops_test<S: Storage<String>>() {
let data = String::from("hello world");
let key;
{
// create an owner
let owner = S::owner();
// insert data into the store
key = owner.insert(data);
// drop the owner
}
assert!(key.try_read().is_err());
}
drops_test::<UnsyncStorage>();
drops_test::<SyncStorage>();
}
#[test]
fn works() {
fn works_test<S: Storage<i32>>() {
let owner = S::owner();
let key = owner.insert(1);
assert_eq!(*key.read(), 1);
}
works_test::<UnsyncStorage>();
works_test::<SyncStorage>();
}
#[test]
fn insert_while_reading() {
fn insert_while_reading_test<S: Storage<String> + Storage<&'static i32>>() {
let owner = S::owner();
let key;
{
let data: String = "hello world".to_string();
key = owner.insert(data);
}
let value = key.read();
owner.insert(&1);
assert_eq!(*value, "hello world");
}
insert_while_reading_test::<UnsyncStorage>();
insert_while_reading_test::<SyncStorage>();
}
#[test]
#[should_panic]
fn panics() {
fn panics_test<S: Storage<i32>>() {
let owner = S::owner();
let key = owner.insert(1);
drop(owner);
assert_eq!(*key.read(), 1);
}
panics_test::<UnsyncStorage>();
panics_test::<SyncStorage>();
}
#[test]
fn fuzz() {
fn maybe_owner_scope<S: Storage<String>>(
valid_keys: &mut Vec<GenerationalBox<String, S>>,
invalid_keys: &mut Vec<GenerationalBox<String, S>>,
path: &mut Vec<u8>,
) {
let branch_cutoff = 5;
let children = if path.len() < branch_cutoff {
rand::random::<u8>() % 4
} else {
rand::random::<u8>() % 2
};
for i in 0..children {
let owner = S::owner();
let key = owner.insert(format!("hello world {path:?}"));
println!("created new box {key:?}");
valid_keys.push(key);
path.push(i);
// read all keys
println!("{:?}", path);
for key in valid_keys.iter() {
println!("reading {key:?}");
let value = key.read();
println!("{:?}", &*value);
assert!(value.starts_with("hello world"));
}
for key in invalid_keys.iter() {
println!("reading invalid {key:?}");
assert!(key.try_read().is_err());
}
maybe_owner_scope(valid_keys, invalid_keys, path);
let invalid = valid_keys.pop().unwrap();
println!("popping {invalid:?}");
invalid_keys.push(invalid);
path.pop();
}
}
for _ in 0..10 {
maybe_owner_scope::<UnsyncStorage>(&mut Vec::new(), &mut Vec::new(), &mut Vec::new());
}
for _ in 0..10 {
maybe_owner_scope::<SyncStorage>(&mut Vec::new(), &mut Vec::new(), &mut Vec::new());
}
}