Skip to content

Commit

Permalink
Updated tests for testthat edition 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
mhahsler committed Jun 25, 2024
1 parent 76a4378 commit eacb4e5
Show file tree
Hide file tree
Showing 23 changed files with 119 additions and 205 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
## Bugfixes
* Fixed 'Error in .basicRuleMeasure(..) unused argument
(complement = TRUE)' reported by bachnguyen-tomo.

## Changes
* Updated tests for testthat edition 3.

# arules 1.7-7 (11/28/2023)

Expand All @@ -12,6 +15,7 @@
* improved some error messages.
* Added Wikipedia links to measures.md.
* Added interest measure LIC.


## Bugfixes
* Fixed appearance code for fim4r (reported by mytarmail).
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library("testthat")

library("arules")
test_check("arules")

test_check("arules")
104 changes: 4 additions & 100 deletions tests/testthat/test-apriori.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
library("arules")
library("testthat")

verb <- FALSE

options(digits = 2)

data <- list(
Expand All @@ -17,17 +13,12 @@ data <- list(
names(data) <- paste("Tr", c(1:7), sep = "")
trans <- transactions(data)


##########################################################
# test the APRIORI interface
context("APRIORI interface")

### rules (all warnings are for low support)
r <- apriori(trans,
parameter = list(supp = 0.25, conf = 0),
control = list(verb = verb))

expect_identical(length(r), 18L)
expect_length(r, 18L)

#r
#summary(r)
Expand All @@ -41,7 +32,7 @@ r <- apriori(
control = list(verb = verb)
)

expect_identical(length(r), 6L)
expect_length(r, 6L)
#inspect(r)

r <- apriori(
Expand All @@ -50,15 +41,15 @@ r <- apriori(
appearance = list(lhs = c("a", "b"), rhs = "c"),
control = list(verb = verb)
)
expect_identical(length(r), 2L)
expect_length(r, 2L)

r <- apriori(
trans,
parameter = list(supp = 0.25, conf = 0),
appearance = list(none = c("a", "b")),
control = list(verb = verb)
)
expect_identical(length(r), 3L)
expect_length(r, 3L)

expect_error(as(list(
rhs = c("a", "b"),
Expand All @@ -82,71 +73,6 @@ r <- apriori(
expect_true("coverage" %in% colnames(quality(r)))
#inspect(r[1:2])



##########################################################
# test the ECLAT interface

context("ECLAT interface")

f <- eclat(trans, control = list(verb = verb))

expect_identical(length(f), 20L)
sup <- c(
0.14,
0.14,
0.14,
0.29,
0.29,
0.29,
0.14,
0.14,
0.29,
0.14,
0.14,
0.14,
0.14,
0.29,
0.57,
0.71,
0.71,
0.43,
0.43,
0.29
)
expect_equal(round(quality(f)$support, 2), sup)
expect_equal(labels(f)[5], "{a,d}")

#f
#summary(f)
#inspect(f[1:2])
#labels(f[1:2])


### test subset
f.sub <- subset(f, subset = items %in% "a")
l <- labels(f.sub)
expect_equal(l, grep("a", l, value = T))

### test tidlists
f <-
eclat(trans,
parameter = list(tidLists = TRUE),
control = list(verb = verb))

#f
#summary(f)
tl <- tidLists(f)

expect_identical(dim(tl), c(20L, 7L))

#tl
#summary(tl)
#inspect(tl)

expect_equal(as(tl[5], "list"), list('{a,d}' = c("Tr3", "Tr7")))


## Compare if APRIOR and ECLAT produce the same results
data("Income")
fsets <-
Expand All @@ -165,25 +91,3 @@ esets <-
## compare if output is the same
expect_true(all(table(match(fsets, esets)) == 1))

##########################################################
# test maximal and closed itemset mining

context("Maximal and closed itemsets")

is_a_freq <- apriori(trans, parameter = list(target = "frequent"), control = list(verb = verb))
is_a_max <- apriori(trans, parameter = list(target = "max"), control = list(verb = verb))
is_a_closed <- apriori(trans, parameter = list(target = "closed"), control = list(verb = verb))
is_a_gen <- apriori(trans, parameter = list(target = "generator"), control = list(verb = verb))

is_e_freq <- eclat(trans, parameter = list(target = "frequent"), control = list(verb = verb))
is_e_max <- eclat(trans, parameter = list(target = "max"), control = list(verb = verb))
is_e_closed <- eclat(trans, parameter = list(target = "closed"), control = list(verb = verb))
is_e_gen <- eclat(trans, parameter = list(target = "generator"), control = list(verb = verb))

expect_true(setequal(is_a_freq, is_e_freq))
expect_true(setequal(is_a_max, is_e_max))
expect_true(setequal(is_a_closed, is_e_closed))
expect_true(setequal(is_a_gen, is_e_gen))

#inspect(is_a_freq)
#inspect(is_e_freq)
9 changes: 0 additions & 9 deletions tests/testthat/test-associations.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
library("testthat")
library("arules")

context("associations")

set.seed(20070611)

m <- matrix(as.integer(runif(100000) > 0.8), ncol = 20)
Expand All @@ -13,7 +8,6 @@ t <- as(m, "transactions")

expect_identical(dim(t), dim(m))


r <-
apriori(t,
parameter = list(supp = 0.01, conf = 0.1),
Expand All @@ -27,7 +21,6 @@ ss <- subset(r, subset = lift > 1.4 & lhs %in% "item3")
expect_identical(labels(lhs(ss)), "{item2,item3}")
expect_true(quality(ss)$lift > 1.4)


f <- eclat(t,
parameter = list(supp = 0.01),
control = list(verb = FALSE))
Expand Down Expand Up @@ -73,7 +66,6 @@ qual <-
expect_warning(r <- rules(lhs, rhs, quality = qual))
#inspect(r)

context("subsetting")

## subsetting (also tests itemMatrix)
# numeric
Expand Down Expand Up @@ -175,7 +167,6 @@ expect_identical(r[2:10] %in% r, 2:10)
expect_identical(match(f[2:10], f), 2:10)
expect_identical(f[2:10] %in% f, 2:10)


expect_identical(rhs(r[1:10]) %pin% "item1",
c(FALSE, FALSE, TRUE, TRUE, TRUE,
TRUE, TRUE, FALSE, FALSE, TRUE))
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-concise.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ trans_list <- list(
trans <- transactions(trans_list)
its <- apriori(trans, support = 1/4, target = "frequent itemsets", control = list(verb = FALSE))

context("is.generator")
# with minsup = 1, frequent generator itemsets are: emptyset, b, c, d, bd, cd.
ig <- is.generator(its)
gens <- its[ig]
Expand All @@ -25,15 +24,13 @@ expect_true(setequal(gens, gens_ap))
expect_true(setequal(gens, gens_ec))


context("is.closed")
# with minsup = 1, frequent closed itemsets are: a, ac, ad, abc, abcd.
ic <- is.closed(its)
expect_true(setequal(names(ic)[ic], c("{a}", "{a,c}", "{a,d}", "{a,b,c}", "{a,b,c,d}")))

ic2 <- apriori(trans, support = 1/4, target = "closed frequent itemsets", control = list(verb = FALSE))
expect_true(setequal(its[ic], ic2))

context("is.maximal")
#
im <- is.maximal(its)
expect_true(setequal(names(im)[im], c("{a,b,c,d}")))
Expand Down
5 changes: 0 additions & 5 deletions tests/testthat/test-confint.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
library("arules")
library("testthat")

debug <- FALSE

context("confint")

data("Adult")
## Mine association rules.
rules <- apriori(Adult,
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-crossTable.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
library("testthat")
library("arules")

data(Groceries)

itset <- new("itemsets",
Expand Down
6 changes: 0 additions & 6 deletions tests/testthat/test-discretize.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
library("arules")
library("testthat")


options(digits=2)

context("discretize")

nums1 <- sample(rep(0:10, time = 5))

nums2 <-
Expand Down
75 changes: 75 additions & 0 deletions tests/testthat/test-eclat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
library("arules")
library("testthat")

verb <- FALSE

options(digits = 2)

data <- list(
c("a", "b", "c"),
c("a", "b"),
c("a", "b", "d"),
c("b", "e"),
c("a", "c"),
c("c", "e"),
c("a", "b", "d", "e")
)
names(data) <- paste("Tr", c(1:7), sep = "")
trans <- transactions(data)

f <- eclat(trans, control = list(verb = verb))

expect_identical(length(f), 20L)
sup <- c(
0.14,
0.14,
0.14,
0.29,
0.29,
0.29,
0.14,
0.14,
0.29,
0.14,
0.14,
0.14,
0.14,
0.29,
0.57,
0.71,
0.71,
0.43,
0.43,
0.29
)
expect_equal(round(quality(f)$support, 2), sup)
expect_equal(labels(f)[5], "{a,d}")

#f
#summary(f)
#inspect(f[1:2])
#labels(f[1:2])


### test subset
f.sub <- subset(f, subset = items %in% "a")
l <- labels(f.sub)
expect_equal(l, grep("a", l, value = T))

### test tidlists
f <-
eclat(trans,
parameter = list(tidLists = TRUE),
control = list(verb = verb))

#f
#summary(f)
tl <- tidLists(f)

expect_identical(dim(tl), c(20L, 7L))

#tl
#summary(tl)
#inspect(tl)

expect_equal(as(tl[5], "list"), list('{a,d}' = c("Tr3", "Tr7")))
6 changes: 0 additions & 6 deletions tests/testthat/test-extract.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
library("arules")
library("testthat")

context("extract")

data("Adult")

expect_equal(Adult, c(Adult[1:100], Adult[-(1:100)]))


ec <- eclat(Adult,control=list(verb=FALSE))
expect_equal(ec, c(ec[1:100], ec[-(1:100)]))

Expand Down
4 changes: 0 additions & 4 deletions tests/testthat/test-fim4r.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
library("arules")
library("testthat")

skip_if_not_installed("fim4r")

verb <- FALSE

options(digits = 2)

data <- list(
Expand Down
Loading

0 comments on commit eacb4e5

Please sign in to comment.