Debian Codegraph @

tests/testthat/tests.R

raw
1 context("violinscatter functions")
2 test_that("Test offsetting",{
3 expect_equal(offsetX(1,1), 0)
4 expect_equal(offsetX(rep(1,10),1:10), rep(0,10))
5 expect_error(offsetX(1,1:2), "not the same length")
6 expect_error(offsetX(1:2,1), "not the same length")
7 expect_equal(length(unique(offsetX(rep(1,100)))), 100)
8 expect_equal(offsetX(rep(1,100)), offsetX(rep(1,100)))
9 expect_equal(offsetX(rep(1,100),width=.2)*2, offsetX(rep(1,100),width=.4))
10 expect_equal(
11 offsetX(rep(1:2,c(10,1000)),rep(1:2,c(10,1000)),varwidth=TRUE)[1:10]*10,
12 offsetX(rep(1:2,c(1000,1000)),rep(1:2,c(1000,1000)),varwidth=TRUE)[1:10]
13 )
14 })
15
16 test_that("Test single group offsetting",{
17 expect_equal(offsetSingleGroup(1), 0)
18 expect_equal(offsetSingleGroup(NULL), NULL)
19 expect_equal(offsetSingleGroup(rep(1,100)), offsetSingleGroup(rep(1,100)))
20 expect_equal(length(offsetSingleGroup(rnorm(1000))), 1000)
21 x<-rnorm(1000)
22 expect_equal(offsetSingleGroup(x), offsetSingleGroup(x,maxLength=1000))
23 expect_equal(offsetSingleGroup(x)*10, offsetSingleGroup(x,maxLength=10))
24 expect_equal(offsetSingleGroup(x)/10, offsetSingleGroup(x,maxLength=100000))
25 for(ii in c('quasirandom','pseudorandom','smiley','frowney','tukey','tukeyDense')){
26 expect_lte(max(offsetX(rnorm(100),method=ii,width=.1)),.1)
27 expect_gte(min(offsetX(rnorm(100),method=ii,width=.1)),-.1)
28 expect_equal(length(offsetSingleGroup(rnorm(100),method=ii)),100)
29 expect_equal(length(offsetSingleGroup(rnorm(2),method=ii)),2)
30 expect_equal(length(offsetSingleGroup(rnorm(1),method=ii)),1)
31 #make sure no errors with small groups
32 expect_error(offsetSingleGroup(1,method=ii),NA)
33 expect_error(offsetSingleGroup(1:2,method=ii),NA)
34 expect_error(offsetSingleGroup(1:3,method=ii),NA)
35 }
36 })
37
38 test_that("Test ave with args",{
39 expect_equal(aveWithArgs(1:10,rep(1:5,2)), ave(1:10,rep(1:5,2)))
40 expect_equal(aveWithArgs(1:10,rep(1:5,2),FUN=median), ave(1:10,rep(1:5,2),FUN=median))
41 expect_equal(aveWithArgs(100:1+.01,rep(1:5,20),FUN=median), ave(100:1+.01,rep(1:5,20),FUN=median))
42 expect_equal(aveWithArgs(100:1+.01,rep(1:5,20),FUN=max), ave(100:1+.01,rep(1:5,20),FUN=max))
43 expect_equal(aveWithArgs(100:1+.01,rep(1:5,20),FUN=max), ave(100:1+.01,rep(1:5,20),FUN=max))
44 expect_equal(aveWithArgs(c(1:5,NA),rep(1:3,2),FUN=max,na.rm=TRUE), rep(c(4,5,3),2))
45 expect_equal(aveWithArgs(c(1:6),rep(1:3,2),FUN=function(x,y)sum(x)+y,3), rep(c(8,10,12),2))
46 expect_equal(aveWithArgs(c(1:6)),rep(mean(1:6),6))
47 expect_equal(aveWithArgs(c(1:6),rep(1:3,2),FUN=function(x,y)x+y,3), 1:6+3)
48 expect_equal(aveWithArgs(c(6:1),rep(1:3,2),FUN=function(x,y)sqrt(x)+y,3), sqrt(6:1)+3)
49 expect_error(aveWithArgs(c(6:1),rep(1:3,2),FUN=function(x,y)sqrt(x)+y), 'argument.*missing')
50 })
51
52 test_that("Test top bottom distribute",{
53 expect_equal(topBottomDistribute(1:10), topBottomDistribute(1:10+100))
54 expect_equal(topBottomDistribute(-1:-10), topBottomDistribute(1:10,TRUE))
55 expect_equal(topBottomDistribute(1000), .5)
56 expect_equal(topBottomDistribute(-1000), .5)
57 expect_equal(topBottomDistribute(1:3)[1], .5) #could do left to right or right to left
58 expect_equal(topBottomDistribute(-1:-3)[3], .5) #could do left to right or right to left
59 expect_equal(length(topBottomDistribute(1:100)), 100)
60 expect_equal(sort(tail(topBottomDistribute(1:100),2)), c(0,1))
61 expect_equal(sort(tail(topBottomDistribute(1:1000),2)), c(0,1))
62 expect_equal(sort(head(topBottomDistribute(1:1000,TRUE),2)), c(0,1))
63 expect_equal(sort(head(topBottomDistribute(1:100,TRUE),2)), c(0,1))
64 expect_equal(sort(head(topBottomDistribute(1:100,TRUE,FALSE),2)), c(1,100))
65 expect_equal(sort(tail(topBottomDistribute(1:100,FALSE,FALSE),2)), c(1,100))
66 expect_equal(sort(tail(topBottomDistribute(1:1000,prop=FALSE),2)), c(1,1000))
67 expect_equal(sort(head(topBottomDistribute(1:1000,prop=FALSE),5)), 498:502)
68 expect_equal(sort(tail(topBottomDistribute(1:1000,TRUE,prop=FALSE),5)), 498:502)
69 })
70
71 test_that("Test van der Corput generation",{
72 expect_equal(vanDerCorput(3), c(1/2,1/4,3/4))
73 expect_equal(vanDerCorput(8), c(1/2,1/4,3/4,1/8,5/8,6/16,14/16,1/16))
74 expect_equal(vanDerCorput(8,start=1), c(1/2,1/4,3/4,1/8,5/8,6/16,14/16,1/16))
75 expect_equal(vanDerCorput(5,start=4), c(1/8,5/8,6/16,14/16,1/16))
76 expect_equal(vanDerCorput(3,3), c(1/3,2/3,1/9))
77 expect_equal(vanDerCorput(3,10), c(1/10,2/10,3/10))
78 expect_equal(vanDerCorput(0,10), c())
79 expect_error(vanDerCorput(3,1), 'base')
80 expect_error(vanDerCorput(3,-10), 'base')
81 expect_error(vanDerCorput(10,0), 'base')
82 expect_error(vanDerCorput(-10,10), 'n ')
83 expect_error(vanDerCorput(10,10,-100), 'start')
84 expect_equal(length(vanDerCorput(10000)), 10000)
85 expect_equal(length(unique(vanDerCorput(10000))), 10000)
86 })
87
88 test_that("Test number splitting",{
89 expect_equal(number2digits(0), c())
90 expect_error(number2digits(-1), 'negative')
91 expect_error(number2digits(10,1), 'base')
92 expect_error(number2digits(10,0), 'base')
93 expect_error(number2digits(10,-1), 'base')
94 expect_equal(number2digits(1,123), 1)
95 expect_equal(number2digits(1,10), 1)
96 expect_equal(number2digits(101,102), 101)
97 expect_equal(number2digits(5,4), c(1,1))
98 expect_equal(number2digits(255), rep(1,8))
99 expect_equal(number2digits(65535), rep(1,16))
100 expect_equal(number2digits(65535,16), rep(15,4))
101 expect_equal(number2digits(65534,16), c(14,15,15,15))
102 expect_equal(number2digits(65533,16), c(13,15,15,15))
103 expect_equal(number2digits(4095,8), rep(7,4))
104 expect_equal(length(number2digits(4095,8)), 4)
105 expect_equal(length(number2digits(4096,8)), 5)
106 expect_equal(length(number2digits(4096,2)), 13)
107 })
108
109 test_that("Test digit combining",{
110 expect_equal(digits2number(c(1,1)), 3)
111 expect_equal(digits2number(c(1,1,1)), 7)
112 expect_equal(digits2number(rep(15,4),16), 65535)
113 expect_equal(digits2number(c(14,15,15,15),16), 65534)
114 expect_equal(digits2number(c(0,1,1)), 6)
115 expect_equal(digits2number(c(1,1,rep(0,100))), 3)
116 expect_equal(digits2number(c(rep(0,15),1)), 2^15)
117 expect_equal(digits2number(c(rep(0,15),1),5), 5^15)
118 expect_equal(digits2number(c(rep(0,5),1),5), 5^5)
119 expect_equal(digits2number(c(1,1,1),1), 3)
120 expect_equal(digits2number(rep(0,16),2), 0)
121 expect_equal(digits2number(c()), 0)
122 expect_equal(digits2number(c(),11), 0)
123 expect_error(digits2number(c(1,1,1),0), 'base') #doesn't really require an error but probably does not produce a desired result
124 expect_error(digits2number(1,-1), 'base') #doesn't really require an error but probably does not produce a desired result
125 expect_error(digits2number(-1,10), 'digit') #doesn't really require an error but probably does not produce a desired result
126 #test fractional. Note never reaches 1
127 expect_equal(digits2number(1,base=2,fractional=TRUE),1/2)
128 expect_equal(digits2number(7,base=8,fractional=TRUE),7/8)
129 expect_equal(digits2number(c(0,1),base=2,fractional=TRUE),2/4)
130 #not really well formed but might as well handle
131 expect_equal(digits2number(20,base=2),20)
132 expect_equal(digits2number(c(1,20),base=2),41)
133 expect_equal(digits2number(20,base=8,fractional=TRUE),20/8)
134 })
135
136 test_that("Test consistency",{
137 expect_equal(digits2number(number2digits(100)), 100)
138 expect_equal(digits2number(number2digits(100,5),5), 100)
139 expect_equal(digits2number(number2digits(123456,23),23), 123456)
140 })
141
142 dat<-rnorm(1000)
143 labs<-rep(1:4,250)
144 test_that("vpPlot returns x positions",{
145 expect_equal(length(vpPlot(y=rnorm(100))), 100)
146 expect_equal(vpPlot(y=dat), 1+offsetX(dat))
147 expect_equal(vpPlot(labs,dat), labs+offsetX(dat,labs))
148 expect_silent(vpPlot(labs,dat,col='black'))
149 })