Path: blob/master/2021-spring/materials/tutorial_01/tests_tutorial_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_revision <- function(){47test_that('Solution is incorrect', {48expect_equal(digest(A), 'dbc09cba9fe2583fb01d63c70e1555a8') # we hid the answer to the test here so you can't see it, but we can still run the test49expect_equal(digest(B), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test)50expect_equal(digest(C), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test51expect_equal(digest(D), '5e338704a8e069ebd8b38ca71991cf94') # we hid the answer to the test here so you can't see it, but we can still run the test52expect_equal(digest(E), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test53})54print("Success!")55}5657test_1.1 <- function(){58test_that('Solution is incorrect', {59expect_equal(digest(answer1.1), '75f1160e72554f4270c809f041c7a776') # we hid the answer to the test here so you can't see it, but we can still run the test60})61print("Success!")62}6364test_1.2 <- function(){65test_that('Solution is incorrect', {66expect_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 test67})68print("Success!")69}7071test_1.3 <- function(){72test_that('Solution is incorrect', {73expect_equal(digest(answer1.3), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test74})75print("Success!")76}7778test_1.4 <- function(){79test_that('The tidyverse package needs to be loaded.', {80expect_true("package:tidyverse" %in% search())81})82print("Success!")83}8485test_1.5 <- function(){86test_that('Did not create an object named marathon_small.', {87expect_true(exists("marathon_small"))88})89test_that('read.csv() used instead of read_csv()', {90expect_true(class(marathon_small$sex) == "character")91})92test_that('marathon_small should be a data frame.', {93expect_true('data.frame' %in% class(marathon_small))94})95test_that('marathon_small does not contain the correct data.', {96expect_equal(dim(marathon_small), c(1833, 5))97expect_equal(digest(int_round(sum(marathon_small$age), 2)), 'b0c43657ea54a87600e9a39a810e7d79')98expect_equal(colnames(marathon_small), c("age", "bmi", "km5_time_seconds", "km10_time_seconds", "sex"))99})100print("Success!")101}102103test_1.6 <- function(){104test_that('Did not create an object named marathon_age.', {105expect_true(exists("marathon_age"))106})107test_that('Did not create an object named marathon_select.', {108expect_true(exists("marathon_select"))109})110test_that('marathon_age does not contain the correct number of rows and/or columns.', {111expect_equal(dim(marathon_age), c(922, 5))112})113test_that('marathon_select does not contain the correct number of rows and/or columns.', {114expect_equal(dim(marathon_select), c(922, 2))115})116test_that('Columns in marathon_select contain incorrect values.', {117expect_equal(digest(int_round(sum(marathon_select$bmi), 2)), '8e852b0968d72c659936a14f778a1f48') # we hid the answer to the test here so you can't see it, but we can still run the test118expect_equal(digest(int_round(sum(marathon_select$km5_time_seconds, na.rm = TRUE), 2)), '8c35a70483c1be5d9f9ddbfbd62aca95') # we hid the answer to the test here so you can't see it, but we can still run the test119})120print("Success!")121}122123test_1.7 <- function(){124test_that('Did not create an object named marathon_mutate.', {125expect_true(exists("marathon_mutate"))126})127test_that('Did not create an object named marathon_exact.', {128expect_true(exists("marathon_exact"))129})130test_that('marathon_mutate does not contain the correct number of rows and/or columns.', {131expect_equal(dim(marathon_mutate), c(922, 3))132})133test_that('marathon_exact does not contain the correct number of rows and/or columns.', {134expect_equal(dim(marathon_exact), c(922, 2))135})136test_that('Columns in marathon_exact contain incorrect values.', {137expect_equal(digest(int_round(sum(marathon_exact$bmi), 2)), '8e852b0968d72c659936a14f778a1f48') # we hid the answer to the test here so you can't see it, but we can still run the test138expect_equal(digest(int_round(sum(marathon_exact$km5_time_minutes, na.rm = TRUE), 2)), '7f7f1d2422fdaef28da76e2f8f9c55ef') # we hid the answer to the test here so you can't see it, but we can still run the test139})140print("Success!")141}142143test_1.8 <- function(){144test_that('Did not create a plot named marathon_plot', {145expect_true(exists("marathon_plot"))146})147148properties <- c(marathon_plot$layers[[1]]$mapping, marathon_plot$mapping)149test_that('bmi should be on the x-axis.', {150expect_true("bmi" == rlang::get_expr(properties$x))151})152test_that('km5_time_minutes should be on the y-axis.', {153expect_true("km5_time_minutes" == rlang::get_expr(properties$y))154})155test_that('marathon_plot should be a scatter plot.', {156expect_true("GeomPoint" %in% c(class(marathon_plot$layers[[1]]$geom)))157})158test_that('Labels on the axes should be descriptive and human readable.', {159expect_false(marathon_plot$labels$y == 'km5_time_minutes')160expect_false(marathon_plot$labels$x == 'bmi')161})162print("Success!")163}164165test_1.10 <- function(){166test_that('Did not create a plot named age_vs_time', {167expect_true(exists("age_vs_time"))168})169test_that('Did not create a data frame named marathon_small_mins', {170expect_true(exists("marathon_small_mins"))171})172173properties <- c(age_vs_time$layers[[1]]$mapping, age_vs_time$mapping)174test_that('age should be on the x-axis.', {175expect_true("age" == rlang::get_expr(properties$x))176})177test_that('km5_time_minutes should be on the y-axis.', {178expect_true("km5_time_minutes" == rlang::get_expr(properties$y))179})180test_that('age_vs_time should be a scatter plot.', {181expect_true("GeomPoint" %in% c(class(age_vs_time$layers[[1]]$geom)))182})183test_that('Labels on the axes should be descriptive and human readable.', {184expect_false(age_vs_time$labels$y == 'km5_time_minutes')185expect_false(age_vs_time$labels$x == 'age') # removed since 'age' is human-readable186})187print("Success!")188}189190test_2.1 <- function(){191test_that('Solution is incorrect', {192expect_equal(digest(answer2.1), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test193})194print("Success!")195}196197test_2.2 <- function(){198test_that('Solution is incorrect', {199expect_equal(digest(answer2.2), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test200})201print("Success!")202}203204test_2.3 <- function(){205test_that('Did not create an object named bike_data.', {206expect_true(exists("bike_data"))207})208test_that('bike_data should be a data frame.', {209expect_true('data.frame' %in% class(bike_data))210})211test_that('bike_data does not contain the correct information.', {212expect_equal(dim(bike_data), c(731, 4))213expect_equal(digest(int_round(sum(bike_data$casual_users), 2)), '7aff4e006e15c73072e7c5aacc5924e3')214expect_equal(colnames(bike_data), c("temperature", "casual_users", "registered_users", "season"))215})216test_that('read.csv() used instead of read_csv()', {217expect_true(class(bike_data$season) == "character")218})219print("Success!")220}221222test_2.4 <- function(){223test_that('Did not create an object named bike_mutate.', {224expect_true(exists("bike_mutate"))225})226test_that('bike_mutate does not contain the correct number of rows and/or columns.', {227expect_equal(dim(bike_mutate), c(731, 5))228})229test_that('Columns in bike_mutate contain incorrect values.', {230expect_equal(digest(int_round(sum(bike_mutate$total_users), 2)), 'a48685b34390b4162a28ca604d58da19') # we hid the answer to the test here so you can't see it, but we can still run the test231expect_equal(digest(int_round(sum(bike_mutate$temperature, na.rm = TRUE), 2)), '5977c1efdc30d1d4c73b8c291c018836') # we hid the answer to the test here so you can't see it, but we can still run the test232})233print("Success!")234}235236test_2.5 <- function(){237test_that('Did not create an object named bike_filter.', {238expect_true(exists("bike_filter"))239})240test_that('The season column in bike_filter should only contain Spring.', {241expect_equal(unique(bike_filter$season), "Spring")242})243test_that('bike_filter does not contain the correct number of rows and/or columns.', {244expect_equal(dim(bike_filter), c(181, 5))245})246test_that('Columns in bike_filter contain incorrect values.', {247expect_equal(digest(int_round(sum(bike_filter$total_users), 2)), 'ab86544bfa851e18c0b0ab0ac4f571a7') # we hid the answer to the test here so you can't see it, but we can still run the test248expect_equal(digest(int_round(sum(bike_filter$temperature, na.rm = TRUE), 2)), 'd2192d973d9361d9018ad1df3155ee24') # we hid the answer to the test here so you can't see it, but we can still run the test249})250print("Success!")251}252253test_3.1 <- function(){254test_that('Solution is incorrect', {255expect_equal(digest(answer3.1), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test256})257print("Success!")258}259260261