|
| 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-28 |
| 4 | + |
| 5 | +import unittest |
| 6 | + |
| 7 | +from baffling_birthdays import ( |
| 8 | + estimated_probability_of_shared_birthday, |
| 9 | + random_birthdates, |
| 10 | + shared_birthday, |
| 11 | +) |
| 12 | + |
| 13 | +from calendar import isleap |
| 14 | + |
| 15 | + |
| 16 | +class BafflingBirthdaysTest(unittest.TestCase): |
| 17 | + # shared birthday |
| 18 | + |
| 19 | + def test_one_birthdate(self): |
| 20 | + self.assertIs(shared_birthday(["2000-01-01"]), False) |
| 21 | + |
| 22 | + def test_two_birthdates_with_same_year_month_and_day(self): |
| 23 | + self.assertIs(shared_birthday(["2000-01-01", "2000-01-01"]), True) |
| 24 | + |
| 25 | + def test_two_birthdates_with_same_year_and_month_but_different_day(self): |
| 26 | + self.assertIs(shared_birthday(["2012-05-09", "2012-05-17"]), False) |
| 27 | + |
| 28 | + def test_two_birthdates_with_same_month_and_day_but_different_year(self): |
| 29 | + self.assertIs(shared_birthday(["1999-10-23", "1988-10-23"]), True) |
| 30 | + |
| 31 | + def test_two_birthdates_with_same_year_but_different_month_and_day(self): |
| 32 | + self.assertIs(shared_birthday(["2007-12-19", "2007-04-27"]), False) |
| 33 | + |
| 34 | + def test_two_birthdates_with_different_year_month_and_day(self): |
| 35 | + self.assertIs(shared_birthday(["1997-08-04", "1963-11-23"]), False) |
| 36 | + |
| 37 | + def test_multiple_birthdates_without_shared_birthday(self): |
| 38 | + self.assertIs( |
| 39 | + shared_birthday(["1966-07-29", "1977-02-12", "2001-12-25", "1980-11-10"]), |
| 40 | + False, |
| 41 | + ) |
| 42 | + |
| 43 | + def test_multiple_birthdates_with_one_shared_birthday(self): |
| 44 | + self.assertIs( |
| 45 | + shared_birthday(["1966-07-29", "1977-02-12", "2001-07-29", "1980-11-10"]), |
| 46 | + True, |
| 47 | + ) |
| 48 | + |
| 49 | + def test_multiple_birthdates_with_more_than_one_shared_birthday(self): |
| 50 | + self.assertIs( |
| 51 | + shared_birthday( |
| 52 | + ["1966-07-29", "1977-02-12", "2001-12-25", "1980-07-29", "2019-02-12"] |
| 53 | + ), |
| 54 | + True, |
| 55 | + ) |
| 56 | + |
| 57 | + # random birthdates |
| 58 | + |
| 59 | + def test_random_birthdates_generate_requested_number_of_birthdates(self): |
| 60 | + self.assertTrue( |
| 61 | + all( |
| 62 | + len(random_birthdates(groupsize)) == groupsize |
| 63 | + for groupsize in range(1, 20) |
| 64 | + ) |
| 65 | + ) |
| 66 | + |
| 67 | + def test_random_birthdates_are_not_in_leap_years(self): |
| 68 | + self.assertFalse( |
| 69 | + any([isleap(randyear.year) for randyear in random_birthdates(100)]) |
| 70 | + ) |
| 71 | + |
| 72 | + def test_random_birthdates_appear_random(self): |
| 73 | + birthdates = random_birthdates(500) |
| 74 | + months = set([bdate.month for bdate in birthdates]) |
| 75 | + days = set([bdate.day for bdate in birthdates]) |
| 76 | + self.assertTrue(len(months) >= 10) |
| 77 | + self.assertTrue(len(days) >= 28) |
| 78 | + |
| 79 | + # estimated probability of at least one shared birthday |
| 80 | + |
| 81 | + def test_for_one_person(self): |
| 82 | + |
| 83 | + self.assertAlmostEqual( |
| 84 | + estimated_probability_of_shared_birthday(1), 0.0, delta=0.1 |
| 85 | + ) |
| 86 | + |
| 87 | + def test_among_ten_people(self): |
| 88 | + |
| 89 | + self.assertAlmostEqual( |
| 90 | + estimated_probability_of_shared_birthday(10), 0.11694818, delta=0.5 |
| 91 | + ) |
| 92 | + |
| 93 | + def test_among_twenty_three_people(self): |
| 94 | + |
| 95 | + self.assertAlmostEqual( |
| 96 | + estimated_probability_of_shared_birthday(23), 0.50729723, delta=0.5 |
| 97 | + ) |
| 98 | + |
| 99 | + def test_among_seventy_people(self): |
| 100 | + |
| 101 | + self.assertAlmostEqual( |
| 102 | + estimated_probability_of_shared_birthday(70), 0.99915958, delta=0.1 |
| 103 | + ) |
0 commit comments