blob: 0314864504827279862612c0076e6ab10de8d174 [file] [log] [blame]
Marc Kupietzef5b7c12026-09-01 07:24:43 +02001lcp <- RKorAPClient:::longestCommonPrefixLength
2lcs <- RKorAPClient:::longestCommonSuffixLength
3
4test_that("longestCommonPrefixLength counts the shared prefix", {
5 expect_equal(lcp(c("abc", "abd")), 2)
6 expect_equal(lcp(c("abc", "abc")), 3)
7 expect_equal(lcp(c("xyz", "abc")), 0)
8 expect_equal(lcp(c("aaa", "aa", "a")), 1)
9 expect_equal(lcp(c("prefix_A_suffix", "prefix_B_suffix")), 7)
10 expect_equal(lcp(c("üöä-test", "üöä-rest")), 4)
11})
12
13test_that("longestCommonSuffixLength counts the shared suffix", {
14 expect_equal(lcs(c("abc", "dbc")), 2)
15 expect_equal(lcs(c("abc", "abc")), 3)
16 expect_equal(lcs(c("xyz", "abc")), 0)
17 expect_equal(lcs(c("aaa", "aa", "a")), 1)
18 expect_equal(lcs(c("prefix_A_suffix", "prefix_B_suffix")), 7)
19 expect_equal(lcs(c("abc-üöä", "xyz-üöä")), 4)
20})
21
22test_that("empty vectors, empty strings and single strings behave as before", {
23 # a single string is its own prefix and suffix, as in the PTXQC functions
24 # these replace
25 expect_equal(lcp(character(0)), 0)
26 expect_equal(lcs(character(0)), 0)
27 expect_equal(lcp("single"), 6)
28 expect_equal(lcs("single"), 6)
29 expect_equal(lcp(c("", "")), 0)
30 expect_equal(lcs(c("", "")), 0)
31 expect_equal(lcp(c("abc", "")), 0)
32 expect_equal(lcs(c("", "abc")), 0)
33})
34
35test_that("queryStringToLabel clips common prefixes and suffixes", {
36 expect_equal(
37 queryStringToLabel(paste("textType = /Zeit.*/ & pubDate in", 2010:2019)),
38 as.character(2010:2019)
39 )
40 expect_equal(
41 queryStringToLabel(c("[marmot/m=mood:subj]", "[marmot/m=mood:ind]")),
42 c("subj", "ind")
43 )
44 expect_equal(
45 queryStringToLabel(c("wegen dem [tt/p=NN]", "wegen des [tt/p=NN]")),
46 c("dem", "des")
47 )
48})
49
50test_that("queryStringToLabel honours pubDateOnly and excludePubDate", {
51 vc <- paste("textType = /Zeit.*/ & pubDate in", 2010:2012)
52 expect_equal(queryStringToLabel(vc, pubDateOnly = TRUE), as.character(2010:2012))
53 expect_equal(
54 queryStringToLabel(c("textDomain = /Wirtschaft.*/ & pubDate in 2010",
55 "textDomain != /Wirtschaft.*/ & pubDate in 2010"),
56 excludePubDate = TRUE),
57 c("=", "!=")
58 )
59})