Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2019-fall/materials/worksheet_12/tests_worksheet_12.R
2051 views
1
library(testthat)
2
library(digest)
3
4
test_1.1 <- function(){
5
test_that("Answer is incorrect", {
6
expect_equal(digest(answer1.1), '81949aed6f8e18b150efa97ff46a6fc3')
7
})
8
print("Success!")
9
}
10
11
test_1.2 <- function(){
12
test_that("Answer is incorrect", {
13
expect_equal(digest(paste(answer1.2, collapse="")), 'd04127a9755e9ea38971707b06bd7127')
14
})
15
print("Success!")
16
}
17
18
test_1.3 <- function(){
19
test_that("Answer is incorrect", {
20
expect_equal(digest(answer1.3), '475bf9280aab63a82af60791302736f6')
21
})
22
print("Success!")
23
}
24
25
test_1.4 <- function(){
26
test_that("Answer is incorrect", {
27
expect_equal(digest(answer1.4), 'c1f86f7430df7ddb256980ea6a3b57a4')
28
})
29
print("Success!")
30
}
31
32
test_1.5 <- function(){
33
test_that('one_sample_estimates should have one column named mean, and one row.', {
34
expect_equal(nrow(one_sample_estimates), 1)
35
expect_equal(ncol(one_sample_estimates), 1)
36
expect_equal(digest(paste(sort(colnames(one_sample_estimates)), collapse = "")), '01e0708f75fc4f568f278b875b2e0740')
37
expect_equal(digest(as.numeric(one_sample_estimates$mean[1])), 'fdc85f75591a9288990774c3adfe804e')
38
})
39
print("Success!")
40
}
41
42
test_1.6 <- function(){
43
test_that("boot1 should have 2 columns, named replicate and age", {
44
expect_equal(digest(paste(sort(colnames(boot1)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')
45
})
46
test_that("boot1 have 40 rows (the same number of observations as one_sample)", {
47
expect_equal(nrow(boot1), 40)
48
})
49
test_that("boot1 does not have the correct values in the age column", {
50
expect_equal(digest(as.numeric(sum(boot1$age))), 'fab7a17459dd8bc984e46800328a6ec7')
51
})
52
print("Success!")
53
}
54
55
test_1.7 <- function() {
56
test_that("Answer is incorrect", {
57
expect_equal(digest(answer1.7), 'c1f86f7430df7ddb256980ea6a3b57a4')
58
})
59
print("Success!")
60
}
61
62
test_1.8 <- function() {
63
properties <- c(boot1_dist$layers[[1]]$mapping, boot1_dist$mapping)
64
test_that('age should be on the x-axis.', {
65
expect_true("age" == rlang::get_expr(properties$x))
66
})
67
test_that('boot1_dist should be a histogram.', {
68
expect_that("GeomBar" %in% class(boot1_dist$layers[[1]]$geom), is_true())
69
})
70
test_that('boot1 data should be used to create the histogram', {
71
expect_equal(nrow(boot1_dist$data), 40)
72
expect_equal(round(as.numeric(sum(boot1_dist$data))), 3224)
73
})
74
test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {
75
expect_that((boot1_dist$labels$x) == 'age', is_false())
76
expect_false(is.null(boot1_dist$labels$title))
77
})
78
print("Success!")
79
}
80
81
test_1.9 <- function(){
82
test_that("boot6 should have 2 columns, named replicate and age", {
83
expect_equal(digest(paste(sort(colnames(boot6)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')
84
})
85
test_that("boot6 have 240 rows (six times the number of observations in one_sample)", {
86
expect_equal(nrow(boot6), 240)
87
})
88
test_that("boot6 does not have the correct values in the age column", {
89
expect_equal(digest(as.numeric(sum(boot6$age))), '847ca481db71d85abce1e6f8689273ba')
90
})
91
print("Success!")
92
}
93
94
test_2.0 <- function(){
95
properties <- c(boot6_dist$layers[[1]]$mapping, boot6_dist$mapping)
96
test_that('age should be on the x-axis.', {
97
expect_true("age" == rlang::get_expr(properties$x))
98
})
99
test_that('boot6_dist should be a histogram.', {
100
expect_that("GeomBar" %in% class(boot6_dist$layers[[1]]$geom), is_true())
101
})
102
test_that('boot6 data should be used to create the histogram', {
103
expect_equal(nrow(boot6_dist$data), 240)
104
expect_equal(round(as.numeric(sum(boot6_dist$data))), 20445)
105
})
106
test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {
107
expect_that((boot6_dist$labels$x) == 'age', is_false())
108
expect_false(is.null(boot6_dist$labels$title))
109
})
110
test_that('boot6_dist should use facet_wrap.', {
111
expect_that("FacetWrap" %in% class(boot6_dist$facet), is_true())
112
})
113
print("Success!")
114
}
115
116
test_2.1 <- function(){
117
test_that('boot6_means should have 2 columns (named replicate & mean), and six rows.', {
118
expect_equal(nrow(boot6_means), 6)
119
expect_equal(ncol(boot6_means), 2)
120
expect_equal(digest(paste(sort(colnames(boot6_means)), collapse = "")), '35d687b4f0369a9d4e0a6ef74556908e')
121
expect_equal(digest(as.numeric(boot6_means$mean[1])), 'a547c316ac6b2a6873a09d5ac66b6e4b')
122
})
123
print("Success!")
124
}
125
126
test_2.2 <- function(){
127
test_that("boot1000 should have 2 columns, named replicate and age", {
128
expect_equal(digest(paste(sort(colnames(boot1000)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')
129
})
130
test_that("boot1000 have 40000 rows (1000 times the number of observations in one_sample)", {
131
expect_equal(nrow(boot1000), 40000)
132
})
133
test_that("boot1000 does not have the correct values in the age column", {
134
expect_equal(digest(as.numeric(sum(boot1000$age))), '9ebba4b35bfcb23e9a575ae8802e5f20')
135
})
136
print("Success!")
137
}
138
139
test_2.3 <- function(){
140
test_that('boot1000_means should have 2 columns (named replicate & mean), and 1000 rows.', {
141
expect_equal(nrow(boot1000_means), 1000)
142
expect_equal(ncol(boot1000_means), 2)
143
expect_equal(digest(paste(sort(colnames(boot1000_means)), collapse = "")), '35d687b4f0369a9d4e0a6ef74556908e')
144
expect_equal(digest(as.numeric(boot1000_means$mean[1])), 'a547c316ac6b2a6873a09d5ac66b6e4b')
145
})
146
print("Success!")
147
}
148
149
test_2.4 <- function(){
150
properties <- c(boot_est_dist$layers[[1]]$mapping, boot_est_dist$mapping)
151
test_that('mean should be on the x-axis.', {
152
expect_true("mean" == rlang::get_expr(properties$x))
153
})
154
test_that('boot_est_dist should be a histogram.', {
155
expect_that("GeomBar" %in% class(boot_est_dist$layers[[1]]$geom), is_true())
156
})
157
test_that('boot1000_means data should be used to create the histogram', {
158
expect_equal(nrow(boot_est_dist$data), 1000)
159
expect_equal(round(as.numeric(sum(boot_est_dist$data))), 578442)
160
})
161
test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {
162
expect_that((boot_est_dist$labels$x) == 'age', is_false())
163
expect_false(is.null(boot_est_dist$labels$title))
164
})
165
print("Success!")
166
}
167
168
test_2.5 <- function(){
169
test_that("Answer is incorrect", {
170
expect_equal(digest(answer2.5), '81949aed6f8e18b150efa97ff46a6fc3')
171
})
172
print("Success!")
173
}
174
175
test_2.6 <- function(){
176
test_that("Answer is incorrect", {
177
expect_equal(digest(answer2.6), '96c24a598c808db5ff9c1aa505c6aa15')
178
})
179
print("Success!")
180
}
181
182
test_2.7 <- function(){
183
test_that("Answer is incorrect", {
184
expect_equal(digest(answer2.7), '81949aed6f8e18b150efa97ff46a6fc3')
185
})
186
print("Success!")
187
}
188
189
test_2.8 <- function(){
190
test_that("Answer is incorrect", {
191
expect_equal(digest(answer2.8), '96c24a598c808db5ff9c1aa505c6aa15')
192
})
193
print("Success!")
194
}
195
196
test_2.9 <- function(){
197
test_that("Answer is incorrect", {
198
expect_equal(digest(answer2.9), '81949aed6f8e18b150efa97ff46a6fc3')
199
})
200
print("Success!")
201
}
202