Skip to content

Commit

Permalink
tests to compare MIT codes to pack/unpack codes
Browse files Browse the repository at this point in the history
  • Loading branch information
bbejanov committed Sep 7, 2023
1 parent a4d948a commit bc4c69a
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 70 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Expand All @@ -19,7 +20,7 @@ TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"

[compat]
CEnum = "0.4"
DataEcon_jll = "0.2.2"
DataEcon_jll = "0.2.3"
MacroTools = "0.5"
OrderedCollections = "1.4"
RecipesBase = "1.0"
Expand Down
157 changes: 89 additions & 68 deletions src/dataecon/C.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@ end
DE_BAD_CLASS = -998
DE_BAD_TYPE = -997
DE_BAD_NAME = -996
DE_SHORT_BUF = -995
DE_OBJ_DNE = -994
DE_AXIS_DNE = -993
DE_ARG = -992
DE_NO_OBJ = -991
DE_EXISTS = -990
DE_BAD_OBJ = -989
DE_NULL = -988
DE_DEL_ROOT = -987
DE_MIS_ATTR = -986
DE_INTERNAL = -985
DE_BAD_FREQ = -995
DE_SHORT_BUF = -994
DE_OBJ_DNE = -993
DE_AXIS_DNE = -992
DE_ARG = -991
DE_NO_OBJ = -990
DE_EXISTS = -989
DE_BAD_OBJ = -988
DE_NULL = -987
DE_DEL_ROOT = -986
DE_MIS_ATTR = -985
DE_INEXACT = -984
DE_RANGE = -983
DE_INTERNAL = -982
end

const de_file = Ptr{Cvoid}
Expand Down Expand Up @@ -81,63 +84,6 @@ end
type_any = -1
end

@cenum frequency_t::UInt32 begin
freq_none = 0
freq_unit = 1
freq_daily = 4
freq_bdaily = 5
freq_monthly = 8
freq_weekly = 16
freq_weekly_sun0 = 16
freq_weekly_mon = 17
freq_weekly_tue = 18
freq_weekly_wed = 19
freq_weekly_thu = 20
freq_weekly_fri = 21
freq_weekly_sat = 22
freq_weekly_sun7 = 23
freq_weekly_sun = 23
freq_quarterly = 32
freq_quarterly_jan = 33
freq_quarterly_feb = 34
freq_quarterly_mar = 35
freq_quarterly_apr = 33
freq_quarterly_may = 34
freq_quarterly_jun = 35
freq_quarterly_jul = 33
freq_quarterly_aug = 34
freq_quarterly_sep = 35
freq_quarterly_oct = 33
freq_quarterly_nov = 34
freq_quarterly_dec = 35
freq_halfyearly = 64
freq_halfyearly_jan = 65
freq_halfyearly_feb = 66
freq_halfyearly_mar = 67
freq_halfyearly_apr = 68
freq_halfyearly_may = 69
freq_halfyearly_jun = 70
freq_halfyearly_jul = 65
freq_halfyearly_aug = 66
freq_halfyearly_sep = 67
freq_halfyearly_oct = 68
freq_halfyearly_nov = 69
freq_halfyearly_dec = 70
freq_yearly = 128
freq_yearly_jan = 129
freq_yearly_feb = 130
freq_yearly_mar = 131
freq_yearly_apr = 132
freq_yearly_may = 133
freq_yearly_jun = 134
freq_yearly_jul = 135
freq_yearly_aug = 136
freq_yearly_sep = 137
freq_yearly_oct = 138
freq_yearly_nov = 139
freq_yearly_dec = 140
end

const obj_id_t = Int64

struct object_t
Expand Down Expand Up @@ -184,6 +130,81 @@ function de_new_catalog(de, pid, name, id)
ccall((:de_new_catalog, libdaec), Cint, (de_file, obj_id_t, Ptr{Cchar}, Ptr{obj_id_t}), de, pid, name, id)
end

@cenum frequency_t::UInt32 begin
freq_none = 0
freq_unit = 1
freq_daily = 4
freq_bdaily = 5
freq_weekly = 16
freq_weekly_sun0 = 16
freq_weekly_mon = 17
freq_weekly_tue = 18
freq_weekly_wed = 19
freq_weekly_thu = 20
freq_weekly_fri = 21
freq_weekly_sat = 22
freq_weekly_sun7 = 23
freq_weekly_sun = 23
freq_monthly = 32
freq_quarterly = 64
freq_quarterly_jan = 65
freq_quarterly_feb = 66
freq_quarterly_mar = 67
freq_quarterly_apr = 65
freq_quarterly_may = 66
freq_quarterly_jun = 67
freq_quarterly_jul = 65
freq_quarterly_aug = 66
freq_quarterly_sep = 67
freq_quarterly_oct = 65
freq_quarterly_nov = 66
freq_quarterly_dec = 67
freq_halfyearly = 128
freq_halfyearly_jan = 129
freq_halfyearly_feb = 130
freq_halfyearly_mar = 131
freq_halfyearly_apr = 132
freq_halfyearly_may = 133
freq_halfyearly_jun = 134
freq_halfyearly_jul = 129
freq_halfyearly_aug = 130
freq_halfyearly_sep = 131
freq_halfyearly_oct = 132
freq_halfyearly_nov = 133
freq_halfyearly_dec = 134
freq_yearly = 256
freq_yearly_jan = 257
freq_yearly_feb = 258
freq_yearly_mar = 259
freq_yearly_apr = 260
freq_yearly_may = 261
freq_yearly_jun = 262
freq_yearly_jul = 263
freq_yearly_aug = 264
freq_yearly_sep = 265
freq_yearly_oct = 266
freq_yearly_nov = 267
freq_yearly_dec = 268
end

const date_t = Int64

function de_pack_year_period_date(freq, year, period, date)
ccall((:de_pack_year_period_date, libdaec), Cint, (frequency_t, Int32, UInt32, Ptr{date_t}), freq, year, period, date)
end

function de_unpack_year_period_date(freq, date, year, period)
ccall((:de_unpack_year_period_date, libdaec), Cint, (frequency_t, date_t, Ptr{Int32}, Ptr{UInt32}), freq, date, year, period)
end

function de_pack_calendar_date(freq, year, month, day, date)
ccall((:de_pack_calendar_date, libdaec), Cint, (frequency_t, Int32, UInt32, UInt32, Ptr{date_t}), freq, year, month, day, date)
end

function de_unpack_calendar_date(freq, date, year, month, day)
ccall((:de_unpack_calendar_date, libdaec), Cint, (frequency_t, date_t, Ptr{Int32}, Ptr{UInt32}, Ptr{UInt32}), freq, date, year, month, day)
end

struct scalar_t
object::object_t
frequency::frequency_t
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020-2021, Bank of Canada
# Copyright (c) 2020-2023, Bank of Canada
# All rights reserved.

using Test
Expand Down
103 changes: 103 additions & 0 deletions test/test_dataecon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# All rights reserved.

using Dates
using Random

DE = TimeSeriesEcon.DataEcon
test_file = "test.daec"
Expand Down Expand Up @@ -299,3 +300,105 @@ rm(test_file, force=true)

end

##################################################################################################

# Compare the TimeSeriesEcon internal encoding of MITs to the
# encodings produced by de_pack_xyz and de_unpack_xyz.

# Known bug: in TimeSeriesEcon, MIT{BDaily} misbehave when the year is 0 or negative.

# Frequency \ pack/unpack || year+period | year+month+day
# ===============================================================
# YP Date || works | not implemented
# Cal Date || works | works

@testset "pack/unpack year_period" begin
# here we test the first column of the table - that is packing and unpacking
# MITs given year-period
Random.seed!(0x007)
fc = Dict{Type{<:Frequency},Base.RefValue{Int}}()
jfreqs = [Daily, BDaily, (Weekly{i} for i = 1:7)...,
Monthly, (Quarterly{i} for i = 1:3)...,
(HalfYearly{i} for i = 1:6)..., (Yearly{i} for i = 1:12)...]
for i = 1:1000
fr = rand(jfreqs)
d1 = convert(Int, rand(Int16))
if fr == BDaily && d1 < 1
# make sure it's non-negative to work around known bug
d1 = 1 - d1
end
d = MIT{fr}(d1)
y, p = isweekly(fr) ? TimeSeriesEcon._mit2yp(d) : TimeSeriesEcon.mit2yp(d)
d2 = Ref{Int64}(0)
f = DE.I._to_de_scalar_freq(fr)
@test DE.C.DE_SUCCESS == DE.C.de_pack_year_period_date(f, y, p, d2)
if d1 != d2[]
@info "Not equal" fr d1 d y p d2
continue
end
@test d1 == d2[]
yr = Ref{Int32}(0)
pr = Ref{UInt32}(0)
@test DE.C.DE_SUCCESS == DE.C.de_unpack_year_period_date(f, d2[], yr, pr)
@test y == yr[] && p == pr[]
if !(y == yr[] && p == pr[])
@info "Not equal" fr d1 d y p d2 yr pr
end

get!(fc, fr, Ref(0))[] += 1
end
# make sure we tested all frequencies
foreach(jfreqs) do fr
@test get!(fc, fr, Ref(0))[] > 10
end
end;


@testset "pack/unpack year_month_day" begin
# here we test the second column of the table - that is packing and unpacking
# MITs given year-month-day. This doesn't work for YP frequencies.
Random.seed!(0x007)
fc = Dict{Type{<:Frequency},Base.RefValue{Int}}()
calfreqs = [Daily, BDaily, (Weekly{i} for i = 1:7)...,]
for i = 1:1000
fr = rand(calfreqs)
d1 = convert(Int, rand(Int16))
if fr == BDaily && d1 < 1
# make sure it's non-negative to work around known bug
d1 = 1 - d1
end
d = MIT{fr}(d1)
date = Date(d)
yr = Dates.year(date)
mn = Dates.month(date)
dy = Dates.day(date)

f = DE.I._to_de_scalar_freq(fr)

d2 = Ref{Int64}(0)
@test DE.C.DE_SUCCESS == DE.C.de_pack_calendar_date(f, yr, mn, dy, d2)
if d1 != d2[]
@info "Not equal" fr d1 d y p d2
continue
end
@test d1 == d2[]

Y = Ref{Int32}()
M = Ref{UInt32}()
D = Ref{UInt32}()
@test DE.C.DE_SUCCESS == DE.C.de_unpack_calendar_date(f, d1, Y, M, D)
if !(yr == Y[] && mn == M[] && dy == D[])
@info "Not equal" fr d1 d y p d2 yr pr
end
@test yr == Y[] && mn == M[] && dy == D[]

get!(fc, fr, Ref(0))[] += 1
end
# make sure we tested all supported frequencies
foreach(calfreqs) do fr
@test get!(fc, fr, Ref(0))[] > 10
end
end;



0 comments on commit bc4c69a

Please sign in to comment.