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