Move append_vec::test_utils into its own file (#25407)

This commit is contained in:
Brooks Prumo 2022-05-20 13:37:45 -05:00 committed by GitHub
parent de2033f2f2
commit a25212b087
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 47 deletions

View File

@ -28,6 +28,8 @@ use {
},
};
pub mod test_utils;
// Data placement should be aligned at the next boundary. Without alignment accessing the memory may
// crash on some architectures.
pub const ALIGN_BOUNDARY_OFFSET: usize = mem::size_of::<u64>();
@ -569,53 +571,6 @@ impl AppendVec {
}
}
pub mod test_utils {
use {
super::StoredMeta,
rand::{distributions::Alphanumeric, thread_rng, Rng},
solana_sdk::{account::AccountSharedData, pubkey::Pubkey},
std::{fs::create_dir_all, path::PathBuf},
};
pub struct TempFile {
pub path: PathBuf,
}
impl Drop for TempFile {
fn drop(&mut self) {
let mut path = PathBuf::new();
std::mem::swap(&mut path, &mut self.path);
let _ignored = std::fs::remove_file(path);
}
}
pub fn get_append_vec_dir() -> String {
std::env::var("FARF_DIR").unwrap_or_else(|_| "farf/append_vec_tests".to_string())
}
pub fn get_append_vec_path(path: &str) -> TempFile {
let out_dir = get_append_vec_dir();
let rand_string: String = thread_rng().sample_iter(&Alphanumeric).take(30).collect();
let dir = format!("{}/{}", out_dir, rand_string);
let mut buf = PathBuf::new();
buf.push(&format!("{}/{}", dir, path));
create_dir_all(dir).expect("Create directory failed");
TempFile { path: buf }
}
pub fn create_test_account(sample: usize) -> (StoredMeta, AccountSharedData) {
let data_len = sample % 256;
let mut account = AccountSharedData::new(sample as u64, 0, &Pubkey::default());
account.set_data((0..data_len).map(|_| data_len as u8).collect());
let stored_meta = StoredMeta {
write_version: 0,
pubkey: Pubkey::default(),
data_len: data_len as u64,
};
(stored_meta, account)
}
}
#[cfg(test)]
pub mod tests {
use {

View File

@ -0,0 +1,48 @@
//! Helpers for AppendVec tests and benches
use {
super::StoredMeta,
rand::{distributions::Alphanumeric, Rng},
solana_sdk::{account::AccountSharedData, pubkey::Pubkey},
std::path::PathBuf,
};
pub struct TempFile {
pub path: PathBuf,
}
impl Drop for TempFile {
fn drop(&mut self) {
let mut path = PathBuf::new();
std::mem::swap(&mut path, &mut self.path);
let _ignored = std::fs::remove_file(path);
}
}
pub fn get_append_vec_dir() -> String {
std::env::var("FARF_DIR").unwrap_or_else(|_| "farf/append_vec_tests".to_string())
}
pub fn get_append_vec_path(path: &str) -> TempFile {
let out_dir = get_append_vec_dir();
let rand_string: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(30)
.collect();
let dir = format!("{}/{}", out_dir, rand_string);
let mut buf = PathBuf::new();
buf.push(&format!("{}/{}", dir, path));
std::fs::create_dir_all(dir).expect("Create directory failed");
TempFile { path: buf }
}
pub fn create_test_account(sample: usize) -> (StoredMeta, AccountSharedData) {
let data_len = sample % 256;
let mut account = AccountSharedData::new(sample as u64, 0, &Pubkey::default());
account.set_data((0..data_len).map(|_| data_len as u8).collect());
let stored_meta = StoredMeta {
write_version: 0,
pubkey: Pubkey::default(),
data_len: data_len as u64,
};
(stored_meta, account)
}