Path: blob/master/2021-summer/materials/worksheet_01/tests_worksheet_01.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}3233test_that('int_round gives expected types', {34expect_equal(typeof(int_round(234.56789, 2)), "integer")35expect_equal(typeof(int_round(234.56789, -2)), "integer")36expect_equal(typeof(int_round(234L, 2)), "integer")37})3839test_that('int_round gives expected values', {40expect_equal(int_round(234.56789, 2), 23457)41expect_equal(int_round(234.56789, -2), 2)42expect_equal(int_round(234L, 2), 23400)43})44# -4546test_3.2 <- function(){47test_that('seconds_in_a_minute should exist and have the value of how many seconds are in a minute.', {48expect_equal(digest(int_round(seconds_in_a_minute, 2)), 'fd54bb9b93cc9d4df5d0e1b0ef6d2588') # we hid the answer to the test here so you can't see it, but we can still run the test49})50test_that('seconds_in_a_hour should exist and have the value of how many seconds are in a hour.', {51expect_equal(digest(int_round(seconds_in_an_hour, 2)), '20dbbc2a608b1e18079e3a446a9cfb38') # we hid the answer to the test here so you can't see it, but we can still run the test52})53print("Success!")54}5556test_4.0 <- function(){57test_that('Solution is incorrect', {58expect_equal(digest(title), 'c76933115bc8095b2140c11556800725') # we hid the answer to the test here so you can't see it, but we can still run the test59})60print("Success!")61}6263test_4.1 <- function(){64test_that('Solution is incorrect', {65expect_equal(digest(smallest), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test66})67print("Success!")68}6970test_5.1 <- function(){71test_that('Solution is incorrect, the rvest package needs to be loaded', {72expect_that("package:rvest" %in% search() , is_true())73})74print("Success!")75}7677test_6.1 <- function(){78test_that('Solution is incorrect', {79expect_equal(digest(answer6.1), '75f1160e72554f4270c809f041c7a776') # we hid the answer to the test here so you can't see it, but we can still run the test80})81print("Success!")82}8384test_7.0.1 <- function(){85test_that('Solution is incorrect', {86expect_equal(digest(answer7.0.1), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test87})88print("Success!")89}9091test_7.0.2 <- function(){92test_that('Solution is incorrect', {93expect_equal(digest(answer7.0.2), '05ca18b596514af73f6880309a21b5dd') # we hid the answer to the test here so you can't see it, but we can still run the test94})95print("Success!")96}9798test_7.0.3 <- function(){99test_that('Solution is incorrect', {100expect_equal(digest(answer7.0.3), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test101})102print("Success!")103}104105test_7.1.1 <- function(){106test_that('Did not create an object named marathon_small', {107expect_true(exists("marathon_small"))108})109test_that('marathon_small should be a data frame', {110expect_true('data.frame' %in% class(marathon_small))111})112test_that('marathon_small does not contain the correct data', {113expect_equal(dim(marathon_small), c(1833, 5))114expect_equal(digest(int_round(sum(marathon_small$age), 2)), 'b0c43657ea54a87600e9a39a810e7d79')115expect_equal(colnames(marathon_small), c("age", "bmi", "km5_time_seconds", "km10_time_seconds", "sex"))116})117print("Success!")118}119120test_7.1.2 <- function(){121test_that('Solution is incorrect', {122expect_equal(digest(answer7.1.2), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test123})124print("Success!")125}126127test_7.2.1 <- function(){128test_that('Solution is incorrect.', {129expect_equal(digest(number_rows), '58fac55045cec17cd9f4006f4b5ab349') # we hid the answer to the test here so you can't see it, but we can still run the test130})131print("Success!")132}133134test_7.3.1 <- function(){135test_that('marathon_filtered has the incorrect number of rows', {136expect_equal(digest(int_round(nrow(marathon_filtered), 0)), 'd9509be2b148230926a2df0f355c16b2') # we hid the answer to the test here so you can't see it, but we can still run the test137})138test_that('marathon_filtered has the incorrect number of column', {139expect_equal(digest(int_round(ncol(marathon_filtered), 0)), 'dd4ad37ee474732a009111e3456e7ed7') # we hid the answer to the test here so you can't see it, but we can still run the test140})141test_that('marathon_filtered bmi column contains the incorrect values', {142expect_equal(colnames(marathon_filtered), c("age", "bmi", "km5_time_seconds", "km10_time_seconds", "sex"))143expect_equal(digest(int_round(sum(marathon_filtered$bmi), 2)), '875f8815cddc21b22a1b1eb1d5ed6ab6') # we hid the answer to the test here so you can't see it, but we can still run the test144})145print("Success!")146}147148test_7.4.1 <- function(){149test_that('marathon_male has the incorrect number of rows', {150expect_equal(digest(int_round(nrow(marathon_male), 0)), 'd9509be2b148230926a2df0f355c16b2') # we hid the answer to the test here so you can't see it, but we can still run the test151})152test_that('marathon_male has the incorrect number of columns', {153expect_equal(digest(int_round(ncol(marathon_male), 0)), 'c01f179e4b57ab8bd9de309e6d576c48') # we hid the answer to the test here so you can't see it, but we can still run the test154})155test_that('marathon_male bmi and/or km10_time_seconds column(s) contains the incorrect values', {156expect_equal(digest(int_round(sum(marathon_male$bmi), 2)), '875f8815cddc21b22a1b1eb1d5ed6ab6') # we hid the answer to the test here so you can't see it, but we can still run the test157expect_equal(digest(int_round(sum(marathon_male$km10_time_seconds, na.rm = TRUE), 2)), 'c35b7f74b3421852308c5f7722b30667') # we hid the answer to the test here so you can't see it, but we can still run the test158})159print("Success!")160}161162test_7.4.2 <- function(){163test_that('Solution is incorrect', {164expect_match(digest(answer7.4.2), "a9cf135185e7fe4ae642c8dcb228cd2d")165})166print("Success!")167}168169test_7.4.3 <- function(){170test_that('Solution is incorrect', {171expect_match(digest(answer7.4.3), "edf7faf67d063030eba4ec85c6f7cc55")172})173print("Success!")174}175176test_7.5.1 <- function(){177test_that('marathon_minutes has the incorrect number of rows', {178expect_equal(digest(int_round(nrow(marathon_minutes), 0)), 'd9509be2b148230926a2df0f355c16b2') # we hid the answer to the test here so you can't see it, but we can still run the test179})180test_that('marathon_minutes has the incorrect number of columns', {181expect_equal(digest(int_round(ncol(marathon_minutes), 0)), '11946e7a3ed5e1776e81c0f0ecd383d0') # we hid the answer to the test here so you can't see it, but we can still run the test182})183test_that('km10_time_minutes column does not exist contains incorrect values', {184expect_equal(digest(int_round(sum(marathon_minutes$km10_time_minutes, na.rm = TRUE), 2)), 'df88470bb77695f5d6bccdc54dd5c6bb') # we hid the answer to the test here so you can't see it, but we can still run the test185})186print("Success!")187}188189test_7.6.1 <- function(){190test_that('Solution is incorrect', {191expect_match(digest(answer7.6.1), '3a5505c06543876fe45598b5e5e5195d')192})193print("Success!")194}195196197