Skip to content

Commit

Permalink
Add additional tests to clock exercise for extreme I64 values, fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
ageron authored Oct 19, 2024
1 parent f5c8e69 commit d99ef97
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 19 deletions.
16 changes: 8 additions & 8 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,6 @@
"difficulty": 2,
"status": "deprecated"
},
{
"slug": "clock",
"name": "Clock",
"uuid": "b1ebc201-5d23-4953-88d7-58c82def95b3",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "collatz-conjecture",
"name": "Collatz Conjecture",
Expand Down Expand Up @@ -331,6 +323,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "clock",
"name": "Clock",
"uuid": "b1ebc201-5d23-4953-88d7-58c82def95b3",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "crypto-square",
"name": "Crypto Square",
Expand Down
10 changes: 6 additions & 4 deletions exercises/practice/clock/.meta/Example.roc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ minutesPerDay = 24 * 60

create : { hours ? I64, minutes ? I64 }* -> Clock
create = \{ hours ? 0, minutes ? 0 } ->
totalMinutes = ((hours * 60 + minutes) % minutesPerDay + minutesPerDay) % minutesPerDay
hours24 = (hours % 24 + minutes // 60) % 24
minutes60 = minutes % 60
totalMinutes = ((hours24 * 60 + minutes60) % minutesPerDay + minutesPerDay) % minutesPerDay
hh = totalMinutes // 60 |> Num.toU8
mm = totalMinutes % 60 |> Num.toU8
{ hour: hh, minute: mm }
Expand All @@ -18,10 +20,10 @@ toStr = \{ hour, minute } ->
add : Clock, { hours ? I64, minutes ? I64 }* -> Clock
add = \{ hour, minute }, { hours ? 0, minutes ? 0 } ->
totalHours = Num.toI64 hour + hours
totalMinutes = Num.toI64 minute + minutes
totalHours = Num.toI64 hour + (hours % 24 + minutes // 60)
totalMinutes = Num.toI64 minute + minutes % 60
create { hours: totalHours, minutes: totalMinutes }
subtract : Clock, { hours ? I64, minutes ? I64 }* -> Clock
subtract = \clock, { hours ? 0, minutes ? 0 } ->
clock |> add { hours: -hours, minutes: -minutes }
clock |> add { hours: -(hours % 24 + minutes // 60), minutes: -(minutes % 60) }
62 changes: 62 additions & 0 deletions exercises/practice/clock/.meta/additional_tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"cases": [
{
"description": "Can create a clock with max I64 values",
"property": "create",
"input": {
"hour": 9223372036854775807,
"minute": 9223372036854775807
},
"expected": "01:07"
},
{
"description": "Can create a clock with min I64 values",
"property": "create",
"input": {
"hour": -9223372036854775808,
"minute": -9223372036854775808
},
"expected": "21:52"
},
{
"description": "Can add max I64 values to a clock",
"property": "add",
"input": {
"hour": 23,
"minute": 59,
"value": 9223372036854775807
},
"expected": "18:06"
},
{
"description": "Can add min I64 values to a clock",
"property": "add",
"input": {
"hour": 23,
"minute": 59,
"value": -9223372036854775808
},
"expected": "05:51"
},
{
"description": "Can subtract max I64 values from a clock",
"property": "subtract",
"input": {
"hour": 23,
"minute": 59,
"value": 9223372036854775807
},
"expected": "05:52"
},
{
"description": "Can subtract min I64 values from a clock",
"property": "subtract",
"input": {
"hour": 23,
"minute": 59,
"value": -9223372036854775808
},
"expected": "18:07"
}
]
}
24 changes: 18 additions & 6 deletions exercises/practice/clock/.meta/template.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@

import {{ exercise | to_pascal }} exposing [create, add, subtract, toStr]

{% for supercase in cases %}
##
## {{ supercase["description"] }}
##

{% for case in supercase["cases"] -%}
{% macro test_case(case) %}
# {{ case["description"] }}

{%- if case["property"] == "create" %}
expect
clock = create {{ plugins.to_hours_minutes_record(case["input"]) }}
Expand All @@ -33,6 +29,22 @@ expect
# this test case is not implemented yet. Perhaps you can give it a try?
{%- endif %}

{% endmacro %}

{% for supercase in cases %}
##
## {{ supercase["description"] }}
##

{% for case in supercase["cases"] -%}
{{ test_case(case) }}
{% endfor %}
{% endfor %}

##
## Extreme I64 values should not crash with overflow errors
##

{% for case in additional_cases -%}
{{ test_case(case) }}
{% endfor %}
48 changes: 47 additions & 1 deletion exercises/practice/clock/clock-test.roc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# These tests are auto-generated with test data from:
# https://github.com/exercism/problem-specifications/tree/main/exercises/clock/canonical-data.json
# File last updated on 2024-09-11
# File last updated on 2024-10-13
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
}
Expand Down Expand Up @@ -374,3 +374,49 @@ expect
clock2 = create {}
clock1 == clock2

##
## Extreme I64 values should not crash with overflow errors
##

# Can create a clock with max I64 values
expect
clock = create { hours: 9223372036854775807, minutes: 9223372036854775807 }
result = clock |> toStr
expected = "01:07"
result == expected

# Can create a clock with min I64 values
expect
clock = create { hours: -9223372036854775808, minutes: -9223372036854775808 }
result = clock |> toStr
expected = "21:52"
result == expected

# Can add max I64 values to a clock
expect
clock = create { hours: 23, minutes: 59 }
result = clock |> add { minutes: 9223372036854775807 } |> toStr
expected = "18:06"
result == expected

# Can add min I64 values to a clock
expect
clock = create { hours: 23, minutes: 59 }
result = clock |> add { minutes: -9223372036854775808 } |> toStr
expected = "05:51"
result == expected

# Can subtract max I64 values from a clock
expect
clock = create { hours: 23, minutes: 59 }
result = clock |> subtract { minutes: 9223372036854775807 } |> toStr
expected = "05:52"
result == expected

# Can subtract min I64 values from a clock
expect
clock = create { hours: 23, minutes: 59 }
result = clock |> subtract { minutes: -9223372036854775808 } |> toStr
expected = "18:07"
result == expected

0 comments on commit d99ef97

Please sign in to comment.