Skip to content

Commit 32ada20

Browse files
authored
[clock] Add the equals tests (#880)
1 parent 23f6b1d commit 32ada20

4 files changed

Lines changed: 134 additions & 21 deletions

File tree

exercises/practice/clock/.meta/example.sh

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ isnumeric() {
88
[[ $1 == ?([+-])+([[:digit:]]) ]]
99
}
1010

11-
main() {
12-
(( $# == 2 || $# == 4 )) || die "invalid arguments"
13-
11+
hhmm_to_minutes () {
1412
isnumeric "$1" || die "non-numeric argument"
1513
isnumeric "$2" || die "non-numeric argument"
1614

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

2321
local -i minutes=$(( 60 * h + m ))
22+
# wrap around every 24 hours
23+
minutes=$(( minutes % (24 * 60)))
24+
echo "$minutes"
25+
}
26+
27+
main() {
28+
(( $# == 2 || $# == 4 || $# == 5 )) || die "invalid arguments"
29+
30+
local -i minutes minutes2
31+
minutes=$( hhmm_to_minutes "$1" "$2" ) || exit
2432

2533
case $3 in
34+
=)
35+
(( $# == 5 )) || die "invalid arguments"
36+
minutes2=$( hhmm_to_minutes "$4" "$5" ) || exit
37+
(( minutes == minutes2 )) && echo true || echo false
38+
return
39+
;;
2640
[+-])
41+
(( $# == 4 )) || die "invalid arguments"
2742
isnumeric "$4" || die "non-numeric argument"
2843
minutes=$(( minutes $3 $4 ))
2944
;;

exercises/practice/clock/.meta/template.j2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
run bash {{ solution }} {{ case["input"]["hour"] }} {{ case["input"]["minute"] }} + {{ case["input"]["value"] }}
2626
{%- elif case["property"] == "subtract" %}
2727
run bash {{ solution }} {{ case["input"]["hour"] }} {{ case["input"]["minute"] }} - {{ case["input"]["value"] }}
28+
{%- elif case["property"] == "equal" %}
29+
run bash {{ solution }} {{ case["input"]["clock1"]["hour"] }} {{ case["input"]["clock1"]["minute"] }} = {{ case["input"]["clock2"]["hour"] }} {{ case["input"]["clock2"]["minute"] }}
2830
{%- endif %}
2931
assert_success
3032
assert_output "{{ case["expected"] }}"

exercises/practice/clock/.meta/tests.toml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,64 +112,48 @@ description = "subtract more than two days"
112112

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

173158
[96969ca8-875a-48a1-86ae-257a528c44f5]
174159
description = "full clock and zeroed clock"
175-
include = false

exercises/practice/clock/clock.bats

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bats
22
load bats-extra
33

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

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

275+
@test "clocks with same time" {
276+
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
277+
run bash clock.sh 15 37 = 15 37
278+
assert_success
279+
assert_output "true"
280+
}
281+
282+
@test "clocks a minute apart" {
283+
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
284+
run bash clock.sh 15 36 = 15 37
285+
assert_success
286+
assert_output "false"
287+
}
288+
289+
@test "clocks an hour apart" {
290+
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
291+
run bash clock.sh 14 37 = 15 37
292+
assert_success
293+
assert_output "false"
294+
}
295+
296+
@test "clocks with hour overflow" {
297+
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
298+
run bash clock.sh 10 37 = 34 37
299+
assert_success
300+
assert_output "true"
301+
}
302+
303+
@test "clocks with hour overflow by several days" {
304+
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
305+
run bash clock.sh 3 11 = 99 11
306+
assert_success
307+
assert_output "true"
308+
}
309+
310+
@test "clocks with negative hour" {
311+
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
312+
run bash clock.sh 22 40 = -2 40
313+
assert_success
314+
assert_output "true"
315+
}
316+
317+
@test "clocks with negative hour that wraps" {
318+
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
319+
run bash clock.sh 17 3 = -31 3
320+
assert_success
321+
assert_output "true"
322+
}
323+
324+
@test "clocks with negative hour that wraps multiple times" {
325+
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
326+
run bash clock.sh 13 49 = -83 49
327+
assert_success
328+
assert_output "true"
329+
}
330+
331+
@test "clocks with minute overflow" {
332+
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
333+
run bash clock.sh 0 1 = 0 1441
334+
assert_success
335+
assert_output "true"
336+
}
337+
338+
@test "clocks with minute overflow by several days" {
339+
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
340+
run bash clock.sh 2 2 = 2 4322
341+
assert_success
342+
assert_output "true"
343+
}
344+
345+
@test "clocks with negative minute" {
346+
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
347+
run bash clock.sh 2 40 = 3 -20
348+
assert_success
349+
assert_output "true"
350+
}
351+
352+
@test "clocks with negative minute that wraps" {
353+
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
354+
run bash clock.sh 4 10 = 5 -1490
355+
assert_success
356+
assert_output "true"
357+
}
358+
359+
@test "clocks with negative minute that wraps multiple times" {
360+
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
361+
run bash clock.sh 6 15 = 6 -4305
362+
assert_success
363+
assert_output "true"
364+
}
365+
366+
@test "clocks with negative hours and minutes" {
367+
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
368+
run bash clock.sh 7 32 = -12 -268
369+
assert_success
370+
assert_output "true"
371+
}
372+
373+
@test "clocks with negative hours and minutes that wrap" {
374+
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
375+
run bash clock.sh 18 7 = -54 -11513
376+
assert_success
377+
assert_output "true"
378+
}
379+
380+
@test "full clock and zeroed clock" {
381+
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
382+
run bash clock.sh 24 0 = 0 0
383+
assert_success
384+
assert_output "true"
385+
}
386+
275387

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

0 commit comments

Comments
 (0)