Path: blob/master/2020-spring/materials/worksheet_12/tests_worksheet_12.R
2711 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)103})104test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {105expect_that((boot6_dist$labels$x) == 'age', is_false())106expect_false(is.null(boot6_dist$labels$title))107})108test_that('boot6_dist should use facet_wrap.', {109expect_that("FacetWrap" %in% class(boot6_dist$facet), is_true())110})111print("Success!")112}113114test_2.1 <- function(){115test_that('boot6_means should have 2 columns (named replicate & mean), and six rows.', {116expect_equal(nrow(boot6_means), 6)117expect_equal(ncol(boot6_means), 2)118expect_equal(digest(paste(sort(colnames(boot6_means)), collapse = "")), '35d687b4f0369a9d4e0a6ef74556908e')119expect_equal(digest(as.numeric(boot6_means$mean[1])), 'a547c316ac6b2a6873a09d5ac66b6e4b')120})121print("Success!")122}123124test_2.2 <- function(){125test_that("boot1000 should have 2 columns, named replicate and age", {126expect_equal(digest(paste(sort(colnames(boot1000)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')127})128test_that("boot1000 have 40000 rows (1000 times the number of observations in one_sample)", {129expect_equal(nrow(boot1000), 40000)130})131test_that("boot1000 does not have the correct values in the age column", {132expect_equal(digest(as.numeric(sum(boot1000$age))), '9ebba4b35bfcb23e9a575ae8802e5f20')133})134print("Success!")135}136137test_2.3 <- function(){138test_that('boot1000_means should have 2 columns (named replicate & mean), and 1000 rows.', {139expect_equal(nrow(boot1000_means), 1000)140expect_equal(ncol(boot1000_means), 2)141expect_equal(digest(paste(sort(colnames(boot1000_means)), collapse = "")), '35d687b4f0369a9d4e0a6ef74556908e')142expect_equal(digest(as.numeric(boot1000_means$mean[1])), 'a547c316ac6b2a6873a09d5ac66b6e4b')143})144print("Success!")145}146147test_2.4 <- function(){148properties <- c(boot_est_dist$layers[[1]]$mapping, boot_est_dist$mapping)149test_that('mean should be on the x-axis.', {150expect_true("mean" == rlang::get_expr(properties$x))151})152test_that('boot_est_dist should be a histogram.', {153expect_that("GeomBar" %in% class(boot_est_dist$layers[[1]]$geom), is_true())154})155test_that('boot1000_means data should be used to create the histogram', {156expect_equal(nrow(boot_est_dist$data), 1000)157expect_equal(round(as.numeric(sum(boot_est_dist$data))), 578442)158})159test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {160expect_that((boot_est_dist$labels$x) == 'age', is_false())161expect_false(is.null(boot_est_dist$labels$title))162})163print("Success!")164}165166test_2.5 <- function(){167test_that("Answer is incorrect", {168expect_equal(digest(answer2.5), '81949aed6f8e18b150efa97ff46a6fc3')169})170print("Success!")171}172173test_2.6 <- function(){174test_that("Answer is incorrect", {175expect_equal(digest(answer2.6), '96c24a598c808db5ff9c1aa505c6aa15')176})177print("Success!")178}179180test_2.7 <- function(){181test_that("Answer is incorrect", {182expect_equal(digest(answer2.7), '81949aed6f8e18b150efa97ff46a6fc3')183})184print("Success!")185}186187test_2.8 <- function(){188test_that("Answer is incorrect", {189expect_equal(digest(answer2.8), '96c24a598c808db5ff9c1aa505c6aa15')190})191print("Success!")192}193194test_2.9 <- function(){195test_that("Answer is incorrect", {196expect_equal(digest(answer2.9), '81949aed6f8e18b150efa97ff46a6fc3')197})198print("Success!")199}200201