Added some extra tests for parse_range

cron
Jef Roosens 2022-04-10 16:58:55 +02:00
parent f92a20fcf8
commit 799fe2e454
Signed by untrusted user: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 31 additions and 5 deletions

View File

@ -17,17 +17,15 @@ fn parse_range(s string, min u32, max u32) ?[]u32 {
if s != '*' { if s != '*' {
exps := s.split('/') exps := s.split('/')
start = math.min(max, math.max(exps[0].u32(), min))
if exps.len > 1 { if exps.len > 1 {
interval = exps[1].u32() interval = exps[1].u32()
} }
// Here, s solely consists of a number, so that's the only value we // Here, s solely consists of a number, so that's the only value we
// should return. // should return.
else { else {
return [exps[0].u32()] return [start]
}
if exps[0] != '*' {
start = math.max(exps[0].u32(), min)
} }
} }

View File

@ -7,3 +7,31 @@ fn test_parse_star_range() ? {
fn test_parse_number() ? { fn test_parse_number() ? {
assert parse_range('4', 0, 5) ? == [u32(4)] assert parse_range('4', 0, 5) ? == [u32(4)]
} }
fn test_parse_number_too_large() ? {
assert parse_range('10', 0, 6) ? == [u32(6)]
}
fn test_parse_number_too_small() ? {
assert parse_range('0', 2, 6) ? == [u32(2)]
}
fn test_parse_step_star() ? {
assert parse_range('*/4', 0, 20) ? == [u32(0), 4, 8, 12, 16, 20]
}
fn test_parse_step_star_too_large() ? {
assert parse_range('*/21', 0, 20) ? == [u32(0)]
}
fn test_parse_step_number() ? {
assert parse_range('5/4', 0, 20) ? == [u32(5), 9, 13, 17]
}
fn test_parse_step_number_too_large() ? {
assert parse_range('10/4', 0, 5) ? == [u32(5)]
}
fn test_parse_step_number_too_small() ? {
assert parse_range('2/4', 5, 10) ? == [u32(5), 9]
}