From cbd071c4c9efa1a71efa3608830ba61ce8c3cfba Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Sun, 11 Feb 2024 12:48:02 +0530 Subject: [PATCH] test: precompile `@register_unit` in an external module. --- src/register_units.jl | 5 ++--- src/symbolic_dimensions.jl | 12 ++++++++++- .../ExternalUnitRegistration.jl | 21 +++++++++++++++++++ test/unittests.jl | 13 ++++++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 test/precompile_test/ExternalUnitRegistration.jl diff --git a/src/register_units.jl b/src/register_units.jl index 905726ae..bbd9e7c2 100644 --- a/src/register_units.jl +++ b/src/register_units.jl @@ -1,6 +1,5 @@ import .Units: UNIT_MAPPING, UNIT_SYMBOLS, UNIT_VALUES, _lazy_register_unit -import .SymbolicUnits: - SymbolicDimensionsSingleton, SYMBOLIC_UNIT_VALUES, update_symbolic_unit_values! +import .SymbolicUnits: update_external_symbolic_unit_value # Update the unit collections const UNIT_UPDATE_LOCK = Threads.SpinLock() @@ -12,7 +11,7 @@ function update_all_values(name_symbol, unit) i = lastindex(ALL_VALUES) ALL_MAPPING[name_symbol] = i UNIT_MAPPING[name_symbol] = i - update_symbolic_unit_values!(name_symbol) + update_external_symbolic_unit_value(name_symbol) end end diff --git a/src/symbolic_dimensions.jl b/src/symbolic_dimensions.jl index 84f2069c..dcd19772 100644 --- a/src/symbolic_dimensions.jl +++ b/src/symbolic_dimensions.jl @@ -417,7 +417,17 @@ module SymbolicUnits update_symbolic_unit_values!(w::WriteOnceReadMany) = update_symbolic_unit_values!.(w._raw_data) update_symbolic_unit_values!(UNIT_SYMBOLS) - """ + # Non-eval version of `update_symbolic_unit_values!` for registering units in + # an external module. + function update_external_symbolic_unit_value(unit) + unit = constructorof(DEFAULT_SYMBOLIC_QUANTITY_TYPE)( + DEFAULT_VALUE_TYPE(1.0), + SymbolicDimensionsSingleton{DEFAULT_DIM_BASE_TYPE}(unit) + ) + push!(SYMBOLIC_UNIT_VALUES, unit) + end + +""" sym_uparse(raw_string::AbstractString) Parse a string containing an expression of units and return the diff --git a/test/precompile_test/ExternalUnitRegistration.jl b/test/precompile_test/ExternalUnitRegistration.jl new file mode 100644 index 00000000..48314590 --- /dev/null +++ b/test/precompile_test/ExternalUnitRegistration.jl @@ -0,0 +1,21 @@ +module ExternalUnitRegistration + +using DynamicQuantities: @register_unit, @u_str, @us_str, + ALL_MAPPING, ALL_SYMBOLS, DEFAULT_QUANTITY_TYPE, + DEFAULT_SYMBOLIC_QUANTITY_OUTPUT_TYPE, UNIT_SYMBOLS, UNIT_MAPPING +using Test + +@register_unit Wb u"m^2*kg*s^-2*A^-1" + +@testset " Register Unit Inside a Module" begin + for collection in (UNIT_SYMBOLS, ALL_SYMBOLS, keys(ALL_MAPPING._raw_data), keys(UNIT_MAPPING._raw_data)) + @test :Wb ∈ collection + end + + w = u"Wb" + ws = us"Wb" + @test w isa DEFAULT_QUANTITY_TYPE + @test ws isa DEFAULT_SYMBOLIC_QUANTITY_OUTPUT_TYPE +end + +end diff --git a/test/unittests.jl b/test/unittests.jl index 84b3751a..150e6705 100644 --- a/test/unittests.jl +++ b/test/unittests.jl @@ -8,6 +8,7 @@ using DynamicQuantities: promote_quantity_on_quantity, promote_quantity_on_value using DynamicQuantities: UNIT_VALUES, UNIT_MAPPING, UNIT_SYMBOLS, ALL_MAPPING, ALL_SYMBOLS, ALL_VALUES using DynamicQuantities.SymbolicUnits: SYMBOLIC_UNIT_VALUES using DynamicQuantities: map_dimensions +using DynamicQuantities: _register_unit using Ratios: SimpleRatio using SaferIntegers: SafeInt16 using StaticArrays: SArray, MArray @@ -1861,6 +1862,8 @@ all_map_count_before_registering = length(ALL_MAPPING) @register_unit MySV us"V" @register_unit MySV2 us"km/h" +@test_throws "Unit `m` is already defined as `1.0 m`" esc(_register_unit(:m, u"s")) + @testset "Register Unit" begin @test MyV === u"V" @test MyV == us"V" @@ -1880,3 +1883,13 @@ all_map_count_before_registering = length(ALL_MAPPING) @test my_unit in ALL_SYMBOLS end end + +push!(LOAD_PATH, joinpath(@__DIR__, "precompile_test")) + +using ExternalUnitRegistration: Wb +@testset "Type of Extenral Unit" begin + @test Wb isa DEFAULT_QUANTITY_TYPE + @test Wb/u"m^2*kg*s^-2*A^-1" == 1.0 +end + +pop!(LOAD_PATH)