## ---- hallucination_rate -------------------------------------------- test_that("hallucination_rate equals the mean of adjudicated labels", { labels <- c(0, 1, 0, 1, 1) expect_equal(hallucination_rate(labels), 0.6) }) test_that("hallucination_rate accepts logical verdicts", { verdicts <- c(TRUE, FALSE, TRUE, TRUE) expect_equal(hallucination_rate(verdicts), 0.75) }) test_that("hallucination_rate returns per-group rates when 'by' is supplied", { labels <- c(1, 1, 0, 0) grp <- c("a", "a", "b", "b") out <- hallucination_rate(labels, by = grp) expect_equal(out[["a"]], 1) expect_equal(out[["b"]], 0) }) test_that("hallucination_rate ignores NA labels", { labels <- c(1, 0, NA, 1) expect_equal(hallucination_rate(labels), 2/3) }) ## ---- lexical_overlap ----------------------------------------------- test_that("lexical_overlap is 1 for identical text", { expect_equal(lexical_overlap("the cat sat", "the cat sat"), 1) }) test_that("lexical_overlap is 0 for fully disjoint text", { expect_equal(lexical_overlap("cat dog", "fish bird"), 0) }) test_that("lexical_overlap is case-insensitive and punctuation-robust", { expect_equal(lexical_overlap("The Cat, sat!", "the cat sat"), 1) }) test_that("lexical_overlap returns NA for empty input", { expect_true(is.na(lexical_overlap("", "something"))) expect_true(is.na(lexical_overlap("something", ""))) }) test_that("lexical_overlap computes correct partial Jaccard", { # tokens {a,b,c} vs {b,c,d}: intersection 2, union 4 -> 0.5 expect_equal(lexical_overlap("a b c", "b c d"), 0.5) }) ## ---- citation_accuracy --------------------------------------------- test_that("citation_accuracy detects fabricated citations", { cited <- c("doi:1", "doi:2", "doi:FAKE") known <- c("doi:1", "doi:2", "doi:3") out <- citation_accuracy(cited, known) expect_equal(out$exists_rate, 2/3) expect_equal(out$fabricated, "doi:FAKE") }) test_that("citation_accuracy reports full accuracy when all citations are real", { out <- citation_accuracy(c("a", "b"), c("a", "b", "c")) expect_equal(out$exists_rate, 1) expect_length(out$fabricated, 0) })