Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2020-spring/materials/worksheet_12/tests_worksheet_12.R
2711 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
})
105
test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {
106
expect_that((boot6_dist$labels$x) == 'age', is_false())
107
expect_false(is.null(boot6_dist$labels$title))
108
})
109
test_that('boot6_dist should use facet_wrap.', {
110
expect_that("FacetWrap" %in% class(boot6_dist$facet), is_true())
111
})
112
print("Success!")
113
}
114
115
test_2.1 <- function(){
116
test_that('boot6_means should have 2 columns (named replicate & mean), and six rows.', {
117
expect_equal(nrow(boot6_means), 6)
118
expect_equal(ncol(boot6_means), 2)
119
expect_equal(digest(paste(sort(colnames(boot6_means)), collapse = "")), '35d687b4f0369a9d4e0a6ef74556908e')
120
expect_equal(digest(as.numeric(boot6_means$mean[1])), 'a547c316ac6b2a6873a09d5ac66b6e4b')
121
})
122
print("Success!")
123
}
124
125
test_2.2 <- function(){
126
test_that("boot1000 should have 2 columns, named replicate and age", {
127
expect_equal(digest(paste(sort(colnames(boot1000)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')
128
})
129
test_that("boot1000 have 40000 rows (1000 times the number of observations in one_sample)", {
130
expect_equal(nrow(boot1000), 40000)
131
})
132
test_that("boot1000 does not have the correct values in the age column", {
133
expect_equal(digest(as.numeric(sum(boot1000$age))), '9ebba4b35bfcb23e9a575ae8802e5f20')
134
})
135
print("Success!")
136
}
137
138
test_2.3 <- function(){
139
test_that('boot1000_means should have 2 columns (named replicate & mean), and 1000 rows.', {
140
expect_equal(nrow(boot1000_means), 1000)
141
expect_equal(ncol(boot1000_means), 2)
142
expect_equal(digest(paste(sort(colnames(boot1000_means)), collapse = "")), '35d687b4f0369a9d4e0a6ef74556908e')
143
expect_equal(digest(as.numeric(boot1000_means$mean[1])), 'a547c316ac6b2a6873a09d5ac66b6e4b')
144
})
145
print("Success!")
146
}
147
148
test_2.4 <- function(){
149
properties <- c(boot_est_dist$layers[[1]]$mapping, boot_est_dist$mapping)
150
test_that('mean should be on the x-axis.', {
151
expect_true("mean" == rlang::get_expr(properties$x))
152
})
153
test_that('boot_est_dist should be a histogram.', {
154
expect_that("GeomBar" %in% class(boot_est_dist$layers[[1]]$geom), is_true())
155
})
156
test_that('boot1000_means data should be used to create the histogram', {
157
expect_equal(nrow(boot_est_dist$data), 1000)
158
expect_equal(round(as.numeric(sum(boot_est_dist$data))), 578442)
159
})
160
test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {
161
expect_that((boot_est_dist$labels$x) == 'age', is_false())
162
expect_false(is.null(boot_est_dist$labels$title))
163
})
164
print("Success!")
165
}
166
167
test_2.5 <- function(){
168
test_that("Answer is incorrect", {
169
expect_equal(digest(answer2.5), '81949aed6f8e18b150efa97ff46a6fc3')
170
})
171
print("Success!")
172
}
173
174
test_2.6 <- function(){
175
test_that("Answer is incorrect", {
176
expect_equal(digest(answer2.6), '96c24a598c808db5ff9c1aa505c6aa15')
177
})
178
print("Success!")
179
}
180
181
test_2.7 <- function(){
182
test_that("Answer is incorrect", {
183
expect_equal(digest(answer2.7), '81949aed6f8e18b150efa97ff46a6fc3')
184
})
185
print("Success!")
186
}
187
188
test_2.8 <- function(){
189
test_that("Answer is incorrect", {
190
expect_equal(digest(answer2.8), '96c24a598c808db5ff9c1aa505c6aa15')
191
})
192
print("Success!")
193
}
194
195
test_2.9 <- function(){
196
test_that("Answer is incorrect", {
197
expect_equal(digest(answer2.9), '81949aed6f8e18b150efa97ff46a6fc3')
198
})
199
print("Success!")
200
}
201