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_05/tests_worksheet_05.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
35
test_1.1 <- function(){
36
test_that('Solution is incorrect.', {
37
expect_equal(digest(answer1.1), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test
38
})
39
print("Success!")
40
}
41
42
test_1.2 <- function(){
43
test_that('Solution is incorrect. Git is a tool for version control that is used locally on your computer.', {
44
expect_equal(digest(answer1.2), 'd2a90307aac5ae8d0ef58e2fe730d38b') # we hid the answer to the test here so you can't see it, but we can still run the test
45
})
46
print("Success! Git is a tool for version control that is used locally on your computer, whereas GitHub is an example of a remote/cloud repository hosting service where you can backup and share your files with collaborators.")
47
}
48
49
test_2.1 <- function(){
50
test_that('Solution is incorrect.', {
51
expect_equal(digest(answer2.1), 'c1f86f7430df7ddb256980ea6a3b57a4') # 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_3.1 <- function(){
57
test_that('Solution is incorrect. Technically, you can write any kind of message you want, but they are only useful if they describe what the change to the file(s) was about!', {
58
expect_equal(digest(answer3.1), 'd2a90307aac5ae8d0ef58e2fe730d38b') # 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! Yes commit messages are required, and yes what is in the message is important! The most useful messages describe what the change to the file(s) was about so that you can easily and effectively review the project's history!")
61
}
62
63
test_4.1 <- function(){
64
test_that('Solution is incorrect.', {
65
expect_equal(digest(answer4.1), '05ca18b596514af73f6880309a21b5dd') # 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_6.1 <- function(){
71
test_that('Solution is incorrect.', {
72
expect_equal(digest(answer6.1), '01a75cb73d67b0f895ff0e61449c7bf8') # we hid the answer to the test here so you can't see it, but we can still run the test
73
})
74
print("Success!")
75
}
76
77
test_7.1 <- function(){
78
test_that('Solution is incorrect. Committing your changes only puts them in the Git history on the local computer you are working on (i.e., your workspace on the JupyterHub or your laptop). To get the changes on GitHub you need to do an additional step of pushing the changes to the remote repository on GitHub.', {
79
expect_equal(digest(answer7.1), 'd2a90307aac5ae8d0ef58e2fe730d38b') # 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! You're right! The changes (and all the associated information) are not yet on GitHub, they are only in the Git history on the local computer you are working on (i.e., your workspace on the JupyterHub or your laptop).")
82
}
83
84
test_8.1 <- function(){
85
test_that('Solution is incorrect.', {
86
expect_equal(digest(answer8.1), '75f1160e72554f4270c809f041c7a776') # 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_9.1 <- function(){
92
test_that('Solution is incorrect. Public repositories are viewable by anyone, but not editable by everyone.', {
93
expect_equal(digest(answer9.1), '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! Nice work! You can share your work publicly on GitHub while still retaining control over who can edit the shared work.")
96
}
97
98
test_10.1 <- function(){
99
test_that('Solution is incorrect.', {
100
expect_equal(digest(answer10.1), '3a5505c06543876fe45598b5e5e5195d') # 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