Path: blob/master/2021-fall/materials/tutorial_04/tests_tutorial_04.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}31# -32test_0.1 <- function(){33test_that('Solution is incorrect', {34expect_equal(digest(A), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test35expect_equal(digest(B), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test36expect_equal(digest(C), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test37expect_equal(digest(D), 'dbc09cba9fe2583fb01d63c70e1555a8') # we hid the answer to the test here so you can't see it, but we can still run the test38expect_equal(digest(E), '0aee9b78301d7ec8998971363be87c03') # we hid the answer to the test here so you can't see it, but we can still run the test39expect_equal(digest(F), '5e338704a8e069ebd8b38ca71991cf94') # we hid the answer to the test here so you can't see it, but we can still run the test40})41print("Success!")42}4344test_0.2 <- function(){45test_that('Solution is incorrect', {46expect_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 test47})48print("Success!")49}5051test_1.1 <- function(){52test_that('Solution is incorrect', {53expect_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 test54})55print("Success!")56}5758test_1.2 <- function(){59test_that('Solution is incorrect', {60expect_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 test61})62print("Success!")63}6465test_1.3 <- function(){66test_that('Did not create an object named insurance', {67expect_true(exists("insurance"))68})69test_that('insurance should be a data frame.', {70expect_true('data.frame' %in% class(insurance))71})72test_that('insurance does not contain the correct number of rows and/or columns.', {73expect_equal(dim(insurance), c(1338, 7))74})75test_that('Columns in insurance contain incorrect values.', {76expect_equal(digest(int_round(sum(insurance$age), 2)), 'de3a0ef8b98df33e016e2f91ab060fb5')77expect_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 test78})79print("Success!")80}8182test_1.5 <- function(){83properties <- c(bmi_plot$layers[[1]]$mapping, bmi_plot$mapping)84labels <- bmi_plot$labels85test_that('Did not create a plot named bmi_plot', {86expect_true(exists("bmi_plot"))87})88test_that('bmi should be on the x-axis.', {89expect_true("bmi" == rlang::get_expr(properties$x))90})91test_that('charges should be on the y-axis.', {92expect_true("charges" == rlang::get_expr(properties$y))93})94test_that('bmi_plot should be a scatter plot.', {95expect_true("GeomPoint" %in% c(class(bmi_plot$layers[[1]]$geom)))96})97test_that('Labels on the axes and legend should be descriptive and human readable.', {98expect_false((labels$y) == 'charges')99expect_false((labels$x) == 'bmi')100})101print("Success!")102}103104105test_1.9.0 <- function(){106test_that('Solution is incorrect', {107expect_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 test108})109print("Success!")110}111112test_1.9.1 <- function(){113test_that('Solution is incorrect', {114expect_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 test115})116print("Success!")117}118119test_1.10_old <- function(){120test_that('Did not create a plot named bar_plot', {121expect_true(exists("bar_plot"))122})123# test_that('Count up the number of rows.')124# expect125test_that('sex should be on the x-axis.', {126expect_true("sex" %in% c(rlang::get_expr(bar_plot$mapping$x),rlang::get_expr(bar_plot$layers[[1]]$mapping$x)))127})128test_that('bar_plot should map smoker to the colour filled in the bars.', {129expect_equal(digest(as.character(rlang::get_expr(bar_plot$mapping$fill))) , '059bc865bfafc56a35be79573fc6f02b')130})131test_that('bar_plot should be a bar chart.', {132expect_true("GeomBar" %in% c(class(bar_plot$layers[[1]]$geom)))133})134test_that('Label for the legend should be descriptive and human readable.', {135expect_equal(digest(bar_plot$labels$fill), '2ba2a41537b71ba968e6f15e9d2f914e')136})137# Need to test for position = fill138print("Success!")139}140141test_1.10 <- function(){142properties <- c(bar_plot$layers[[1]]$mapping, bar_plot$mapping)143labels <- bar_plot$labels144test_that('Did not create a plot named bar_plot', {145expect_true(exists("bar_plot"))146})147test_that('sex should be on the x-axis.', {148expect_true("sex" == rlang::get_expr(properties$x))149})150test_that('the smoker variable should be used to colour fill the bars.', {151expect_true("smoker" == rlang::get_expr(properties$fill))152})153test_that('bar_plot should be a bar plot.', {154expect_true("GeomBar" %in% c(class(bar_plot$layers[[1]]$geom)))155})156test_that('bar_plot position should be fill', {157expect_true("PositionFill" %in% class(bar_plot$layers[[1]]$position))158})159test_that('Labels on the axes and legend should be descriptive and human readable.', {160expect_false((labels$x) == 'sex')161expect_false((labels$fill) == 'smoker')162})163print("Success!")164}165166test_1.11 <- function(){167test_that('Solution is incorrect', {168expect_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 test169})170print("Success!")171}172173174