| 1 |
use super::*; |
| 2 |
use std::cell::Cell; |
| 3 |
use std::mem; |
| 4 |
use std::panic::{self, AssertUnwindSafe}; |
| 5 |
use std::ptr; |
| 6 |
|
| 7 |
struct DropTracker<'a>(&'a Cell<u32>); |
| 8 |
impl<'a> Drop for DropTracker<'a> { |
| 9 |
fn drop(&mut self) { |
| 10 |
self.0.set(self.0.get() + 1); |
| 11 |
} |
| 12 |
} |
| 13 |
|
| 14 |
struct Node<'a, 'b: 'a>(Option<&'a Node<'a, 'b>>, u32, DropTracker<'b>); |
| 15 |
|
| 16 |
#[test] |
| 17 |
fn arena_as_intended() { |
| 18 |
let drop_counter = Cell::new(0); |
| 19 |
{ |
| 20 |
let arena = Arena::with_capacity(2); |
| 21 |
|
| 22 |
let mut node: &Node = arena.alloc(Node(None, 1, DropTracker(&drop_counter))); |
| 23 |
assert_eq!(arena.chunks.borrow().rest.len(), 0); |
| 24 |
|
| 25 |
node = arena.alloc(Node(Some(node), 2, DropTracker(&drop_counter))); |
| 26 |
assert_eq!(arena.chunks.borrow().rest.len(), 0); |
| 27 |
|
| 28 |
node = arena.alloc(Node(Some(node), 3, DropTracker(&drop_counter))); |
| 29 |
assert_eq!(arena.chunks.borrow().rest.len(), 1); |
| 30 |
|
| 31 |
node = arena.alloc(Node(Some(node), 4, DropTracker(&drop_counter))); |
| 32 |
assert_eq!(arena.chunks.borrow().rest.len(), 1); |
| 33 |
|
| 34 |
assert_eq!(node.1, 4); |
| 35 |
assert_eq!(node.0.unwrap().1, 3); |
| 36 |
assert_eq!(node.0.unwrap().0.unwrap().1, 2); |
| 37 |
assert_eq!(node.0.unwrap().0.unwrap().0.unwrap().1, 1); |
| 38 |
assert!(node.0.unwrap().0.unwrap().0.unwrap().0.is_none()); |
| 39 |
|
| 40 |
assert_eq!(arena.len(), 4); |
| 41 |
|
| 42 |
mem::drop(node); |
| 43 |
assert_eq!(drop_counter.get(), 0); |
| 44 |
|
| 45 |
let mut node: &Node = arena.alloc(Node(None, 5, DropTracker(&drop_counter))); |
| 46 |
assert_eq!(arena.chunks.borrow().rest.len(), 1); |
| 47 |
|
| 48 |
node = arena.alloc(Node(Some(node), 6, DropTracker(&drop_counter))); |
| 49 |
assert_eq!(arena.chunks.borrow().rest.len(), 1); |
| 50 |
|
| 51 |
node = arena.alloc(Node(Some(node), 7, DropTracker(&drop_counter))); |
| 52 |
assert_eq!(arena.chunks.borrow().rest.len(), 2); |
| 53 |
|
| 54 |
assert_eq!(drop_counter.get(), 0); |
| 55 |
|
| 56 |
assert_eq!(node.1, 7); |
| 57 |
assert_eq!(node.0.unwrap().1, 6); |
| 58 |
assert_eq!(node.0.unwrap().0.unwrap().1, 5); |
| 59 |
assert!(node.0.unwrap().0.unwrap().0.is_none()); |
| 60 |
|
| 61 |
assert_eq!(drop_counter.get(), 0); |
| 62 |
} |
| 63 |
assert_eq!(drop_counter.get(), 7); |
| 64 |
} |
| 65 |
|
| 66 |
#[test] |
| 67 |
fn ensure_into_vec_maintains_order_of_allocation() { |
| 68 |
let arena = Arena::with_capacity(1); // force multiple inner vecs |
| 69 |
for &s in &["t", "e", "s", "t"] { |
| 70 |
arena.alloc(String::from(s)); |
| 71 |
} |
| 72 |
let vec = arena.into_vec(); |
| 73 |
assert_eq!(vec, vec!["t", "e", "s", "t"]); |
| 74 |
} |
| 75 |
|
| 76 |
#[test] |
| 77 |
fn test_zero_cap() { |
| 78 |
let arena = Arena::with_capacity(0); |
| 79 |
let a = arena.alloc(1); |
| 80 |
let b = arena.alloc(2); |
| 81 |
assert_eq!(*a, 1); |
| 82 |
assert_eq!(*b, 2); |
| 83 |
assert_eq!(arena.len(), 2); |
| 84 |
} |
| 85 |
|
| 86 |
#[test] |
| 87 |
fn test_alloc_extend() { |
| 88 |
let arena = Arena::with_capacity(2); |
| 89 |
for i in 0..15 { |
| 90 |
let slice = arena.alloc_extend(0..i); |
| 91 |
for (j, &elem) in slice.iter().enumerate() { |
| 92 |
assert_eq!(j, elem); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
#[test] |
| 98 |
fn test_alloc_uninitialized() { |
| 99 |
const LIMIT: usize = 15; |
| 100 |
let drop_counter = Cell::new(0); |
| 101 |
unsafe { |
| 102 |
let arena: Arena<Node> = Arena::with_capacity(4); |
| 103 |
for i in 0..LIMIT { |
| 104 |
let slice = arena.alloc_uninitialized(i); |
| 105 |
for (j, elem) in slice.iter_mut().enumerate() { |
| 106 |
ptr::write(elem.as_mut_ptr(), Node(None, j as u32, DropTracker(&drop_counter))); |
| 107 |
} |
| 108 |
assert_eq!(drop_counter.get(), 0); |
| 109 |
} |
| 110 |
} |
| 111 |
assert_eq!(drop_counter.get(), (0..LIMIT).fold(0, |a, e| a + e) as u32); |
| 112 |
} |
| 113 |
|
| 114 |
#[test] |
| 115 |
fn test_alloc_extend_with_drop_counter() { |
| 116 |
let drop_counter = Cell::new(0); |
| 117 |
{ |
| 118 |
let arena = Arena::with_capacity(2); |
| 119 |
let iter = (0..100).map(|j| Node(None, j as u32, DropTracker(&drop_counter))); |
| 120 |
let older_ref = Some(&arena.alloc_extend(iter)[0]); |
| 121 |
assert_eq!(drop_counter.get(), 0); |
| 122 |
let iter = (0..100).map(|j| Node(older_ref, j as u32, DropTracker(&drop_counter))); |
| 123 |
arena.alloc_extend(iter); |
| 124 |
assert_eq!(drop_counter.get(), 0); |
| 125 |
} |
| 126 |
assert_eq!(drop_counter.get(), 200); |
| 127 |
} |
| 128 |
|
| 129 |
/// Test with bools. |
| 130 |
/// |
| 131 |
/// Bools, unlike integers, have invalid bit patterns. Therefore, ever having an uninitialized bool |
| 132 |
/// is insta-UB. Make sure miri doesn't find any such thing. |
| 133 |
#[test] |
| 134 |
fn test_alloc_uninitialized_bools() { |
| 135 |
const LEN: usize = 20; |
| 136 |
unsafe { |
| 137 |
let arena: Arena<bool> = Arena::with_capacity(2); |
| 138 |
let slice = arena.alloc_uninitialized(LEN); |
| 139 |
for elem in slice.iter_mut() { |
| 140 |
ptr::write(elem.as_mut_ptr(), true); |
| 141 |
} |
| 142 |
// Now it is fully initialized, we can safely transmute the slice. |
| 143 |
let slice: &mut [bool] = mem::transmute(slice); |
| 144 |
assert_eq!(&[true; LEN], slice); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/// Check nothing bad happens by panicking during initialization of borrowed slice. |
| 149 |
#[test] |
| 150 |
fn alloc_uninitialized_with_panic() { |
| 151 |
struct Dropper(bool); |
| 152 |
|
| 153 |
impl Drop for Dropper { |
| 154 |
fn drop(&mut self) { |
| 155 |
// Just make sure we touch the value, to make sure miri would bite if it was |
| 156 |
// unitialized |
| 157 |
if self.0 { |
| 158 |
panic!(); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
let mut reached_first_init = false; |
| 163 |
panic::catch_unwind(AssertUnwindSafe(|| unsafe { |
| 164 |
let arena: Arena<Dropper> = Arena::new(); |
| 165 |
arena.reserve_extend(2); |
| 166 |
let uninitialized = arena.uninitialized_array(); |
| 167 |
assert!((*uninitialized).len() >= 2); |
| 168 |
ptr::write((*uninitialized)[0].as_mut_ptr(), Dropper(false)); |
| 169 |
reached_first_init = true; |
| 170 |
panic!("To drop the arena"); |
| 171 |
// If it didn't panic, we would continue by initializing the second one and confirming by |
| 172 |
// .alloc_uninitialized(); |
| 173 |
})).unwrap_err(); |
| 174 |
assert!(reached_first_init); |
| 175 |
} |
| 176 |
|
| 177 |
#[test] |
| 178 |
fn test_uninitialized_array() { |
| 179 |
let arena = Arena::with_capacity(2); |
| 180 |
let uninit = arena.uninitialized_array(); |
| 181 |
arena.alloc_extend(0..2); |
| 182 |
unsafe { |
| 183 |
for (&a, b) in (&*uninit).iter().zip(0..2) { |
| 184 |
assert_eq!(a.assume_init(), b); |
| 185 |
} |
| 186 |
assert!((&*arena.uninitialized_array()).as_ptr() != (&*uninit).as_ptr()); |
| 187 |
arena.alloc(0); |
| 188 |
let uninit = arena.uninitialized_array(); |
| 189 |
assert_eq!((&*uninit).len(), 3); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
#[test] |
| 194 |
fn dont_trust_the_iterator_size() { |
| 195 |
use std::iter::repeat; |
| 196 |
|
| 197 |
struct WrongSizeIter<I>(I); |
| 198 |
impl<I> Iterator for WrongSizeIter<I> |
| 199 |
where |
| 200 |
I: Iterator, |
| 201 |
{ |
| 202 |
type Item = I::Item; |
| 203 |
|
| 204 |
fn next(&mut self) -> Option<Self::Item> { |
| 205 |
self.0.next() |
| 206 |
} |
| 207 |
|
| 208 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 209 |
(0, Some(0)) |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
impl<I> ExactSizeIterator for WrongSizeIter<I> where I: Iterator {} |
| 214 |
|
| 215 |
let arena = Arena::with_capacity(2); |
| 216 |
arena.alloc(0); |
| 217 |
let slice = arena.alloc_extend(WrongSizeIter(repeat(1).take(1_000))); |
| 218 |
// Allocation of 1000 elements should have created a new chunk |
| 219 |
assert_eq!(arena.chunks.borrow().rest.len(), 1); |
| 220 |
assert_eq!(slice.len(), 1000); |
| 221 |
} |
| 222 |
|
| 223 |
#[test] |
| 224 |
fn arena_is_send() { |
| 225 |
fn assert_is_send<T: Send>(_: T) {} |
| 226 |
|
| 227 |
// If `T` is `Send`, ... |
| 228 |
assert_is_send(42_u32); |
| 229 |
|
| 230 |
// Then `Arena<T>` is also `Send`. |
| 231 |
let arena: Arena<u32> = Arena::new(); |
| 232 |
assert_is_send(arena); |
| 233 |
} |
| 234 |
|
| 235 |
#[test] |
| 236 |
fn iter_mut_low_capacity() { |
| 237 |
#[derive(Debug, PartialEq, Eq)] |
| 238 |
struct NonCopy(usize); |
| 239 |
|
| 240 |
const MAX: usize = 1_000; |
| 241 |
const CAP: usize = 16; |
| 242 |
|
| 243 |
let arena = Arena::with_capacity(CAP); |
| 244 |
for i in 1..MAX { |
| 245 |
arena.alloc(NonCopy(i)); |
| 246 |
} |
| 247 |
|
| 248 |
assert!( |
| 249 |
arena.chunks.borrow().rest.len() > 1, |
| 250 |
"expected multiple chunks" |
| 251 |
); |
| 252 |
|
| 253 |
let mut iter = arena.iter(); |
| 254 |
for i in 1..MAX { |
| 255 |
assert_eq!(Some(&NonCopy(i)), iter.next()); |
| 256 |
} |
| 257 |
|
| 258 |
assert_eq!(None, iter.next()); |
| 259 |
} |
| 260 |
|
| 261 |
#[test] |
| 262 |
fn iter_mut_high_capacity() { |
| 263 |
#[derive(Debug, PartialEq, Eq)] |
| 264 |
struct NonCopy(usize); |
| 265 |
|
| 266 |
const MAX: usize = 1_000; |
| 267 |
const CAP: usize = 8192; |
| 268 |
|
| 269 |
let arena = Arena::with_capacity(CAP); |
| 270 |
for i in 1..MAX { |
| 271 |
arena.alloc(NonCopy(i)); |
| 272 |
} |
| 273 |
|
| 274 |
assert!( |
| 275 |
arena.chunks.borrow().rest.is_empty(), |
| 276 |
"expected single chunk" |
| 277 |
); |
| 278 |
|
| 279 |
let mut iter = arena.iter(); |
| 280 |
for i in 1..MAX { |
| 281 |
assert_eq!(Some(&NonCopy(i)), iter.next()); |
| 282 |
} |
| 283 |
|
| 284 |
assert_eq!(None, iter.next()); |
| 285 |
} |
| 286 |
|
| 287 |
fn assert_size_hint<T>(arena_len: usize, iter: Iter<'_, T>) { |
| 288 |
let (min, max) = iter.size_hint(); |
| 289 |
|
| 290 |
assert!(max.is_some()); |
| 291 |
let max = max.unwrap(); |
| 292 |
|
| 293 |
// Check that the actual arena length lies between the estimated min and max |
| 294 |
assert!(min <= arena_len); |
| 295 |
assert!(max >= arena_len); |
| 296 |
|
| 297 |
// Check that the min and max estimates are within a factor of 3 |
| 298 |
assert!(min >= arena_len / 3); |
| 299 |
assert!(max <= arena_len * 3); |
| 300 |
} |
| 301 |
|
| 302 |
#[test] |
| 303 |
fn size_hint() { |
| 304 |
#[derive(Debug, PartialEq, Eq)] |
| 305 |
struct NonCopy(usize); |
| 306 |
|
| 307 |
const MAX: usize = 32; |
| 308 |
const CAP: usize = 0; |
| 309 |
|
| 310 |
for cap in CAP..(CAP + 16/* check some non-power-of-two capacities */) { |
| 311 |
let arena = Arena::with_capacity(cap); |
| 312 |
for i in 1..MAX { |
| 313 |
arena.alloc(NonCopy(i)); |
| 314 |
let iter = arena.iter(); |
| 315 |
assert_size_hint(i, iter); |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
#[test] |
| 321 |
#[cfg_attr(miri, ignore)] |
| 322 |
fn size_hint_low_initial_capacities() { |
| 323 |
#[derive(Debug, PartialEq, Eq)] |
| 324 |
struct NonCopy(usize); |
| 325 |
|
| 326 |
const MAX: usize = 25_000; |
| 327 |
const CAP: usize = 0; |
| 328 |
|
| 329 |
for cap in CAP..(CAP + 128/* check some non-power-of-two capacities */) { |
| 330 |
let arena = Arena::with_capacity(cap); |
| 331 |
for i in 1..MAX { |
| 332 |
arena.alloc(NonCopy(i)); |
| 333 |
let iter = arena.iter(); |
| 334 |
assert_size_hint(i, iter); |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
#[test] |
| 340 |
#[cfg_attr(miri, ignore)] |
| 341 |
fn size_hint_high_initial_capacities() { |
| 342 |
#[derive(Debug, PartialEq, Eq)] |
| 343 |
struct NonCopy(usize); |
| 344 |
|
| 345 |
const MAX: usize = 25_000; |
| 346 |
const CAP: usize = 8164; |
| 347 |
|
| 348 |
for cap in CAP..(CAP + 128/* check some non-power-of-two capacities */) { |
| 349 |
let arena = Arena::with_capacity(cap); |
| 350 |
for i in 1..MAX { |
| 351 |
arena.alloc(NonCopy(i)); |
| 352 |
let iter = arena.iter(); |
| 353 |
assert_size_hint(i, iter); |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
#[test] |
| 359 |
#[cfg_attr(miri, ignore)] |
| 360 |
fn size_hint_many_items() { |
| 361 |
#[derive(Debug, PartialEq, Eq)] |
| 362 |
struct NonCopy(usize); |
| 363 |
|
| 364 |
const MAX: usize = 5_000_000; |
| 365 |
const CAP: usize = 16; |
| 366 |
|
| 367 |
let arena = Arena::with_capacity(CAP); |
| 368 |
for i in 1..MAX { |
| 369 |
arena.alloc(NonCopy(i)); |
| 370 |
let iter = arena.iter(); |
| 371 |
assert_size_hint(i, iter); |
| 372 |
} |
| 373 |
} |