Path: blob/master/2021-spring/materials/worksheet_05/tests_worksheet_05.R
2051 views
# +1library(testthat)2library(digest)34#' Round double to precise integer5#'6#' `int_round` works to create an integer corresponding to a number that is7#' tested up to a particular decimal point of precision. This is useful when8#' 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#' @examples16#' # to get an integer up to two decimals of precision from 234.5678917#' int_round(234.56789, 2)18#'19#' to get an integer rounded to the hundred digit from 234.5678920#' int_round(234.56789, -2)21int_round <- function(x, digits){22x = x*10^digits23xint = as.integer(x)24xint1 = xint + 1L25if (abs(xint - x) < abs(xint1 - x)){26return(xint)27}28else {29return(xint1)30}31}32# -3334test_1.1 <- function(){35test_that('Solution is incorrect.', {36expect_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 test37})38print("Success!")39}4041test_1.2 <- function(){42test_that('Solution is incorrect. Git is a tool for version control that is used locally on your computer.', {43expect_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 test44})45print("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.")46}4748test_2.1 <- function(){49test_that('Solution is incorrect.', {50expect_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 test51})52print("Success!")53}5455test_3.1 <- function(){56test_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!', {57expect_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 test58})59print("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!")60}6162test_4.1 <- function(){63test_that('Solution is incorrect.', {64expect_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 test65})66print("Success!")67}6869test_6.1 <- function(){70test_that('Solution is incorrect.', {71expect_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 test72})73print("Success!")74}7576test_7.1 <- function(){77test_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.', {78expect_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 test79})80print("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).")81}8283test_8.1 <- function(){84test_that('Solution is incorrect.', {85expect_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 test86})87print("Success!")88}8990test_9.1 <- function(){91test_that('Solution is incorrect. Public repositories are viewable by anyone, but not editable by everyone.', {92expect_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 test93})94print("Success! Nice work! You can share your work publicly on GitHub while still retaining control over who can edit the shared work.")95}9697test_10.1 <- function(){98test_that('Solution is incorrect.', {99expect_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 test100})101print("Success!")102}103104105