forked from JuliaPhysics/Unitful.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntests.jl
More file actions
2622 lines (2402 loc) · 119 KB
/
runtests.jl
File metadata and controls
2622 lines (2402 loc) · 119 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Unitful
using Test, LinearAlgebra, Random, ConstructionBase, InverseFunctions, Printf
import Unitful: DimensionError, AffineError
import Unitful: LogScaled, LogInfo, Level, Gain, MixedUnits, Decibel
import Unitful: FreeUnits, ContextUnits, FixedUnits, AffineUnits, AffineQuantity
import ForwardDiff
import NaNMath
import Latexify: Latexify, latexify, @latexify, FancyNumberFormatter, SiunitxNumberFormatter
import LaTeXStrings: LaTeXString, @L_str
import Unitful:
nm, μm, mm, cm, m, km, inch, ft, mi,
ac,
mg, g, kg,
Ra, °F, °C, K,
rad, mrad, °, deg,
ms, s, minute, hr, d, yr, Hz,
J, A, N, mol, V, mJ, eV, dyn, mN,
mW, W,
dB, dB_rp, dB_p, dBm, dBV, dBSPL, Decibel,
Np, Np_rp, Np_p, Neper,
C
import Unitful: 𝐋, 𝐓, 𝐍, 𝚯
import Unitful:
Length, Area, Volume,
Luminosity,
Time, Frequency,
Mass,
Current,
Temperature, AbsoluteScaleTemperature, RelativeScaleTemperature,
Action,
Power,
MassFlow,
MolarFlow,
VolumeFlow
import Unitful: LengthUnits, AreaUnits, MassUnits, TemperatureUnits
using Dates:
Dates,
Nanosecond, Microsecond, Millisecond, Second, Minute, Hour, Day, Week,
Month, Year,
CompoundPeriod
const colon = Base.:(:)
macro test_or_throws(extype, ex)
return :(
try
# if the first line throws, go to @test_throws in catch clause
# if not: test expression normally
result = $ex
@test result
catch
@test_throws $extype $ex
end
)
end
is_finite_nonzero(x) = isfinite(x) && !iszero(x)
@testset "Construction" begin
@test isa(NoUnits, FreeUnits)
@test typeof(𝐋) === Unitful.Dimensions{(Unitful.Dimension{:Length}(1),)}
@test 𝐋*𝐋 === 𝐋^2
@test typeof(1.0m) === Unitful.Quantity{Float64, 𝐋,
Unitful.FreeUnits{(Unitful.Unit{:Meter, 𝐋}(0,1),), 𝐋, nothing}}
@test typeof(1m^2) === Unitful.Quantity{Int, 𝐋^2,
Unitful.FreeUnits{(Unitful.Unit{:Meter, 𝐋}(0,2),), 𝐋^2, nothing}}
@test typeof(1ac) === Unitful.Quantity{Int, 𝐋^2,
Unitful.FreeUnits{(Unitful.Unit{:Acre, 𝐋^2}(0,1),), 𝐋^2, nothing}}
@test typeof(ContextUnits(m,μm)) ===
ContextUnits{(Unitful.Unit{:Meter, 𝐋}(0,1),), 𝐋, typeof(μm), nothing}
@test typeof(1.0*ContextUnits(m,μm)) === Unitful.Quantity{Float64, 𝐋,
ContextUnits{(Unitful.Unit{:Meter, 𝐋}(0,1),), 𝐋, typeof(μm), nothing}}
@test typeof(1.0*FixedUnits(m)) === Unitful.Quantity{Float64, 𝐋,
FixedUnits{(Unitful.Unit{:Meter, 𝐋}(0,1),), 𝐋, nothing}}
@test 3mm != 3*(m*m) # mm not interpreted as m*m
@test (3+4im)*V === V*(3+4im) === (3V+4V*im) # Complex quantity construction
@test !isreal(Base.complex(3.0/m, 4.0/m))
@test !isreal(Base.complex((3.0+4.0im)/m))
@test Base.reim(Base.complex((3.0+4.0im)/m)) == (3.0/m, 4.0/m)
@test Base.complex(1m, 1.5m) == Base.complex(1.0m, 1.5m)
@test Base.widen(Base.complex(Float32(3.0)/m)) == Base.complex(Float64(3.0)/m)
@test Base.complex(1.0/m) == (1.0/m + (0.0/m)*im)
@test Base.complex(1.0/m + (1.5/m)*im) == (1.0/m + (1.5/m)*im)
@test 3*NoUnits === 3
@test 3*(FreeUnits(m)/FreeUnits(m)) === 3
@test 3*(ContextUnits(m)/ContextUnits(m)) === 3
@test 3*(FixedUnits(m)/FixedUnits(m)) === 3
@test ContextUnits(mm) === ContextUnits(mm,m)
@test Quantity(3, NoUnits) === 3
@test FreeUnits(ContextUnits(mm,m)) === FreeUnits(mm)
@test FreeUnits(FixedUnits(mm)) === FreeUnits(mm)
@test isa(FreeUnits(m), FreeUnits)
@test isa(ContextUnits(m), ContextUnits)
@test isa(ContextUnits(m,mm), ContextUnits)
@test isa(FixedUnits(m), FixedUnits)
@test ContextUnits(m, FixedUnits(mm)) === ContextUnits(m, mm)
@test ContextUnits(m, ContextUnits(mm, mm)) === ContextUnits(m, mm)
@test_throws DimensionError ContextUnits(m,kg)
@test ConstructionBase.constructorof(typeof(1.0m))(2) === 2m
end
@testset "inverse" begin
InverseFunctions.test_inverse(Base.Fix1(ustrip, m), 2m)
InverseFunctions.test_inverse(Base.Fix1(ustrip, m), 2mm)
end
@testset "Types" begin
@test Base.complex(Quantity{Float64,NoDims,NoUnits}) ==
Quantity{Complex{Float64},NoDims,NoUnits}
end
# A number type for which the results of `real` and `float`
# are not `<:Real` or `<:AbstractFloat`, respectively
struct NonReal <: Number
num::Int
end
Base.:*(x::NonReal, y::Float64) = x.num * y
Base.real(x::NonReal) = x
Base.float(x::NonReal) = x
# A number type for which `real` and `float` throw an error
struct ErrReal <: Number
num::Int
end
Base.:*(x::ErrReal, y::Float64) = x.num * y
Base.real(x::ErrReal) = error("real not defined")
Base.float(x::ErrReal) = error("float not defined")
# A number type for which `real` and `float` do not return a built-in type
struct MyFloat64 <: AbstractFloat
num::Float64
end
Base.:*(x::MyFloat64, y::Float64) = x.num * y
# Base.real(x::MyFloat) = x
# Base.float(x::MyFloat) = x
@testset "Conversion" begin
@testset "> Unitless ↔ unitful conversion" begin
@test_throws DimensionError convert(typeof(3m), 1)
@test_throws DimensionError convert(Quantity{Float64, typeof(𝐋)}, 1)
@test_throws DimensionError convert(Float64, 3m)
@test @inferred(3m/unit(3m)) === 3
@test @inferred(3.0g/unit(3.0g)) === 3.0
# 1-arg ustrip
@test @inferred(ustrip(3*FreeUnits(m))) === 3
@test @inferred(ustrip(3*ContextUnits(m,μm))) === 3
@test @inferred(ustrip(3*FixedUnits(m))) === 3
@test @inferred(ustrip(3)) === 3
@test @inferred(ustrip(3.0m)) === 3.0
# ustrip with type and unit arguments
@test @inferred(ustrip(m, 3.0m)) === 3.0
@test @inferred(ustrip(m, 2mm)) === 1//500
@test @inferred(ustrip(mm, 3.0m)) === 3000.0
@test @inferred(ustrip(NoUnits, 3.0m/1.0m)) === 3.0
@test @inferred(ustrip(NoUnits, 3.0m/1.0cm)) === 300.0
@test @inferred(ustrip(cm, missing)) === missing
@test @inferred(ustrip(NoUnits, missing)) === missing
@test_throws DimensionError ustrip(NoUnits, 3.0m/1.0s)
@test @inferred(ustrip(Float64, m, 2mm)) === 0.002
@test @inferred(ustrip(Int, mm, 2.0m)) === 2000
@test @inferred(ustrip(Float32, NoUnits, 5.0u"m"/2.0u"m")) === Float32(2.5)
@test @inferred(ustrip(Int, NoUnits, 3.0u"m"/1.0u"cm")) === 300
# convert
@test convert(typeof(1mm/m), 3) == 3000mm/m
@test convert(typeof(1mm/m), 3*NoUnits) == 3000mm/m
@test convert(typeof(1*ContextUnits(mm/m, NoUnits)), 3) == 3000mm/m
@test convert(typeof(1*FixedUnits(mm/m)), 3) == 3000*FixedUnits(mm/m)
@test convert(Int, 1*FreeUnits(m/mm)) === 1000
@test convert(Int, 1*FixedUnits(m/mm)) === 1000
@test convert(Int, 1*ContextUnits(m/mm, NoUnits)) === 1000
for U = (NoUnits, FixedUnits(NoUnits), ContextUnits(NoUnits, m/mm))
@test convert(Quantity{Int,NoDims,typeof(U)}, 1*FreeUnits(m/mm)) === Quantity{Int,NoDims,typeof(U)}(1000)
@test convert(Quantity{Int,NoDims,typeof(U)}, 1*FixedUnits(m/mm)) === Quantity{Int,NoDims,typeof(U)}(1000)
@test convert(Quantity{Int,NoDims,typeof(U)}, 1*ContextUnits(m/mm, NoUnits)) === Quantity{Int,NoDims,typeof(U)}(1000)
@test convert(Quantity{Int,NoDims,typeof(U)}, 1) === Quantity{Int,NoDims,typeof(U)}(1)
end
@test convert(Quantity{Int}, 1) === Quantity{Int,NoDims,typeof(NoUnits)}(1)
@test convert(Quantity{Int,NoDims}, 1) === Quantity{Int,NoDims,typeof(NoUnits)}(1)
# w/ units distinct from w/o units
@test 1m != 1
@test 1 != 1m
@test (3V+4V*im) != (3+4im)
# Issue 26
@unit altL "altL" altLiter 1000*cm^3 true
@test convert(Float64, 1altL/cm^3) === 1000.0
# Issue 327
@test uconvert(u"√cm", 1u"√m") == 10u"√cm"
end
@testset "> Unitful ↔ unitful conversion" begin
@testset ">> Numeric conversion" begin
@test @inferred(float(3m)) === 3.0m
@test @inferred(Float32(3m)) === 3.0f0m
@test @inferred(Integer(3.0A)) === 3A
@test Rational(3.0m) === (Int64(3)//1)*m
@test typeof(convert(typeof(0.0°), 90°)) == typeof(0.0°)
@test (3.0+4.0im)*V == (3+4im)*V
@test im*V == Complex(0,1)*V
for x = (3, 3.0, 3//1, 1+2im, 1.0+2.0im, (1//2)+(3//4)im)
for u = (m, °C)
@test @inferred(big(x*u)) == big(x)*u
@test typeof(big(x*u)) == typeof(big(x)*u)
@test big(typeof(x*u)) == typeof(big(x)*u)
end
q = Quantity{typeof(x),NoDims,typeof(NoUnits)}(x)
big_q = Quantity{big(typeof(x)),NoDims,typeof(NoUnits)}(big(x))
@test @inferred(big(q)) == big_q
@test typeof(big(q)) == typeof(big_q)
@test big(typeof(q)) == typeof(big_q)
end
end
@testset ">> Intra-unit conversion" begin
# an essentially no-op uconvert should not disturb numeric type
@test @inferred(uconvert(g,1g)) === 1g
@test @inferred(uconvert(m,0x01*m)) === 0x01*m
@test @inferred(convert(Quantity{Float64, 𝐋}, 1m)) === 1.0m
@test 1kg === 1kg
@test typeof(1m)(1m) === 1m
@test 1*FreeUnits(kg) == 1*ContextUnits(kg,g)
@test 1*ContextUnits(kg,g) == 1*FreeUnits(kg)
@test 1*FreeUnits(kg) == 1*FixedUnits(kg)
@test 1*FixedUnits(kg) == 1*FreeUnits(kg)
@test 1*ContextUnits(kg,g) == 1*FixedUnits(kg)
@test 1*FixedUnits(kg) == 1*ContextUnits(kg,g)
# No auto conversion when working with FixedUnits exclusively.
@test_throws ErrorException 1*FixedUnits(kg) == 1000*FixedUnits(g)
end
@testset ">> Inter-unit conversion" begin
@test 1g == 0.001kg # Issue 56
@test 0.001kg == 1g # Issue 56
@test 1kg == 1000g
@test !(1kg === 1000g)
@test 1inch == (254//100)*cm
@test 1ft == 12inch
@test 1/mi == 1//(5280ft)
@test 1minute == 60s
@test 1hr == 60minute
@test 1d == 24hr
@test 1yr == 365.25d
@test 1J == 1kg*m^2/s^2
@test typeof(1cm)(1m) === 100cm
@test (3V+4V*im) != (3m+4m*im)
@test_throws DimensionError uconvert(m, 1kg)
@test_throws DimensionError uconvert(m, 1*ContextUnits(kg,g))
@test_throws DimensionError uconvert(ContextUnits(m,mm), 1kg)
@test_throws DimensionError uconvert(m, 1*FixedUnits(kg))
@test uconvert(g, 1*FixedUnits(kg)) == 1000g # manual conversion okay
@test (1kg, 2g, 3mg, missing) .|> g === (1000g, 2g, (3//1000)g, missing)
# Issue 79:
@test isapprox(upreferred(Unitful.ε0), 8.85e-12u"F/m", atol=0.01e-12u"F/m")
# Issue 261:
@test 1u"rps" == 360°/s
@test 1u"rps" == 2π/s
@test 1u"rpm" == 360°/minute
@test 1u"rpm" == 2π/minute
# Issue 430:
# definition for arcsecond taken from UnitfulAngles.jl
# definition for Jy arcsecond taken from UnitfulAstro.jl
@unit angles_arcsecond "angles_″" angles_Arcsecond °//3600 false
@unit astro_Jy "astro_Jy" astro_Jansky 1e-23u"erg*s^-1*cm^-2*Hz^-1" true
@test uconvert(
astro_Jy / angles_arcsecond^2, 1.0u"GHz^2 * J * c^-2"
) ≈ 2.6152205956835644e16 * astro_Jy * angles_arcsecond^-2
# Issue 458:
@test deg2rad(360°) ≈ 2π * rad
@test rad2deg(2π * rad) ≈ 360°
# Issue 647:
@test uconvert(u"kb^1000", 1u"kb^1001 * b^-1") === 1000u"kb^1000"
@test uconvert(u"kOe^1000", 1u"kOe^1001 * Oe^-1") === 1000u"kOe^1000"
# Issue 753:
# preserve the floating point precision of quantities
for T = [Float16, Float32, Float64, BigFloat]
@test Unitful.numtype(uconvert(m, T(100)cm)) === T
@test Unitful.numtype(uconvert(cm, (T(1)π + im) * m)) === Complex{T}
@test Unitful.numtype(uconvert(rad, T(360)°)) === T
@test Unitful.numtype(uconvert(°, (T(2)π + im) * rad)) === Complex{T}
@test typeof(upreferred(T(360)°)) === T
end
@test uconvert(rad, NonReal(360)°) == uconvert(rad, 360°)
@test uconvert(rad, ErrReal(360)°) == uconvert(rad, 360°)
@test uconvert(rad, MyFloat64(360)°) == uconvert(rad, 360°)
@test upreferred(NonReal(360)°) == upreferred(360°)
@test upreferred(ErrReal(360)°) == upreferred(360°)
@test upreferred(MyFloat64(360)°) == upreferred(360°)
# Floating point overflow/underflow in uconvert can happen if the
# conversion factor is large, because uconvert does not cancel
# common basefactors (or just for really large exponents and/or
# SI prefixes). This test makes sure that uconvert does not silently
# return NaN, Inf, or 0 in these cases, i.e. either returns a finite
# result or throws an error indicating that it cannot handle the
# conversion.
@test_or_throws ArgumentError is_finite_nonzero(uconvert(u"kb^12", 1u"b^12"))
@test_or_throws ArgumentError is_finite_nonzero(uconvert(u"ab^11", 1u"Tb^11"))
@test_or_throws ArgumentError is_finite_nonzero(uconvert(u"Tb^11", 1u"ab^11"))
@test_or_throws ArgumentError is_finite_nonzero(uconvert(u"b^11 * eV", 1u"m^22 * J"))
@test_or_throws ArgumentError is_finite_nonzero(uconvert(u"m^22 * J", 1u"b^11 * eV"))
# min/max had code doing the equivalent of uconvert, and suffering
# from similar problems as issue 647 (see above)
@test_or_throws ArgumentError max(1u"Ym^18", 1u"Em^18") === 1u"Ym^18"
@test_or_throws ArgumentError max(1u"Em^18", 1u"Ym^18") === 1u"Ym^18"
@test_or_throws ArgumentError min(1u"Ym^18", 1u"Em^18") === 1u"Em^18"
@test_or_throws ArgumentError min(1u"Em^18", 1u"Ym^18") === 1u"Em^18"
@test_or_throws ArgumentError minmax(1u"Ym^18", 1u"Em^18") ===
(1u"Em^18", 1u"Em^18")
@test_or_throws ArgumentError max(1u"fb^8", 1u"ab^8") === 1u"fb^8"
@test_or_throws ArgumentError max(1u"ab^8", 1u"fb^8") === 1u"fb^8"
@test_or_throws ArgumentError min(1u"fb^8", 1u"ab^8") === 1u"ab^8"
@test_or_throws ArgumentError min(1u"ab^8", 1u"fb^8") === 1u"ab^8"
@test_or_throws ArgumentError minmax(1u"fb^8", 1u"ab^8") ===
(1u"ab^8", 1u"fb^8")
# Issue 660:
@test uconvert(u"Å * ps^-2", 1.0u"kcal*Å^-1*g^-1") ≈ 418.4u"Å * ps^-2"
# Issue 780:
@unit Fr "Fr" franklin 1sqrt(dyn)*cm false
@unit Test780 "Test780" test780 1sqrt(mN)*cm false
@test uconvert(dyn, 1Fr^2/cm^2) ≈ 1dyn
@test uconvert(mN, 1Test780^2/cm^2) ≈ 1mN
@test_broken uconvert(dyn, 1Fr^2/cm^2) === 1dyn
@test_broken uconvert(mN, 1Test780^2/cm^2) === 1mN
end
end
end
include("dates.jl")
@testset "Temperature and affine quantities" begin
@testset "Affine transforms and quantities" begin
@test 1°C isa RelativeScaleTemperature
@test !isa(1°C, AbsoluteScaleTemperature)
@test 1K isa AbsoluteScaleTemperature
@test !isa(1K, RelativeScaleTemperature)
@test_throws AffineError °C*°C
@test_throws AffineError °C*K
@test_throws AffineError (0°C)*(0°C)
@test_throws AffineError (1°C)/(1°C)
@test_throws AffineError °C^2
let x = 2
@test_throws AffineError °C^x
end
@test_throws AffineError inv(°C)
@test_throws AffineError inv(0°C)
@test_throws AffineError sqrt(°C)
@test_throws AffineError sqrt(0°C)
@test_throws AffineError cbrt(°C)
@test_throws AffineError cbrt(0°C)
@test_throws AffineError 32°F + 1°F
@test_throws AffineError (32°F) * 2
@test_throws AffineError 2 * (32°F)
@test_throws AffineError (32°F) / 2
@test_throws AffineError 2 / (32°F)
for f = (:div, :rem, :divrem)
@eval for r = (RoundNearest, RoundNearestTiesAway, RoundNearestTiesUp,
RoundToZero, RoundUp, RoundDown)
@test_throws AffineError $f(32°F, 2°F, r)
@test_throws AffineError $f(32°F, 2K, r)
@test_throws AffineError $f(32K, 2°F, r)
end
end
for f = (:div, :cld, :fld, :rem, :mod, :divrem, :fldmod)
@eval begin
@test_throws AffineError $f(32°F, 2°F)
@test_throws AffineError $f(32°F, 2K)
@test_throws AffineError $f(32K, 2°F)
end
end
@test zero(100°C) === 0K
@test zero(typeof(100°C)) === 0K
@test oneunit(100°C) === 1K
@test oneunit(typeof(100°C)) === 1K
@test_throws AffineError one(100°C)
@test_throws AffineError one(typeof(100°C))
@test 0°C isa AffineQuantity{T, 𝚯} where T # is "relative temperature"
@test 0°C isa Temperature # dimensional correctness
@test °C isa AffineUnits{N, 𝚯} where N
@test °C isa TemperatureUnits
@test @inferred(uconvert(°F, 0°C)) === (32//1)°F # Some known conversions...
@test @inferred(uconvert(°C, 32°F)) === (0//1)°C # ⋮
@test @inferred(uconvert(°C, 212°F)) === (100//1)°C # ⋮
@test @inferred(uconvert(°C, 0x01*°C)) === 0x01*°C # Preserve numeric type
# The next test is a little funky but checks the `affineunit` functionality
@test @inferred(uconvert(°F,
0*Unitful.affineunit(27315K//100 + 5K//9))) === (33//1)°F
end
@testset "Temperature differences" begin
@test @inferred(uconvert(Ra, 0K)) === 0Ra//1
@test @inferred(uconvert(K, 1Ra)) === 5K//9
@test @inferred(uconvert(μm/(m*Ra), 9μm/(m*K))) === 5μm/(m*Ra)//1
@test @inferred(uconvert(FreeUnits(Ra), 4.2K)) ≈ 7.56Ra
@test @inferred(unit(uconvert(FreeUnits(Ra), 4.2K))) === FreeUnits(Ra)
@test @inferred(uconvert(FreeUnits(Ra), 4.2*ContextUnits(K))) ≈ 7.56Ra
@test @inferred(unit(uconvert(FreeUnits(Ra), 4.2*ContextUnits(K)))) === FreeUnits(Ra)
@test @inferred(unit(uconvert(ContextUnits(Ra), 4.2K))) === ContextUnits(Ra)
let cc = ContextUnits(°C, °C), kc = ContextUnits(K, °C), rac = ContextUnits(Ra, °C)
@test 100°C + 1K === (7483//20)K
@test 100cc + 1K === (101//1)cc
@test 100cc + 1K == (101//1)°C
@test 1K + 100cc === (101//1)cc
@test 1K + 100cc == (101//1)°C
@test 100°C + 1Ra === (67267//180)K
@test 100°C - 212°F === (0//1)K
@test 100°C - 211°F === (5//9)K
@test 100°C - 1°C === 99K
@test 100°C - 32°F === (100//1)K
@test 10cc + 2.0K/hr * 60minute + 3.0K/hr * 60minute === 15.0cc
@test 10cc + 5kc === (15//1)cc
@test 10°C + 5kc === (15//1)cc
@test 10°C + (9//5)rac === (11//1)cc
end
end
@testset "Promotion" begin
@test_throws ErrorException Unitful.preferunits(°C)
@test @inferred(eltype([1°C, 1K])) <: Quantity{Rational{Int}, 𝚯, typeof(K)}
@test @inferred(eltype([1.0°C, 1K])) <: Quantity{Float64, 𝚯, typeof(K)}
@test @inferred(eltype([1°C, 1°F])) <: Quantity{Rational{Int}, 𝚯, typeof(K)}
@test @inferred(eltype([1.0°C, 1°F])) <: Quantity{Float64, 𝚯, typeof(K)}
# context units should be identifiable as affine
@test ContextUnits(°C, °F) isa AffineUnits
let fc = ContextUnits(°F, °C), cc = ContextUnits(°C, °C)
@test @inferred(promote(1fc, 1cc)) === ((-155//9)cc, (1//1)cc)
@test @inferred(eltype([1cc, 1°C])) <: Quantity{Rational{Int}, 𝚯, typeof(cc)}
end
end
end
# preferred units work on AbstractQuantity
struct QQQ <: Unitful.AbstractQuantity{Float64,𝐋,typeof(cm)}
val::Float64
end
Unitful.uconvert(U::Unitful.Units, q::QQQ) = uconvert(U, Quantity(q.val, cm))
@testset "Promotion" begin
@testset "> Unit preferences" begin
# Should warn on possible redundant units issue (ms and s)
@test_logs (:warn, r"^Preferred units contain complex units") Unitful.preferunits(C/ms)
# Test for wacky preferred units functionality
Unitful.preferunits(C/s)
@test @inferred(upreferred(V/m)) == kg*m*C^-1*s^-2
@test dimension(upreferred(V/m)) == dimension(V/m)
# Reset preferred units to default, except for units of dimension 𝐋*𝐌*𝐈^-1*𝐓^-3,
# because upreferred has already been called for that dimension
Unitful.preferunits(A)
# Only because we favor SI, we have the following:
@test @inferred(upreferred(N)) === kg*m/s^2
@test @inferred(upreferred(dimension(N))) === kg*m/s^2
@test @inferred(upreferred(g)) === kg
@test @inferred(upreferred(FreeUnits(g))) === FreeUnits(kg)
# Test special units behaviors
@test @inferred(upreferred(ContextUnits(g,mg))) === ContextUnits(mg,mg)
@test @inferred(upreferred(FixedUnits(kg))) === FixedUnits(kg)
@test @inferred(upreferred(upreferred(1.0ContextUnits(kg,g)))) ===
1000.0ContextUnits(g,g)
@test @inferred(upreferred(unit(1g |> ContextUnits(g,mg)))) === ContextUnits(mg,mg)
@test @inferred(upreferred(1g |> ContextUnits(g,mg))) == 1000mg
@test @inferred(upreferred(1N)) === 1*kg*m/s^2
@test ismissing(upreferred(missing))
# preferred units work on AbstractQuantity
@test @inferred(upreferred(QQQ(10))) == 0.1m
end
@testset "> promote_unit" begin
@test Unitful.promote_unit(FreeUnits(m)) === FreeUnits(m)
@test Unitful.promote_unit(ContextUnits(m,mm)) === ContextUnits(m,mm)
@test Unitful.promote_unit(FixedUnits(kg)) === FixedUnits(kg)
@test Unitful.promote_unit(ContextUnits(m,mm), ContextUnits(km,mm)) ===
ContextUnits(mm,mm)
@test Unitful.promote_unit(FreeUnits(m), ContextUnits(mm,km)) ===
ContextUnits(km,km)
@test Unitful.promote_unit(FixedUnits(kg), ContextUnits(g,g)) === FixedUnits(kg)
@test Unitful.promote_unit(ContextUnits(g,g), FixedUnits(kg)) === FixedUnits(kg)
@test Unitful.promote_unit(FixedUnits(kg), FreeUnits(g)) === FixedUnits(kg)
@test Unitful.promote_unit(FreeUnits(g), FixedUnits(kg)) === FixedUnits(kg)
@test_throws DimensionError Unitful.promote_unit(m,kg)
# FixedUnits throw a promotion error
@test_throws ErrorException Unitful.promote_unit(FixedUnits(m), FixedUnits(mm))
# Only because we favor SI, we have the following:
@test Unitful.promote_unit(m,km) === m
@test Unitful.promote_unit(m,km,cm) === m
@test Unitful.promote_unit(ContextUnits(m,mm), ContextUnits(km,cm)) ===
FreeUnits(m)
end
@testset "> Simple promotion" begin
# promotion should do nothing to units alone
# promote throws an error if no types are be changed
@test_throws ErrorException promote(m, km)
@test_throws ErrorException promote(ContextUnits(m, km), ContextUnits(mm, km))
@test_throws ErrorException promote(FixedUnits(m), FixedUnits(km))
# promote the numeric type
@test @inferred(promote(1.0m, 1m)) === (1.0m, 1.0m)
@test @inferred(promote(1m, 1.0m)) === (1.0m, 1.0m)
@test @inferred(promote(1.0g, 1kg)) === (0.001kg, 1.0kg)
@test @inferred(promote(1g, 1.0kg)) === (0.001kg, 1.0kg)
@test @inferred(promote(1.0m, 1kg)) === (1.0m, 1.0kg)
@test @inferred(promote(1kg, 1.0m)) === (1.0kg, 1.0m)
@test_broken @inferred(promote(1.0m, 1)) === (1.0m, 1.0) # issue 52
@test @inferred(promote(π, 180°)) === (float(π), float(π)) # issue 168
@test @inferred(promote(180°, π)) === (float(π), float(π)) # issue 168
# prefer no units for dimensionless numbers
@test @inferred(promote(1.0mm/m, 1.0km/m)) === (0.001,1000.0)
@test @inferred(promote(1.0cm/m, 1.0mm/m, 1.0km/m)) === (0.01,0.001,1000.0)
@test @inferred(promote(1.0rad,1.0°)) === (1.0,π/180.0)
# Quantities with promotion context
# Context overrides free units
nm2μm = ContextUnits(nm,μm)
μm2μm = ContextUnits(μm,μm)
μm2mm = ContextUnits(μm,mm)
@test @inferred(promote(1.0nm2μm, 1.0m)) === (0.001μm2μm, 1e6μm2μm)
@test @inferred(promote(1.0m, 1.0μm2μm)) === (1e6μm2μm, 1.0μm2μm)
@test ===(upreferred.(unit.(promote(1.0nm2μm, 2nm2μm)))[1], ContextUnits(μm,μm))
@test ===(upreferred.(unit.(promote(1.0nm2μm, 2nm2μm)))[2], ContextUnits(μm,μm))
# Context agreement
@test @inferred(promote(1.0nm2μm, 1.0μm2μm)) ===
(0.001μm2μm, 1.0μm2μm)
@test @inferred(promote(1μm2μm, 1.0nm2μm, 1.0m)) ===
(1.0μm2μm, 0.001μm2μm, 1e6μm2μm)
@test @inferred(promote(1μm2μm, 1.0nm2μm, 1.0s)) ===
(1.0μm2μm, 1.0nm2μm, 1.0s)
# Context disagreement: fall back to free units
@test @inferred(promote(1.0nm2μm, 1.0μm2mm)) === (1e-9m, 1e-6m)
end
@testset "> Promotion during array creation" begin
@test typeof([1.0m,1.0m]) == Array{typeof(1.0m),1}
@test typeof([1.0m,1m]) == Array{typeof(1.0m),1}
@test typeof([1.0m,1cm]) == Array{typeof(1.0m),1}
@test typeof([1kg,1g]) == Array{typeof(1kg//1),1}
@test typeof([1.0m,1]) == Array{Quantity{Float64},1}
@test typeof([1.0m,1kg]) == Array{Quantity{Float64},1}
@test typeof([1.0m/s 1; 1 0]) == Array{Quantity{Float64},2}
end
@testset "> Issue 52" begin
x,y = 10m, 1
px,py = promote(x,y)
# promoting the second time should not change the types
@test_throws ErrorException promote(px, py)
end
@testset "> Some internal behaviors" begin
# quantities
@test Unitful.numtype(Quantity{Float64}) <: Float64
@test Unitful.numtype(Quantity{Float64, 𝐋}) <: Float64
@test Unitful.numtype(typeof(1.0kg)) <: Float64
@test Unitful.numtype(1.0kg) <: Float64
end
end
@testset "Hashing" begin
@test hash(big(1.0)m) === hash(big(1.0)m)
@test hash(1.0m) === hash(1m)
@test hash(0.5m) === hash((1//2)m)
@test hash(2.0m) === hash(2.0ContextUnits(m, cm))
@test hash(3.0m) === hash(3.0FixedUnits(m))
@test_broken hash(0.5m) === hash(500mm)
@test_broken hash(1rad) === hash(1)
end
@testset "Unit string parsing" begin
@test uparse("m") == m
@test uparse("m,s") == (m,s)
@test uparse("1.0") == 1.0
@test uparse("m/s") == m/s
@test uparse("N*m") == N*m
@test uparse("1.0m/s") == 1.0m/s
@test uparse("m^-1") == m^-1
@test uparse("dB/Hz") == dB/Hz
@test uparse("3.0dB/Hz") == 3.0dB/Hz
# Invalid unit strings
@test_throws Meta.ParseError uparse("N m")
@test_throws ArgumentError uparse("abs(2)")
@test_throws ArgumentError uparse("(1,2)")
@test_throws ArgumentError uparse("begin end")
# test ustrcheck_bool
@test_throws ArgumentError uparse("basefactor") # non-Unit symbols
# ustrcheck_bool(::Quantity)
@test uparse("h") == Unitful.h
@test uparse("π") == π # issue 112
end
@testset "Unit and dimensional analysis" begin
@test @inferred(unit(1m^2)) === m^2
@test @inferred(unit(typeof(1m^2))) === m^2
@test @inferred(unit(Float64)) === NoUnits
@test @inferred(unit(Union{typeof(1m^2),Missing})) === m^2
@test @inferred(unit(Union{Float64,Missing})) === NoUnits
@test @inferred(unit(missing)) === missing
@test @inferred(unit(Missing)) === missing
@test @inferred(dimension(1m^2)) === 𝐋^2
@test @inferred(dimension(1*ContextUnits(m,km)^2)) === 𝐋^2
@test @inferred(dimension(typeof(1m^2))) === 𝐋^2
@test @inferred(dimension(Float64)) === NoDims
@test @inferred(dimension(m^2)) === 𝐋^2
@test @inferred(dimension(1m/s)) === 𝐋/𝐓
@test @inferred(dimension(m/s)) === 𝐋/𝐓
@test @inferred(dimension(1u"mol")) === 𝐍
@test @inferred(dimension(μm/m)) === NoDims
@test @inferred(dimension(missing)) === missing
@test @inferred(dimension(Missing)) === missing
@test dimension.([1u"m", 1u"s"]) == [𝐋, 𝐓]
@test dimension.([u"m", u"s"]) == [𝐋, 𝐓]
@test (𝐋/𝐓)^2 === 𝐋^2 / 𝐓^2
@test isa(m, LengthUnits)
@test isa(ContextUnits(m,km), LengthUnits)
@test isa(FixedUnits(m), LengthUnits)
@test !isa(m, AreaUnits)
@test !isa(ContextUnits(m,km), AreaUnits)
@test !isa(FixedUnits(m), AreaUnits)
@test !isa(m, MassUnits)
@test isa(m^2, AreaUnits)
@test !isa(m^2, LengthUnits)
@test isa(1m, Length)
@test isa(1*ContextUnits(m,km), Length)
@test isa(1*FixedUnits(m), Length)
@test !isa(1m, LengthUnits)
@test !isa(1m, Area)
@test !isa(1m, Luminosity)
@test isa(1ft, Length)
@test isa(1m^2, Area)
@test !isa(1m^2, Length)
@test isa(1inch^3, Volume)
@test isa(1/s, Frequency)
@test isa(1kg, Mass)
@test isa(1s, Time)
@test isa(1A, Current)
@test isa(1K, Temperature)
@test isa(1u"cd", Luminosity)
@test isa(2π*rad*1.0m, Length)
@test isa(u"h", Action)
@test isa(3u"dBm", Power)
@test isa(3u"dBm*Hz*s", Power)
@test isa(1kg/s, MassFlow)
@test isa(1mol/s, MolarFlow)
@test isa(1m^3/s, VolumeFlow)
end
# A number type with non-commutative multiplication
struct MatNum <: Number
mat::Matrix{Int}
end
Base.:(==)(x::MatNum, y::MatNum) = x.mat == y.mat
Base.:*(x::MatNum, y::MatNum) = MatNum(x.mat*y.mat)
# A number type that defines only `<=`, not `==` or `<`
struct Issue399 <: Integer
num::Int
end
Base.:<(::Issue399, ::Issue399) = error("< not defined")
Base.:(==)(::Issue399, ::Issue399) = error("== not defined")
Base.:(<=)(x::Issue399, y::Issue399) = x.num <= y.num
@testset "Mathematics" begin
@testset "> Comparisons" begin
# make sure we are just picking one of the arguments, without surprising conversions
# happening to the units...
@test min(1FreeUnits(hr), 1FreeUnits(s)) === 1FreeUnits(s)
@test min(1FreeUnits(hr), 1ContextUnits(s,ms)) === 1ContextUnits(s,ms)
@test min(1ContextUnits(s,ms), 1FreeUnits(hr)) === 1ContextUnits(s,ms)
@test min(1ContextUnits(s,minute), 1ContextUnits(hr,s)) ===
1ContextUnits(s,minute)
@test max(1ContextUnits(ft, mi), 1ContextUnits(m,mm)) ===
1ContextUnits(m,mm)
@test min(1FreeUnits(hr), 1FixedUnits(s)) === 1FixedUnits(s)
@test min(1FixedUnits(s), 1FreeUnits(hr)) === 1FixedUnits(s)
@test min(1FixedUnits(s), 1ContextUnits(ms,s)) === 1ContextUnits(ms,s)
@test min(1ContextUnits(ms,s), 1FixedUnits(s)) === 1ContextUnits(ms,s)
# automatic conversion prohibited
@test_throws ErrorException min(1FixedUnits(s), 1FixedUnits(hr))
@test min(1FixedUnits(s), 1FixedUnits(s)) === 1FixedUnits(s)
# now move on, presuming that's working well.
@test max(1ft, 1m) == 1m
@test max(10J, 1kg*m^2/s^2) === 10J
@test max(1J//10, 1kg*m^2/s^2) === 1kg*m^2/s^2
@test @inferred(0m < 1.0m)
@test @inferred(2.0m < 3.0m)
@test @inferred(2.0m .< 3.0m)
@test !(@inferred 3.0m .< 3.0m)
@test @inferred(2.0m <= 3.0m)
@test @inferred(3.0m <= 3.0m)
@test @inferred(3.0m <= 3000.0mm)
@test @inferred(2.0m .<= 3.0m)
@test @inferred(3.0m .<= 3.0m)
@test !@inferred(1.0m/mm <= 999)
@test @inferred(1.0m/mm <= 1000)
@test !@inferred(1.1 <= 1000mm/m)
@test @inferred(1.0 <= 1000mm/m)
@test @inferred(1μm/m < 1)
@test @inferred(1 > 1μm/m)
@test @inferred(1μm/m < 1mm/m)
@test @inferred(1mm/m > 1μm/m)
@test_throws DimensionError 1m < 1kg
@test_throws DimensionError 1m < 1
@test_throws DimensionError 1 < 1m
@test_throws DimensionError 1mm/m < 1m
@test_throws DimensionError 1mm/m <= 1m
@test Base.rtoldefault(typeof(1.0u"m")) === Base.rtoldefault(typeof(1.0))
@test Base.rtoldefault(typeof(1u"m")) === Base.rtoldefault(Int)
@test_throws ErrorException Issue399(1)m < Issue399(2)m
@test_throws ErrorException Issue399(1)m == Issue399(1)m
@test @inferred(Issue399(1)m <= Issue399(2)m)
# check NaN handling in min, max (consistent with isless)
@test isequal(min(NaN * u"m", 1.0u"m"), 1.0u"m")
@test isequal(min(1.0u"m", NaN * u"m"), 1.0u"m")
@test isequal(max(NaN * u"m", 1.0u"m"), NaN * u"m")
@test isequal(max(1.0u"m", NaN * u"m"), NaN * u"m")
end
@testset "> Addition and subtraction" begin
@test @inferred(+(1A)) == 1A # Unary addition
@test @inferred(3m + 3m) == 6m # Binary addition
@test @inferred(-(1kg)) == (-1)*kg # Unary subtraction
@test @inferred(3m - 2m) == 1m # Binary subtraction
@test @inferred(zero(1m)) === 0m # Additive identity
@test @inferred(zero(typeof(1m))) === 0m
@test @inferred(zero(typeof(1.0m))) === 0.0m
@test_throws ArgumentError zero(Quantity{Int})
@test zero(Quantity{Int, 𝐋}) == 0m
@test zero(Quantity{Int, 𝐋}) isa Quantity{Int}
@test @inferred(π/2*u"rad" + 90u"°") ≈ π # Dimless quantities
@test @inferred(π/2*u"rad" - 90u"°") ≈ 0 # Dimless quantities
@test @inferred(90u"deg" - 90u"°") == 0
@test_throws DimensionError 1+1m # Dim mismatched
@test_throws DimensionError 1-1m
end
@testset "> Multiplication" begin
@test @inferred(FreeUnits(g)*FreeUnits(kg)) === FreeUnits(g*kg)
@test @inferred(ContextUnits(m,mm)*ContextUnits(kg,g)) ===
ContextUnits(m*kg,mm*g)
@test @inferred(ContextUnits(m,mm)*FreeUnits(g)) ===
ContextUnits(m*g,mm*kg)
@test @inferred(FreeUnits(g)*ContextUnits(m,mm)) ===
ContextUnits(m*g,mm*kg)
@test @inferred(FixedUnits(g)*FixedUnits(kg)) === FixedUnits(g*kg)
@test @inferred(FixedUnits(g)*FreeUnits(kg)) === FixedUnits(g*kg)
@test @inferred(FixedUnits(g)*ContextUnits(kg,kg)) === FixedUnits(g*kg)
@test @inferred(*(1s)) === 1s # Unary multiplication
@test @inferred(3m * 2cm) === 3cm * 2m # Binary multiplication
@test @inferred((3m)*m) === 3*(m*m) # Associative multiplication
@test @inferred(true*1kg) === 1kg # Boolean multiplication (T)
@test @inferred(false*1kg) === 0kg # Boolean multiplication (F)
@test @inferred(true*(1+im)kg) === (1+im)kg # Boolean-complex multiplication (T)
@test @inferred(false*(1+im)kg) === (0+0im)kg # Boolean-complex multiplication (F)
@test @inferred((1+im)kg*true) === (1+im)kg # Complex-boolean multiplication (T)
@test @inferred((1+im)kg*false) === (0+0im)kg # Complex-boolean multiplication (F)
@test @inferred((NaN*kg)*false) === 0.0kg # `false` acts as "strong zero"
@test @inferred(false*(-Inf*kg)) === -0.0kg # `false` acts as "strong zero"
@test typeof(one(eltype([1.0s, 1kg]))) <: Float64 # issue 159, multiplicative identity
# Multiplication can be non-commutative
@test Quantity(MatNum([1 2; 3 4]), m) * MatNum([5 6; 7 8]) == Quantity(MatNum([19 22; 43 50]), m)
@test MatNum([5 6; 7 8]) * Quantity(MatNum([1 2; 3 4]), m) == Quantity(MatNum([23 34; 31 46]), m)
end
@testset "> Division" begin
@test 360° / 2 === 180.0° # Issue 110
@test 360° // 2 === 180°//1
@test 2m // 5s == (2//5)*(m/s) # Units propagate through rationals
@test (2//3)*m // 5 == (2//15)*m # Quantity // Real
@test 5.0m // s === 5.0m/s # Quantity // Unit. Just pass units through
@test s//(5m) === (1//5)*s/m # Unit // Quantity. Will fail if denom is float
@test (m//2) === 1//2 * m # Unit // Real
@test (2//m) === (2//1) / m # Real // Unit
@test (m//s) === m/s # Unit // Unit
@test m / missing === missing # Unit / missing
@test missing / m === missing # Missing / Unit (// is not defined for Missing)
@test missing * u"dB" === missing # Missing * MixedUnits
@test u"dB" * missing === missing # MixedUnits * Missing
@test missing / u"dB" === missing # Missing / MixedUnits
@test u"dB" / missing === missing # MixedUnits / Missing
@test @inferred(div(10m, -3cm)) === -333
@test @inferred(div(10m, 3)) === 3m
@test @inferred(div(10, 3m)) === 3/m
@test @inferred(fld(10m, -3cm)) === -334
@test @inferred(fld(10m, 3)) === 3m
@test @inferred(fld(10, 3m)) === 3/m
@test @inferred(cld(10m, 3)) === 4m
@test @inferred(cld(10, 3m)) === 4/m
@test rem(10m, -3cm) == 1.0cm
@test mod(10m, -3cm) == -2.0cm
@test mod(1hr+3minute+5s, 24s) == 17s
@test mod2pi(360°) === 0° # 2pi is 360°
@test mod2pi(0.5pi*u"m/dm") ≈ pi # just testing the dimensionless fallback
@test modf(2.5rad) === (0.5, 2.0)
@test modf(-250cm/m) === (-1//2, -2//1)
@test_throws MethodError modf(1m)
@test @inferred(inv(s)) === s^-1
@test inv(ContextUnits(m,km)) === ContextUnits(m^-1,km^-1)
@test inv(FixedUnits(m)) === FixedUnits(m^-1)
end
@testset "> Exponentiation" begin
@test @inferred(m^3/m) === m^2
@test @inferred(𝐋^3/𝐋) === 𝐋^2
@test @inferred(sqrt(4m^2)) === 2.0m
@test sqrt(4m^(2//3)) === 2.0m^(1//3)
@test @inferred(sqrt(𝐋^2)) === 𝐋
@test @inferred(sqrt(m^2)) === m
@test @inferred(cbrt(8m^3)) === 2.0m
@test cbrt(8m) === 2.0m^(1//3)
@test @inferred(cbrt(𝐋^3)) === 𝐋
@test @inferred(cbrt(m^3)) === m
@test (2m)^3 === 8*m^3
@test (8m)^(1//3) === 2.0*m^(1//3)
@test @inferred(cis(90°)) == im
@test @inferred(cis((90 - rad2deg(1)*im)°)) ≈ ℯ*im
# Test inferrability of literal powers
_pow_m3(x) = x^-3
_pow_0(x) = x^0
_pow_3(x) = x^3
_pow_2_3(x) = x^(2//3)
@static if VERSION ≥ v"1.8.0-DEV.501"
@test @inferred(_pow_2_3(m)) == m^(2//3)
@test @inferred(_pow_2_3(𝐋)) == 𝐋^(2//3)
@test @inferred(_pow_2_3(1.0m)) == 1.0m^(2//3)
end
@test @inferred(_pow_m3(m)) == m^-3
@test @inferred(_pow_0(m)) == NoUnits
@test @inferred(_pow_3(m)) == m^3
@test @inferred(_pow_m3(𝐋)) == 𝐋^-3
@test @inferred(_pow_0(𝐋)) == NoDims
@test @inferred(_pow_3(𝐋)) == 𝐋^3
@test @inferred(_pow_m3(1.0m)) == 1.0m^-3
@test @inferred(_pow_0(1.0m)) == 1.0
@test @inferred(_pow_3(1.0m)) == 1.0m^3
end
@testset "> Trigonometry" begin
@test @inferred(sin(0.0rad)) == 0.0
@test @inferred(cos(π*rad)) == -1
@test @inferred(tan(π*rad/4)) ≈ 1
@test @inferred(csc(π*rad/2)) == 1
@test @inferred(sec(0.0*rad)) == 1
@test @inferred(cot(π*rad/4)) ≈ 1
@test @inferred(sin(90°)) == 1
@test @inferred(cos(0.0°)) == 1
@test @inferred(tan(45°)) == 1
@test @inferred(csc(90°)) == 1
@test @inferred(sec(0°)) == 1
@test @inferred(cot(45°)) == 1
@test @inferred(asin(1m/1000mm)) == 90°
@test @inferred(acos(-1mm/1000μm)) == π*rad
@test @inferred(atan(2000sqrt(3)ms/2.0s)) == 60°
@test @inferred(acsc(5.0Hz*0.2s)) == π/2
@test @inferred(asec(1m/1nm)) ≈ π/2
@test @inferred(acot(2sqrt(3)s/2000ms)) ≈ 30°
@test @inferred(sincos(250mrad)) === sincos(0.25)
@test @inferred(sincos((1+2im)rad)) === sincos(1+2im)
@test @inferred(sincos(30°)) === (sind(30), cosd(30))
@test @inferred(sinh(0.0rad)) == 0.0
@test @inferred(sinh(1J/N/m) + cosh(1rad)) ≈ MathConstants.e
@test @inferred(tanh(1m/1μm)) == 1
@test @inferred(csch(0.0°)) == Inf
@test @inferred(sech(0K/Ra)) == 1
@test @inferred(coth(1e3m*mm^-1)) == 1
@test @inferred(asinh(0.0mg/kg)) == 0
@test @inferred(acosh(1mm/1000μm)) == 0
@test @inferred(atanh(0W*s/J)) == 0
@test @inferred(acsch(hr/yr * 0)) == Inf
@test @inferred(asech(1.0m/1000.0mm)) == 0
@test @inferred(acoth(1km/1000m)) == Inf
@test @inferred(sinpi(rad/2)) == 1
@test @inferred(cospi(1rad)) == -1
@test @inferred(sinc(1rad)) === 0
@test @inferred(cosc(1ft/3inch)) === 0.25
@test @inferred(cispi(rad/2)) === complex(0.0, 1.0)
@test @inferred(cispi(rad/2 + im*rad)) ≈ complex(0.0, exp(-π))
@test @inferred(sincospi(rad/2)) === (1.0, 0.0)
if isdefined(Base, :tanpi)
@test @inferred(tanpi(1f0rad)) === tanpi(1f0)
@test @inferred(tanpi(250mrad)) === tanpi(0.25)
@test @inferred(tanpi(-100mm/m)) === tanpi(-1//10)
end
@test @inferred(atan(m*sqrt(3),1m)) ≈ 60°
@test @inferred(atan(m*sqrt(3),1.0m)) ≈ 60°
@test @inferred(atan(m*sqrt(3),1000mm)) ≈ 60°
@test @inferred(atan(m*sqrt(3),1e+3mm)) ≈ 60°
@test_throws DimensionError atan(m*sqrt(3),1e+3s)
@test @inferred(angle((3im)*V)) ≈ 90°
@test @inferred(sincosd(5°)) == sincos(5°) == (sind(5°), cosd(5°))
end
@testset "> Exponentials and logarithms" begin
for f in (exp, exp10, exp2, expm1, log, log10, log1p, log2)
@test f(1.0 * u"m/dm") ≈ f(10)
end
end
@testset "> Matrix inversion" begin
@test inv([1 1; -1 1]u"nm") ≈ [0.5 -0.5; 0.5 0.5]u"nm^-1"
end
@testset "> Is functions" begin
@test_throws ErrorException isinteger(1.0m)
@test isinteger(1.4m/mm)
@test !isinteger(1.4mm/m)
@test_throws ErrorException iseven(1.0m)
@test iseven(2rad)
@test !iseven(1rad)
if hasmethod(iseven, (Float64,))
@test iseven(0.5m/mm)
@test !iseven(0.125m/mm)
end
@test_throws ErrorException isodd(1.0m)
@test isodd(1rad)
@test !isodd(2rad)
if hasmethod(isodd, (Float64,))
@test isodd(0.125m/mm)
@test !isodd(0.5m/mm)
end
@test isfinite(1.0m)
@test !isfinite(Inf*m)
@test isnan(NaN*m)
@test !isnan(1.0m)
@static if VERSION ≥ v"1.7.0-DEV.119"
@test isunordered(NaN*m)
@test !isunordered(Inf*m)
@test !isunordered(1.0*m)
end
end
@testset "> Floating point tests" begin
@test isapprox(1.0u"m",(1.0+eps(1.0))u"m")
@test isapprox(1.0u"μm/m",1e-6)
@test !isapprox(1.0u"μm/m",1e-7)
@test !isapprox(1.0u"m",5)
@test frexp(1.5m) == (0.75m, 1.0)
@test unit(nextfloat(0.0m)) == m
@test unit(nextfloat(0.0m, 4)) == m
@test ustrip(nextfloat(0.0m, 4)) == nextfloat(0.0, 4)
@test unit(prevfloat(0.0m)) == m
@test unit(prevfloat(0.0m, 4)) == m
@test ustrip(prevfloat(0.0m, 4)) == prevfloat(0.0, 4)
# NaN behavior
@test NaN*m != NaN*m
@test isequal(NaN*m, NaN*m)
@test isapprox(1.0u"m", 1.1u"m"; atol=0.2u"m")
@test !isapprox(1.0u"m", 1.1u"m"; atol=0.05u"m")
@test isapprox(1.0u"m", 1.1u"m"; atol=200u"mm")
@test !isapprox(1.0u"m", 1.1u"m"; atol=50u"mm")
@test isapprox(1.0u"m", 1.1u"m"; rtol=0.2)
@test !isapprox(1.0u"m", 1.1u"m"; rtol=0.05)
# Issue 465:
z = fill((1+im)m, 2, 3)
@test !isapprox(z, 2z)
@test isapprox(z, z * (1 + 1e-15))
# Test eps
@test eps(1.0u"s") == eps(1.0)u"s"
@test eps(typeof(1.0u"s")) == eps(Float64)
# Test promotion behavior
@test !isapprox(1.0u"m", 1.0u"s")
@test isapprox(1.0u"m", 1000.0u"mm")
@test_throws ErrorException isapprox(1.0*FixedUnits(u"m"), 1000.0*FixedUnits(u"mm"))
end
end
@testset "Fast mathematics" begin
@testset "> fma and muladd" begin
m2cm = ContextUnits(m,cm)
m2mm = ContextUnits(m,mm)
mm2cm = ContextUnits(mm,cm)
cm2cm = ContextUnits(cm,cm)
fm, fmm = FixedUnits(m), FixedUnits(mm)
@test @inferred(fma(2.0, 3.0m, 1.0m)) === 7.0m # llvm good
@test @inferred(fma(2.0, 3.0m2cm, 1.0mm2cm)) === 600.1cm2cm