Path: blob/master/2021-summer/materials/worksheet_12/tests_worksheet_12.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_1.1 <- function(){33test_that("Answer is incorrect", {34expect_equal(digest(answer1.1), 'd2a90307aac5ae8d0ef58e2fe730d38b')35})36print("Success!")37}3839test_1.2 <- function(){40test_that("Answer is incorrect", {41expect_equal(digest(paste(answer1.2, collapse="")), 'd04127a9755e9ea38971707b06bd7127')42})43print("Success!")44}4546test_1.3 <- function(){47test_that("Answer is incorrect", {48expect_equal(digest(answer1.3), '475bf9280aab63a82af60791302736f6')49})50print("Success!")51}5253test_1.4 <- function(){54test_that("Answer is incorrect", {55expect_equal(digest(answer1.4), 'c1f86f7430df7ddb256980ea6a3b57a4')56})57print("Success!")58}5960test_1.5 <- function(){61test_that('one_sample_estimates should have one column named mean, and one row.', {62expect_equal(int_round(nrow(one_sample_estimates), 0), 1)63expect_equal(int_round(ncol(one_sample_estimates), 0), 1)64expect_equal(digest(paste(sort(colnames(one_sample_estimates)), collapse = "")), '01e0708f75fc4f568f278b875b2e0740')65expect_equal(digest(int_round(one_sample_estimates$mean[1], 2)), 'c054e6da6a916431a27931c4e3a1efe5')66})67print("Success!")68}6970test_1.6 <- function(){71test_that("boot1 should have 2 columns, named replicate and age", {72expect_equal(digest(paste(sort(colnames(boot1)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')73})74test_that("boot1 have 40 rows (the same number of observations as one_sample)", {75expect_equal(int_round(nrow(boot1), 0), 40)76})77test_that("boot1 does not have the correct values in the age column", {78expect_equal(digest(int_round(sum(boot1$age), 2)), '112ddeb87a12f6976a1d15f6612eda87')79})80test_that("size and reps do not contain the correct values", {81expect_equal(digest(int_round(sum(as.integer(unlist(attr(boot1, "groups")))), 2)), '67a199c96b75217a12f8fa73c51e93fc')82})83print("Success!")84}8586test_1.7 <- function() {87test_that("Answer is incorrect", {88expect_equal(digest(answer1.7), 'c1f86f7430df7ddb256980ea6a3b57a4')89})90print("Success!")91}9293test_1.8 <- function() {94properties <- c(boot1_dist$layers[[1]]$mapping, boot1_dist$mapping)95labels <- boot1_dist$labels96test_that('age should be on the x-axis.', {97expect_true("age" == rlang::get_expr(properties$x))98})99test_that('boot1_dist should be a histogram.', {100expect_true("GeomBar" %in% class(boot1_dist$layers[[1]]$geom))101})102test_that('boot1 data should be used to create the histogram', {103expect_equal(int_round(nrow(boot1_dist$data), 0), 40)104expect_equal(digest(int_round(sum(boot1_dist$data), 2)), 'd3e914baed4511182de1e98d25219ac8')105})106test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {107expect_false((labels$x) == 'age')108expect_false(is.null(labels$title))109})110print("Success!")111}112113test_1.9 <- function(){114test_that("boot6 should have 2 columns, named replicate and age", {115expect_equal(digest(paste(sort(colnames(boot6)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')116})117test_that("boot6 have 240 rows (six times the number of observations in one_sample)", {118expect_equal(int_round(nrow(boot6), 0), 240)119})120test_that("boot6 does not have the correct values in the age column", {121expect_equal(digest(int_round(sum(boot6$age), 2)), 'f3f7f979ba3e6a29874aac628c26ef4f')122})123test_that("size and reps do not contain the correct values", {124expect_equal(digest(int_round(sum(as.integer(unlist(attr(boot6, "groups")))), 2)), 'c553d74ed95c022e74dce82e82d6e6dd')125})126print("Success!")127}128129test_2.0 <- function(){130properties <- c(boot6_dist$layers[[1]]$mapping, boot6_dist$mapping)131labels <- boot6_dist$labels132test_that('age should be on the x-axis.', {133expect_true("age" == rlang::get_expr(properties$x))134})135test_that('boot6_dist should be a histogram.', {136expect_true("GeomBar" %in% class(boot6_dist$layers[[1]]$geom))137})138test_that('boot6 data should be used to create the histogram', {139expect_equal(int_round(nrow(boot6_dist$data), 0), 240)140})141test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {142expect_false((labels$x) == 'age')143expect_false(is.null(labels$title))144})145test_that('boot6_dist should use facet_wrap.', {146expect_true("FacetWrap" %in% class(boot6_dist$facet))147})148print("Success!")149}150151test_2.1 <- function(){152test_that('boot6_means should have 2 columns (named replicate & mean), and six rows.', {153expect_equal(int_round(nrow(boot6_means), 0), 6)154expect_equal(int_round(ncol(boot6_means), 0), 2)155expect_equal(digest(paste(sort(colnames(boot6_means)), collapse = "")), '35d687b4f0369a9d4e0a6ef74556908e')156expect_equal(digest(int_round(boot6_means$mean[1], 2)), '1940ea892300bba15c54ed5bdbda7cb9')157})158print("Success!")159}160161test_2.2 <- function(){162test_that("boot1000 should have 2 columns, named replicate and age", {163expect_equal(digest(paste(sort(colnames(boot1000)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')164})165test_that("boot1000 have 40000 rows (1000 times the number of observations in one_sample)", {166expect_equal(int_round(nrow(boot1000), 0), 40000)167})168test_that("boot1000 does not have the correct values in the age column", {169expect_equal(digest(int_round(sum(boot1000$age), 2)), '81452ed8488b320217742924137c2e99')170})171test_that("size and reps do not contain the correct values", {172expect_equal(digest(int_round(sum(as.integer(unlist(attr(boot1000, "groups")))), 0)), 'c611e93a1a0b0bdeb5e0c5acf678ee5b')173})174print("Success!")175}176177test_2.3 <- function(){178test_that('boot1000_means should have 2 columns (named replicate & mean), and 1000 rows.', {179expect_equal(int_round(nrow(boot1000_means), 0), 1000)180expect_equal(int_round(ncol(boot1000_means), 0), 2)181expect_equal(digest(paste(sort(colnames(boot1000_means)), collapse = "")), '35d687b4f0369a9d4e0a6ef74556908e')182expect_equal(digest(int_round(boot1000_means$mean[1], 2)), '1940ea892300bba15c54ed5bdbda7cb9')183})184print("Success!")185}186187test_2.4 <- function(){188properties <- c(boot_est_dist$layers[[1]]$mapping, boot_est_dist$mapping)189labels <- boot_est_dist$labels190test_that('mean should be on the x-axis.', {191expect_true("mean" == rlang::get_expr(properties$x))192})193test_that('boot_est_dist should be a histogram.', {194expect_true("GeomBar" %in% class(boot_est_dist$layers[[1]]$geom))195})196test_that('boot1000_means data should be used to create the histogram', {197expect_equal(int_round(nrow(boot_est_dist$data), 0), 1000)198expect_equal(digest(int_round(sum(boot_est_dist$data), 2)), 'f84934414055b43f674c20306aaf69d9')199})200test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {201expect_false((labels$x) == 'age')202expect_false(is.null(labels$title))203})204print("Success!")205}206207test_2.5 <- function(){208test_that("Answer is incorrect", {209expect_equal(digest(answer2.5), 'd2a90307aac5ae8d0ef58e2fe730d38b')210})211print("Success!")212}213214test_2.6 <- function(){215test_that("Answer is incorrect", {216expect_equal(digest(answer2.6), '05ca18b596514af73f6880309a21b5dd')217})218print("Success!")219}220221test_2.7 <- function(){222test_that("Answer is incorrect", {223expect_equal(digest(answer2.7), 'd2a90307aac5ae8d0ef58e2fe730d38b')224})225print("Success!")226}227228test_2.8 <- function(){229test_that("Answer is incorrect", {230expect_equal(digest(answer2.8), '05ca18b596514af73f6880309a21b5dd')231})232print("Success!")233}234235test_2.9 <- function(){236test_that("Answer is incorrect", {237expect_equal(digest(answer2.9), 'd2a90307aac5ae8d0ef58e2fe730d38b')238})239print("Success!")240}241242243