mod common;
use tokenx::{
estimate_token_count, estimate_token_count_with, LanguageConfig, TokenEstimationOptions,
};
#[test]
fn custom_language_configs_override_default() {
assert_eq!(estimate_token_count("wonderful"), 2);
let opts = TokenEstimationOptions {
language_configs: Some(vec![LanguageConfig::new("o", 2.0).unwrap()]),
..Default::default()
};
assert_eq!(estimate_token_count_with("wonderful", &opts), 5);
}
#[test]
fn empty_language_configs_falls_back_to_default_chars() {
let opts = TokenEstimationOptions {
language_configs: Some(vec![]),
default_chars_per_token: Some(3.0),
};
assert_eq!(estimate_token_count_with("wonderful", &opts), 3);
}
#[test]
fn case_insensitive_config_matches_uppercase() {
let opts = TokenEstimationOptions {
language_configs: Some(vec![LanguageConfig::case_insensitive("z", 2.0).unwrap()]),
..Default::default()
};
assert_eq!(estimate_token_count_with("puzzling", &opts), 4);
assert_eq!(estimate_token_count_with("PUZZLING", &opts), 4);
}
#[test]
fn zero_chars_per_token_saturates_without_panic() {
let opts = TokenEstimationOptions {
default_chars_per_token: Some(0.0),
..Default::default()
};
assert_eq!(
estimate_token_count_with("hello world this is text", &opts),
u64::MAX
);
}
#[test]
#[allow(deprecated)]
fn deprecated_alias_matches_estimate() {
for input in ["", "Hello, world!", "wonderful", "道德經"] {
assert_eq!(
tokenx::approximate_token_size(input),
estimate_token_count(input),
"alias mismatch for {input:?}"
);
}
}