|
| 1 | +BafflingBirthdays = require 'baffling_birthdays' |
| 2 | + |
| 3 | +describe 'baffling-birthdays', -> |
| 4 | + -- ---------------------------------------- |
| 5 | + -- https://lunarmodules.github.io/Penlight/libraries/pl.tablex.html |
| 6 | + tablex = require 'pl.tablex' |
| 7 | + |
| 8 | + -- |
| 9 | + epsilon = 0.5 |
| 10 | + is_close_to = (state, arguments) -> |
| 11 | + {a, b} = arguments |
| 12 | + math.abs(a - b) <= epsilon |
| 13 | + |
| 14 | + say = require 'say' |
| 15 | + say\set 'assertion.approx_equal.positive', "Expected %s and %s to be within #{epsilon}" |
| 16 | + say\set 'assertion.approx_equal.negative', "Expected %s and %s not to be within #{epsilon}" |
| 17 | + assert\register 'assertion', 'approx_equal', is_close_to, 'assertion.approx_equal.positive', 'assertion.approx_equal.negative' |
| 18 | + |
| 19 | + -- |
| 20 | + isLeapYear = (year) -> year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) |
| 21 | + -- ---------------------------------------- |
| 22 | + |
| 23 | + describe 'shared birthday', -> |
| 24 | + it 'one birthdate', -> |
| 25 | + birthdates = {'2000-01-01'} |
| 26 | + assert.is.false BafflingBirthdays.sharedBirthday birthdates |
| 27 | + |
| 28 | + pending 'two birthdates with same year, month, and day', -> |
| 29 | + birthdates = {'2000-01-01', '2000-01-01'} |
| 30 | + assert.is.true BafflingBirthdays.sharedBirthday birthdates |
| 31 | + |
| 32 | + pending 'two birthdates with same year and month, but different day', -> |
| 33 | + birthdates = {'2012-05-09', '2012-05-17'} |
| 34 | + assert.is.false BafflingBirthdays.sharedBirthday birthdates |
| 35 | + |
| 36 | + pending 'two birthdates with same month and day, but different year', -> |
| 37 | + birthdates = {'1999-10-23', '1988-10-23'} |
| 38 | + assert.is.true BafflingBirthdays.sharedBirthday birthdates |
| 39 | + |
| 40 | + pending 'two birthdates with same year, but different month and day', -> |
| 41 | + birthdates = {'2007-12-19', '2007-04-27'} |
| 42 | + assert.is.false BafflingBirthdays.sharedBirthday birthdates |
| 43 | + |
| 44 | + pending 'two birthdates with different year, month, and day', -> |
| 45 | + birthdates = {'1997-08-04', '1963-11-23'} |
| 46 | + assert.is.false BafflingBirthdays.sharedBirthday birthdates |
| 47 | + |
| 48 | + pending 'multiple birthdates without shared birthday', -> |
| 49 | + birthdates = {'1966-07-29', '1977-02-12', '2001-12-25', '1980-11-10'} |
| 50 | + assert.is.false BafflingBirthdays.sharedBirthday birthdates |
| 51 | + |
| 52 | + pending 'multiple birthdates with one shared birthday', -> |
| 53 | + birthdates = {'1966-07-29', '1977-02-12', '2001-07-29', '1980-11-10'} |
| 54 | + assert.is.true BafflingBirthdays.sharedBirthday birthdates |
| 55 | + |
| 56 | + pending 'multiple birthdates with more than one shared birthday', -> |
| 57 | + birthdates = {'1966-07-29', '1977-02-12', '2001-12-25', '1980-07-29', '2019-02-12'} |
| 58 | + assert.is.true BafflingBirthdays.sharedBirthday birthdates |
| 59 | + |
| 60 | + describe 'random birthdates', -> |
| 61 | + pending 'generate requested number of birthdates', -> |
| 62 | + result = true |
| 63 | + for n = 1, 100 |
| 64 | + birthdates = BafflingBirthdays.randomBirthdates n |
| 65 | + result = result and (#birthdates == n) |
| 66 | + assert.is.true result |
| 67 | + |
| 68 | + pending 'years are not leap years', -> |
| 69 | + result = true |
| 70 | + for birthdate in *BafflingBirthdays.randomBirthdates(100) |
| 71 | + year = birthdate\sub 1, 4 |
| 72 | + result = result and not isLeapYear tonumber(year) |
| 73 | + assert.is.true result |
| 74 | + |
| 75 | + pending 'months are random', -> |
| 76 | + months = tablex.new 12, 0 |
| 77 | + for birthdate in *BafflingBirthdays.randomBirthdates(100) |
| 78 | + month = tonumber birthdate\sub 6, 7 |
| 79 | + assert.is.true 1 <= month and month <= 12 |
| 80 | + months[month] += 1 |
| 81 | + notSeen = [month for month,count in pairs months when count == 0] |
| 82 | + assert.is.equal 0, #notSeen |
| 83 | + |
| 84 | + pending 'days are random', -> |
| 85 | + days = tablex.new 31, 0 |
| 86 | + for birthdate in *BafflingBirthdays.randomBirthdates(300) |
| 87 | + day = tonumber birthdate\sub 9, 10 |
| 88 | + assert.is.true 1 <= day and day <= 31 |
| 89 | + days[day] += 1 |
| 90 | + notSeen = [day for day,count in pairs days when count == 0] |
| 91 | + assert.is.equal 0, #notSeen |
| 92 | + |
| 93 | + describe 'estimated probability of at least one shared birthday', -> |
| 94 | + pending 'for one person', -> |
| 95 | + result = BafflingBirthdays.estimatedProbabilityOfSharedBirthday 1 |
| 96 | + assert.is.approx_equal 0.0, result |
| 97 | + |
| 98 | + pending 'among ten people', -> |
| 99 | + result = BafflingBirthdays.estimatedProbabilityOfSharedBirthday 10 |
| 100 | + assert.is.approx_equal 11.694818, result |
| 101 | + |
| 102 | + pending 'among twenty-three people', -> |
| 103 | + result = BafflingBirthdays.estimatedProbabilityOfSharedBirthday 23 |
| 104 | + assert.is.approx_equal 50.729723, result |
| 105 | + |
| 106 | + pending 'among seventy people', -> |
| 107 | + result = BafflingBirthdays.estimatedProbabilityOfSharedBirthday 70 |
| 108 | + assert.is.approx_equal 99.915958, result |
0 commit comments