Skip to content

Commit 4b9dc27

Browse files
Fix tests
1 parent 8133242 commit 4b9dc27

5 files changed

Lines changed: 169 additions & 56 deletions

File tree

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,50 @@
11
shared_birthday :: (birthdates: [] Calendar_Time) -> bool {
2+
unique_birthdays: [..] int;
3+
4+
for birthdate: birthdates {
5+
birthday_key := cast(s16)birthdate.month_starting_at_0 * 31 + birthdate.day_of_month_starting_at_0;
6+
is_unique, _ := array_add_if_unique(*unique_birthdays, birthday_key);
7+
if !is_unique return true;
8+
}
9+
210
return false;
311
}
412

513
random_birthdates :: (count: int) -> [] Calendar_Time {
6-
return .[];
14+
birthdates : [..] Calendar_Time;
15+
array_reserve(*birthdates, count);
16+
for 1..count array_add(*birthdates, random_birthdate());
17+
return birthdates;
18+
}
19+
20+
random_birthdate :: () -> Calendar_Time {
21+
is_leap_year :: (year: int) -> bool {
22+
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
23+
}
24+
25+
DAYS_IN_MONTH := u64.[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
26+
27+
year := cast(s32) (random_get() % 100 + 1920);
28+
month := cast(s8) (random_get() % 12);
29+
day := cast(s8) (random_get() % DAYS_IN_MONTH[month]);
30+
31+
if is_leap_year(year) year -= 1;
32+
33+
return .{year = year, month_starting_at_0 = month, day_of_month_starting_at_0 = day};
734
}
835

936
estimated_probability_of_shared_birthday :: (group_size: int) -> float {
10-
return 0.0;
37+
ITERATION_COUNT :: 1000;
38+
39+
matching_birthday_iterations := 0.0;
40+
41+
for 1..ITERATION_COUNT {
42+
birthdates := random_birthdates(group_size);
43+
if shared_birthday(birthdates) matching_birthday_iterations += 1;
44+
}
45+
46+
return (matching_birthday_iterations / ITERATION_COUNT) * 100;
1147
}
48+
49+
#import "Basic";
50+
#import "Random";

exercises/practice/baffling-birthdays/.meta/generator.jai

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,71 @@ to_test_case :: (canonical_case: CanonicalCase) -> Test {
66
if canonical_case.property == {
77
case "sharedBirthday";
88
_, birthdates := get_property(canonical_case.input, "birthdates");
9+
10+
lines: [..]string;
11+
array_add(*lines, "birthdates : [..] Calendar_Time;");
912

10-
body = tprint("assert_equal(%, %(%));", to_code(canonical_case.expected), to_test_name(canonical_case.property), to_code(birthdates));
13+
for birthdate: birthdates.array {
14+
parts := split(birthdate.str, "-");
15+
year := to_integer(parts[0]);
16+
month := to_integer(parts[1]);
17+
day := to_integer(parts[2]);
18+
array_add(*lines, tprint("array_add(*birthdates, .{year = %, month_starting_at_0 = %, day_of_month_starting_at_0 = %});", to_code(year), to_code(month - 1), to_code(day - 1)));
19+
}
20+
array_add(*lines, tprint("assert_equal(%, shared_birthday(birthdates));", to_code(canonical_case.expected.boolean)));
21+
multiline_body = lines;
1122
case "randomBirthdates";
1223
lines: [..]string;
13-
array_add(*lines, "for 1..10 {");
14-
array_add(*lines, tprint(" assert_equal(it, %(it).count);", to_test_name(canonical_case.property)));
15-
array_add(*lines, "}");
24+
25+
if canonical_case.expected.type == .STRING && canonical_case.expected.str == "length == groupsize" {
26+
array_add(*lines, "for 1..10 {");
27+
array_add(*lines, tprint(" assert_equal(it, random_birthdates(it).count);"));
28+
array_add(*lines, "}");
29+
} else if get_property(canonical_case.expected, "years", "leapYear") {
30+
array_add(*lines, "is_leap_year :: (year: int) -> bool {");
31+
array_add(*lines, " return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);");
32+
array_add(*lines, "}");
33+
array_add(*lines, "");
34+
array_add(*lines, "for birthdate: random_birthdates(50) {");
35+
array_add(*lines, tprint(" assert_equal(false, is_leap_year(birthdate.year));"));
36+
array_add(*lines, "}");
37+
} else if get_property(canonical_case.expected, "months", "random") {
38+
array_add(*lines, "generated_unique_months: [..]s8;");
39+
array_add(*lines, "for birthdate: random_birthdates(100) {");
40+
array_add(*lines, tprint(" array_add_if_unique(*generated_unique_months, birthdate.month_starting_at_0);"));
41+
array_add(*lines, "}");
42+
array_add(*lines, "");
43+
array_add(*lines, tprint("assert_equal(12, generated_unique_months.count);"));
44+
} else if get_property(canonical_case.expected, "days", "random") {
45+
array_add(*lines, "generated_unique_days: [..]s8;");
46+
array_add(*lines, "for birthdate: random_birthdates(300) {");
47+
array_add(*lines, tprint(" array_add_if_unique(*generated_unique_days, birthdate.day_of_month_starting_at_0);"));
48+
array_add(*lines, "}");
49+
array_add(*lines, "");
50+
array_add(*lines, tprint("assert_equal(31, generated_unique_days.count);"));
51+
} else {
52+
assert(false, "Unexpected randomBirthdates test case");
53+
}
1654

1755
multiline_body = lines;
1856
case "estimatedProbabilityOfSharedBirthday";
1957
_, group_size := get_property(canonical_case.input, "groupSize");
2058
expected := ifx canonical_case.expected.number then to_code(canonical_case.expected.number) else "0.0";
2159

22-
body = tprint("assert_equal(%, %(%));", expected, to_test_name(canonical_case.property), to_code(group_size));
60+
lines: [..]string;
61+
array_add(*lines, tprint("estimated_probability := estimated_probability_of_shared_birthday(%);", to_code(group_size)));
62+
array_add(*lines, tprint("expected_probability := %;", expected));
63+
array_add(*lines, tprint("assert_equal(true, estimated_probability > expected_probability - 1.0);"));
64+
array_add(*lines, tprint("assert_equal(true, estimated_probability < expected_probability + 1.0);"));
65+
66+
multiline_body = lines;
2367
}
2468

2569
return test;
2670
}
2771

28-
AFTER_TESTS :: #string SUFFIX
29-
to_date :: (str: string) -> Calendar_Time {
30-
using ct: Calendar_Time;
31-
parts = split(str, "-");
32-
year = to_integer(parts[0]);
33-
month_starting_at_0 = to_integer(parts[1]) - 1;
34-
day_of_month_starting_at_0 = to_integer(parts[2]) - 1;
35-
return ct;
36-
}
37-
SUFFIX;
38-
39-
#run run_generator("baffling-birthdays", to_test_case, AFTER_TESTS);
72+
#run run_generator("baffling-birthdays", to_test_case);
4073

4174
#import "Basic";
75+
#import "String";
4276
#import,dir "../../../shared/generator";

exercises/practice/baffling-birthdays/tests.jai

Lines changed: 74 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,72 @@ run_tests :: () {
1919
}
2020

2121
shared_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

2527
shared_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

2934
shared_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

3341
shared_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

3748
shared_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

4155
shared_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

4562
shared_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

4971
shared_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

5380
shared_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

5790
random_birthdates_generate_requested_number_of_birthdates :: () {
@@ -61,46 +94,59 @@ random_birthdates_generate_requested_number_of_birthdates :: () {
6194
}
6295

6396
random_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

69106
random_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

75115
random_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

81124
estimated_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

85131
estimated_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

89138
estimated_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

93145
estimated_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 {

exercises/shared/generator/json.jai

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,6 @@ parse_array :: (str: string) -> [] JSON_Value, string {
148148

149149
parse_object :: (str: string) -> JSON_Object, string {
150150
result: JSON_Object;
151-
init(*result);
152-
153151
remainder := trim_left(advance(str));
154152

155153
while remainder {

exercises/shared/generator/tests.jai

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ to_test_name :: (description: string) -> string {
2020
return to_snake_case(description);
2121
}
2222

23-
build_test_file_contents :: (tests: [] Test, after_tests: string) -> string {
23+
build_test_file_contents :: (tests: [] Test) -> string {
2424
builder: String_Builder;
2525

2626
print_to_builder(*builder, "run_tests :: () {\n");
@@ -47,10 +47,6 @@ build_test_file_contents :: (tests: [] Test, after_tests: string) -> string {
4747
print_to_builder(*builder, "}\n");
4848
}
4949

50-
if after_tests {
51-
print_to_builder(*builder, "\n%", after_tests);
52-
}
53-
5450
print_to_builder(*builder, "\n");
5551
print_to_builder(*builder, "#run {\n");
5652
print_to_builder(*builder, " before_running_tests();\n");
@@ -64,7 +60,7 @@ build_test_file_contents :: (tests: [] Test, after_tests: string) -> string {
6460
return builder_to_string(*builder);
6561
}
6662

67-
run_generator :: (exercise: string, to_test_case: (canonical_case: CanonicalCase) -> Test, after_tests := "") {
63+
run_generator :: (exercise: string, to_test_case: (canonical_case: CanonicalCase) -> Test) {
6864
canonical := download_canonical_data(exercise);
6965

7066
test_cases: [..] Test;
@@ -73,7 +69,7 @@ run_generator :: (exercise: string, to_test_case: (canonical_case: CanonicalCase
7369
array_add(*test_cases, test_case);
7470
}
7571

76-
contents := build_test_file_contents(test_cases, after_tests);
72+
contents := build_test_file_contents(test_cases);
7773
write_entire_file("../tests.jai", contents);
7874

7975
// Don't create an executable as the generator can run at compile-time

0 commit comments

Comments
 (0)