Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2021-fall/materials/tutorial_11/tests_tutorial_11.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.0 <- function(){
34
properties <- c(pop_dist$layers[[1]]$mapping, pop_dist$mapping)
35
test_that('grade should be on the x-axis.', {
36
expect_true("grade" == rlang::get_expr(properties$x))
37
})
38
test_that('pop_dist should be a histogram.', {
39
expect_true("GeomBar" %in% class(pop_dist$layers[[1]]$geom))
40
})
41
test_that('students_pop data should be used to create the histogram', {
42
expect_equal(int_round(nrow(pop_dist$data), 0), 10000)
43
expect_equal(digest(int_round(sum(pop_dist$data), 2)), '3136d0838b3ab31dbff61d1de4659826')
44
})
45
#test_that('Labels on the x axis should be descriptive and human readable.', {
46
#expect_that((pop_dist$labels$x) == 'grade', is_false())
47
# })
48
print("Success!")
49
}
50
51
test_1.2 <- function(){
52
test_that('pop_parameters has 3 columns and one row, with column names pop_mean, pop_med and pop_sd.', {
53
expect_equal(int_round(nrow(pop_parameters), 0), 1)
54
expect_equal(int_round(ncol(pop_parameters), 0), 3)
55
expect_equal(digest(int_round(pop_parameters$pop_mean, 2)), '4eb3276e1ae5b53d7b41cd23f552d8cf')
56
expect_equal(digest(int_round(pop_parameters$pop_sd, 2)), '8c0a944402768b5b9b1e68abfeabbe7b')
57
})
58
print("Success!")
59
}
60
61
test_1.2.1 <- function(){
62
test_that('ests_5 has one row, with column names mean_5, med_5 and sd_5.', {
63
expect_equal(int_round(nrow(ests_5), 0), 1)
64
expect_true("mean_5" %in% colnames(ests_5))
65
expect_true("med_5" %in% colnames(ests_5))
66
expect_true("sd_5" %in% colnames(ests_5))
67
expect_equal(digest(int_round(ests_5$mean_5, 1)), 'aa945d05c992fcdb745752143342acd3')
68
expect_equal(digest(int_round(ests_5$sd_5, 1)), '98616fd038a28bd3aac2db8f903ebb2d')
69
})
70
print("Success!")
71
}
72
73
test_1.2.2 <- function(){
74
test_that('Solution is incorrect', {
75
expect_equal(digest(answer1.2.2), '3a5505c06543876fe45598b5e5e5195d')
76
})
77
print("Success!")
78
}
79
80
test_1.2.3 <- function(){
81
test_that('ests_100 has one row, with column names mean_100, med_100 and sd_100.', {
82
expect_equal(int_round(nrow(ests_100), 0), 1)
83
expect_true("mean_100" %in% colnames(ests_100))
84
expect_true("med_100" %in% colnames(ests_100))
85
expect_true("sd_100" %in% colnames(ests_100))
86
expect_equal(digest(int_round(ests_100$mean_100, 1)), '13d94d89c771d00b29ac574f6a3ebaf6')
87
expect_equal(digest(int_round(ests_100$sd_100, 1)), 'e4ced46f028c228b02d106d874876fe8')
88
})
89
print("Success!")
90
}
91
92
test_1.3 <- function(){
93
test_that('samples should have 7500 rows and 2 columns', {
94
expect_equal(int_round(ncol(samples), 0), 2)
95
expect_equal(int_round(nrow(samples), 0), 7500)
96
})
97
test_that('the column names of samples should be replicate and grade', {
98
expect_equal(digest(paste(sort(colnames(samples)), collapse = "")), '0454d7f37ea4f0b0109a37b637be0481')
99
})
100
test_that('size and rep arguments are incorrect', {
101
expect_equal(digest(int_round(sum(attr(samples, "groups")$replicate), 2)), '976edfa66891286b03609c35209572f5')
102
})
103
print("Success!")
104
}
105
106
test_1.4 <- function(){
107
test_that('sample_estimates should have 1500 rows and 2 columns', {
108
expect_equal(int_round(ncol(sample_estimates), 0), 2)
109
expect_equal(int_round(nrow(sample_estimates), 0), 1500)
110
})
111
test_that('the column names of sample_estimates should be replicate and sample_mean', {
112
expect_equal(digest(paste(sort(colnames(sample_estimates)), collapse = "")), '7453089f8086e9a98a067f3eeac63363')
113
})
114
print("Success!")
115
}
116
117
test_1.5 <- function(){
118
properties <- c(sampling_distribution_5$layers[[1]]$mapping, sampling_distribution_5$mapping)
119
labels <- sampling_distribution_5$labels
120
test_that('sample_mean should be on the x-axis.', {
121
expect_true("sample_mean" == rlang::get_expr(properties$x))
122
})
123
test_that('sampling_distribution_5 should be a histogram.', {
124
expect_true("GeomBar" %in% class(sampling_distribution_5$layers[[1]]$geom))
125
})
126
test_that('sampling_distribution data should be used to create the histogram', {
127
expect_equal(int_round(nrow(sampling_distribution_5$data), 0), 1500)
128
expect_equal(digest(int_round(sum(sampling_distribution_5$data), 2)), 'b4152c2ce7dc65945435c167ebaed7be')
129
})
130
test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {
131
expect_false((labels$x) == 'sample_mean')
132
expect_false(is.null(labels$title))
133
})
134
print("Success!")
135
}
136
137
#test_1.8 <- function(){
138
# properties <- c(sampling_distribution_5$layers[[1]]$mapping, sampling_distribution_5$mapping)
139
# labels <- sampling_distribution_5$labels
140
# test_that('sample_mean should be on the x-axis.', {
141
# expect_true("sample_mean" == rlang::get_expr(properties$x))
142
# })
143
# test_that('sampling_distribution_5 should be a histogram.', {
144
# expect_true("GeomBar" %in% class(sampling_distribution_5$layers[[1]]$geom))
145
# })
146
# test_that('sampling_distribution data should be used to create the histogram', {
147
# expect_equal(int_round(nrow(sampling_distribution_5$data), 0), 1500)
148
# expect_equal(digest(int_round(sum(sampling_distribution_5$data), 2)), '18d1759ac02cbbc83c6f2d30e733a093')
149
# })
150
# test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {
151
# expect_false((labels$x) == 'cups')
152
# expect_false(is.null(labels$title))
153
# })
154
# test_that('size and rep values are incorrect', {
155
# test_that(digest(int_round(sum(attr(samples, "groups")$replicate), 2)), '976edfa66891286b03609c35209572f5')
156
# })
157
# print("Success!")
158
#}
159
160
test_1.8 <- function(){
161
properties <- c(coffee_sampling_distribution_5$layers[[1]]$mapping, coffee_sampling_distribution_5$mapping)
162
labels <- coffee_sampling_distribution_5$labels
163
test_that('Did not create an object called coffee_samples_5', {
164
expect_true(exists('coffee_samples_5'))
165
})
166
test_that('coffee_samples_5 does not contain the correct data', {
167
expect_equal(dim(coffee_samples_5), c(7500, 2))
168
expect_equal(digest(int_round(sum(coffee_samples_5$replicate), 2)), '85af0fba15fcfe28a2f8ef66fdcccbcf')
169
})
170
test_that('Did not create an object called coffee_sample_estimates_5', {
171
expect_true(exists('coffee_sample_estimates_5'))
172
})
173
test_that('coffee_sample_estimates_5 does not contain a column called coffee_sample_mean_5', {
174
expect_true('coffee_sample_mean_5' %in% colnames(coffee_sample_estimates_5))
175
})
176
test_that('coffee_sample_estimates_5 does not contain the correct data', {
177
expect_equal(dim(coffee_sample_estimates_5), c(1500, 2))
178
expect_equal(digest(int_round(sum(coffee_sample_estimates_5$coffee_sample_mean_5), 2)), '1c69dbae0a048e8a95be34cbdd4ccb9f')
179
})
180
test_that('coffee_sample_mean_5 should be on the x-axis.', {
181
expect_true("coffee_sample_mean_5" == rlang::get_expr(properties$x))
182
})
183
test_that('coffee_sampling_distribution_5 should be a histogram.', {
184
expect_true("GeomBar" %in% class(coffee_sampling_distribution_5$layers[[1]]$geom))
185
})
186
test_that('coffee_sampling_distribution_5 data should be used to create the histogram', {
187
expect_equal(int_round(nrow(coffee_sampling_distribution_5$data), 0), 1500)
188
expect_equal(digest(int_round(sum(coffee_sampling_distribution_5$data), 2)), '0393ea270fd8d93548cce5b02d06d2cf')
189
})
190
test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {
191
expect_false((labels$x) == 'coffee_sample_mean_5')
192
expect_false(is.null(labels$title))
193
})
194
test_that('size and rep values are incorrect', {
195
expect_equal(digest(int_round(sum(attr(coffee_samples_5, "groups")$replicate), 2)), '976edfa66891286b03609c35209572f5')
196
})
197
print("Success!")
198
}
199
200
#test_2.0 <- function(){
201
# properties <- c(sampling_distribution_30$layers[[1]]$mapping, sampling_distribution_30$mapping)
202
# labels <- sampling_distribution_30$labels
203
# test_that('sample_mean should be on the x-axis.', {
204
# expect_true("sample_mean" == rlang::get_expr(properties$x))
205
# })
206
# test_that('sampling_distribution_30 should be a histogram.', {
207
# expect_true("GeomBar" %in% class(sampling_distribution_30$layers[[1]]$geom))
208
# })
209
# test_that('sampling_distribution_30 data should be used to create the histogram', {
210
# expect_equal(int_round(nrow(sampling_distribution_30$data), 0), 1500)
211
# expect_equal(digest(int_round(sum(sampling_distribution_30$data), 2)), "94bb219a42d934ffd9d349ddcbfa74f2")
212
# })
213
# test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {
214
# expect_false((labels$x) == 'sample_mean')
215
# expect_false(is.null(labels$title))
216
# })
217
# print("Success!")
218
#}
219
220
test_2.0 <- function(){
221
properties <- c(coffee_sampling_distribution_30$layers[[1]]$mapping, coffee_sampling_distribution_30$mapping)
222
labels <- coffee_sampling_distribution_30$labels
223
test_that('Did not create an object called coffee_samples_30', {
224
expect_true(exists('coffee_samples_30'))
225
})
226
test_that('coffee_samples_30 does not contain the correct data', {
227
expect_equal(dim(coffee_samples_30), c(45000, 2))
228
expect_equal(digest(int_round(sum(coffee_samples_30$replicate), 0)), 'e9f03592cbd42086a7a75ba269f05d5e')
229
})
230
test_that('Did not create an object called coffee_sample_estimates_30', {
231
expect_true(exists('coffee_sample_estimates_30'))
232
})
233
test_that('coffee_sample_estimates_30 does not contain a column called coffee_sample_mean_30', {
234
expect_true('coffee_sample_mean_30' %in% colnames(coffee_sample_estimates_30))
235
})
236
test_that('coffee_sample_estimates_30 does not contain the correct data', {
237
expect_equal(dim(coffee_sample_estimates_5), c(1500, 2))
238
expect_equal(digest(int_round(sum(coffee_sample_estimates_30$coffee_sample_mean_30), 2)), 'c7c6210820892e507d0173ba167d416f')
239
})
240
test_that('coffee_sample_mean_30 should be on the x-axis.', {
241
expect_true("coffee_sample_mean_30" == rlang::get_expr(properties$x))
242
})
243
test_that('coffee_sampling_distribution_30 should be a histogram.', {
244
expect_true("GeomBar" %in% class(coffee_sampling_distribution_30$layers[[1]]$geom))
245
})
246
test_that('coffee_sampling_distribution_30 data should be used to create the histogram', {
247
expect_equal(int_round(nrow(coffee_sampling_distribution_30$data), 0), 1500)
248
expect_equal(digest(int_round(sum(coffee_sampling_distribution_30$data), 2)), '87c3a46c577cba8da795a55da1f8c17b')
249
})
250
test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {
251
expect_false((labels$x) == 'coffee_sample_mean_30')
252
expect_false(is.null(labels$title))
253
})
254
test_that('size and rep values are incorrect', {
255
expect_equal(digest(int_round(sum(attr(coffee_samples_30, "groups")$replicate), 2)), '976edfa66891286b03609c35209572f5')
256
})
257
print("Success!")
258
}
259
260