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_01/tests_worksheet_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_3.2 <- function(){
48
test_that('seconds_in_a_minute should exist and have the value of how many seconds are in a minute.', {
49
expect_equal(digest(int_round(seconds_in_a_minute, 2)), 'fd54bb9b93cc9d4df5d0e1b0ef6d2588') # we hid the answer to the test here so you can't see it, but we can still run the test
50
})
51
test_that('seconds_in_a_hour should exist and have the value of how many seconds are in a hour.', {
52
expect_equal(digest(int_round(seconds_in_an_hour, 2)), '20dbbc2a608b1e18079e3a446a9cfb38') # 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_4.0 <- function(){
58
test_that('Solution is incorrect', {
59
expect_equal(digest(title), 'c76933115bc8095b2140c11556800725') # 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_4.1 <- function(){
65
test_that('Solution is incorrect', {
66
expect_equal(digest(smallest), 'db8e490a925a60e62212cefc7674ca02') # 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_5.1 <- function(){
72
test_that('Solution is incorrect, the rvest package needs to be loaded', {
73
expect_that("package:rvest" %in% search() , is_true())
74
})
75
print("Success!")
76
}
77
78
test_6.1 <- function(){
79
test_that('Solution is incorrect', {
80
expect_equal(digest(answer6.1), '75f1160e72554f4270c809f041c7a776') # we hid the answer to the test here so you can't see it, but we can still run the test
81
})
82
print("Success!")
83
}
84
85
test_7.0.1 <- function(){
86
test_that('Solution is incorrect', {
87
expect_equal(digest(answer7.0.1), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test
88
})
89
print("Success!")
90
}
91
92
test_7.0.2 <- function(){
93
test_that('Solution is incorrect', {
94
expect_equal(digest(answer7.0.2), '05ca18b596514af73f6880309a21b5dd') # we hid the answer to the test here so you can't see it, but we can still run the test
95
})
96
print("Success!")
97
}
98
99
test_7.0.3 <- function(){
100
test_that('Solution is incorrect', {
101
expect_equal(digest(answer7.0.3), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
102
})
103
print("Success!")
104
}
105
106
test_7.1.1 <- function(){
107
test_that('Did not create an object named marathon_small', {
108
expect_true(exists("marathon_small"))
109
})
110
test_that('marathon_small should be a data frame', {
111
expect_true('data.frame' %in% class(marathon_small))
112
})
113
test_that('marathon_small does not contain the correct data', {
114
expect_equal(dim(marathon_small), c(1833, 5))
115
expect_equal(digest(int_round(sum(marathon_small$age), 2)), 'b0c43657ea54a87600e9a39a810e7d79')
116
expect_equal(colnames(marathon_small), c("age", "bmi", "km5_time_seconds", "km10_time_seconds", "sex"))
117
})
118
print("Success!")
119
}
120
121
test_7.1.2 <- function(){
122
test_that('Solution is incorrect', {
123
expect_equal(digest(answer7.1.2), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test
124
})
125
print("Success!")
126
}
127
128
test_7.2.1 <- function(){
129
test_that('Solution is incorrect.', {
130
expect_equal(digest(number_rows), '58fac55045cec17cd9f4006f4b5ab349') # we hid the answer to the test here so you can't see it, but we can still run the test
131
})
132
print("Success!")
133
}
134
135
test_7.3.1 <- function(){
136
test_that('marathon_filtered has the incorrect number of rows', {
137
expect_equal(digest(int_round(nrow(marathon_filtered), 0)), 'd9509be2b148230926a2df0f355c16b2') # we hid the answer to the test here so you can't see it, but we can still run the test
138
})
139
test_that('marathon_filtered has the incorrect number of column', {
140
expect_equal(digest(int_round(ncol(marathon_filtered), 0)), 'dd4ad37ee474732a009111e3456e7ed7') # we hid the answer to the test here so you can't see it, but we can still run the test
141
})
142
test_that('marathon_filtered bmi column contains the incorrect values', {
143
expect_equal(colnames(marathon_filtered), c("age", "bmi", "km5_time_seconds", "km10_time_seconds", "sex"))
144
expect_equal(digest(int_round(sum(marathon_filtered$bmi), 2)), '875f8815cddc21b22a1b1eb1d5ed6ab6') # we hid the answer to the test here so you can't see it, but we can still run the test
145
})
146
print("Success!")
147
}
148
149
test_7.4.1 <- function(){
150
test_that('marathon_male has the incorrect number of rows', {
151
expect_equal(digest(int_round(nrow(marathon_male), 0)), 'd9509be2b148230926a2df0f355c16b2') # we hid the answer to the test here so you can't see it, but we can still run the test
152
})
153
test_that('marathon_male has the incorrect number of columns', {
154
expect_equal(digest(int_round(ncol(marathon_male), 0)), 'c01f179e4b57ab8bd9de309e6d576c48') # we hid the answer to the test here so you can't see it, but we can still run the test
155
})
156
test_that('marathon_male bmi and/or km10_time_seconds column(s) contains the incorrect values', {
157
expect_equal(digest(int_round(sum(marathon_male$bmi), 2)), '875f8815cddc21b22a1b1eb1d5ed6ab6') # we hid the answer to the test here so you can't see it, but we can still run the test
158
expect_equal(digest(int_round(sum(marathon_male$km10_time_seconds, na.rm = TRUE), 2)), 'c35b7f74b3421852308c5f7722b30667') # we hid the answer to the test here so you can't see it, but we can still run the test
159
})
160
print("Success!")
161
}
162
163
test_7.4.2 <- function(){
164
test_that('Solution is incorrect', {
165
expect_match(digest(answer7.4.2), "a9cf135185e7fe4ae642c8dcb228cd2d")
166
})
167
print("Success!")
168
}
169
170
test_7.4.3 <- function(){
171
test_that('Solution is incorrect', {
172
expect_match(digest(answer7.4.3), "edf7faf67d063030eba4ec85c6f7cc55")
173
})
174
print("Success!")
175
}
176
177
test_7.5.1 <- function(){
178
test_that('marathon_minutes has the incorrect number of rows', {
179
expect_equal(digest(int_round(nrow(marathon_minutes), 0)), 'd9509be2b148230926a2df0f355c16b2') # we hid the answer to the test here so you can't see it, but we can still run the test
180
})
181
test_that('marathon_minutes has the incorrect number of columns', {
182
expect_equal(digest(int_round(ncol(marathon_minutes), 0)), '11946e7a3ed5e1776e81c0f0ecd383d0') # we hid the answer to the test here so you can't see it, but we can still run the test
183
})
184
test_that('km10_time_minutes column does not exist contains incorrect values', {
185
expect_equal(digest(int_round(sum(marathon_minutes$km10_time_minutes, na.rm = TRUE), 2)), 'df88470bb77695f5d6bccdc54dd5c6bb') # we hid the answer to the test here so you can't see it, but we can still run the test
186
})
187
print("Success!")
188
}
189
190
test_7.6.1 <- function(){
191
test_that('Solution is incorrect', {
192
expect_match(digest(answer7.6.1), '3a5505c06543876fe45598b5e5e5195d')
193
})
194
print("Success!")
195
}
196
197