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_04/tests_tutorial_04.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
23
int_round <- function(x, digits){
24
x = x*10^digits
25
xint = as.integer(x)
26
xint1 = xint + 1L
27
if (abs(xint - x) < abs(xint1 - x)){
28
return(xint)
29
}
30
else {
31
return(xint1)
32
}
33
}
34
# -
35
test_0.1 <- function(){
36
test_that('Solution is incorrect', {
37
expect_equal(digest(A), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test
38
expect_equal(digest(B), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test
39
expect_equal(digest(C), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test
40
expect_equal(digest(D), 'dbc09cba9fe2583fb01d63c70e1555a8') # we hid the answer to the test here so you can't see it, but we can still run the test
41
expect_equal(digest(E), '0aee9b78301d7ec8998971363be87c03') # we hid the answer to the test here so you can't see it, but we can still run the test
42
expect_equal(digest(F), '5e338704a8e069ebd8b38ca71991cf94') # we hid the answer to the test here so you can't see it, but we can still run the test
43
})
44
print("Success!")
45
}
46
47
test_0.2 <- function(){
48
test_that('Solution is incorrect', {
49
expect_equal(digest(answer0.2), '05ca18b596514af73f6880309a21b5dd') # we hid the answer to the test here so you can't see it, but we can still run the test
50
})
51
print("Success!")
52
}
53
54
test_1.1 <- function(){
55
test_that('Solution is incorrect', {
56
expect_equal(digest(answer1.1), '0590b0427c1b19a6eb612d19888aa52f') # 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_1.2 <- function(){
62
test_that('Solution is incorrect', {
63
expect_equal(digest(answer1.2), '75f1160e72554f4270c809f041c7a776') # we hid the answer to the test here so you can't see it, but we can still run the test
64
})
65
print("Success!")
66
}
67
68
test_1.3 <- function(){
69
test_that('Did not create an object named insurance', {
70
expect_true(exists("insurance"))
71
})
72
test_that('insurance should be a data frame.', {
73
expect_true('data.frame' %in% class(insurance))
74
})
75
test_that('insurance does not contain the correct number of rows and/or columns.', {
76
expect_equal(dim(insurance), c(1338, 7))
77
})
78
test_that('Columns in insurance contain incorrect values.', {
79
expect_equal(digest(int_round(sum(insurance$age), 2)), 'de3a0ef8b98df33e016e2f91ab060fb5')
80
expect_equal(digest(int_round(length(unique(insurance$region)), 0)), '234a2a5581872457b9fe1187d1616b13') # 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_1.5 <- function(){
86
properties <- c(bmi_plot$layers[[1]]$mapping, bmi_plot$mapping)
87
labels <- bmi_plot$labels
88
test_that('Did not create a plot named bmi_plot', {
89
expect_true(exists("bmi_plot"))
90
})
91
test_that('bmi should be on the x-axis.', {
92
expect_true("bmi" == rlang::get_expr(properties$x))
93
})
94
test_that('charges should be on the y-axis.', {
95
expect_true("charges" == rlang::get_expr(properties$y))
96
})
97
test_that('bmi_plot should be a scatter plot.', {
98
expect_true("GeomPoint" %in% c(class(bmi_plot$layers[[1]]$geom)))
99
})
100
test_that('Labels on the axes and legend should be descriptive and human readable.', {
101
expect_false((labels$y) == 'charges')
102
expect_false((labels$x) == 'bmi')
103
})
104
print("Success!")
105
}
106
107
108
test_1.9.0 <- function(){
109
test_that('Solution is incorrect', {
110
expect_equal(digest(answer1.9.0), 'd2a90307aac5ae8d0ef58e2fe730d38b') # we hid the answer to the test here so you can't see it, but we can still run the test
111
})
112
print("Success!")
113
}
114
115
test_1.9.1 <- function(){
116
test_that('Solution is incorrect', {
117
expect_equal(digest(answer1.9.1), '05ca18b596514af73f6880309a21b5dd') # we hid the answer to the test here so you can't see it, but we can still run the test
118
})
119
print("Success!")
120
}
121
122
test_1.10_old <- function(){
123
test_that('Did not create a plot named bar_plot', {
124
expect_true(exists("bar_plot"))
125
})
126
# test_that('Count up the number of rows.')
127
# expect
128
test_that('sex should be on the x-axis.', {
129
expect_true("sex" %in% c(rlang::get_expr(bar_plot$mapping$x),rlang::get_expr(bar_plot$layers[[1]]$mapping$x)))
130
})
131
test_that('bar_plot should map smoker to the colour filled in the bars.', {
132
expect_equal(digest(as.character(rlang::get_expr(bar_plot$mapping$fill))) , '059bc865bfafc56a35be79573fc6f02b')
133
})
134
test_that('bar_plot should be a bar chart.', {
135
expect_true("GeomBar" %in% c(class(bar_plot$layers[[1]]$geom)))
136
})
137
test_that('Label for the legend should be descriptive and human readable.', {
138
expect_equal(digest(bar_plot$labels$fill), '2ba2a41537b71ba968e6f15e9d2f914e')
139
})
140
# Need to test for position = fill
141
print("Success!")
142
}
143
144
test_1.10 <- function(){
145
properties <- c(bar_plot$layers[[1]]$mapping, bar_plot$mapping)
146
labels <- bar_plot$labels
147
test_that('Did not create a plot named bar_plot', {
148
expect_true(exists("bar_plot"))
149
})
150
test_that('sex should be on the x-axis.', {
151
expect_true("sex" == rlang::get_expr(properties$x))
152
})
153
test_that('the smoker variable should be used to colour fill the bars.', {
154
expect_true("smoker" == rlang::get_expr(properties$fill))
155
})
156
test_that('bar_plot should be a bar plot.', {
157
expect_true("GeomBar" %in% c(class(bar_plot$layers[[1]]$geom)))
158
})
159
test_that('bar_plot position should be fill', {
160
expect_true("PositionFill" %in% class(bar_plot$layers[[1]]$position))
161
})
162
test_that('Labels on the axes and legend should be descriptive and human readable.', {
163
expect_false((labels$x) == 'sex')
164
expect_false((labels$fill) == 'smoker')
165
})
166
print("Success!")
167
}
168
169
test_1.11 <- function(){
170
test_that('Solution is incorrect', {
171
expect_equal(digest(answer1.11), 'a92f67bf4e1efa7b94661c5af29effc2') # we hid the answer to the test here so you can't see it, but we can still run the test
172
})
173
print("Success!")
174
}
175
176