use std::alloc::System;
use std::borrow::Cow;
use snowstem::{Algorithm, Stemmer};
use stats_alloc::{Region, StatsAlloc, INSTRUMENTED_SYSTEM};
#[global_allocator]
static GLOBAL: &StatsAlloc<System> = &INSTRUMENTED_SYSTEM;
#[test]
fn allocation_bounds() {
let stemmer = Stemmer::create(Algorithm::English);
let region = Region::new(GLOBAL);
let stem = stemmer.stem("internal");
let stats = region.change();
assert!(matches!(stem, Cow::Borrowed("internal")));
assert_eq!(stats.allocations, 0, "unchanged word allocated");
let region = Region::new(GLOBAL);
let stem = stemmer.stem("international");
let stats = region.change();
assert_eq!(stem, "internat");
assert_eq!(stats.allocations, 1, "changed word allocation count");
assert_eq!(stats.reallocations, 0, "changed word reallocated");
drop(stem);
let mut buffer = String::with_capacity(64);
let region = Region::new(GLOBAL);
stemmer.stem_into("international", &mut buffer);
stemmer.stem_into("internal", &mut buffer);
let stats = region.change();
assert_eq!(buffer, "internal");
assert_eq!(stats.allocations, 0, "stem_into allocated past the buffer");
assert_eq!(stats.reallocations, 0, "stem_into grew the buffer");
}