|
| 1 | +to_test_case :: (canonical_case: CanonicalCase) -> Test { |
| 2 | + using test: Test; |
| 3 | + name = to_test_name(canonical_case.description_path); |
| 4 | + skip = canonical_case.index > 0; |
| 5 | + |
| 6 | + if canonical_case.property == { |
| 7 | + case "sharedBirthday"; |
| 8 | + _, birthdates := get_property(canonical_case.input, "birthdates"); |
| 9 | + |
| 10 | + lines: [..]string; |
| 11 | + array_add(*lines, "birthdates : [..] Calendar_Time;"); |
| 12 | + |
| 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; |
| 22 | + case "randomBirthdates"; |
| 23 | + lines: [..]string; |
| 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 | + } |
| 54 | + |
| 55 | + multiline_body = lines; |
| 56 | + case "estimatedProbabilityOfSharedBirthday"; |
| 57 | + _, group_size := get_property(canonical_case.input, "groupSize"); |
| 58 | + expected := ifx canonical_case.expected.number then to_code(canonical_case.expected.number) else "0.0"; |
| 59 | + |
| 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; |
| 67 | + } |
| 68 | + |
| 69 | + return test; |
| 70 | +} |
| 71 | + |
| 72 | +#run run_generator("baffling-birthdays", to_test_case); |
| 73 | + |
| 74 | +#import "Basic"; |
| 75 | +#import "String"; |
| 76 | +#import,dir "../../../shared/generator"; |
0 commit comments