Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2022-spring/materials/worksheet_intro/tests.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_3.2 <- function(){
47
test_that('seconds_in_a_minute should exist and have the value of how many seconds are in a minute.', {
48
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
49
})
50
test_that('seconds_in_a_hour should exist and have the value of how many seconds are in a hour.', {
51
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
52
})
53
print("Success!")
54
}
55
56
test_4.0 <- function(){
57
test_that('Solution is incorrect', {
58
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
59
})
60
print("Success!")
61
}
62
63
test_4.1 <- function(){
64
test_that('Solution is incorrect', {
65
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
66
})
67
print("Success!")
68
}
69
70
test_5.1 <- function(){
71
test_that('Solution is incorrect, the rvest package needs to be loaded', {
72
expect_that("package:rvest" %in% search() , is_true())
73
})
74
print("Success!")
75
}
76
77
test_6.1 <- function(){
78
test_that('Solution is incorrect', {
79
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
80
})
81
print("Success!")
82
}
83
84
test_7.0.1 <- function(){
85
test_that('Solution is incorrect', {
86
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
87
})
88
print("Success!")
89
}
90
91
test_7.0.2 <- function(){
92
test_that('Solution is incorrect', {
93
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
94
})
95
print("Success!")
96
}
97
98
test_7.0.3 <- function(){
99
test_that('Solution is incorrect', {
100
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
101
})
102
print("Success!")
103
}
104
105
test_7.1.1 <- function(){
106
test_that('Did not create an object named marathon_small', {
107
expect_true(exists("marathon_small"))
108
})
109
test_that('marathon_small should be a data frame', {
110
expect_true('data.frame' %in% class(marathon_small))
111
})
112
test_that('marathon_small does not contain the correct data', {
113
expect_equal(dim(marathon_small), c(1833, 5))
114
expect_equal(digest(int_round(sum(marathon_small$age), 2)), 'b0c43657ea54a87600e9a39a810e7d79')
115
expect_equal(colnames(marathon_small), c("age", "bmi", "km5_time_seconds", "km10_time_seconds", "sex"))
116
})
117
print("Success!")
118
}
119
120
test_7.1.2 <- function(){
121
test_that('Solution is incorrect', {
122
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
123
})
124
print("Success!")
125
}
126
127
test_7.2.1 <- function(){
128
test_that('Solution is incorrect.', {
129
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
130
})
131
print("Success!")
132
}
133
134
test_7.3.1 <- function(){
135
test_that('marathon_filtered has the incorrect number of rows', {
136
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
137
})
138
test_that('marathon_filtered has the incorrect number of column', {
139
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
140
})
141
test_that('marathon_filtered bmi column contains the incorrect values', {
142
expect_equal(colnames(marathon_filtered), c("age", "bmi", "km5_time_seconds", "km10_time_seconds", "sex"))
143
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
144
})
145
print("Success!")
146
}
147
148
test_7.4.1 <- function(){
149
test_that('marathon_male has the incorrect number of rows', {
150
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
151
})
152
test_that('marathon_male has the incorrect number of columns', {
153
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
154
})
155
test_that('marathon_male bmi and/or km10_time_seconds column(s) contains the incorrect values', {
156
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
157
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
158
})
159
print("Success!")
160
}
161
162
test_7.4.2 <- function(){
163
test_that('Solution is incorrect', {
164
expect_match(digest(answer7.4.2), "a9cf135185e7fe4ae642c8dcb228cd2d")
165
})
166
print("Success!")
167
}
168
169
test_7.4.3 <- function(){
170
test_that('Solution is incorrect', {
171
expect_match(digest(answer7.4.3), "edf7faf67d063030eba4ec85c6f7cc55")
172
})
173
print("Success!")
174
}
175
176
test_7.5.1 <- function(){
177
test_that('marathon_minutes has the incorrect number of rows', {
178
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
179
})
180
test_that('marathon_minutes has the incorrect number of columns', {
181
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
182
})
183
test_that('km10_time_minutes column does not exist contains incorrect values', {
184
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
185
})
186
print("Success!")
187
}
188
189
test_7.6.1 <- function(){
190
test_that('Solution is incorrect', {
191
expect_match(digest(answer7.6.1), '3a5505c06543876fe45598b5e5e5195d')
192
})
193
print("Success!")
194
}
195
196