Path: blob/master/2021-summer/materials/tutorial_07/tests_tutorial_07.R
2051 views
library(testthat)1library(digest)2library(rlang)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}323334test_1.0 <- function(){35test_that('Solution is incorrect', {36expect_true(exists('answer1.0'))37expect_equal(digest(answer1.0), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test38})39print("Success!")40}4142test_1.1 <- function(){43test_that('Solution is incorrect', {44expect_true(exists('answer1.1'))45expect_equal(digest(answer1.1), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test46})47print("Success!")48}4950test_2.0 <- function(){51test_that('number_of_rows does not exist', {52expect_true(exists('number_of_rows'))53})54test_that('The number of rows is incorrect', {55expect_equal(digest(int_round(number_of_rows, 0)), 'a58969729b9f8713e03743154dfa7b57') # 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_2.1 <- function(){61test_that('Solution is incorrect', {62expect_equal(int_round(nrow(counts), 0), 10)63expect_equal(digest(int_round(sum(as.integer(counts$y)), 2)), 'ae80db6ce6693e364a09f88dd4e3d553')64expect_equal(dim(counts), c(10, 2))65expect_equal(digest(int_round(sum(as.integer(counts$n)), 2)), '9ecacdf4df8df05e2501eb3f18bd13ab')66})67print("Success!")68}6970test_2.2 <- function(){71test_that('answer2.2 does not exist', {72expect_true(exists('answer2.2'))73})74test_that('Solution is incorrect', {75expect_equal(digest(answer2.2), 'd2a90307aac5ae8d0ef58e2fe730d38b')76})77print("Success!")78}7980test_2.4 <- function(){81test_that('Did not create an object called label_102', {82expect_true(exists('label_102'))83})84test_that('label_102 is not a tibble', {85expect_true('tbl' %in% class(label_102))86})87test_that('y is not a factor', {88expect_true('factor' %in% class(label_102$y))89})90test_that('Solution is incorrect', {91expect_equal(digest(int_round(as.integer(label_102[[1]]), 2)), '069c0949cc402f30063b90e9a31f87e1') # we hid the answer to the test here so you can't see it, but we can still run the test92})93print("Success!")94}9596test_3.0 <- function(){97test_that('testing_set does not exist', {98expect_true(exists('testing_set'))99})100test_that('testing_set does not contain the correct data', {101expect_equal(dim(testing_set), c(600, 786))102expect_equal(digest(int_round(sum(as.integer(testing_set$y)), 2)), '733395e0768b9479c1feffa0b067fcdd')103})104test_that('training_set does not exist', {105expect_true(exists('training_set'))106})107test_that('training_set does not contain the correct data',{108expect_equal(dim(training_set), c(600, 785))109expect_equal(digest(int_round(sum(as.integer(training_set$y)), 2)), '4eb0b7cdd24a983324274d66e0860365')110})111print("Success!")112}113114test_3.2 <- function(){115properties <- c(cross_val_plot$layers[[1]]$mapping, cross_val_plot$mapping)116labels <- cross_val_plot$labels117test_that('Did not create an object named cross_val_plot',{118expect_true(exists('cross_val_plot'))119})120test_that('cross_val_plot is not a lineplot', {121expect_true('GeomPoint' %in% class(cross_val_plot$layers[[1]]$geom))122expect_true('GeomLine' %in% class(cross_val_plot$layers[[2]]$geom))123})124test_that('neighbors should be on the x-axis.', {125expect_true("neighbors" == rlang::get_expr(properties$x))126})127test_that('mean should be on the y-axis.', {128expect_true("mean" == rlang::get_expr(properties$y))129})130test_that('cross_val_plot does not contain the correct data.', {131expect_equal(digest(int_round(sum(cross_val_plot$data$mean), 2)), '088459b2025cd3f9cbf1451f8c2c1636')132expect_equal(digest(int_round(sum(cross_val_plot$data$n), 2)), '189e2f1b2fbb3743811990e9708c226a')133})134test_that('cross_val_plot should only contain accuracy.', {135expect_true('accuracy' %in% unique(cross_val_plot$data$.metric))136})137print("Success!")138}139140test_3.3 <- function(){141test_that('Did not create an object named answer3.3', {142expect_true(exists('answer3.3'))143})144test_that('Solution is incorrect', {145expect_equal(digest(answer3.3), '75f1160e72554f4270c809f041c7a776')146})147print("Success!")148}149150test_4.1 <- function(){151test_that('Did not create an object named mnist_predictions', {152expect_true(exists('mnist_predictions'))153})154test_that('mnist_predictions does not contain the correct data', {155expect_equal(digest(int_round(sum(as.integer(mnist_predictions$.pred_class)), 2)), 'ad1b496bd820958116aa67cdb147352b')156expect_equal(digest(int_round(sum(as.integer(mnist_predictions$y)), 2)), '733395e0768b9479c1feffa0b067fcdd')157})158test_that('Did not create an objected named mnist_conf_mat', {159expect_true(exists('mnist_conf_mat'))160})161test_that('mnist_conf_mat is not a confusion matrix', {162expect_true('conf_mat' %in% class(mnist_conf_mat))163})164test_that('mnist_conf_mat contains the incorrect data', {165expect_equal(digest(int_round(sum(as.integer(mnist_conf_mat$table)), 2)), '9ecacdf4df8df05e2501eb3f18bd13ab')166})167test_that('Did not create an objected named mnist_metrics', {168expect_true(exists('mnist_metrics'))169})170test_that('mnist_metrics is not a data frame', {171expect_true('tbl_df' %in% class(mnist_metrics))172})173test_that('mnist_metrics contains the incorrect data', {174expect_equal(digest(int_round(sum(as.integer(mnist_metrics$.estimate)), 2)), '1473d70e5646a26de3c52aa1abd85b1f')175})176177178179print("Success!")180}181182test_4.4 <- function(){183test_that('Did not create an object named answer4.4', {184expect_true(exists('answer4.4'))185})186test_that('Solution is incorrect', {187expect_equal(digest(answer4.4), '05ca18b596514af73f6880309a21b5dd')188})189print("Success!")190}191192193