Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2021-spring/materials/tutorial_01/tests_tutorial_01.R
2051 views
1
# +
2
library(testthat)
3
library(digest)
4
5
#' Round double to precise integer
6
#'
7
#' `int_round` works to create an integer corresponding to a number that is
8
#' tested up to a particular decimal point of precision. This is useful when
9
#' there is a need to compare a numeric value using hashes.
10
#'
11
#' @param x Double vector of length one.
12
#' @param digits Double vector of length one to specify decimal point of precision. Negative numbers can be used to specifying significant digits > 0.1.
13
#'
14
#' @return Integer vector of length one corresponding to a particular decimal point of precision.
15
#'
16
#' @examples
17
#' # to get an integer up to two decimals of precision from 234.56789
18
#' int_round(234.56789, 2)
19
#'
20
#' to get an integer rounded to the hundred digit from 234.56789
21
#' int_round(234.56789, -2)
22
int_round <- function(x, digits){
23
x = x * 10^digits
24
xint = as.integer(x)
25
xint1 = xint + 1L
26
if (abs(xint - x) < abs(xint1 - x)){
27
return(xint)
28
}
29
else {
30
return(xint1)
31
}
32
}
33
34
test_that('int_round gives expected types', {
35
expect_equal(typeof(int_round(234.56789, 2)), "integer")
36
expect_equal(typeof(int_round(234.56789, -2)), "integer")
37
expect_equal(typeof(int_round(234L, 2)), "integer")
38
})
39
40
test_that('int_round gives expected values', {
41
expect_equal(int_round(234.56789, 2), 23457)
42
expect_equal(int_round(234.56789, -2), 2)
43
expect_equal(int_round(234L, 2), 23400)
44
})
45
# -
46
47
test_revision <- function(){
48
test_that('Solution is incorrect', {
49
expect_equal(digest(A), 'dbc09cba9fe2583fb01d63c70e1555a8') # we hid the answer to the test here so you can't see it, but we can still run the test
50
expect_equal(digest(B), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test)
51
expect_equal(digest(C), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test
52
expect_equal(digest(D), '5e338704a8e069ebd8b38ca71991cf94') # we hid the answer to the test here so you can't see it, but we can still run the test
53
expect_equal(digest(E), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test
54
})
55
print("Success!")
56
}
57
58
test_1.1 <- function(){
59
test_that('Solution is incorrect', {
60
expect_equal(digest(answer1.1), '75f1160e72554f4270c809f041c7a776') # we hid the answer to the test here so you can't see it, but we can still run the test
61
})
62
print("Success!")
63
}
64
65
test_1.2 <- function(){
66
test_that('Solution is incorrect', {
67
expect_equal(digest(answer1.2), 'd2a90307aac5ae8d0ef58e2fe730d38b') # we hid the answer to the test here so you can't see it, but we can still run the test
68
})
69
print("Success!")
70
}
71
72
test_1.3 <- function(){
73
test_that('Solution is incorrect', {
74
expect_equal(digest(answer1.3), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
75
})
76
print("Success!")
77
}
78
79
test_1.4 <- function(){
80
test_that('The tidyverse package needs to be loaded.', {
81
expect_true("package:tidyverse" %in% search())
82
})
83
print("Success!")
84
}
85
86
test_1.5 <- function(){
87
test_that('Did not create an object named marathon_small.', {
88
expect_true(exists("marathon_small"))
89
})
90
test_that('read.csv() used instead of read_csv()', {
91
expect_true(class(marathon_small$sex) == "character")
92
})
93
test_that('marathon_small should be a data frame.', {
94
expect_true('data.frame' %in% class(marathon_small))
95
})
96
test_that('marathon_small does not contain the correct data.', {
97
expect_equal(dim(marathon_small), c(1833, 5))
98
expect_equal(digest(int_round(sum(marathon_small$age), 2)), 'b0c43657ea54a87600e9a39a810e7d79')
99
expect_equal(colnames(marathon_small), c("age", "bmi", "km5_time_seconds", "km10_time_seconds", "sex"))
100
})
101
print("Success!")
102
}
103
104
test_1.6 <- function(){
105
test_that('Did not create an object named marathon_age.', {
106
expect_true(exists("marathon_age"))
107
})
108
test_that('Did not create an object named marathon_select.', {
109
expect_true(exists("marathon_select"))
110
})
111
test_that('marathon_age does not contain the correct number of rows and/or columns.', {
112
expect_equal(dim(marathon_age), c(922, 5))
113
})
114
test_that('marathon_select does not contain the correct number of rows and/or columns.', {
115
expect_equal(dim(marathon_select), c(922, 2))
116
})
117
test_that('Columns in marathon_select contain incorrect values.', {
118
expect_equal(digest(int_round(sum(marathon_select$bmi), 2)), '8e852b0968d72c659936a14f778a1f48') # we hid the answer to the test here so you can't see it, but we can still run the test
119
expect_equal(digest(int_round(sum(marathon_select$km5_time_seconds, na.rm = TRUE), 2)), '8c35a70483c1be5d9f9ddbfbd62aca95') # we hid the answer to the test here so you can't see it, but we can still run the test
120
})
121
print("Success!")
122
}
123
124
test_1.7 <- function(){
125
test_that('Did not create an object named marathon_mutate.', {
126
expect_true(exists("marathon_mutate"))
127
})
128
test_that('Did not create an object named marathon_exact.', {
129
expect_true(exists("marathon_exact"))
130
})
131
test_that('marathon_mutate does not contain the correct number of rows and/or columns.', {
132
expect_equal(dim(marathon_mutate), c(922, 3))
133
})
134
test_that('marathon_exact does not contain the correct number of rows and/or columns.', {
135
expect_equal(dim(marathon_exact), c(922, 2))
136
})
137
test_that('Columns in marathon_exact contain incorrect values.', {
138
expect_equal(digest(int_round(sum(marathon_exact$bmi), 2)), '8e852b0968d72c659936a14f778a1f48') # we hid the answer to the test here so you can't see it, but we can still run the test
139
expect_equal(digest(int_round(sum(marathon_exact$km5_time_minutes, na.rm = TRUE), 2)), '7f7f1d2422fdaef28da76e2f8f9c55ef') # we hid the answer to the test here so you can't see it, but we can still run the test
140
})
141
print("Success!")
142
}
143
144
test_1.8 <- function(){
145
test_that('Did not create a plot named marathon_plot', {
146
expect_true(exists("marathon_plot"))
147
})
148
149
properties <- c(marathon_plot$layers[[1]]$mapping, marathon_plot$mapping)
150
test_that('bmi should be on the x-axis.', {
151
expect_true("bmi" == rlang::get_expr(properties$x))
152
})
153
test_that('km5_time_minutes should be on the y-axis.', {
154
expect_true("km5_time_minutes" == rlang::get_expr(properties$y))
155
})
156
test_that('marathon_plot should be a scatter plot.', {
157
expect_true("GeomPoint" %in% c(class(marathon_plot$layers[[1]]$geom)))
158
})
159
test_that('Labels on the axes should be descriptive and human readable.', {
160
expect_false(marathon_plot$labels$y == 'km5_time_minutes')
161
expect_false(marathon_plot$labels$x == 'bmi')
162
})
163
print("Success!")
164
}
165
166
test_1.10 <- function(){
167
test_that('Did not create a plot named age_vs_time', {
168
expect_true(exists("age_vs_time"))
169
})
170
test_that('Did not create a data frame named marathon_small_mins', {
171
expect_true(exists("marathon_small_mins"))
172
})
173
174
properties <- c(age_vs_time$layers[[1]]$mapping, age_vs_time$mapping)
175
test_that('age should be on the x-axis.', {
176
expect_true("age" == rlang::get_expr(properties$x))
177
})
178
test_that('km5_time_minutes should be on the y-axis.', {
179
expect_true("km5_time_minutes" == rlang::get_expr(properties$y))
180
})
181
test_that('age_vs_time should be a scatter plot.', {
182
expect_true("GeomPoint" %in% c(class(age_vs_time$layers[[1]]$geom)))
183
})
184
test_that('Labels on the axes should be descriptive and human readable.', {
185
expect_false(age_vs_time$labels$y == 'km5_time_minutes')
186
expect_false(age_vs_time$labels$x == 'age') # removed since 'age' is human-readable
187
})
188
print("Success!")
189
}
190
191
test_2.1 <- function(){
192
test_that('Solution is incorrect', {
193
expect_equal(digest(answer2.1), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test
194
})
195
print("Success!")
196
}
197
198
test_2.2 <- function(){
199
test_that('Solution is incorrect', {
200
expect_equal(digest(answer2.2), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
201
})
202
print("Success!")
203
}
204
205
test_2.3 <- function(){
206
test_that('Did not create an object named bike_data.', {
207
expect_true(exists("bike_data"))
208
})
209
test_that('bike_data should be a data frame.', {
210
expect_true('data.frame' %in% class(bike_data))
211
})
212
test_that('bike_data does not contain the correct information.', {
213
expect_equal(dim(bike_data), c(731, 4))
214
expect_equal(digest(int_round(sum(bike_data$casual_users), 2)), '7aff4e006e15c73072e7c5aacc5924e3')
215
expect_equal(colnames(bike_data), c("temperature", "casual_users", "registered_users", "season"))
216
})
217
test_that('read.csv() used instead of read_csv()', {
218
expect_true(class(bike_data$season) == "character")
219
})
220
print("Success!")
221
}
222
223
test_2.4 <- function(){
224
test_that('Did not create an object named bike_mutate.', {
225
expect_true(exists("bike_mutate"))
226
})
227
test_that('bike_mutate does not contain the correct number of rows and/or columns.', {
228
expect_equal(dim(bike_mutate), c(731, 5))
229
})
230
test_that('Columns in bike_mutate contain incorrect values.', {
231
expect_equal(digest(int_round(sum(bike_mutate$total_users), 2)), 'a48685b34390b4162a28ca604d58da19') # we hid the answer to the test here so you can't see it, but we can still run the test
232
expect_equal(digest(int_round(sum(bike_mutate$temperature, na.rm = TRUE), 2)), '5977c1efdc30d1d4c73b8c291c018836') # we hid the answer to the test here so you can't see it, but we can still run the test
233
})
234
print("Success!")
235
}
236
237
test_2.5 <- function(){
238
test_that('Did not create an object named bike_filter.', {
239
expect_true(exists("bike_filter"))
240
})
241
test_that('The season column in bike_filter should only contain Spring.', {
242
expect_equal(unique(bike_filter$season), "Spring")
243
})
244
test_that('bike_filter does not contain the correct number of rows and/or columns.', {
245
expect_equal(dim(bike_filter), c(181, 5))
246
})
247
test_that('Columns in bike_filter contain incorrect values.', {
248
expect_equal(digest(int_round(sum(bike_filter$total_users), 2)), 'ab86544bfa851e18c0b0ab0ac4f571a7') # we hid the answer to the test here so you can't see it, but we can still run the test
249
expect_equal(digest(int_round(sum(bike_filter$temperature, na.rm = TRUE), 2)), 'd2192d973d9361d9018ad1df3155ee24') # we hid the answer to the test here so you can't see it, but we can still run the test
250
})
251
print("Success!")
252
}
253
254
test_3.1 <- function(){
255
test_that('Solution is incorrect', {
256
expect_equal(digest(answer3.1), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test
257
})
258
print("Success!")
259
}
260
261