Crate testdrop [] [src]

This crate helps test the following drop issues:

  • Assert that an item was dropped
  • Assert that an item was not dropped
  • Assert that an item was not dropped multiple times (this is implicit tested)

This kind of test is useful for objects that manages the lifetime of other objects, like smart pointers and containers.

Examples

Test if std::mem::forget works.

use testdrop::TestDrop;
use std::mem;

let td = TestDrop::new();
let (id, item) = td.new_item();

mem::forget(item);

td.assert_no_drop(id);

Test if the std::rc::Rc drop implementation works.

use testdrop::TestDrop;
use std::rc::Rc;

let td = TestDrop::new();
let (id, item) = td.new_item();
let item = Rc::new(item);
let item_clone = item.clone();

// Decrease the reference counter, but do not drop.
drop(item_clone);
td.assert_no_drop(id);

// Decrease the reference counter and then drop.
drop(item);
td.assert_drop(id);

Test if the std::vec::Vec drop implementation works.

use testdrop::TestDrop;

let td = TestDrop::new();
let v: Vec<_> = (0..10).map(|_| td.new_item().1).collect();

drop(v);

// Vec::drop should drop all items.
assert_eq!(10, td.num_tracked_items());
assert_eq!(10, td.num_dropped_items());

Structs

Item

An item tracked by TestDrop.

TestDrop

A struct to help test drop related issues.