hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit c863284bf6b6c05d3bf29c2c7d3c385d0fb3ccb7
parent 4cf1a66f739cdaaa68d3825df19c6fabdcc027de
Author: Byron Torres <b@torresjrjr.com>
Date:   Sat, 25 Dec 2021 22:21:17 +0000

reorganise +test code

Signed-off-by: Byron Torres <b@torresjrjr.com>

Diffstat:
Ddatetime/date+test.ha | 149-------------------------------------------------------------------------------
Mdatetime/date.ha | 147+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ddatetime/format+test.ha | 139-------------------------------------------------------------------------------
Mdatetime/format.ha | 137+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 284 insertions(+), 288 deletions(-)

diff --git a/datetime/date+test.ha b/datetime/date+test.ha @@ -1,149 +0,0 @@ -use errors; -use time::chrono; - -@test fn calc_epochal_from_ymd() void = { - const cases = [ - ((-0768, 02, 05), -999999, false), - ((-0001, 12, 31), -719529, false), - (( 0000, 01, 01), -719528, false), - (( 0000, 01, 02), -719527, false), - (( 0000, 12, 31), -719163, false), - (( 0001, 01, 01), -719162, false), - (( 0001, 01, 02), -719161, false), - (( 1965, 03, 23), -1745, false), - (( 1969, 12, 31), -1, false), - (( 1970, 01, 01), 0, false), - (( 1970, 01, 02), 1, false), - (( 1999, 12, 31), 10956, false), - (( 2000, 01, 01), 10957, false), - (( 2000, 01, 02), 10958, false), - (( 2038, 01, 18), 24854, false), - (( 2038, 01, 19), 24855, false), - (( 2038, 01, 20), 24856, false), - (( 2243, 10, 17), 100000, false), - (( 4707, 11, 28), 999999, false), - (( 4707, 11, 29), 1000000, false), - ((29349, 01, 25), 9999999, false), - - (( 1970,-99,-99), 0, true), - (( 1970, -9, -9), 0, true), - (( 1970, -1, -1), 0, true), - (( 1970, 00, 00), 0, true), - (( 1970, 00, 01), 0, true), - (( 1970, 01, 99), 0, true), - (( 1970, 99, 99), 0, true), - ]; - for (let i = 0z; i < len(cases); i += 1) { - const params = cases[i].0; - const expect = cases[i].1; - const should_error = cases[i].2; - const actual = calc_epochal_from_ymd( - params.0, params.1, params.2, - ); - - if (should_error) { - assert(actual is errors::invalid, "invalid date accepted"); - } else { - assert(actual is chrono::epochal, "valid date not accepted"); - assert(actual as chrono::epochal == expect, "epochal miscalculation"); - }; - }; -}; - -@test fn calc_ymd() void = { - const cases = [ - (-999999, (-0768, 02, 05)), - (-719529, (-0001, 12, 31)), - (-719528, ( 0000, 01, 01)), - (-719527, ( 0000, 01, 02)), - (-719163, ( 0000, 12, 31)), - (-719162, ( 0001, 01, 01)), - (-719161, ( 0001, 01, 02)), - ( -1745, ( 1965, 03, 23)), - ( -1, ( 1969, 12, 31)), - ( 0, ( 1970, 01, 01)), - ( 1, ( 1970, 01, 02)), - ( 10956, ( 1999, 12, 31)), - ( 10957, ( 2000, 01, 01)), - ( 10958, ( 2000, 01, 02)), - ( 24854, ( 2038, 01, 18)), - ( 24855, ( 2038, 01, 19)), - ( 24856, ( 2038, 01, 20)), - ( 100000, ( 2243, 10, 17)), - ( 999999, ( 4707, 11, 28)), - (1000000, ( 4707, 11, 29)), - (9999999, (29349, 01, 25)), - ]; - for (let i = 0z; i < len(cases); i += 1) { - const paramt = cases[i].0; - const expect = cases[i].1; - const actual = calc_ymd(paramt); - assert(expect.0 == actual.0, "year mismatch"); - assert(expect.1 == actual.1, "month mismatch"); - assert(expect.2 == actual.2, "day mismatch"); - }; -}; - -@test fn calc_yearday() void = { - const cases = [ - ((-0768, 02, 05), 037), - ((-0001, 12, 31), 365), - (( 0000, 01, 01), 001), - (( 0000, 01, 02), 002), - (( 0000, 12, 31), 366), - (( 0001, 01, 01), 001), - (( 0001, 01, 02), 002), - (( 1965, 03, 23), 082), - (( 1969, 12, 31), 365), - (( 1970, 01, 01), 001), - (( 1970, 01, 02), 002), - (( 1999, 12, 31), 365), - (( 2000, 01, 01), 001), - (( 2000, 01, 02), 002), - (( 2038, 01, 18), 018), - (( 2038, 01, 19), 019), - (( 2038, 01, 20), 020), - (( 2243, 10, 17), 290), - (( 4707, 11, 28), 332), - (( 4707, 11, 29), 333), - ((29349, 01, 25), 025), - ]; - for (let i = 0z; i < len(cases); i += 1) { - const params = cases[i].0; - const expect = cases[i].1; - const actual = calc_yearday(params.0, params.1, params.2); - assert(expect == actual, "yearday miscalculation"); - }; -}; - -@test fn calc_weekday() void = { - const cases = [ - (-999999, 4), // -0768-02-05 - (-719529, 5), // -0001-12-31 - (-719528, 6), // 0000-01-01 - (-719527, 7), // 0000-01-02 - (-719163, 7), // 0000-12-31 - (-719162, 1), // 0001-01-01 - (-719161, 2), // 0001-01-02 - ( -1745, 2), // 1965-03-23 - ( -1, 3), // 1969-12-31 - ( 0, 4), // 1970-01-01 - ( 1, 5), // 1970-01-02 - ( 10956, 5), // 1999-12-31 - ( 10957, 6), // 2000-01-01 - ( 10958, 7), // 2000-01-02 - ( 24854, 1), // 2038-01-18 - ( 24855, 2), // 2038-01-19 - ( 24856, 3), // 2038-01-20 - ( 100000, 2), // 2243-10-17 - ( 999999, 4), // 4707-11-28 - (1000000, 5), // 4707-11-29 - (9999999, 6), // 29349-01-25 - ]; - for (let i = 0z; i < len(cases); i += 1) { - const paramt = cases[i].0; - const expect = cases[i].1; - const actual = calc_weekday(paramt); - assert(expect == actual, "weekday miscalculation"); - }; -}; diff --git a/datetime/date.ha b/datetime/date.ha @@ -175,3 +175,150 @@ fn calc_epochal_from_yd(y: int, yd: int) (chrono::epochal | errors::invalid) = { // TODO return 0; }; + +@test fn calc_epochal_from_ymd() void = { + const cases = [ + ((-0768, 02, 05), -999999, false), + ((-0001, 12, 31), -719529, false), + (( 0000, 01, 01), -719528, false), + (( 0000, 01, 02), -719527, false), + (( 0000, 12, 31), -719163, false), + (( 0001, 01, 01), -719162, false), + (( 0001, 01, 02), -719161, false), + (( 1965, 03, 23), -1745, false), + (( 1969, 12, 31), -1, false), + (( 1970, 01, 01), 0, false), + (( 1970, 01, 02), 1, false), + (( 1999, 12, 31), 10956, false), + (( 2000, 01, 01), 10957, false), + (( 2000, 01, 02), 10958, false), + (( 2038, 01, 18), 24854, false), + (( 2038, 01, 19), 24855, false), + (( 2038, 01, 20), 24856, false), + (( 2243, 10, 17), 100000, false), + (( 4707, 11, 28), 999999, false), + (( 4707, 11, 29), 1000000, false), + ((29349, 01, 25), 9999999, false), + + (( 1970,-99,-99), 0, true), + (( 1970, -9, -9), 0, true), + (( 1970, -1, -1), 0, true), + (( 1970, 00, 00), 0, true), + (( 1970, 00, 01), 0, true), + (( 1970, 01, 99), 0, true), + (( 1970, 99, 99), 0, true), + ]; + for (let i = 0z; i < len(cases); i += 1) { + const params = cases[i].0; + const expect = cases[i].1; + const should_error = cases[i].2; + const actual = calc_epochal_from_ymd( + params.0, params.1, params.2, + ); + + if (should_error) { + assert(actual is errors::invalid, "invalid date accepted"); + } else { + assert(actual is chrono::epochal, "valid date not accepted"); + assert(actual as chrono::epochal == expect, "epochal miscalculation"); + }; + }; +}; + +@test fn calc_ymd() void = { + const cases = [ + (-999999, (-0768, 02, 05)), + (-719529, (-0001, 12, 31)), + (-719528, ( 0000, 01, 01)), + (-719527, ( 0000, 01, 02)), + (-719163, ( 0000, 12, 31)), + (-719162, ( 0001, 01, 01)), + (-719161, ( 0001, 01, 02)), + ( -1745, ( 1965, 03, 23)), + ( -1, ( 1969, 12, 31)), + ( 0, ( 1970, 01, 01)), + ( 1, ( 1970, 01, 02)), + ( 10956, ( 1999, 12, 31)), + ( 10957, ( 2000, 01, 01)), + ( 10958, ( 2000, 01, 02)), + ( 24854, ( 2038, 01, 18)), + ( 24855, ( 2038, 01, 19)), + ( 24856, ( 2038, 01, 20)), + ( 100000, ( 2243, 10, 17)), + ( 999999, ( 4707, 11, 28)), + (1000000, ( 4707, 11, 29)), + (9999999, (29349, 01, 25)), + ]; + for (let i = 0z; i < len(cases); i += 1) { + const paramt = cases[i].0; + const expect = cases[i].1; + const actual = calc_ymd(paramt); + assert(expect.0 == actual.0, "year mismatch"); + assert(expect.1 == actual.1, "month mismatch"); + assert(expect.2 == actual.2, "day mismatch"); + }; +}; + +@test fn calc_yearday() void = { + const cases = [ + ((-0768, 02, 05), 037), + ((-0001, 12, 31), 365), + (( 0000, 01, 01), 001), + (( 0000, 01, 02), 002), + (( 0000, 12, 31), 366), + (( 0001, 01, 01), 001), + (( 0001, 01, 02), 002), + (( 1965, 03, 23), 082), + (( 1969, 12, 31), 365), + (( 1970, 01, 01), 001), + (( 1970, 01, 02), 002), + (( 1999, 12, 31), 365), + (( 2000, 01, 01), 001), + (( 2000, 01, 02), 002), + (( 2038, 01, 18), 018), + (( 2038, 01, 19), 019), + (( 2038, 01, 20), 020), + (( 2243, 10, 17), 290), + (( 4707, 11, 28), 332), + (( 4707, 11, 29), 333), + ((29349, 01, 25), 025), + ]; + for (let i = 0z; i < len(cases); i += 1) { + const params = cases[i].0; + const expect = cases[i].1; + const actual = calc_yearday(params.0, params.1, params.2); + assert(expect == actual, "yearday miscalculation"); + }; +}; + +@test fn calc_weekday() void = { + const cases = [ + (-999999, 4), // -0768-02-05 + (-719529, 5), // -0001-12-31 + (-719528, 6), // 0000-01-01 + (-719527, 7), // 0000-01-02 + (-719163, 7), // 0000-12-31 + (-719162, 1), // 0001-01-01 + (-719161, 2), // 0001-01-02 + ( -1745, 2), // 1965-03-23 + ( -1, 3), // 1969-12-31 + ( 0, 4), // 1970-01-01 + ( 1, 5), // 1970-01-02 + ( 10956, 5), // 1999-12-31 + ( 10957, 6), // 2000-01-01 + ( 10958, 7), // 2000-01-02 + ( 24854, 1), // 2038-01-18 + ( 24855, 2), // 2038-01-19 + ( 24856, 3), // 2038-01-20 + ( 100000, 2), // 2243-10-17 + ( 999999, 4), // 4707-11-28 + (1000000, 5), // 4707-11-29 + (9999999, 6), // 29349-01-25 + ]; + for (let i = 0z; i < len(cases); i += 1) { + const paramt = cases[i].0; + const expect = cases[i].1; + const actual = calc_weekday(paramt); + assert(expect == actual, "weekday miscalculation"); + }; +}; diff --git a/datetime/format+test.ha b/datetime/format+test.ha @@ -1,139 +0,0 @@ -use errors; -use fmt; -use time::chrono; - -@test fn strptime() void = { - let dt = datetime {...}; - - // General tests - strptime("%Y-%m-%d %H:%M:%S.%N", "1994-08-27 11:01:02.123", &dt)!; - assert(dt.date.year as int == 1994 && - dt.date.month as int == 08 && - dt.date.day as int == 27 && - dt.time.hour as int == 11 && - dt.time.min as int == 01 && - dt.time.sec as int == 02 && - dt.time.nsec as int == 123, "invalid parsing results"); - - strptime("%k:%M:%S.%N%n%t%%", " 9:01:02.123\n\t%", &dt)!; - assert(dt.time.hour as int == 9 && - dt.time.min as int == 01 && - dt.time.sec as int == 02 && - dt.time.nsec as int == 123, "invalid parsing results"); - - strptime("%G-%m-%e", "994-8- 9", &dt)!; - assert(dt.date.isoweekyear as int == 994 && - dt.date.month as int == 8 && - dt.date.day as int == 9, "invalid parsing results"); - - // General errors - assert(strptime("%Y-%m-%d", "1a94-08-27", &dt) is errors::invalid, - "invalid datetime string did not throw error"); - - assert(strptime("%Y-%m-%d", "1994-123-27", &dt) is errors::invalid, - "invalid datetime string did not throw error"); - - assert(strptime("%Y-%m-%d", "a994-08-27", &dt) is errors::invalid, - "invalid datetime string did not throw error"); - - // Basic specifiers - strptime("%a", "Tue", &dt)!; - assert(dt.date.weekday as int == 2, "invalid parsing results"); - - strptime("%a %d", "Tue 27", &dt)!; - assert(dt.date.weekday as int == 2 && - dt.date.day as int == 27, "invalid parsing results"); - - strptime("%A", "Tuesday", &dt)!; - assert(dt.date.weekday as int == 2, "invalid parsing results"); - - strptime("%b", "Feb", &dt)!; - assert(dt.date.month as int == 2, "invalid parsing results"); - - strptime("%h", "Feb", &dt)!; - assert(dt.date.month as int == 2, "invalid parsing results"); - - strptime("%B", "February", &dt)!; - assert(dt.date.month as int == 2, "invalid parsing results"); - - strptime("%I", "14", &dt)!; - assert(dt.time.hour as int == 2, "invalid parsing results"); - - strptime("%j", "123", &dt)!; - assert(dt.date.yearday as int == 123, "invalid parsing results"); - - strptime("%l", " 9", &dt)!; - assert(dt.time.hour as int == 9, "invalid parsing results"); - - strptime("%H %p", "6 AM", &dt)!; - assert(dt.time.hour as int == 6, "invalid parsing results"); - - strptime("%H %p", "6 PM", &dt)!; - assert(dt.time.hour as int == 18, "invalid parsing results"); - - assert(strptime("%H %p", "13 PM", &dt) is errors::invalid, - "invalid parsing results"); - - assert(strptime("%H %p", "PM 6", &dt) is errors::invalid, - "invalid parsing results"); - - strptime("%H %P", "6 am", &dt)!; - assert(dt.time.hour as int == 6, "invalid parsing results"); - - strptime("%u", "7", &dt)!; - assert(dt.date.weekday as int == 7, "invalid parsing results"); - - strptime("%U", "2", &dt)!; - assert(dt.date.week as int == 2, "invalid parsing results"); - - strptime("%U", "99", &dt)!; - assert(dt.date.week as int == 53, "invalid parsing results"); - - strptime("%V", "12", &dt)!; - assert(dt.date.isoweek as int == 12, "invalid parsing results"); - - strptime("%w", "0", &dt)!; - assert(dt.date.weekday as int == 7, "invalid parsing results"); - - strptime("%W", "2", &dt)!; - assert(dt.date.week as int == 2, "invalid parsing results"); - - // Expansion specifiers - strptime("%c", "Tue Feb 2 22:12:50 1994", &dt)!; - assert(dt.date.day as int == 2 && - dt.date.month as int == 2 && - dt.date.year as int == 1994 && - dt.date.weekday as int == 2 && - dt.time.hour as int == 22 && - dt.time.min as int == 12 && - dt.time.sec as int == 50, "invalid parsing results"); - - strptime("%D", "08/2/1994", &dt)!; - assert(dt.date.day as int == 2 && - dt.date.month as int == 8 && - dt.date.year as int == 1994, "invalid parsing results"); - - strptime("%F", "1994-08-27", &dt)!; - assert(dt.date.day as int == 27 && - dt.date.month as int == 08 && - dt.date.year as int == 1994, "invalid parsing results"); - - strptime("%r", "04:20:12 PM", &dt)!; - assert(dt.time.hour as int == 16 && - dt.time.min as int == 20 && - dt.time.sec as int == 12, "invalid parsing results"); - - strptime("%r", "04:20:12 AM", &dt)!; - assert(dt.time.hour as int == 04 && - dt.time.min as int == 20 && - dt.time.sec as int == 12, "invalid parsing results"); - - strptime("%R", "12:2", &dt)!; - assert(dt.time.hour as int == 12 && - dt.time.min as int == 2, "invalid parsing results"); - - strptime("%T", "12:2:12", &dt)!; - assert(dt.time.hour as int == 12 && - dt.time.min as int == 2 && - dt.time.sec as int == 12, "invalid parsing results"); -}; diff --git a/datetime/format.ha b/datetime/format.ha @@ -5,6 +5,7 @@ use io; use strconv; use strings; use strio; +use time::chrono; def WEEKDAYS: [_]str = [ "Monday", @@ -602,3 +603,139 @@ fn clamp_int(i: int, min: int, max: int) int = { yield i; }; }; + +@test fn strptime() void = { + let dt = datetime {...}; + + // General tests + strptime("%Y-%m-%d %H:%M:%S.%N", "1994-08-27 11:01:02.123", &dt)!; + assert(dt.date.year as int == 1994 && + dt.date.month as int == 08 && + dt.date.day as int == 27 && + dt.time.hour as int == 11 && + dt.time.min as int == 01 && + dt.time.sec as int == 02 && + dt.time.nsec as int == 123, "invalid parsing results"); + + strptime("%k:%M:%S.%N%n%t%%", " 9:01:02.123\n\t%", &dt)!; + assert(dt.time.hour as int == 9 && + dt.time.min as int == 01 && + dt.time.sec as int == 02 && + dt.time.nsec as int == 123, "invalid parsing results"); + + strptime("%G-%m-%e", "994-8- 9", &dt)!; + assert(dt.date.isoweekyear as int == 994 && + dt.date.month as int == 8 && + dt.date.day as int == 9, "invalid parsing results"); + + // General errors + assert(strptime("%Y-%m-%d", "1a94-08-27", &dt) is errors::invalid, + "invalid datetime string did not throw error"); + + assert(strptime("%Y-%m-%d", "1994-123-27", &dt) is errors::invalid, + "invalid datetime string did not throw error"); + + assert(strptime("%Y-%m-%d", "a994-08-27", &dt) is errors::invalid, + "invalid datetime string did not throw error"); + + // Basic specifiers + strptime("%a", "Tue", &dt)!; + assert(dt.date.weekday as int == 2, "invalid parsing results"); + + strptime("%a %d", "Tue 27", &dt)!; + assert(dt.date.weekday as int == 2 && + dt.date.day as int == 27, "invalid parsing results"); + + strptime("%A", "Tuesday", &dt)!; + assert(dt.date.weekday as int == 2, "invalid parsing results"); + + strptime("%b", "Feb", &dt)!; + assert(dt.date.month as int == 2, "invalid parsing results"); + + strptime("%h", "Feb", &dt)!; + assert(dt.date.month as int == 2, "invalid parsing results"); + + strptime("%B", "February", &dt)!; + assert(dt.date.month as int == 2, "invalid parsing results"); + + strptime("%I", "14", &dt)!; + assert(dt.time.hour as int == 2, "invalid parsing results"); + + strptime("%j", "123", &dt)!; + assert(dt.date.yearday as int == 123, "invalid parsing results"); + + strptime("%l", " 9", &dt)!; + assert(dt.time.hour as int == 9, "invalid parsing results"); + + strptime("%H %p", "6 AM", &dt)!; + assert(dt.time.hour as int == 6, "invalid parsing results"); + + strptime("%H %p", "6 PM", &dt)!; + assert(dt.time.hour as int == 18, "invalid parsing results"); + + assert(strptime("%H %p", "13 PM", &dt) is errors::invalid, + "invalid parsing results"); + + assert(strptime("%H %p", "PM 6", &dt) is errors::invalid, + "invalid parsing results"); + + strptime("%H %P", "6 am", &dt)!; + assert(dt.time.hour as int == 6, "invalid parsing results"); + + strptime("%u", "7", &dt)!; + assert(dt.date.weekday as int == 7, "invalid parsing results"); + + strptime("%U", "2", &dt)!; + assert(dt.date.week as int == 2, "invalid parsing results"); + + strptime("%U", "99", &dt)!; + assert(dt.date.week as int == 53, "invalid parsing results"); + + strptime("%V", "12", &dt)!; + assert(dt.date.isoweek as int == 12, "invalid parsing results"); + + strptime("%w", "0", &dt)!; + assert(dt.date.weekday as int == 7, "invalid parsing results"); + + strptime("%W", "2", &dt)!; + assert(dt.date.week as int == 2, "invalid parsing results"); + + // Expansion specifiers + strptime("%c", "Tue Feb 2 22:12:50 1994", &dt)!; + assert(dt.date.day as int == 2 && + dt.date.month as int == 2 && + dt.date.year as int == 1994 && + dt.date.weekday as int == 2 && + dt.time.hour as int == 22 && + dt.time.min as int == 12 && + dt.time.sec as int == 50, "invalid parsing results"); + + strptime("%D", "08/2/1994", &dt)!; + assert(dt.date.day as int == 2 && + dt.date.month as int == 8 && + dt.date.year as int == 1994, "invalid parsing results"); + + strptime("%F", "1994-08-27", &dt)!; + assert(dt.date.day as int == 27 && + dt.date.month as int == 08 && + dt.date.year as int == 1994, "invalid parsing results"); + + strptime("%r", "04:20:12 PM", &dt)!; + assert(dt.time.hour as int == 16 && + dt.time.min as int == 20 && + dt.time.sec as int == 12, "invalid parsing results"); + + strptime("%r", "04:20:12 AM", &dt)!; + assert(dt.time.hour as int == 04 && + dt.time.min as int == 20 && + dt.time.sec as int == 12, "invalid parsing results"); + + strptime("%R", "12:2", &dt)!; + assert(dt.time.hour as int == 12 && + dt.time.min as int == 2, "invalid parsing results"); + + strptime("%T", "12:2:12", &dt)!; + assert(dt.time.hour as int == 12 && + dt.time.min as int == 2 && + dt.time.sec as int == 12, "invalid parsing results"); +};