Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2021-summer/materials/tutorial_03/tests_tutorial_03.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_0.1 <- function(){
34
test_that('Solution is incorrect', {
35
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
36
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
37
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
38
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
39
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
40
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
41
})
42
print("Success!")
43
}
44
45
test_1.1 <- function(){
46
test_that('Did not create an object named avocado', {
47
expect_true(exists("avocado"))
48
})
49
test_that('avocado should be a data frame.', {
50
expect_true('data.frame' %in% class(avocado))
51
})
52
test_that('avocado does not contain the correct number of rows and/or columns.', {
53
expect_equal(dim(avocado), c(17911, 9))
54
})
55
test_that('avocado does not contain the correct data.', {
56
expect_equal(digest(int_round(sum(avocado$average_price), 2)), '925e36908ff4830300330ada3666458c')
57
expect_equal(colnames(avocado), c("Date", "average_price", "small_hass_volume", "large_hass_volume", "extra_l_hass_volume", "type", "yr", "region", "wk"))
58
})
59
print("Success!")
60
}
61
62
test_1.2 <- function(){
63
test_that('Did not create an object named cheapest', {
64
expect_true(exists("cheapest"))
65
})
66
test_that('cheapest should be a data frame.', {
67
expect_true('data.frame' %in% class(cheapest))
68
})
69
test_that('avocado does not contain the correct number of rows and/or columns.', {
70
expect_equal(dim(cheapest), c(1, 2))
71
})
72
test_that('cheapest does not contain the correct data.', {
73
expect_equal(digest(cheapest$region[0]), '5152ac13bdd09110d9ee9c169a3d9237') # we hid the answer to the test here so you can't see it, but we can still run the test
74
expect_equal(digest(int_round(as.integer(unlist(select(cheapest, -region))), 2)), '5d6e7fe43b3b73e5fd2961d5162486fa') # we hid the answer to the test here so you can't see it, but we can still run the test
75
})
76
print("Success!")
77
}
78
79
test_1.3 <- function(){
80
test_that('Did not create a plot named avocado_plot', {
81
expect_true(exists("avocado_plot"))
82
})
83
properties <- c(avocado_plot$layers[[1]]$mapping, avocado_plot$mapping)
84
test_that('total_volume should be on the x-axis.', {
85
expect_true("total_volume" == rlang::get_expr(properties$x))
86
})
87
test_that('average_price should be on the y-axis.', {
88
expect_true("average_price" == rlang::get_expr(properties$y))
89
})
90
test_that('region should be Houston.', {
91
expect_true(unique(avocado_plot$data$region) == "Houston")
92
})
93
test_that('avocado_plot should be a scatter plot.', {
94
expect_true("GeomPoint" %in% c(class(avocado_plot$layers[[1]]$geom)))
95
})
96
test_that('Labels on the axes should be descriptive and human readable.', {
97
expect_false(avocado_plot$labels$y == 'average_price')
98
expect_false(avocado_plot$labels$x == 'total_volume')
99
})
100
print("Success!")
101
}
102
103
test_3.1 <- function(){
104
test_that('Did not create an object named sea_surface', {
105
expect_true(exists("sea_surface"))
106
})
107
test_that('sea_surface should be a data frame.', {
108
expect_true('data.frame' %in% class(sea_surface))
109
})
110
test_that('sea_surface does not contain the correct number of rows and/or columns.', {
111
expect_equal(dim(sea_surface), c(105, 13))
112
})
113
test_that('sea_surface does not contain the correct data.', {
114
expect_equal(digest(int_round(sum(sea_surface$Dec, na.rm = TRUE), 2)), '15045e9db8607a868e0cc475a3f7b9b8')
115
expect_equal(colnames(sea_surface), c("Year", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
116
})
117
print("Success!")
118
}
119
120