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_07/tests_tutorial_07.R
2051 views
1
library(testthat)
2
library(digest)
3
library(rlang)
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
35
test_1.0 <- function(){
36
test_that('Solution is incorrect', {
37
expect_true(exists('answer1.0'))
38
expect_equal(digest(answer1.0), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test
39
})
40
print("Success!")
41
}
42
43
test_1.1 <- function(){
44
test_that('Solution is incorrect', {
45
expect_true(exists('answer1.1'))
46
expect_equal(digest(answer1.1), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
47
})
48
print("Success!")
49
}
50
51
test_2.0 <- function(){
52
test_that('number_of_rows does not exist', {
53
expect_true(exists('number_of_rows'))
54
})
55
test_that('The number of rows is incorrect', {
56
expect_equal(digest(int_round(number_of_rows, 0)), 'a58969729b9f8713e03743154dfa7b57') # we hid the answer to the test here so you can't see it, but we can still run the test
57
})
58
print("Success!")
59
}
60
61
test_2.1 <- function(){
62
test_that('Solution is incorrect', {
63
expect_equal(int_round(nrow(counts), 0), 10)
64
expect_equal(digest(int_round(sum(as.integer(counts$y)), 2)), 'ae80db6ce6693e364a09f88dd4e3d553')
65
expect_equal(dim(counts), c(10, 2))
66
expect_equal(digest(int_round(sum(as.integer(counts$n)), 2)), '9ecacdf4df8df05e2501eb3f18bd13ab')
67
})
68
print("Success!")
69
}
70
71
test_2.2 <- function(){
72
test_that('answer2.2 does not exist', {
73
expect_true(exists('answer2.2'))
74
})
75
test_that('Solution is incorrect', {
76
expect_equal(digest(answer2.2), 'd2a90307aac5ae8d0ef58e2fe730d38b')
77
})
78
print("Success!")
79
}
80
81
test_2.4 <- function(){
82
test_that('Did not create an object called label_102', {
83
expect_true(exists('label_102'))
84
})
85
test_that('label_102 is not a tibble', {
86
expect_true('tbl' %in% class(label_102))
87
})
88
test_that('y is not a factor', {
89
expect_true('factor' %in% class(label_102$y))
90
})
91
test_that('Solution is incorrect', {
92
expect_equal(digest(int_round(as.integer(label_102[[1]]), 2)), '069c0949cc402f30063b90e9a31f87e1') # we hid the answer to the test here so you can't see it, but we can still run the test
93
})
94
print("Success!")
95
}
96
97
test_3.0 <- function(){
98
test_that('testing_set does not exist', {
99
expect_true(exists('testing_set'))
100
})
101
test_that('testing_set does not contain the correct data', {
102
expect_equal(dim(testing_set), c(600, 786))
103
expect_equal(digest(int_round(sum(as.integer(testing_set$y)), 2)), '733395e0768b9479c1feffa0b067fcdd')
104
})
105
test_that('training_set does not exist', {
106
expect_true(exists('training_set'))
107
})
108
test_that('training_set does not contain the correct data',{
109
expect_equal(dim(training_set), c(600, 785))
110
expect_equal(digest(int_round(sum(as.integer(training_set$y)), 2)), '4eb0b7cdd24a983324274d66e0860365')
111
})
112
print("Success!")
113
}
114
115
test_3.2 <- function(){
116
properties <- c(cross_val_plot$layers[[1]]$mapping, cross_val_plot$mapping)
117
labels <- cross_val_plot$labels
118
test_that('Did not create an object named cross_val_plot',{
119
expect_true(exists('cross_val_plot'))
120
})
121
test_that('cross_val_plot is not a lineplot', {
122
expect_true('GeomPoint' %in% class(cross_val_plot$layers[[1]]$geom))
123
expect_true('GeomLine' %in% class(cross_val_plot$layers[[2]]$geom))
124
})
125
test_that('neighbors should be on the x-axis.', {
126
expect_true("neighbors" == rlang::get_expr(properties$x))
127
})
128
test_that('mean should be on the y-axis.', {
129
expect_true("mean" == rlang::get_expr(properties$y))
130
})
131
test_that('cross_val_plot does not contain the correct data.', {
132
expect_equal(digest(int_round(sum(cross_val_plot$data$mean), 2)), '088459b2025cd3f9cbf1451f8c2c1636')
133
expect_equal(digest(int_round(sum(cross_val_plot$data$n), 2)), '189e2f1b2fbb3743811990e9708c226a')
134
})
135
test_that('cross_val_plot should only contain accuracy.', {
136
expect_true('accuracy' %in% unique(cross_val_plot$data$.metric))
137
})
138
print("Success!")
139
}
140
141
test_3.3 <- function(){
142
test_that('Did not create an object named answer3.3', {
143
expect_true(exists('answer3.3'))
144
})
145
test_that('Solution is incorrect', {
146
expect_equal(digest(answer3.3), '75f1160e72554f4270c809f041c7a776')
147
})
148
print("Success!")
149
}
150
151
test_4.1 <- function(){
152
test_that('Did not create an object named mnist_predictions', {
153
expect_true(exists('mnist_predictions'))
154
})
155
test_that('mnist_predictions does not contain the correct data', {
156
expect_equal(digest(int_round(sum(as.integer(mnist_predictions$.pred_class)), 2)), 'ad1b496bd820958116aa67cdb147352b')
157
expect_equal(digest(int_round(sum(as.integer(mnist_predictions$y)), 2)), '733395e0768b9479c1feffa0b067fcdd')
158
})
159
test_that('Did not create an objected named mnist_conf_mat', {
160
expect_true(exists('mnist_conf_mat'))
161
})
162
test_that('mnist_conf_mat is not a confusion matrix', {
163
expect_true('conf_mat' %in% class(mnist_conf_mat))
164
})
165
test_that('mnist_conf_mat contains the incorrect data', {
166
expect_equal(digest(int_round(sum(as.integer(mnist_conf_mat$table)), 2)), '9ecacdf4df8df05e2501eb3f18bd13ab')
167
})
168
test_that('Did not create an objected named mnist_metrics', {
169
expect_true(exists('mnist_metrics'))
170
})
171
test_that('mnist_metrics is not a data frame', {
172
expect_true('tbl_df' %in% class(mnist_metrics))
173
})
174
test_that('mnist_metrics contains the incorrect data', {
175
expect_equal(digest(int_round(sum(as.integer(mnist_metrics$.estimate)), 2)), '1473d70e5646a26de3c52aa1abd85b1f')
176
})
177
178
179
180
print("Success!")
181
}
182
183
test_4.4 <- function(){
184
test_that('Did not create an object named answer4.4', {
185
expect_true(exists('answer4.4'))
186
})
187
test_that('Solution is incorrect', {
188
expect_equal(digest(answer4.4), '05ca18b596514af73f6880309a21b5dd')
189
})
190
print("Success!")
191
}
192
193