Skip to content

Commit

Permalink
Updated test suite to use internal data
Browse files Browse the repository at this point in the history
  • Loading branch information
MansMeg committed Oct 30, 2016
1 parent 9b546d8 commit 877e2fa
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions tests/testthat/test-mallet-io.R
@@ -1,35 +1,51 @@
context("mallet-io")

state_file <- "../../data-raw/sotu50.txt.gz"
tmp_dir <- tempdir()
dir.create(tmp_dir, showWarnings = FALSE)
tmp_txt <- file.path(tmp_dir, "sotu_tmp.txt")
tmp_gz <- file.path(tmp_dir, "sotu_tmp.txt.gz")
data("sotu50")

test_that(desc="mallet-io",{

expect_silent(capture.output(state <- read_mallet_statefile(state_file)))
expect_silent(alpha <- read_mallet_statefile(state_file, "alpha"))
expect_silent(beta <- read_mallet_statefile(state_file, "beta"))

expect_silent(write_mallet_statefile(state = state, alpha = alpha[1], beta = beta, state_file = tmp_gz))

state <- sotu50
alpha_prior <-
c(0.29086113, 0.02444446, 0.03161273, 0.01723782, 0.07462461, 0.04539733,
0.09358508, 0.05666769, 0.07400546, 0.09371108, 0.01920851, 0.06307651,
0.06651574, 0.14376023, 0.11661917, 0.02849356, 0.03395349, 0.07415586,
0.04554275, 0.02449970, 0.02941570, 0.02123418, 0.11736314, 0.05787614,
0.12496781, 0.02267272, 0.03654363, 0.06726874, 0.06028669, 0.21446219,
0.05052153, 0.06216679, 0.10779823, 0.06225405, 0.16680843, 0.02384183,
0.03609049, 0.05058202, 0.03235920, 0.01970245, 0.05856258, 0.02690204,
0.12907164, 0.01989379, 0.07178854, 0.18448336, 0.02455222, 0.05687149,
0.06363865, 0.03725172)
beta_prior <- 0.009949733

expect_silent(write_mallet_statefile(state = state, alpha = alpha_prior[1], beta = beta_prior, state_file = tmp_gz))
unlink(tmp_gz)
expect_silent(write_mallet_statefile(state = state, alpha = alpha, beta = beta, state_file = tmp_gz))
expect_silent(write_mallet_statefile(state = state, alpha = alpha, beta = beta, state_file = tmp_txt))
expect_silent(write_mallet_statefile(state = state, alpha = alpha_prior, beta = beta_prior, state_file = tmp_gz))
expect_silent(write_mallet_statefile(state = state, alpha = alpha_prior, beta = beta_prior, state_file = tmp_txt))

expect_silent(state2 <- read_mallet_statefile(tmp_gz))
expect_silent(capture_output(state2 <- read_mallet_statefile(tmp_gz)))
expect_silent(alpha2 <- read_mallet_statefile(tmp_gz, "alpha"))
expect_silent(beta2 <- read_mallet_statefile(tmp_gz, "beta"))

expect_silent(state3 <- read_mallet_statefile(tmp_txt))
expect_silent(capture_output(state3 <- read_mallet_statefile(tmp_txt)))
expect_silent(alpha3 <- read_mallet_statefile(tmp_txt, "alpha"))
expect_silent(beta3 <- read_mallet_statefile(tmp_txt, "beta"))

expect_equal(alpha, alpha2, tolerance = 0.000001)
expect_equal(alpha, alpha3, tolerance = 0.000001)
expect_equal(beta, beta2, tolerance = 0.000001)
expect_equal(beta, beta3, tolerance = 0.000001)
expect_equal(alpha_prior, alpha2, tolerance = 0.000001)
expect_equal(alpha_prior, alpha3, tolerance = 0.000001)
expect_equal(beta_prior, beta2, tolerance = 0.000001)
expect_equal(beta_prior, beta3, tolerance = 0.000001)

state2$pos <- NULL
state3$pos <- NULL

expect_identical(state, state2)
expect_identical(state, state3)

})

unlink(tmp_gz)
unlink(tmp_txt)

0 comments on commit 877e2fa

Please sign in to comment.