Path: blob/master/2022-spring/materials/worksheet_intro/tests.R
2051 views
library(testthat)1library(digest)23# Round double to precise integer4#5# `int_round` works to create an integer corresponding to a number that is6# tested up to a particular decimal point of precision. This is useful when7# there is a need to compare a numeric value using hashes.8#9# @param x Double vector of length one.10# @param digits Double vector of length one to specify decimal point of precision. Negative numbers can be used to specifying significant digits > 0.1.11#12# @return Integer vector of length one corresponding to a particular decimal point of precision.13#14# @examples15# # to get an integer up to two decimals of precision from 234.5678916# int_round(234.56789, 2)17#18# to get an integer rounded to the hundred digit from 234.5678919# int_round(234.56789, -2)20int_round <- function(x, digits){21x = x * 10^digits22xint = as.integer(x)23xint1 = xint + 1L24if (abs(xint - x) < abs(xint1 - x)){25return(xint)26}27else {28return(xint1)29}30}3132test_that('int_round gives expected types', {33expect_equal(typeof(int_round(234.56789, 2)), "integer")34expect_equal(typeof(int_round(234.56789, -2)), "integer")35expect_equal(typeof(int_round(234L, 2)), "integer")36})3738test_that('int_round gives expected values', {39expect_equal(int_round(234.56789, 2), 23457)40expect_equal(int_round(234.56789, -2), 2)41expect_equal(int_round(234L, 2), 23400)42})43# -4445test_3.2 <- function(){46test_that('seconds_in_a_minute should exist and have the value of how many seconds are in a minute.', {47expect_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 test48})49test_that('seconds_in_a_hour should exist and have the value of how many seconds are in a hour.', {50expect_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 test51})52print("Success!")53}5455test_4.0 <- function(){56test_that('Solution is incorrect', {57expect_equal(digest(title), 'c76933115bc8095b2140c11556800725') # we hid the answer to the test here so you can't see it, but we can still run the test58})59print("Success!")60}6162test_4.1 <- function(){63test_that('Solution is incorrect', {64expect_equal(digest(smallest), 'db8e490a925a60e62212cefc7674ca02') # 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_5.1 <- function(){70test_that('Solution is incorrect, the rvest package needs to be loaded', {71expect_that("package:rvest" %in% search() , is_true())72})73print("Success!")74}7576test_6.1 <- function(){77test_that('Solution is incorrect', {78expect_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 test79})80print("Success!")81}8283test_7.0.1 <- function(){84test_that('Solution is incorrect', {85expect_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 test86})87print("Success!")88}8990test_7.0.2 <- function(){91test_that('Solution is incorrect', {92expect_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 test93})94print("Success!")95}9697test_7.0.3 <- function(){98test_that('Solution is incorrect', {99expect_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 test100})101print("Success!")102}103104test_7.1.1 <- function(){105test_that('Did not create an object named marathon_small', {106expect_true(exists("marathon_small"))107})108test_that('marathon_small should be a data frame', {109expect_true('data.frame' %in% class(marathon_small))110})111test_that('marathon_small does not contain the correct data', {112expect_equal(dim(marathon_small), c(1833, 5))113expect_equal(digest(int_round(sum(marathon_small$age), 2)), 'b0c43657ea54a87600e9a39a810e7d79')114expect_equal(colnames(marathon_small), c("age", "bmi", "km5_time_seconds", "km10_time_seconds", "sex"))115})116print("Success!")117}118119test_7.1.2 <- function(){120test_that('Solution is incorrect', {121expect_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 test122})123print("Success!")124}125126test_7.2.1 <- function(){127test_that('Solution is incorrect.', {128expect_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 test129})130print("Success!")131}132133test_7.3.1 <- function(){134test_that('marathon_filtered has the incorrect number of rows', {135expect_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 test136})137test_that('marathon_filtered has the incorrect number of column', {138expect_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 test139})140test_that('marathon_filtered bmi column contains the incorrect values', {141expect_equal(colnames(marathon_filtered), c("age", "bmi", "km5_time_seconds", "km10_time_seconds", "sex"))142expect_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 test143})144print("Success!")145}146147test_7.4.1 <- function(){148test_that('marathon_male has the incorrect number of rows', {149expect_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 test150})151test_that('marathon_male has the incorrect number of columns', {152expect_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 test153})154test_that('marathon_male bmi and/or km10_time_seconds column(s) contains the incorrect values', {155expect_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 test156expect_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 test157})158print("Success!")159}160161test_7.4.2 <- function(){162test_that('Solution is incorrect', {163expect_match(digest(answer7.4.2), "a9cf135185e7fe4ae642c8dcb228cd2d")164})165print("Success!")166}167168test_7.4.3 <- function(){169test_that('Solution is incorrect', {170expect_match(digest(answer7.4.3), "edf7faf67d063030eba4ec85c6f7cc55")171})172print("Success!")173}174175test_7.5.1 <- function(){176test_that('marathon_minutes has the incorrect number of rows', {177expect_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 test178})179test_that('marathon_minutes has the incorrect number of columns', {180expect_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 test181})182test_that('km10_time_minutes column does not exist contains incorrect values', {183expect_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 test184})185print("Success!")186}187188test_7.6.1 <- function(){189test_that('Solution is incorrect', {190expect_match(digest(answer7.6.1), '3a5505c06543876fe45598b5e5e5195d')191})192print("Success!")193}194195196