|
| 1 | +# These tests are auto-generated with test data from: |
| 2 | +# https://github.com/exercism/problem-specifications/tree/main/exercises/baffling-birthdays/canonical-data.json |
| 3 | +# File last updated on 2026-03-26 |
| 4 | + |
| 5 | +source("./baffling-birthdays.R") |
| 6 | +library(testthat) |
| 7 | +library(lubridate) |
| 8 | + |
| 9 | +# shared birthday |
| 10 | +test_that("one birthdate", { |
| 11 | + input <- c('2000-01-01') |
| 12 | + expect_false(shared_birthday(input)) |
| 13 | +}) |
| 14 | +test_that("two birthdates with same year, month, and day", { |
| 15 | + input <- c('2000-01-01', '2000-01-01') |
| 16 | + expect_true(shared_birthday(input)) |
| 17 | +}) |
| 18 | +test_that("two birthdates with same year and month, but different day", { |
| 19 | + input <- c('2012-05-09', '2012-05-17') |
| 20 | + expect_false(shared_birthday(input)) |
| 21 | +}) |
| 22 | +test_that("two birthdates with same month and day, but different year", { |
| 23 | + input <- c('1999-10-23', '1988-10-23') |
| 24 | + expect_true(shared_birthday(input)) |
| 25 | +}) |
| 26 | +test_that("two birthdates with same year, but different month and day", { |
| 27 | + input <- c('2007-12-19', '2007-04-27') |
| 28 | + expect_false(shared_birthday(input)) |
| 29 | +}) |
| 30 | +test_that("two birthdates with different year, month, and day", { |
| 31 | + input <- c('1997-08-04', '1963-11-23') |
| 32 | + expect_false(shared_birthday(input)) |
| 33 | +}) |
| 34 | +test_that("multiple birthdates without shared birthday", { |
| 35 | + input <- c('1966-07-29', '1977-02-12', '2001-12-25', '1980-11-10') |
| 36 | + expect_false(shared_birthday(input)) |
| 37 | +}) |
| 38 | +test_that("multiple birthdates with one shared birthday", { |
| 39 | + input <- c('1966-07-29', '1977-02-12', '2001-07-29', '1980-11-10') |
| 40 | + expect_true(shared_birthday(input)) |
| 41 | +}) |
| 42 | +test_that("multiple birthdates with more than one shared birthday", { |
| 43 | + input <- c( |
| 44 | + '1966-07-29', |
| 45 | + '1977-02-12', |
| 46 | + '2001-12-25', |
| 47 | + '1980-07-29', |
| 48 | + '2019-02-12' |
| 49 | + ) |
| 50 | + expect_true(shared_birthday(input)) |
| 51 | +}) |
| 52 | + |
| 53 | + |
| 54 | +# random birthdates |
| 55 | + |
| 56 | +test_that("random birthdates generate requested number of birthdates", { |
| 57 | + expect_all_true(sapply(1:20, \(groupsize) { |
| 58 | + length(random_birthdates(groupsize)) == groupsize |
| 59 | + })) |
| 60 | +}) |
| 61 | + |
| 62 | +test_that("random birthdates are not in leap years", { |
| 63 | + expect_all_false(leap_year(random_birthdates(100))) |
| 64 | +}) |
| 65 | + |
| 66 | +test_that("random birthdates appear random", { |
| 67 | + birthdates <- random_birthdates(500) |
| 68 | + months <- month(birthdates) |> unique() |
| 69 | + days <- day(birthdates) |> unique() |
| 70 | + expect_gte(length(months), 10) |
| 71 | + expect_gte(length(days), 28) |
| 72 | +}) |
| 73 | + |
| 74 | + |
| 75 | +# estimated probability of at least one shared birthday |
| 76 | + |
| 77 | +test_that("for one person", { |
| 78 | + expected <- 0.0 |
| 79 | + rtol <- ifelse(expected > 0.1 && expected < 0.9, 0.1, 0.01) |
| 80 | + expect_equal( |
| 81 | + estimate_probability_of_shared_birthday(1), |
| 82 | + expected, |
| 83 | + tolerance = rtol |
| 84 | + ) |
| 85 | +}) |
| 86 | + |
| 87 | +test_that("among ten people", { |
| 88 | + expected <- 0.11694818 |
| 89 | + rtol <- ifelse(expected > 0.1 && expected < 0.9, 0.1, 0.01) |
| 90 | + expect_equal( |
| 91 | + estimate_probability_of_shared_birthday(10), |
| 92 | + expected, |
| 93 | + tolerance = rtol |
| 94 | + ) |
| 95 | +}) |
| 96 | + |
| 97 | +test_that("among twenty-three people", { |
| 98 | + expected <- 0.50729723 |
| 99 | + rtol <- ifelse(expected > 0.1 && expected < 0.9, 0.1, 0.01) |
| 100 | + expect_equal( |
| 101 | + estimate_probability_of_shared_birthday(23), |
| 102 | + expected, |
| 103 | + tolerance = rtol |
| 104 | + ) |
| 105 | +}) |
| 106 | + |
| 107 | +test_that("among seventy people", { |
| 108 | + expected <- 0.99915958 |
| 109 | + rtol <- ifelse(expected > 0.1 && expected < 0.9, 0.1, 0.01) |
| 110 | + expect_equal( |
| 111 | + estimate_probability_of_shared_birthday(70), |
| 112 | + expected, |
| 113 | + tolerance = rtol |
| 114 | + ) |
| 115 | +}) |
0 commit comments