Path: blob/master/2020-fall/materials/worksheet_12/tests_worksheet_12.R
2051 views
# +1library(testthat)2library(digest)34int_round <- function(x, digits){5x = x*10^digits6xint = as.integer(x)7xint1 = xint + 1L8if (abs(xint - x) < abs(xint1 - x)){9return(xint)10}11else {12return(xint1)13}14}15# -1617test_1.1 <- function(){18test_that("Answer is incorrect", {19expect_equal(digest(answer1.1), 'd2a90307aac5ae8d0ef58e2fe730d38b')20})21print("Success!")22}2324test_1.2 <- function(){25test_that("Answer is incorrect", {26expect_equal(digest(paste(answer1.2, collapse="")), 'd04127a9755e9ea38971707b06bd7127')27})28print("Success!")29}3031test_1.3 <- function(){32test_that("Answer is incorrect", {33expect_equal(digest(answer1.3), '475bf9280aab63a82af60791302736f6')34})35print("Success!")36}3738test_1.4 <- function(){39test_that("Answer is incorrect", {40expect_equal(digest(answer1.4), 'c1f86f7430df7ddb256980ea6a3b57a4')41})42print("Success!")43}4445test_1.5 <- function(){46test_that('one_sample_estimates should have one column named mean, and one row.', {47expect_equal(int_round(nrow(one_sample_estimates), 0), 1)48expect_equal(int_round(ncol(one_sample_estimates), 0), 1)49expect_equal(digest(paste(sort(colnames(one_sample_estimates)), collapse = "")), '01e0708f75fc4f568f278b875b2e0740')50expect_equal(digest(int_round(one_sample_estimates$mean[1], 2)), 'c054e6da6a916431a27931c4e3a1efe5')51})52print("Success!")53}5455test_1.6 <- function(){56test_that("boot1 should have 2 columns, named replicate and age", {57expect_equal(digest(paste(sort(colnames(boot1)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')58})59test_that("boot1 have 40 rows (the same number of observations as one_sample)", {60expect_equal(int_round(nrow(boot1), 0), 40)61})62test_that("boot1 does not have the correct values in the age column", {63expect_equal(digest(int_round(sum(boot1$age), 2)), '112ddeb87a12f6976a1d15f6612eda87')64})65test_that("size and reps do not contain the correct values", {66expect_equal(digest(int_round(sum(as.integer(unlist(attr(boot1, "groups")))), 2)), '67a199c96b75217a12f8fa73c51e93fc')67})68print("Success!")69}7071test_1.7 <- function() {72test_that("Answer is incorrect", {73expect_equal(digest(answer1.7), 'c1f86f7430df7ddb256980ea6a3b57a4')74})75print("Success!")76}7778test_1.8 <- function() {79properties <- c(boot1_dist$layers[[1]]$mapping, boot1_dist$mapping)80labels <- boot1_dist$labels81test_that('age should be on the x-axis.', {82expect_true("age" == rlang::get_expr(properties$x))83})84test_that('boot1_dist should be a histogram.', {85expect_true("GeomBar" %in% class(boot1_dist$layers[[1]]$geom))86})87test_that('boot1 data should be used to create the histogram', {88expect_equal(int_round(nrow(boot1_dist$data), 0), 40)89expect_equal(digest(int_round(sum(boot1_dist$data), 2)), 'd3e914baed4511182de1e98d25219ac8')90})91test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {92expect_false((labels$x) == 'age')93expect_false(is.null(labels$title))94})95print("Success!")96}9798test_1.9 <- function(){99test_that("boot6 should have 2 columns, named replicate and age", {100expect_equal(digest(paste(sort(colnames(boot6)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')101})102test_that("boot6 have 240 rows (six times the number of observations in one_sample)", {103expect_equal(int_round(nrow(boot6), 0), 240)104})105test_that("boot6 does not have the correct values in the age column", {106expect_equal(digest(int_round(sum(boot6$age), 2)), 'f3f7f979ba3e6a29874aac628c26ef4f')107})108test_that("size and reps do not contain the correct values", {109expect_equal(digest(int_round(sum(as.integer(unlist(attr(boot6, "groups")))), 2)), 'c553d74ed95c022e74dce82e82d6e6dd')110})111print("Success!")112}113114test_2.0 <- function(){115properties <- c(boot6_dist$layers[[1]]$mapping, boot6_dist$mapping)116labels <- boot6_dist$labels117test_that('age should be on the x-axis.', {118expect_true("age" == rlang::get_expr(properties$x))119})120test_that('boot6_dist should be a histogram.', {121expect_true("GeomBar" %in% class(boot6_dist$layers[[1]]$geom))122})123test_that('boot6 data should be used to create the histogram', {124expect_equal(int_round(nrow(boot6_dist$data), 0), 240)125})126test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {127expect_false((labels$x) == 'age')128expect_false(is.null(labels$title))129})130test_that('boot6_dist should use facet_wrap.', {131expect_true("FacetWrap" %in% class(boot6_dist$facet))132})133print("Success!")134}135136test_2.1 <- function(){137test_that('boot6_means should have 2 columns (named replicate & mean), and six rows.', {138expect_equal(int_round(nrow(boot6_means), 0), 6)139expect_equal(int_round(ncol(boot6_means), 0), 2)140expect_equal(digest(paste(sort(colnames(boot6_means)), collapse = "")), '35d687b4f0369a9d4e0a6ef74556908e')141expect_equal(digest(int_round(boot6_means$mean[1], 2)), '1940ea892300bba15c54ed5bdbda7cb9')142})143print("Success!")144}145146test_2.2 <- function(){147test_that("boot1000 should have 2 columns, named replicate and age", {148expect_equal(digest(paste(sort(colnames(boot1000)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')149})150test_that("boot1000 have 40000 rows (1000 times the number of observations in one_sample)", {151expect_equal(int_round(nrow(boot1000), 0), 40000)152})153test_that("boot1000 does not have the correct values in the age column", {154expect_equal(digest(int_round(sum(boot1000$age), 2)), '81452ed8488b320217742924137c2e99')155})156test_that("size and reps do not contain the correct values", {157expect_equal(digest(int_round(sum(as.integer(unlist(attr(boot1000, "groups")))), 0)), 'c611e93a1a0b0bdeb5e0c5acf678ee5b')158})159print("Success!")160}161162test_2.3 <- function(){163test_that('boot1000_means should have 2 columns (named replicate & mean), and 1000 rows.', {164expect_equal(int_round(nrow(boot1000_means), 0), 1000)165expect_equal(int_round(ncol(boot1000_means), 0), 2)166expect_equal(digest(paste(sort(colnames(boot1000_means)), collapse = "")), '35d687b4f0369a9d4e0a6ef74556908e')167expect_equal(digest(int_round(boot1000_means$mean[1], 2)), '1940ea892300bba15c54ed5bdbda7cb9')168})169print("Success!")170}171172test_2.4 <- function(){173properties <- c(boot_est_dist$layers[[1]]$mapping, boot_est_dist$mapping)174labels <- boot_est_dist$labels175test_that('mean should be on the x-axis.', {176expect_true("mean" == rlang::get_expr(properties$x))177})178test_that('boot_est_dist should be a histogram.', {179expect_true("GeomBar" %in% class(boot_est_dist$layers[[1]]$geom))180})181test_that('boot1000_means data should be used to create the histogram', {182expect_equal(int_round(nrow(boot_est_dist$data), 0), 1000)183expect_equal(digest(int_round(sum(boot_est_dist$data), 2)), 'f84934414055b43f674c20306aaf69d9')184})185test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {186expect_false((labels$x) == 'age')187expect_false(is.null(labels$title))188})189print("Success!")190}191192test_2.5 <- function(){193test_that("Answer is incorrect", {194expect_equal(digest(answer2.5), 'd2a90307aac5ae8d0ef58e2fe730d38b')195})196print("Success!")197}198199test_2.6 <- function(){200test_that("Answer is incorrect", {201expect_equal(digest(answer2.6), '05ca18b596514af73f6880309a21b5dd')202})203print("Success!")204}205206test_2.7 <- function(){207test_that("Answer is incorrect", {208expect_equal(digest(answer2.7), 'd2a90307aac5ae8d0ef58e2fe730d38b')209})210print("Success!")211}212213test_2.8 <- function(){214test_that("Answer is incorrect", {215expect_equal(digest(answer2.8), '05ca18b596514af73f6880309a21b5dd')216})217print("Success!")218}219220test_2.9 <- function(){221test_that("Answer is incorrect", {222expect_equal(digest(answer2.9), 'd2a90307aac5ae8d0ef58e2fe730d38b')223})224print("Success!")225}226227228