File: test_longest.R

package info (click to toggle)
r-cran-triebeard 0.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 392 kB
  • sloc: cpp: 1,095; sh: 13; makefile: 2; ansic: 1
file content (39 lines) | stat: -rw-r--r-- 1,773 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
testthat::context("Test that longest-matching works")

testthat::test_that("Longest matching works for string tries", {
  trie <- trie(keys = c("afford", "affair", "available", "binary", "bind", "blind"),
                      values = c("afford", "affair", "available", "binary", "bind", "blind"))
  testthat::expect_equal(longest_match(trie, "binder"), "bind")
})

testthat::test_that("Longest matching works for integer tries", {
  trie <- trie(keys = c("afford", "affair", "available", "binary", "bind", "blind"),
                      values = c(1, 2, 3, 4, 5, 6))
  testthat::expect_equal(longest_match(trie, "binder"), 5)
})

testthat::test_that("Longest matching works for numeric tries", {
  trie <- trie(keys = c("afford", "affair", "available", "binary", "bind", "blind"),
                      values = as.numeric(c(1, 2, 3, 4, 5, 6)))
  testthat::expect_equal(longest_match(trie, "binder"), 5.0)
})

testthat::test_that("Longest matching works for logical tries", {
  trie <- trie(keys = c("afford", "affair", "available", "binary", "bind", "blind"),
                      values = c(FALSE, FALSE, TRUE, FALSE, TRUE, TRUE))
  testthat::expect_true(longest_match(trie, "binder"))
})

testthat::test_that("Longest matching understands the new include_keys param", {

  trie <- trie(keys = c("afford", "affair", "available", "binary", "bind", "blind"),
               values = c("afford", "affair", "available", "muffin", "bind", "frog"))
  result <- longest_match(trie, "binaryness", TRUE)
  testthat::expect_true(is.data.frame(result))
  testthat::expect_equal(result$match_key, "binary")
  testthat::expect_equal(result$match_value, "muffin")

})
testthat::test_that("Longest matching rejects non-trie objects", {
  expect_error(longest_match("foo", "bar"))
})