Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions exercises/practice/clock/.meta/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ isnumeric() {
[[ $1 == ?([+-])+([[:digit:]]) ]]
}

main() {
(( $# == 2 || $# == 4 )) || die "invalid arguments"

hhmm_to_minutes () {
isnumeric "$1" || die "non-numeric argument"
isnumeric "$2" || die "non-numeric argument"

Expand All @@ -21,9 +19,26 @@ main() {
while ((h < 0)); do ((h += 24)); done

local -i minutes=$(( 60 * h + m ))
# wrap around every 24 hours
minutes=$(( minutes % (24 * 60)))
echo "$minutes"
}

main() {
(( $# == 2 || $# == 4 || $# == 5 )) || die "invalid arguments"

local -i minutes minutes2
minutes=$( hhmm_to_minutes "$1" "$2" ) || exit

case $3 in
=)
(( $# == 5 )) || die "invalid arguments"
minutes2=$( hhmm_to_minutes "$4" "$5" ) || exit
(( minutes == minutes2 )) && echo true || echo false
return
;;
[+-])
(( $# == 4 )) || die "invalid arguments"
isnumeric "$4" || die "non-numeric argument"
minutes=$(( minutes $3 $4 ))
;;
Expand Down
2 changes: 2 additions & 0 deletions exercises/practice/clock/.meta/template.j2
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
run bash {{ solution }} {{ case["input"]["hour"] }} {{ case["input"]["minute"] }} + {{ case["input"]["value"] }}
{%- elif case["property"] == "subtract" %}
run bash {{ solution }} {{ case["input"]["hour"] }} {{ case["input"]["minute"] }} - {{ case["input"]["value"] }}
{%- elif case["property"] == "equal" %}
run bash {{ solution }} {{ case["input"]["clock1"]["hour"] }} {{ case["input"]["clock1"]["minute"] }} = {{ case["input"]["clock2"]["hour"] }} {{ case["input"]["clock2"]["minute"] }}
{%- endif %}
assert_success
assert_output "{{ case["expected"] }}"
Expand Down
16 changes: 0 additions & 16 deletions exercises/practice/clock/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,64 +112,48 @@ description = "subtract more than two days"

[f2fdad51-499f-4c9b-a791-b28c9282e311]
description = "clocks with same time"
include = false

[5d409d4b-f862-4960-901e-ec430160b768]
description = "clocks a minute apart"
include = false

[a6045fcf-2b52-4a47-8bb2-ef10a064cba5]
description = "clocks an hour apart"
include = false

[66b12758-0be5-448b-a13c-6a44bce83527]
description = "clocks with hour overflow"
include = false

[2b19960c-212e-4a71-9aac-c581592f8111]
description = "clocks with hour overflow by several days"
include = false

[6f8c6541-afac-4a92-b0c2-b10d4e50269f]
description = "clocks with negative hour"
include = false

[bb9d5a68-e324-4bf5-a75e-0e9b1f97a90d]
description = "clocks with negative hour that wraps"
include = false

[56c0326d-565b-4d19-a26f-63b3205778b7]
description = "clocks with negative hour that wraps multiple times"
include = false

[c90b9de8-ddff-4ffe-9858-da44a40fdbc2]
description = "clocks with minute overflow"
include = false

[533a3dc5-59a7-491b-b728-a7a34fe325de]
description = "clocks with minute overflow by several days"
include = false

[fff49e15-f7b7-4692-a204-0f6052d62636]
description = "clocks with negative minute"
include = false

[605c65bb-21bd-43eb-8f04-878edf508366]
description = "clocks with negative minute that wraps"
include = false

[b87e64ed-212a-4335-91fd-56da8421d077]
description = "clocks with negative minute that wraps multiple times"
include = false

[822fbf26-1f3b-4b13-b9bf-c914816b53dd]
description = "clocks with negative hours and minutes"
include = false

[e787bccd-cf58-4a1d-841c-ff80eaaccfaa]
description = "clocks with negative hours and minutes that wrap"
include = false

[96969ca8-875a-48a1-86ae-257a528c44f5]
description = "full clock and zeroed clock"
include = false
116 changes: 114 additions & 2 deletions exercises/practice/clock/clock.bats
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bats
load bats-extra

# generated on 2026-06-29T16:53:24+00:00
# generated on 2026-06-30T03:00:58+00:00

# * The canonical "Compare two clocks for equality" tests
# have not been included: for bash they will simply be
Expand Down Expand Up @@ -272,6 +272,118 @@ load bats-extra
assert_output "00:20"
}

@test "clocks with same time" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash clock.sh 15 37 = 15 37
assert_success
assert_output "true"
}

@test "clocks a minute apart" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash clock.sh 15 36 = 15 37
assert_success
assert_output "false"
}

@test "clocks an hour apart" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash clock.sh 14 37 = 15 37
assert_success
assert_output "false"
}

@test "clocks with hour overflow" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash clock.sh 10 37 = 34 37
assert_success
assert_output "true"
}

@test "clocks with hour overflow by several days" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash clock.sh 3 11 = 99 11
assert_success
assert_output "true"
}

@test "clocks with negative hour" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash clock.sh 22 40 = -2 40
assert_success
assert_output "true"
}

@test "clocks with negative hour that wraps" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash clock.sh 17 3 = -31 3
assert_success
assert_output "true"
}

@test "clocks with negative hour that wraps multiple times" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash clock.sh 13 49 = -83 49
assert_success
assert_output "true"
}

@test "clocks with minute overflow" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash clock.sh 0 1 = 0 1441
assert_success
assert_output "true"
}

@test "clocks with minute overflow by several days" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash clock.sh 2 2 = 2 4322
assert_success
assert_output "true"
}

@test "clocks with negative minute" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash clock.sh 2 40 = 3 -20
assert_success
assert_output "true"
}

@test "clocks with negative minute that wraps" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash clock.sh 4 10 = 5 -1490
assert_success
assert_output "true"
}

@test "clocks with negative minute that wraps multiple times" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash clock.sh 6 15 = 6 -4305
assert_success
assert_output "true"
}

@test "clocks with negative hours and minutes" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash clock.sh 7 32 = -12 -268
assert_success
assert_output "true"
}

@test "clocks with negative hours and minutes that wrap" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash clock.sh 18 7 = -54 -11513
assert_success
assert_output "true"
}

@test "full clock and zeroed clock" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash clock.sh 24 0 = 0 0
assert_success
assert_output "true"
}


# Error conditions: We expect non-zero exit status, and
# some error message to be output (your choice for wording).
Expand Down Expand Up @@ -316,4 +428,4 @@ load bats-extra
run bash clock.sh 1 2 - 3delta
assert_failure
assert_output # there is _some_ output
}
}