tests/testthat/test.R

test_that("svds",{
  set.seed(1337)
  m<-matrix(rbinom(100,5,.2),ncol=10,nrow=10)
  numcomp<-num_components(m)
  expect_that(numcomp, equals(10))
  badm<-data.frame(a=1:10,b=11:20,c=c(1:9,NA))
  expect_that(rnumcomp(badm),throws_error())
})

test_that("reduce",{
  set.seed(1337)
  m<-matrix(rbinom(100,5,.2),ncol=10,nrow=10)
  m2<-reduce_components(m,9)
  m4<-reduce_percentage(m,.5)
  badm<-data.frame(a=1:10,b=11:20,c=c(1:9,NA))
  expect_that(reduce_percentage(badm),throws_error())
  expect_that(reduce_components(badm,2),throws_error())
  expect_that(reduce_percentage(m,1.1), gives_warning())
  expect_that(reduce_percentage(m,-.1), throws_error())
})

test_that("exclude",{
  set.seed(1337)
  m<-matrix(rbinom(100,5,.2),ncol=10,nrow=10)
  m4<-reduce_percentage(m,.5)
  m5<-exclude_components(m,2:10)
  badm<-data.frame(a=1:10,b=11:20,c=c(1:9,NA))
  expect_that(exclude_components(badm,2),throws_error())
  expect_that(exclude_components(m,8:12),gives_warning())
  expect_that(exclude_components(m,11:12),throws_error())
})

# test_that("exclude",{
#   set.seed(1337)
#   m<-matrix(rbinom(100,5,.2),ncol=10,nrow=10)
#   numcomp<-num_components(m)
#   m2<-reduce_components(m,9)
#   m3<-exclude_components(m,1:9)
#   m4<-reduce_percentage(m,.5)
#   m5<-exclude_components(m,2:10)
#   plot_explanation(m4)
#   p1<-dev.capture()
#   plot_explanation(m5)
#   p2<-dev.capture()
#   expect_that(numcomp, equals(10))
#   expect_equivalent(round(m2+m3),m)
#   expect_equivalent(m4,m5)
#   expect_equivalent(p1, p2)
#   badm<-data.frame(a=1:10,b=11:20,c=c(1:9,NA))
#   expect_that(reduce_percentage(badm),throws_error())
#   expect_that(reduce_components(badm,2),throws_error())
#   expect_that(exclude_components(badm,2),throws_error())
#   expect_that(num_components(badm),throws_error())
#   expect_that(plot_explanation(badm),throws_error())
#   expect_that(exclude_components(m,8:12),gives_warning())
#   expect_that(exclude_components(m,11:12),throws_error())
#   expect_that(reduce_percentage(m,1.1), gives_warning())
#   expect_that(reduce_percentage(m,-.1), throws_error())
# })

test_that("plots",{
  set.seed(1337)
  m<-matrix(rbinom(100,5,.2),ncol=10,nrow=10)
  numcomp<-num_components(m)
  m4<-reduce_percentage(m,.5)
  m5<-exclude_components(m,2:10)
  plot_explanation(m4)
  p1<-dev.capture()
  plot_explanation(m5)
  p2<-dev.capture()
  expect_equivalent(p1, p2)
  badm<-data.frame(a=1:10,b=11:20,c=c(1:9,NA))
  expect_that(plot_explanation(badm),throws_error())
})
jimeharrisjr/svdtools documentation built on Jan. 7, 2021, 12:47 a.m.