use std::alloc::GlobalAlloc;
use std::alloc::Layout;
use std::alloc::System;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use qubit_progress::Metric;
use qubit_progress::NoopReporter;
use qubit_progress::Progress;
struct CountingAllocator;
static ALLOCATIONS: AtomicUsize = AtomicUsize::new(0);
#[global_allocator]
static GLOBAL_ALLOCATOR: CountingAllocator = CountingAllocator;
unsafe impl GlobalAlloc for CountingAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
ALLOCATIONS.fetch_add(1, Ordering::Relaxed);
unsafe { System.alloc(layout) }
}
unsafe fn dealloc(&self, pointer: *mut u8, layout: Layout) {
unsafe { System.dealloc(pointer, layout) }
}
}
#[test]
fn test_successful_metric_transition_does_not_allocate() {
let reporter = NoopReporter;
let progress = Progress::builder(&reporter)
.metric(Metric::new("tasks", "Tasks").total(2))
.start()
.expect("progress must start");
let tasks = progress.metric("tasks").expect("metric must exist");
tasks.start(1).expect("work must start");
ALLOCATIONS.store(0, Ordering::Relaxed);
tasks.complete(1).expect("work must complete");
assert_eq!(
ALLOCATIONS.load(Ordering::Relaxed),
0,
"successful transitions must not allocate"
);
}