library(testthat) library(xegaBNF) library(xegaDerivationTrees) test_that("booleanGrammar OK", { G<-booleanGrammar() fn<-"~/dev/cran/xega/xegaBNF/BooleanGrammar.txt" expect_identical(G$filename, fn) expect_identical("BNF" %in% names(G), TRUE) } ) test_that("chooseRule OK", { expect_identical(chooseRule(c(7, 8, 9)) %in% c(7, 8, 9) , TRUE) expect_identical(chooseRule(as.vector(1)), 1) expect_identical(chooseRule(as.vector(5)), 5) } ) test_that("substituteSymbol OK", { g<-compileBNF(booleanGrammar()) t1<-substituteSymbol(3, g$PT) expect_identical(t1[[1]], g$PT$LHS[3]) expect_identical(t1[[2]], g$PT$RHS[3][[1]]) } ) test_that("rndsub OK", { g<-compileBNF(booleanGrammar()) set.seed(4) t1<-rndsub(g$Start, g$PT) set.seed(4) r<-chooseRule(xegaBNF::rules(g$Start, g$PT$LHS)) expect_identical(t1[[1]], g$PT$LHS[r]) expect_identical(t1[[2]], g$PT$RHS[r][[1]]) } ) test_that("randomDerivationTree OK", { g<-compileBNF(booleanGrammar()) set.seed(1) t1<-randomDerivationTree(g$Start, g) expect_equal(t1[[1]], g$Start) expect_equal(t1[[2]][[1]][[1]], 5) expect_equal(t1[[2]][[1]][[2]][[1]], 11) } ) test_that("randomDerivationTree OK", { g<-compileBNF(booleanGrammar()) set.seed(21) t1<-randomDerivationTree(g$Start, g) set.seed(21) t2<-randomDerivationTree(g$Start, g) expect_equal(decodeCDT(t1, g$ST), decodeCDT(t2, g$ST)) } ) test_that("randomDerivationTree OK", { g<-compileBNF(booleanGrammar()) set.seed(21) t1<-randomDerivationTree(g$Start, g, CompleteDT=FALSE) set.seed(21) t2<-randomDerivationTree(g$Start, g, CompleteDT=FALSE) expect_equal(decodeDT(t1, g$ST), decodeDT(t2, g$ST)) } ) test_that("rndPartition k=1 OK", { n<-sample(2:10,1) a1<-rndPartition(n, 1) expect_equal(n, a1) } ) test_that("rndPartition k=n OK", { n<-sample(2:10,1) a1<-rndPartition(n, n) expect_identical(rep(1,n), a1) } ) test_that("rndPartition (k in (2:(n-1)), n OK", { n<-sample(2:10,1) k<-sample(2:9,1) a1<-rndPartition(n, k) expect_equal(sum(a1), n) expect_identical(all(a1>0), TRUE) expect_identical(all(a1", "", "D2")) } ) test_that("decodeCDT OK", { g<-compileBNF(booleanGrammar()) set.seed(1) t1<-randomDerivationTree(g$Start, g) a<-decodeCDT(t1, g$ST) expect_identical(a, "D2") } ) test_that("leavesIncompleteDT OK", { g<-compileBNF(booleanGrammar()) set.seed(5) t1<-randomDerivationTree(g$Start, g) a<-leavesIncompleteDT(t1, g$ST) expect_identical(all(unlist(a)==c(12, 2, 10, 3)), TRUE) expect_identical(all(decodeDTsym(t1, g$ST)== c("NOT", "(", "D1", ")")), TRUE) } )