Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2021-fall/materials/worksheet_05/tests_worksheet_05.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_1.1 <- function(){
34
test_that('Solution is incorrect.', {
35
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
36
})
37
print("Success!")
38
}
39
40
test_1.2 <- function(){
41
test_that('Solution is incorrect. Git is a tool for version control that is used locally on your computer.', {
42
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
43
})
44
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.")
45
}
46
47
test_2.1 <- function(){
48
test_that('Solution is incorrect.', {
49
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
50
})
51
print("Success!")
52
}
53
54
test_3.1 <- function(){
55
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!', {
56
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
57
})
58
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!")
59
}
60
61
test_4.1 <- function(){
62
test_that('Solution is incorrect.', {
63
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
64
})
65
print("Success!")
66
}
67
68
test_6.1 <- function(){
69
test_that('Solution is incorrect.', {
70
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
71
})
72
print("Success!")
73
}
74
75
test_7.1 <- function(){
76
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.', {
77
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
78
})
79
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).")
80
}
81
82
test_8.1 <- function(){
83
test_that('Solution is incorrect.', {
84
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
85
})
86
print("Success!")
87
}
88
89
test_9.1 <- function(){
90
test_that('Solution is incorrect. Public repositories are viewable by anyone, but not editable by everyone.', {
91
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
92
})
93
print("Success! Nice work! You can share your work publicly on GitHub while still retaining control over who can edit the shared work.")
94
}
95
96
test_10.1 <- function(){
97
test_that('Solution is incorrect.', {
98
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
99
})
100
print("Success!")
101
}
102
103