snowstem 0.1.0

Snowball stemming for 21 languages, generated from the current Snowball release, with no required dependencies and no_std support
Documentation
//! Asserts the documented allocation bounds of `stem` and `stem_into`.
//!
//! One test function, so no other test's allocations land in a
//! measured region.

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);

    // A word that is its own stem borrows the input and never allocates.
    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");

    // A changed word costs exactly one allocation, sized to the stem.
    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);

    // `stem_into` with a buffer that already fits allocates nothing.
    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");
}