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_01/tests_tutorial_01.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_that('int_round gives expected types', {
34
expect_equal(typeof(int_round(234.56789, 2)), "integer")
35
expect_equal(typeof(int_round(234.56789, -2)), "integer")
36
expect_equal(typeof(int_round(234L, 2)), "integer")
37
})
38
39
test_that('int_round gives expected values', {
40
expect_equal(int_round(234.56789, 2), 23457)
41
expect_equal(int_round(234.56789, -2), 2)
42
expect_equal(int_round(234L, 2), 23400)
43
})
44
# -
45
46
test_revision <- function(){
47
test_that('Solution is incorrect', {
48
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
49
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)
50
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
51
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
52
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
53
})
54
print("Success!")
55
}
56
57
test_1.1 <- function(){
58
test_that('Solution is incorrect', {
59
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
60
})
61
print("Success!")
62
}
63
64
test_1.2 <- function(){
65
test_that('Solution is incorrect', {
66
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
67
})
68
print("Success!")
69
}
70
71
test_1.3 <- function(){
72
test_that('Solution is incorrect', {
73
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
74
})
75
print("Success!")
76
}
77
78
test_1.4 <- function(){
79
test_that('The tidyverse package needs to be loaded.', {
80
expect_true("package:tidyverse" %in% search())
81
})
82
print("Success!")
83
}
84
85
test_1.5 <- function(){
86
test_that('Did not create an object named marathon_small.', {
87
expect_true(exists("marathon_small"))
88
})
89
test_that('read.csv() used instead of read_csv()', {
90
expect_true(class(marathon_small$sex) == "character")
91
})
92
test_that('marathon_small should be a data frame.', {
93
expect_true('data.frame' %in% class(marathon_small))
94
})
95
test_that('marathon_small does not contain the correct data.', {
96
expect_equal(dim(marathon_small), c(1833, 5))
97
expect_equal(digest(int_round(sum(marathon_small$age), 2)), 'b0c43657ea54a87600e9a39a810e7d79')
98
expect_equal(colnames(marathon_small), c("age", "bmi", "km5_time_seconds", "km10_time_seconds", "sex"))
99
})
100
print("Success!")
101
}
102
103
test_1.6 <- function(){
104
test_that('Did not create an object named marathon_age.', {
105
expect_true(exists("marathon_age"))
106
})
107
test_that('Did not create an object named marathon_select.', {
108
expect_true(exists("marathon_select"))
109
})
110
test_that('marathon_age does not contain the correct number of rows and/or columns.', {
111
expect_equal(dim(marathon_age), c(922, 5))
112
})
113
test_that('marathon_select does not contain the correct number of rows and/or columns.', {
114
expect_equal(dim(marathon_select), c(922, 2))
115
})
116
test_that('Columns in marathon_select contain incorrect values.', {
117
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
118
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
119
})
120
print("Success!")
121
}
122
123
test_1.7 <- function(){
124
test_that('Did not create an object named marathon_mutate.', {
125
expect_true(exists("marathon_mutate"))
126
})
127
test_that('Did not create an object named marathon_exact.', {
128
expect_true(exists("marathon_exact"))
129
})
130
test_that('marathon_mutate does not contain the correct number of rows and/or columns.', {
131
expect_equal(dim(marathon_mutate), c(922, 3))
132
})
133
test_that('marathon_exact does not contain the correct number of rows and/or columns.', {
134
expect_equal(dim(marathon_exact), c(922, 2))
135
})
136
test_that('Columns in marathon_exact contain incorrect values.', {
137
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
138
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
139
})
140
print("Success!")
141
}
142
143
test_1.8 <- function(){
144
test_that('Did not create a plot named marathon_plot', {
145
expect_true(exists("marathon_plot"))
146
})
147
148
properties <- c(marathon_plot$layers[[1]]$mapping, marathon_plot$mapping)
149
test_that('bmi should be on the x-axis.', {
150
expect_true("bmi" == rlang::get_expr(properties$x))
151
})
152
test_that('km5_time_minutes should be on the y-axis.', {
153
expect_true("km5_time_minutes" == rlang::get_expr(properties$y))
154
})
155
test_that('marathon_plot should be a scatter plot.', {
156
expect_true("GeomPoint" %in% c(class(marathon_plot$layers[[1]]$geom)))
157
})
158
test_that('Labels on the axes should be descriptive and human readable.', {
159
expect_false(marathon_plot$labels$y == 'km5_time_minutes')
160
expect_false(marathon_plot$labels$x == 'bmi')
161
})
162
print("Success!")
163
}
164
165
test_1.10 <- function(){
166
test_that('Did not create a plot named age_vs_time', {
167
expect_true(exists("age_vs_time"))
168
})
169
test_that('Did not create a data frame named marathon_small_mins', {
170
expect_true(exists("marathon_small_mins"))
171
})
172
173
properties <- c(age_vs_time$layers[[1]]$mapping, age_vs_time$mapping)
174
test_that('age should be on the x-axis.', {
175
expect_true("age" == rlang::get_expr(properties$x))
176
})
177
test_that('km5_time_minutes should be on the y-axis.', {
178
expect_true("km5_time_minutes" == rlang::get_expr(properties$y))
179
})
180
test_that('age_vs_time should be a scatter plot.', {
181
expect_true("GeomPoint" %in% c(class(age_vs_time$layers[[1]]$geom)))
182
})
183
test_that('Labels on the axes should be descriptive and human readable.', {
184
expect_false(age_vs_time$labels$y == 'km5_time_minutes')
185
expect_false(age_vs_time$labels$x == 'age') # removed since 'age' is human-readable
186
})
187
print("Success!")
188
}
189
190
test_2.1 <- function(){
191
test_that('Solution is incorrect', {
192
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
193
})
194
print("Success!")
195
}
196
197
test_2.2 <- function(){
198
test_that('Solution is incorrect', {
199
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
200
})
201
print("Success!")
202
}
203
204
test_2.3 <- function(){
205
test_that('Did not create an object named bike_data.', {
206
expect_true(exists("bike_data"))
207
})
208
test_that('bike_data should be a data frame.', {
209
expect_true('data.frame' %in% class(bike_data))
210
})
211
test_that('bike_data does not contain the correct information.', {
212
expect_equal(dim(bike_data), c(731, 4))
213
expect_equal(digest(int_round(sum(bike_data$casual_users), 2)), '7aff4e006e15c73072e7c5aacc5924e3')
214
expect_equal(colnames(bike_data), c("temperature", "casual_users", "registered_users", "season"))
215
})
216
test_that('read.csv() used instead of read_csv()', {
217
expect_true(class(bike_data$season) == "character")
218
})
219
print("Success!")
220
}
221
222
test_2.4 <- function(){
223
test_that('Did not create an object named bike_mutate.', {
224
expect_true(exists("bike_mutate"))
225
})
226
test_that('bike_mutate does not contain the correct number of rows and/or columns.', {
227
expect_equal(dim(bike_mutate), c(731, 5))
228
})
229
test_that('Columns in bike_mutate contain incorrect values.', {
230
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
231
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
232
})
233
print("Success!")
234
}
235
236
test_2.5 <- function(){
237
test_that('Did not create an object named bike_filter.', {
238
expect_true(exists("bike_filter"))
239
})
240
test_that('The season column in bike_filter should only contain Spring.', {
241
expect_equal(unique(bike_filter$season), "Spring")
242
})
243
test_that('bike_filter does not contain the correct number of rows and/or columns.', {
244
expect_equal(dim(bike_filter), c(181, 5))
245
})
246
test_that('Columns in bike_filter contain incorrect values.', {
247
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
248
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
249
})
250
print("Success!")
251
}
252
253
test_3.1 <- function(){
254
test_that('Solution is incorrect', {
255
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
256
})
257
print("Success!")
258
}
259
260