Path: blob/master/2019-fall/materials/worksheet_12/tests_worksheet_12.R
2051 views
library(testthat)1library(digest)23test_1.1 <- function(){4test_that("Answer is incorrect", {5expect_equal(digest(answer1.1), '81949aed6f8e18b150efa97ff46a6fc3')6})7print("Success!")8}910test_1.2 <- function(){11test_that("Answer is incorrect", {12expect_equal(digest(paste(answer1.2, collapse="")), 'd04127a9755e9ea38971707b06bd7127')13})14print("Success!")15}1617test_1.3 <- function(){18test_that("Answer is incorrect", {19expect_equal(digest(answer1.3), '475bf9280aab63a82af60791302736f6')20})21print("Success!")22}2324test_1.4 <- function(){25test_that("Answer is incorrect", {26expect_equal(digest(answer1.4), 'c1f86f7430df7ddb256980ea6a3b57a4')27})28print("Success!")29}3031test_1.5 <- function(){32test_that('one_sample_estimates should have one column named mean, and one row.', {33expect_equal(nrow(one_sample_estimates), 1)34expect_equal(ncol(one_sample_estimates), 1)35expect_equal(digest(paste(sort(colnames(one_sample_estimates)), collapse = "")), '01e0708f75fc4f568f278b875b2e0740')36expect_equal(digest(as.numeric(one_sample_estimates$mean[1])), 'fdc85f75591a9288990774c3adfe804e')37})38print("Success!")39}4041test_1.6 <- function(){42test_that("boot1 should have 2 columns, named replicate and age", {43expect_equal(digest(paste(sort(colnames(boot1)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')44})45test_that("boot1 have 40 rows (the same number of observations as one_sample)", {46expect_equal(nrow(boot1), 40)47})48test_that("boot1 does not have the correct values in the age column", {49expect_equal(digest(as.numeric(sum(boot1$age))), 'fab7a17459dd8bc984e46800328a6ec7')50})51print("Success!")52}5354test_1.7 <- function() {55test_that("Answer is incorrect", {56expect_equal(digest(answer1.7), 'c1f86f7430df7ddb256980ea6a3b57a4')57})58print("Success!")59}6061test_1.8 <- function() {62properties <- c(boot1_dist$layers[[1]]$mapping, boot1_dist$mapping)63test_that('age should be on the x-axis.', {64expect_true("age" == rlang::get_expr(properties$x))65})66test_that('boot1_dist should be a histogram.', {67expect_that("GeomBar" %in% class(boot1_dist$layers[[1]]$geom), is_true())68})69test_that('boot1 data should be used to create the histogram', {70expect_equal(nrow(boot1_dist$data), 40)71expect_equal(round(as.numeric(sum(boot1_dist$data))), 3224)72})73test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {74expect_that((boot1_dist$labels$x) == 'age', is_false())75expect_false(is.null(boot1_dist$labels$title))76})77print("Success!")78}7980test_1.9 <- function(){81test_that("boot6 should have 2 columns, named replicate and age", {82expect_equal(digest(paste(sort(colnames(boot6)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')83})84test_that("boot6 have 240 rows (six times the number of observations in one_sample)", {85expect_equal(nrow(boot6), 240)86})87test_that("boot6 does not have the correct values in the age column", {88expect_equal(digest(as.numeric(sum(boot6$age))), '847ca481db71d85abce1e6f8689273ba')89})90print("Success!")91}9293test_2.0 <- function(){94properties <- c(boot6_dist$layers[[1]]$mapping, boot6_dist$mapping)95test_that('age should be on the x-axis.', {96expect_true("age" == rlang::get_expr(properties$x))97})98test_that('boot6_dist should be a histogram.', {99expect_that("GeomBar" %in% class(boot6_dist$layers[[1]]$geom), is_true())100})101test_that('boot6 data should be used to create the histogram', {102expect_equal(nrow(boot6_dist$data), 240)103expect_equal(round(as.numeric(sum(boot6_dist$data))), 20445)104})105test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {106expect_that((boot6_dist$labels$x) == 'age', is_false())107expect_false(is.null(boot6_dist$labels$title))108})109test_that('boot6_dist should use facet_wrap.', {110expect_that("FacetWrap" %in% class(boot6_dist$facet), is_true())111})112print("Success!")113}114115test_2.1 <- function(){116test_that('boot6_means should have 2 columns (named replicate & mean), and six rows.', {117expect_equal(nrow(boot6_means), 6)118expect_equal(ncol(boot6_means), 2)119expect_equal(digest(paste(sort(colnames(boot6_means)), collapse = "")), '35d687b4f0369a9d4e0a6ef74556908e')120expect_equal(digest(as.numeric(boot6_means$mean[1])), 'a547c316ac6b2a6873a09d5ac66b6e4b')121})122print("Success!")123}124125test_2.2 <- function(){126test_that("boot1000 should have 2 columns, named replicate and age", {127expect_equal(digest(paste(sort(colnames(boot1000)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')128})129test_that("boot1000 have 40000 rows (1000 times the number of observations in one_sample)", {130expect_equal(nrow(boot1000), 40000)131})132test_that("boot1000 does not have the correct values in the age column", {133expect_equal(digest(as.numeric(sum(boot1000$age))), '9ebba4b35bfcb23e9a575ae8802e5f20')134})135print("Success!")136}137138test_2.3 <- function(){139test_that('boot1000_means should have 2 columns (named replicate & mean), and 1000 rows.', {140expect_equal(nrow(boot1000_means), 1000)141expect_equal(ncol(boot1000_means), 2)142expect_equal(digest(paste(sort(colnames(boot1000_means)), collapse = "")), '35d687b4f0369a9d4e0a6ef74556908e')143expect_equal(digest(as.numeric(boot1000_means$mean[1])), 'a547c316ac6b2a6873a09d5ac66b6e4b')144})145print("Success!")146}147148test_2.4 <- function(){149properties <- c(boot_est_dist$layers[[1]]$mapping, boot_est_dist$mapping)150test_that('mean should be on the x-axis.', {151expect_true("mean" == rlang::get_expr(properties$x))152})153test_that('boot_est_dist should be a histogram.', {154expect_that("GeomBar" %in% class(boot_est_dist$layers[[1]]$geom), is_true())155})156test_that('boot1000_means data should be used to create the histogram', {157expect_equal(nrow(boot_est_dist$data), 1000)158expect_equal(round(as.numeric(sum(boot_est_dist$data))), 578442)159})160test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {161expect_that((boot_est_dist$labels$x) == 'age', is_false())162expect_false(is.null(boot_est_dist$labels$title))163})164print("Success!")165}166167test_2.5 <- function(){168test_that("Answer is incorrect", {169expect_equal(digest(answer2.5), '81949aed6f8e18b150efa97ff46a6fc3')170})171print("Success!")172}173174test_2.6 <- function(){175test_that("Answer is incorrect", {176expect_equal(digest(answer2.6), '96c24a598c808db5ff9c1aa505c6aa15')177})178print("Success!")179}180181test_2.7 <- function(){182test_that("Answer is incorrect", {183expect_equal(digest(answer2.7), '81949aed6f8e18b150efa97ff46a6fc3')184})185print("Success!")186}187188test_2.8 <- function(){189test_that("Answer is incorrect", {190expect_equal(digest(answer2.8), '96c24a598c808db5ff9c1aa505c6aa15')191})192print("Success!")193}194195test_2.9 <- function(){196test_that("Answer is incorrect", {197expect_equal(digest(answer2.9), '81949aed6f8e18b150efa97ff46a6fc3')198})199print("Success!")200}201202