Path: blob/master/2021-fall/materials/tutorial_01/tests_tutorial_01.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_revision <- function(){46test_that('Solution is incorrect', {47expect_equal(digest(A), 'dbc09cba9fe2583fb01d63c70e1555a8') # we hid the answer to the test here so you can't see it, but we can still run the test48expect_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)49expect_equal(digest(C), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test50expect_equal(digest(D), '5e338704a8e069ebd8b38ca71991cf94') # we hid the answer to the test here so you can't see it, but we can still run the test51expect_equal(digest(E), '6717f2823d3202449301145073ab8719') # 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_1.1 <- function(){57test_that('Solution is incorrect', {58expect_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 test59})60print("Success!")61}6263test_1.2 <- function(){64test_that('Solution is incorrect', {65expect_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 test66})67print("Success!")68}6970test_1.3 <- function(){71test_that('Solution is incorrect', {72expect_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 test73})74print("Success!")75}7677test_1.4 <- function(){78test_that('The tidyverse package needs to be loaded.', {79expect_true("package:tidyverse" %in% search())80})81print("Success!")82}8384test_1.5 <- function(){85test_that('Did not create an object named marathon_small.', {86expect_true(exists("marathon_small"))87})88test_that('read.csv() used instead of read_csv()', {89expect_true(class(marathon_small$sex) == "character")90})91test_that('marathon_small should be a data frame.', {92expect_true('data.frame' %in% class(marathon_small))93})94test_that('marathon_small does not contain the correct data.', {95expect_equal(dim(marathon_small), c(1833, 5))96expect_equal(digest(int_round(sum(marathon_small$age), 2)), 'b0c43657ea54a87600e9a39a810e7d79')97expect_equal(colnames(marathon_small), c("age", "bmi", "km5_time_seconds", "km10_time_seconds", "sex"))98})99print("Success!")100}101102test_1.6 <- function(){103test_that('Did not create an object named marathon_age.', {104expect_true(exists("marathon_age"))105})106test_that('Did not create an object named marathon_select.', {107expect_true(exists("marathon_select"))108})109test_that('marathon_age does not contain the correct number of rows and/or columns.', {110expect_equal(dim(marathon_age), c(922, 5))111})112test_that('marathon_select does not contain the correct number of rows and/or columns.', {113expect_equal(dim(marathon_select), c(922, 2))114})115test_that('Columns in marathon_select contain incorrect values.', {116expect_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 test117expect_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 test118})119print("Success!")120}121122test_1.7 <- function(){123test_that('Did not create an object named marathon_mutate.', {124expect_true(exists("marathon_mutate"))125})126test_that('Did not create an object named marathon_exact.', {127expect_true(exists("marathon_exact"))128})129test_that('marathon_mutate does not contain the correct number of rows and/or columns.', {130expect_equal(dim(marathon_mutate), c(922, 3))131})132test_that('marathon_exact does not contain the correct number of rows and/or columns.', {133expect_equal(dim(marathon_exact), c(922, 2))134})135test_that('Columns in marathon_exact contain incorrect values.', {136expect_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 test137expect_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 test138})139print("Success!")140}141142test_1.8 <- function(){143test_that('Did not create a plot named marathon_plot', {144expect_true(exists("marathon_plot"))145})146147properties <- c(marathon_plot$layers[[1]]$mapping, marathon_plot$mapping)148test_that('bmi should be on the x-axis.', {149expect_true("bmi" == rlang::get_expr(properties$x))150})151test_that('km5_time_minutes should be on the y-axis.', {152expect_true("km5_time_minutes" == rlang::get_expr(properties$y))153})154test_that('marathon_plot should be a scatter plot.', {155expect_true("GeomPoint" %in% c(class(marathon_plot$layers[[1]]$geom)))156})157test_that('Labels on the axes should be descriptive and human readable.', {158expect_false(marathon_plot$labels$y == 'km5_time_minutes')159expect_false(marathon_plot$labels$x == 'bmi')160})161print("Success!")162}163164test_1.10 <- function(){165test_that('Did not create a plot named age_vs_time', {166expect_true(exists("age_vs_time"))167})168test_that('Did not create a data frame named marathon_small_mins', {169expect_true(exists("marathon_small_mins"))170})171172properties <- c(age_vs_time$layers[[1]]$mapping, age_vs_time$mapping)173test_that('age should be on the x-axis.', {174expect_true("age" == rlang::get_expr(properties$x))175})176test_that('km5_time_minutes should be on the y-axis.', {177expect_true("km5_time_minutes" == rlang::get_expr(properties$y))178})179test_that('age_vs_time should be a scatter plot.', {180expect_true("GeomPoint" %in% c(class(age_vs_time$layers[[1]]$geom)))181})182test_that('Labels on the axes should be descriptive and human readable.', {183expect_false(age_vs_time$labels$y == 'km5_time_minutes')184expect_false(age_vs_time$labels$x == 'age') # removed since 'age' is human-readable185})186print("Success!")187}188189test_2.1 <- function(){190test_that('Solution is incorrect', {191expect_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 test192})193print("Success!")194}195196test_2.2 <- function(){197test_that('Solution is incorrect', {198expect_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 test199})200print("Success!")201}202203test_2.3 <- function(){204test_that('Did not create an object named bike_data.', {205expect_true(exists("bike_data"))206})207test_that('bike_data should be a data frame.', {208expect_true('data.frame' %in% class(bike_data))209})210test_that('bike_data does not contain the correct information.', {211expect_equal(dim(bike_data), c(731, 4))212expect_equal(digest(int_round(sum(bike_data$casual_users), 2)), '7aff4e006e15c73072e7c5aacc5924e3')213expect_equal(colnames(bike_data), c("temperature", "casual_users", "registered_users", "season"))214})215test_that('read.csv() used instead of read_csv()', {216expect_true(class(bike_data$season) == "character")217})218print("Success!")219}220221test_2.4 <- function(){222test_that('Did not create an object named bike_mutate.', {223expect_true(exists("bike_mutate"))224})225test_that('bike_mutate does not contain the correct number of rows and/or columns.', {226expect_equal(dim(bike_mutate), c(731, 5))227})228test_that('Columns in bike_mutate contain incorrect values.', {229expect_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 test230expect_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 test231})232print("Success!")233}234235test_2.5 <- function(){236test_that('Did not create an object named bike_filter.', {237expect_true(exists("bike_filter"))238})239test_that('The season column in bike_filter should only contain Spring.', {240expect_equal(unique(bike_filter$season), "Spring")241})242test_that('bike_filter does not contain the correct number of rows and/or columns.', {243expect_equal(dim(bike_filter), c(181, 5))244})245test_that('Columns in bike_filter contain incorrect values.', {246expect_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 test247expect_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 test248})249print("Success!")250}251252test_3.1 <- function(){253test_that('Solution is incorrect', {254expect_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 test255})256print("Success!")257}258259260