Migrated tests to new bitv-based implementation

main
Jef Roosens 2022-04-12 09:00:18 +02:00
parent ab4f64b6b6
commit 04e54b8b10
Signed by untrusted user: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 78 additions and 51 deletions

View File

@ -147,6 +147,18 @@ fn parse_range(s string, min int, max int, mut bitv []bool) ? {
}
}
fn bitv_to_ints(bitv []bool, min int) []int {
mut out := []int{}
for i in 0..bitv.len {
if bitv[i] {
out << min + i
}
}
return out
}
fn parse_part(s string, min int, max int) ?[]int {
mut bitv := []bool{init: false, len: max - min + 1}
@ -154,15 +166,7 @@ fn parse_part(s string, min int, max int) ?[]int {
parse_range(range, min, max, mut bitv) ?
}
mut out := []int{}
for i in 0..max + 1 {
if bitv[i] {
out << min + i
}
}
return out
return bitv_to_ints(bitv, min)
}
// min hour day month day-of-week

View File

@ -0,0 +1,65 @@
module cron
// parse_range_error returns the returned error message. If the result is '',
// that means the function didn't error.
fn parse_range_error(s string, min int, max int) string {
mut bitv := []bool{init: false, len: max - min + 1}
parse_range(s, min, max, mut bitv) or {
return err.msg
}
return ''
}
// =====parse_range=====
fn test_parse_star_range() ? {
mut bitv := []bool{init: false, len: 6}
parse_range('*', 0, 5, mut bitv) ?
assert bitv == [true, true, true, true, true, true]
}
fn test_parse_number() ? {
mut bitv := []bool{init: false, len: 6}
parse_range('4', 0, 5, mut bitv) ?
assert bitv_to_ints(bitv, 0) == [4]
}
fn test_parse_number_too_large() ? {
assert parse_range_error('10', 0, 6) == 'Out of range.'
}
fn test_parse_number_too_small() ? {
assert parse_range_error('0', 2, 6) == 'Out of range.'
}
fn test_parse_step_star() ? {
mut bitv := []bool{init: false, len: 21}
parse_range('*/4', 0, 20, mut bitv) ?
assert bitv_to_ints(bitv, 0) == [0, 4, 8, 12, 16, 20]
}
fn test_parse_step_star_too_large() ? {
assert parse_range_error('*/21', 0, 20) == 'Step too large.'
}
fn test_parse_step_zero() ? {
assert parse_range_error('*/0', 0, 20) == 'Step size zero not allowed.'
}
fn test_parse_step_number() ? {
mut bitv := []bool{init: false, len: 21}
parse_range('5/4', 0, 20, mut bitv) ?
assert bitv_to_ints(bitv, 0) == [5, 9, 13, 17]
}
fn test_parse_step_number_too_large() ? {
assert parse_range_error('10/4', 0, 5) == 'Out of range.'
}
fn test_parse_step_number_too_small() ? {
assert parse_range_error('2/4', 5, 10) == 'Out of range.'
}

View File

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