@@ -19,39 +19,72 @@ run_tests :: () {
1919}
2020
2121shared_birthday_one_birthdate :: () {
22- assert_equal (false , shared_birthday (.[" 2000-01-01" ]));
22+ birthdates : [..] Calendar_Time ;
23+ array_add (* birthdates , .{year = 2000 , month_starting_at_0 = 0 , day_of_month_starting_at_0 = 0 });
24+ assert_equal (false , shared_birthday (birthdates ));
2325}
2426
2527shared_birthday_two_birthdates_with_same_year_month_and_day :: () {
26- assert_equal (true , shared_birthday (.[" 2000-01-01" , " 2000-01-01" ]));
28+ birthdates : [..] Calendar_Time ;
29+ array_add (* birthdates , .{year = 2000 , month_starting_at_0 = 0 , day_of_month_starting_at_0 = 0 });
30+ array_add (* birthdates , .{year = 2000 , month_starting_at_0 = 0 , day_of_month_starting_at_0 = 0 });
31+ assert_equal (true , shared_birthday (birthdates ));
2732}
2833
2934shared_birthday_two_birthdates_with_same_year_and_month_but_different_day :: () {
30- assert_equal (false , shared_birthday (.[" 2012-05-09" , " 2012-05-17" ]));
35+ birthdates : [..] Calendar_Time ;
36+ array_add (* birthdates , .{year = 2012 , month_starting_at_0 = 4 , day_of_month_starting_at_0 = 8 });
37+ array_add (* birthdates , .{year = 2012 , month_starting_at_0 = 4 , day_of_month_starting_at_0 = 16 });
38+ assert_equal (false , shared_birthday (birthdates ));
3139}
3240
3341shared_birthday_two_birthdates_with_same_month_and_day_but_different_year :: () {
34- assert_equal (true , shared_birthday (.[" 1999-10-23" , " 1988-10-23" ]));
42+ birthdates : [..] Calendar_Time ;
43+ array_add (* birthdates , .{year = 1999 , month_starting_at_0 = 9 , day_of_month_starting_at_0 = 22 });
44+ array_add (* birthdates , .{year = 1988 , month_starting_at_0 = 9 , day_of_month_starting_at_0 = 22 });
45+ assert_equal (true , shared_birthday (birthdates ));
3546}
3647
3748shared_birthday_two_birthdates_with_same_year_but_different_month_and_day :: () {
38- assert_equal (false , shared_birthday (.[" 2007-12-19" , " 2007-04-27" ]));
49+ birthdates : [..] Calendar_Time ;
50+ array_add (* birthdates , .{year = 2007 , month_starting_at_0 = 11 , day_of_month_starting_at_0 = 18 });
51+ array_add (* birthdates , .{year = 2007 , month_starting_at_0 = 3 , day_of_month_starting_at_0 = 26 });
52+ assert_equal (false , shared_birthday (birthdates ));
3953}
4054
4155shared_birthday_two_birthdates_with_different_year_month_and_day :: () {
42- assert_equal (false , shared_birthday (.[" 1997-08-04" , " 1963-11-23" ]));
56+ birthdates : [..] Calendar_Time ;
57+ array_add (* birthdates , .{year = 1997 , month_starting_at_0 = 7 , day_of_month_starting_at_0 = 3 });
58+ array_add (* birthdates , .{year = 1963 , month_starting_at_0 = 10 , day_of_month_starting_at_0 = 22 });
59+ assert_equal (false , shared_birthday (birthdates ));
4360}
4461
4562shared_birthday_multiple_birthdates_without_shared_birthday :: () {
46- assert_equal (false , shared_birthday (.[" 1966-07-29" , " 1977-02-12" , " 2001-12-25" , " 1980-11-10" ]));
63+ birthdates : [..] Calendar_Time ;
64+ array_add (* birthdates , .{year = 1966 , month_starting_at_0 = 6 , day_of_month_starting_at_0 = 28 });
65+ array_add (* birthdates , .{year = 1977 , month_starting_at_0 = 1 , day_of_month_starting_at_0 = 11 });
66+ array_add (* birthdates , .{year = 2001 , month_starting_at_0 = 11 , day_of_month_starting_at_0 = 24 });
67+ array_add (* birthdates , .{year = 1980 , month_starting_at_0 = 10 , day_of_month_starting_at_0 = 9 });
68+ assert_equal (false , shared_birthday (birthdates ));
4769}
4870
4971shared_birthday_multiple_birthdates_with_one_shared_birthday :: () {
50- assert_equal (true , shared_birthday (.[" 1966-07-29" , " 1977-02-12" , " 2001-07-29" , " 1980-11-10" ]));
72+ birthdates : [..] Calendar_Time ;
73+ array_add (* birthdates , .{year = 1966 , month_starting_at_0 = 6 , day_of_month_starting_at_0 = 28 });
74+ array_add (* birthdates , .{year = 1977 , month_starting_at_0 = 1 , day_of_month_starting_at_0 = 11 });
75+ array_add (* birthdates , .{year = 2001 , month_starting_at_0 = 6 , day_of_month_starting_at_0 = 28 });
76+ array_add (* birthdates , .{year = 1980 , month_starting_at_0 = 10 , day_of_month_starting_at_0 = 9 });
77+ assert_equal (true , shared_birthday (birthdates ));
5178}
5279
5380shared_birthday_multiple_birthdates_with_more_than_one_shared_birthday :: () {
54- assert_equal (true , shared_birthday (.[" 1966-07-29" , " 1977-02-12" , " 2001-12-25" , " 1980-07-29" , " 2019-02-12" ]));
81+ birthdates : [..] Calendar_Time ;
82+ array_add (* birthdates , .{year = 1966 , month_starting_at_0 = 6 , day_of_month_starting_at_0 = 28 });
83+ array_add (* birthdates , .{year = 1977 , month_starting_at_0 = 1 , day_of_month_starting_at_0 = 11 });
84+ array_add (* birthdates , .{year = 2001 , month_starting_at_0 = 11 , day_of_month_starting_at_0 = 24 });
85+ array_add (* birthdates , .{year = 1980 , month_starting_at_0 = 6 , day_of_month_starting_at_0 = 28 });
86+ array_add (* birthdates , .{year = 2019 , month_starting_at_0 = 1 , day_of_month_starting_at_0 = 11 });
87+ assert_equal (true , shared_birthday (birthdates ));
5588}
5689
5790random_birthdates_generate_requested_number_of_birthdates :: () {
@@ -61,46 +94,59 @@ random_birthdates_generate_requested_number_of_birthdates :: () {
6194}
6295
6396random_birthdates_years_are_not_leap_years :: () {
64- for 1 ..10 {
65- assert_equal (it , random_birthdates (it ).count );
97+ is_leap_year :: (year : int ) -> bool {
98+ return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0 );
99+ }
100+
101+ for birthdate : random_birthdates (50 ) {
102+ assert_equal (false , is_leap_year (birthdate .year ));
66103 }
67104}
68105
69106random_birthdates_months_are_random :: () {
70- for 1 ..10 {
71- assert_equal (it , random_birthdates (it ).count );
107+ generated_unique_months : [..]s8 ;
108+ for birthdate : random_birthdates (100 ) {
109+ array_add_if_unique (* generated_unique_months , birthdate .month_starting_at_0 );
72110 }
111+
112+ assert_equal (12 , generated_unique_months .count );
73113}
74114
75115random_birthdates_days_are_random :: () {
76- for 1 ..10 {
77- assert_equal (it , random_birthdates (it ).count );
116+ generated_unique_days : [..]s8 ;
117+ for birthdate : random_birthdates (300 ) {
118+ array_add_if_unique (* generated_unique_days , birthdate .day_of_month_starting_at_0 );
78119 }
120+
121+ assert_equal (31 , generated_unique_days .count );
79122}
80123
81124estimated_probability_of_at_least_one_shared_birthday_for_one_person :: () {
82- assert_equal (0 .0 , estimated_probability_of_shared_birthday (1 ));
125+ estimated_probability := estimated_probability_of_shared_birthday (1 );
126+ expected_probability := 0 .0 ;
127+ assert_equal (true , estimated_probability > expected_probability - 1 .0 );
128+ assert_equal (true , estimated_probability < expected_probability + 1 .0 );
83129}
84130
85131estimated_probability_of_at_least_one_shared_birthday_among_ten_people :: () {
86- assert_equal (11 .694818 , estimated_probability_of_shared_birthday (10 ));
132+ estimated_probability := estimated_probability_of_shared_birthday (10 );
133+ expected_probability := 11 .694818 ;
134+ assert_equal (true , estimated_probability > expected_probability - 1 .0 );
135+ assert_equal (true , estimated_probability < expected_probability + 1 .0 );
87136}
88137
89138estimated_probability_of_at_least_one_shared_birthday_among_twentythree_people :: () {
90- assert_equal (50 .729723 , estimated_probability_of_shared_birthday (23 ));
139+ estimated_probability := estimated_probability_of_shared_birthday (23 );
140+ expected_probability := 50 .729723 ;
141+ assert_equal (true , estimated_probability > expected_probability - 1 .0 );
142+ assert_equal (true , estimated_probability < expected_probability + 1 .0 );
91143}
92144
93145estimated_probability_of_at_least_one_shared_birthday_among_seventy_people :: () {
94- assert_equal (99 .915958 , estimated_probability_of_shared_birthday (70 ));
95- }
96-
97- to_date :: (str : string ) -> Calendar_Time {
98- using ct : Calendar_Time ;
99- parts = split (str , " -" );
100- year = to_integer (parts [0 ]);
101- month_starting_at_0 = to_integer (parts [1 ]) - 1 ;
102- day_of_month_starting_at_0 = to_integer (parts [2 ]) - 1 ;
103- return ct ;
146+ estimated_probability := estimated_probability_of_shared_birthday (70 );
147+ expected_probability := 99 .915958 ;
148+ assert_equal (true , estimated_probability > expected_probability - 1 .0 );
149+ assert_equal (true , estimated_probability < expected_probability + 1 .0 );
104150}
105151
106152#run {
0 commit comments