Path: blob/master/2021-spring/materials/tutorial_04/tests_tutorial_04.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)2122int_round <- function(x, digits){23x = x*10^digits24xint = as.integer(x)25xint1 = xint + 1L26if (abs(xint - x) < abs(xint1 - x)){27return(xint)28}29else {30return(xint1)31}32}33# -34test_0.1 <- function(){35test_that('Solution is incorrect', {36expect_equal(digest(A), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test37expect_equal(digest(B), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test38expect_equal(digest(C), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test39expect_equal(digest(D), 'dbc09cba9fe2583fb01d63c70e1555a8') # we hid the answer to the test here so you can't see it, but we can still run the test40expect_equal(digest(E), '0aee9b78301d7ec8998971363be87c03') # we hid the answer to the test here so you can't see it, but we can still run the test41expect_equal(digest(F), '5e338704a8e069ebd8b38ca71991cf94') # we hid the answer to the test here so you can't see it, but we can still run the test42})43print("Success!")44}4546test_0.2 <- function(){47test_that('Solution is incorrect', {48expect_equal(digest(answer0.2), '05ca18b596514af73f6880309a21b5dd') # we hid the answer to the test here so you can't see it, but we can still run the test49})50print("Success!")51}5253test_1.1 <- function(){54test_that('Solution is incorrect', {55expect_equal(digest(answer1.1), '0590b0427c1b19a6eb612d19888aa52f') # we hid the answer to the test here so you can't see it, but we can still run the test56})57print("Success!")58}5960test_1.2 <- function(){61test_that('Solution is incorrect', {62expect_equal(digest(answer1.2), '75f1160e72554f4270c809f041c7a776') # we hid the answer to the test here so you can't see it, but we can still run the test63})64print("Success!")65}6667test_1.3 <- function(){68test_that('Did not create an object named insurance', {69expect_true(exists("insurance"))70})71test_that('insurance should be a data frame.', {72expect_true('data.frame' %in% class(insurance))73})74test_that('insurance does not contain the correct number of rows and/or columns.', {75expect_equal(dim(insurance), c(1338, 7))76})77test_that('Columns in insurance contain incorrect values.', {78expect_equal(digest(int_round(sum(insurance$age), 2)), 'de3a0ef8b98df33e016e2f91ab060fb5')79expect_equal(digest(int_round(length(unique(insurance$region)), 0)), '234a2a5581872457b9fe1187d1616b13') # 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_1.5 <- function(){85properties <- c(bmi_plot$layers[[1]]$mapping, bmi_plot$mapping)86labels <- bmi_plot$labels87test_that('Did not create a plot named bmi_plot', {88expect_true(exists("bmi_plot"))89})90test_that('bmi should be on the x-axis.', {91expect_true("bmi" == rlang::get_expr(properties$x))92})93test_that('charges should be on the y-axis.', {94expect_true("charges" == rlang::get_expr(properties$y))95})96test_that('bmi_plot should be a scatter plot.', {97expect_true("GeomPoint" %in% c(class(bmi_plot$layers[[1]]$geom)))98})99test_that('Labels on the axes and legend should be descriptive and human readable.', {100expect_false((labels$y) == 'charges')101expect_false((labels$x) == 'bmi')102})103print("Success!")104}105106107test_1.9.0 <- function(){108test_that('Solution is incorrect', {109expect_equal(digest(answer1.9.0), 'd2a90307aac5ae8d0ef58e2fe730d38b') # we hid the answer to the test here so you can't see it, but we can still run the test110})111print("Success!")112}113114test_1.9.1 <- function(){115test_that('Solution is incorrect', {116expect_equal(digest(answer1.9.1), '05ca18b596514af73f6880309a21b5dd') # we hid the answer to the test here so you can't see it, but we can still run the test117})118print("Success!")119}120121test_1.10_old <- function(){122test_that('Did not create a plot named bar_plot', {123expect_true(exists("bar_plot"))124})125# test_that('Count up the number of rows.')126# expect127test_that('sex should be on the x-axis.', {128expect_true("sex" %in% c(rlang::get_expr(bar_plot$mapping$x),rlang::get_expr(bar_plot$layers[[1]]$mapping$x)))129})130test_that('bar_plot should map smoker to the colour filled in the bars.', {131expect_equal(digest(as.character(rlang::get_expr(bar_plot$mapping$fill))) , '059bc865bfafc56a35be79573fc6f02b')132})133test_that('bar_plot should be a bar chart.', {134expect_true("GeomBar" %in% c(class(bar_plot$layers[[1]]$geom)))135})136test_that('Label for the legend should be descriptive and human readable.', {137expect_equal(digest(bar_plot$labels$fill), '2ba2a41537b71ba968e6f15e9d2f914e')138})139# Need to test for position = fill140print("Success!")141}142143test_1.10 <- function(){144properties <- c(bar_plot$layers[[1]]$mapping, bar_plot$mapping)145labels <- bar_plot$labels146test_that('Did not create a plot named bar_plot', {147expect_true(exists("bar_plot"))148})149test_that('sex should be on the x-axis.', {150expect_true("sex" == rlang::get_expr(properties$x))151})152test_that('the smoker variable should be used to colour fill the bars.', {153expect_true("smoker" == rlang::get_expr(properties$fill))154})155test_that('bar_plot should be a bar plot.', {156expect_true("GeomBar" %in% c(class(bar_plot$layers[[1]]$geom)))157})158test_that('bar_plot position should be fill', {159expect_true("PositionFill" %in% class(bar_plot$layers[[1]]$position))160})161test_that('Labels on the axes and legend should be descriptive and human readable.', {162expect_false((labels$x) == 'sex')163expect_false((labels$fill) == 'smoker')164})165print("Success!")166}167168test_1.11 <- function(){169test_that('Solution is incorrect', {170expect_equal(digest(answer1.11), 'a92f67bf4e1efa7b94661c5af29effc2') # we hid the answer to the test here so you can't see it, but we can still run the test171})172print("Success!")173}174175176