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