-
Notifications
You must be signed in to change notification settings - Fork 112
/
runtests.jl
1807 lines (1652 loc) · 79 KB
/
runtests.jl
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
import Unitful: DimensionError, AffineError
import Unitful: LogScaled, LogInfo, Level, Gain, MixedUnits, Decibel
import Unitful: FreeUnits, ContextUnits, FixedUnits, AffineUnits, AffineQuantity
import Unitful:
nm, μm, mm, cm, m, km, inch, ft, mi,
ac,
mg, g, kg,
Ra, °F, °C, K,
rad, °,
ms, s, minute, hr, d, yr, Hz,
J, A, N, mol, V,
mW, W,
dB, dB_rp, dB_p, dBm, dBV, dBSPL, Decibel,
Np, Np_rp, Np_p, Neper
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.:(:)
@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 "Types" begin
@test Base.complex(Quantity{Float64,NoDims,NoUnits}) ==
Quantity{Complex{Float64},NoDims,NoUnits}
end
@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
# 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
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
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
@testset "Promotion" begin
@testset "> Unit preferences" begin
# 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))
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 "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
@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(2.0m .<= 3.0m)
@test @inferred(3.0m .<= 3.0m)
@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 Base.rtoldefault(typeof(1.0u"m")) === Base.rtoldefault(typeof(1.0))
@test Base.rtoldefault(typeof(1u"m")) === Base.rtoldefault(Int)
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_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 typeof(one(eltype([1.0s, 1kg]))) <: Float64 # issue 159, multiplicative identity
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 @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 @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 inferrability of integer literal powers
_pow_m3(x) = x^-3
_pow_0(x) = x^0
_pow_3(x) = x^3
_pow_2_3(x) = x^(2//3)
@test_throws ErrorException @inferred(_pow_2_3(m))
@test_throws ErrorException @inferred(_pow_2_3(𝐋))
@test_throws ErrorException @inferred(_pow_2_3(1.0m))
@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(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(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°
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 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(prevfloat(0.0m)) == m
# 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)
# 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
@test @inferred(fma(2.0, 3.0fm, 1.0fm)) === 7.0fm
@test @inferred(fma(2.0, 3.0m, 35mm)) === 6.035m # llvm good
@test @inferred(fma(2.0, 3.0m2cm, 35mm2cm)) === 603.5cm2cm
@test_throws ErrorException fma(2.0, 3.0fm, 35fmm) #automatic conversion prohibited
@test @inferred(fma(2.0m, 3.0, 35mm)) === 6.035m # llvm good
@test @inferred(fma(2.0m2cm, 3.0, 35mm2cm)) === 603.5cm2cm
@test @inferred(fma(2.0m, 1.0/m, 3.0)) === 5.0 # llvm good
@test @inferred(fma(2.0m2cm, 1.0/m2mm, 3.0)) === 5.0
@test @inferred(fma(2.0cm, 1.0/s, 3.0mm/s)) === .023m/s # llvm good
@test @inferred(fma(2.0cm2cm, 1.0/s, 3.0mm/s)) === 2.3cm2cm/s
@test @inferred(fma(2m, 1/s, 3m/s)) === 5m/s # llvm good
@test @inferred(fma(2, 1.0μm/m, 1)) === 1.000002 # llvm good
@test @inferred(fma(1.0mm/m, 1.0mm/m, 1.0mm/m)) === 0.001001 # llvm good
@test @inferred(fma(1.0mm/m, 1.0, 1.0)) ≈ 1.001 # llvm good
@test @inferred(fma(1.0, 1.0μm/m, 1.0μm/m)) === 2.0μm/m # llvm good
@test @inferred(fma(2, 1.0, 1μm/m)) === 2.000001 # llvm BAD
@test @inferred(fma(2, 1μm/m, 1mm/m)) === 501//500000 # llvm BAD
@test @inferred(muladd(2.0, 3.0m, 1.0m)) === 7.0m
@test @inferred(muladd(2.0, 3.0m, 35mm)) === 6.035m
@test @inferred(muladd(2.0m, 3.0, 35mm)) === 6.035m
@test @inferred(muladd(2.0m, 1.0/m, 3.0)) === 5.0
@test @inferred(muladd(2.0cm, 1.0/s, 3.0mm/s)) === .023m/s
@test @inferred(muladd(2m, 1/s, 3m/s)) === 5m/s
@test @inferred(muladd(2, 1.0μm/m, 1)) === 1.000002
@test @inferred(muladd(1.0mm/m, 1.0mm/m, 1.0mm/m)) === 0.001001
@test @inferred(muladd(1.0mm/m, 1.0, 1.0)) ≈ 1.001
@test @inferred(muladd(1.0, 1.0μm/m, 1.0μm/m)) === 2.0μm/m
@test @inferred(muladd(2, 1.0, 1μm/m)) === 2.000001
@test @inferred(muladd(2, 1μm/m, 1mm/m)) === 501//500000
@test_throws DimensionError fma(2m, 1/m, 1m)
@test_throws DimensionError fma(2, 1m, 1V)
@test muladd(1s, 1.0mol/s, 2.0mol) === 3.0mol # issue 138
end
@testset "> @fastmath" begin
one32 = one(Float32)*m
eps32 = eps(Float32)*m
eps32_2 = eps32/2
# Note: Cannot use local functions since these are not yet optimized
fm_ieee_32(x) = x + eps32_2 + eps32_2
fm_fast_32(x) = @fastmath x + eps32_2 + eps32_2
@test fm_ieee_32(one32) == one32
@test (fm_fast_32(one32) == one32 ||
fm_fast_32(one32) == one32 + eps32 > one32)
one64 = one(Float64)*m
eps64 = eps(Float64)*m
eps64_2 = eps64/2
# Note: Cannot use local functions since these are not yet optimized
fm_ieee_64(x) = x + eps64_2 + eps64_2
fm_fast_64(x) = @fastmath x + eps64_2 + eps64_2
@test fm_ieee_64(one64) == one64
@test (fm_fast_64(one64) == one64 ||
fm_fast_64(one64) == one64 + eps64 > one64)
# check updating operators
fm_ieee_64_upd(x) = (r=x; r+=eps64_2; r+=eps64_2)
fm_fast_64_upd(x) = @fastmath (r=x; r+=eps64_2; r+=eps64_2)
@test fm_ieee_64_upd(one64) == one64
@test (fm_fast_64_upd(one64) == one64 ||
fm_fast_64_upd(one64) == one64 + eps64 > one64)
for T in (Float32, Float64, BigFloat)
_zero = convert(T, 0)*m
_one = convert(T, 1)*m + eps(T)*m
_two = convert(T, 2)*m + 1m//10
_three = convert(T, 3)*m + 1m//100
@test isapprox((@fastmath +_two), +_two)
@test isapprox((@fastmath -_two), -_two)
@test isapprox((@fastmath _zero+_one+_two), _zero+_one+_two)
@test isapprox((@fastmath _zero-_one-_two), _zero-_one-_two)
@test isapprox((@fastmath _one*_two*_three), _one*_two*_three)
@test isapprox((@fastmath _one/_two/_three), _one/_two/_three)
@test isapprox((@fastmath rem(_two, _three)), rem(_two, _three))
@test isapprox((@fastmath mod(_two, _three)), mod(_two, _three))
@test (@fastmath cmp(_two, _two)) == cmp(_two, _two)
@test (@fastmath cmp(_two, _three)) == cmp(_two, _three)
@test (@fastmath cmp(_three, _two)) == cmp(_three, _two)
@test (@fastmath _one/_zero) == convert(T, Inf)
@test (@fastmath -_one/_zero) == -convert(T, Inf)
@test isnan(@fastmath _zero/_zero) # must not throw
for x in (_zero, _two, convert(T, Inf)*m, convert(T, NaN)*m)
@test (@fastmath isfinite(x))
@test !(@fastmath isinf(x))
@test !(@fastmath isnan(x))
@test !(@fastmath issubnormal(x))
end
end
for T in (Complex{Float32}, Complex{Float64}, Complex{BigFloat})
_zero = convert(T, 0)*m
_one = convert(T, 1)*m + im*eps(real(convert(T,1)))*m
_two = convert(T, 2)*m + im*m//10
_three = convert(T, 3)*m + im*m//100
@test isapprox((@fastmath +_two), +_two)
@test isapprox((@fastmath -_two), -_two)
@test isapprox((@fastmath _zero+_one+_two), _zero+_one+_two)
@test isapprox((@fastmath _zero-_one-_two), _zero-_one-_two)
@test isapprox((@fastmath _one*_two*_three), _one*_two*_three)
@test isapprox((@fastmath _one/_two/_three), _one/_two/_three)
@test (@fastmath _three == _two) == (_three == _two)
@test (@fastmath _three != _two) == (_three != _two)
@test isnan(@fastmath _one/_zero) # must not throw
@test isnan(@fastmath -_one/_zero) # must not throw
@test isnan(@fastmath _zero/_zero) # must not throw
for x in (_zero, _two, convert(T, Inf)*m, convert(T, NaN)*m)
@test (@fastmath isfinite(x))
@test !(@fastmath isinf(x))
@test !(@fastmath isnan(x))
@test !(@fastmath issubnormal(x))
end
end
# real arithmetic
for T in (Float32, Float64, BigFloat)
half = 1m/convert(T, 2)
third = 1m/convert(T, 3)
for f in (:+, :-, :abs, :abs2, :conj, :inv, :sign, :sqrt)
@test isapprox((@eval @fastmath $f($half)), (@eval $f($half)))
@test isapprox((@eval @fastmath $f($third)), (@eval $f($third)))
end
for f in (:+, :-, :*, :/, :%, :(==), :!=, :<, :<=, :>, :>=,
:atan, :hypot, :max, :min)
@test isapprox((@eval @fastmath $f($half, $third)),
(@eval $f($half, $third)))
@test isapprox((@eval @fastmath $f($third, $half)),
(@eval $f($third, $half)))
end
for f in (:minmax,)
@test isapprox((@eval @fastmath $f($half, $third))[1],
(@eval $f($half, $third))[1])
@test isapprox((@eval @fastmath $f($half, $third))[2],
(@eval $f($half, $third))[2])
@test isapprox((@eval @fastmath $f($third, $half))[1],
(@eval $f($third, $half))[1])
@test isapprox((@eval @fastmath $f($third, $half))[2],
(@eval $f($third, $half))[2])
end
half = 1°/convert(T, 2)
third = 1°/convert(T, 3)
for f in (:cos, :sin, :tan)
@test isapprox((@eval @fastmath $f($half)), (@eval $f($half)))
@test isapprox((@eval @fastmath $f($third)), (@eval $f($third)))
end
end
# complex arithmetic
for T in (Complex{Float32}, Complex{Float64}, Complex{BigFloat})
half = (1+1im)V/T(2)
third = (1-1im)V/T(3)
# some of these functions promote their result to double
# precision, but we want to check equality at precision T
rtol = Base.rtoldefault(real(T))
for f in (:+, :-, :abs, :abs2, :conj, :inv, :sign, :sqrt)
@test isapprox((@eval @fastmath $f($half)), (@eval $f($half)), rtol=rtol)
@test isapprox((@eval @fastmath $f($third)), (@eval $f($third)), rtol=rtol)
end
for f in (:+, :-, :*, :/, :(==), :!=)
@test isapprox((@eval @fastmath $f($half, $third)),
(@eval $f($half, $third)), rtol=rtol)
@test isapprox((@eval @fastmath $f($third, $half)),
(@eval $f($third, $half)), rtol=rtol)
end
_d = 90°/T(2)
@test isapprox((@fastmath cis(_d)), cis(_d))
end
# mixed real/complex arithmetic
for T in (Float32, Float64, BigFloat)
CT = Complex{T}
half = 1V/T(2)
third = 1V/T(3)
chalf = (1+1im)V/CT(2)
cthird = (1-1im)V/CT(3)
for f in (:+, :-, :*, :/, :(==), :!=)
@test isapprox((@eval @fastmath $f($chalf, $third)),
(@eval $f($chalf, $third)))
@test isapprox((@eval @fastmath $f($half, $cthird)),
(@eval $f($half, $cthird)))
@test isapprox((@eval @fastmath $f($cthird, $half)),
(@eval $f($cthird, $half)))
@test isapprox((@eval @fastmath $f($third, $chalf)),
(@eval $f($third, $chalf)))
end
@test isapprox((@fastmath third^3), third^3)
@test isapprox((@fastmath chalf/third), chalf/third)
@test isapprox((@fastmath chalf^3), chalf^3)
end
end
end
@testset "Rounding" begin
@test_throws ErrorException floor(3.7m)
@test_throws ErrorException ceil(3.7m)
@test_throws ErrorException trunc(3.7m)
@test_throws ErrorException round(3.7m)
@test_throws ErrorException floor(Integer, 3.7m)
@test_throws ErrorException ceil(Integer, 3.7m)
@test_throws ErrorException trunc(Integer, 3.7m)
@test_throws ErrorException round(Integer, 3.7m)
@test_throws ErrorException floor(Int, 3.7m)
@test_throws ErrorException ceil(Int, 3.7m)
@test_throws ErrorException trunc(Int, 3.7m)
@test_throws ErrorException round(Int, 3.7m)
@test floor(1.0314m/mm) === 1031.0
@test floor(1.0314m/mm; digits=1) === 1031.4
@test ceil(1.0314m/mm) === 1032.0
@test ceil(1.0314m/mm; digits=1) === 1031.4
@test trunc(-1.0314m/mm) === -1031.0
@test trunc(-1.0314m/mm; digits=1) === -1031.4
@test round(1.0314m/mm) === 1031.0
@test round(1.0314m/mm; digits=1) === 1031.4
@test floor(Integer, 1.0314m/mm) === Integer(1031.0)
@test ceil(Integer, 1.0314m/mm) === Integer(1032.0)
@test trunc(Integer, -1.0314m/mm) === Integer(-1031.0)
@test round(Integer, 1.0314m/mm) === Integer(1031.0)
@test floor(Int16, 1.0314m/mm) === Int16(1031.0)
@test ceil(Int16, 1.0314m/mm) === Int16(1032.0)
@test trunc(Int16, -1.0314m/mm) === Int16(-1031.0)
@test round(Int16, 1.0314m/mm) === Int16(1031.0)
@test floor(typeof(1mm), 1.0314m) === 1031mm
@test floor(typeof(1.0mm), 1.0314m) === 1031.0mm
@test floor(typeof(1.0mm), 1.0314m; digits=1) === 1031.4mm
@test ceil(typeof(1mm), 1.0314m) === 1032mm
@test ceil(typeof(1.0mm), 1.0314m) === 1032.0mm
@test ceil(typeof(1.0mm), 1.0314m; digits=1) === 1031.4mm
@test trunc(typeof(1mm), -1.0314m) === -1031mm
@test trunc(typeof(1.0mm), -1.0314m) === -1031.0mm
@test trunc(typeof(1.0mm), -1.0314m; digits=1) === -1031.4mm
@test round(typeof(1mm), 1.0314m) === 1031mm
@test round(typeof(1.0mm), 1.0314m) === 1031.0mm
@test round(typeof(1.0mm), 1.0314m; digits=1) === 1031.4mm
@test round(u"inch", 1.0314m) === 41.0u"inch"
@test round(Int, u"inch", 1.0314m) === 41u"inch"
@test round(typeof(1m), 137cm) === 1m
@test round(137cm/m) === 1//1
@test round(u"m", -125u"cm", sigdigits=2) === -1.2u"m"