@@ -198,12 +198,6 @@ Base.round(::Type{T}, x::SimpleRatio) where {T} = round(T, x.num // x.den)
198
198
199
199
end
200
200
201
- @testset " Ranges" begin
202
- x = [xi for xi in 0.0 u " km/s" :0.1 u " km/s" :1.0 u " km/s" ]
203
- @test x[2 ] == 0.1 u " km/s"
204
- @test x[end ] == 1.0 u " km/s"
205
- end
206
-
207
201
@testset " Complex numbers" begin
208
202
x = (0.5 + 0.6im ) * u " km/s"
209
203
@test string (x) == " (500.0 + 600.0im) m s⁻¹"
@@ -358,6 +352,109 @@ end
358
352
@test ustrip (x' ) == ustrip (x)'
359
353
end
360
354
355
+ @testset " Ranges" begin
356
+ @testset " Ranges from units" begin
357
+ x = [xi for xi in 0.0 u " km/s" :0.1 u " km/s" :1.0 u " km/s" ]
358
+ @test x[2 ] == 0.1 u " km/s"
359
+ @test x[end ] == 1.0 u " km/s"
360
+
361
+ # https://github.com/JuliaLang/julia/issues/56610
362
+ c = collect (1 u " inch" :0.25 u " inch" :4 u " inch" )
363
+ @test c[1 ] == 1 u " inch"
364
+ @test c[end ] <= 4 u " inch"
365
+
366
+ # Test dimensionless quantities
367
+ x = collect (1 u " 1" :2 u " 1" :5 u " 1" )
368
+ @test x == [1 , 3 , 5 ] .* u " 1"
369
+ @test eltype (x) <: Quantity
370
+
371
+ # Test error for missing step
372
+ @test_throws " must specify a step" 1 u " km" :2 u " km"
373
+ @test_throws " must specify a step" 1 us " km" :2 us " km"
374
+
375
+ # However, for backwards compatibility, dimensionless ranges are allowed:
376
+ x = collect (1 u " 1" :5 u " 1" )
377
+ @test x == [1 , 2 , 3 , 4 , 5 ]
378
+ @test eltype (x) <: Quantity{Float64}
379
+
380
+ # Test errors for incompatible units
381
+ @test_throws DimensionError 1 u " km" :1 u " s" :5 u " km"
382
+ @test_throws DimensionError 1 u " km" :1 u " m" :5 u " s"
383
+ @test_throws DimensionError 1 u " km" :1 u " km/s" :5 u " km"
384
+
385
+ # Same for symbolic units!
386
+ @test_throws DimensionError 1 us " km" :1 us " m" :5 us " inch"
387
+ @test length (1 u " inch" :1 u " m" :5 u " km" ) == 5000
388
+
389
+ # Test with symbolic units
390
+ x = collect (1 us " inch" :0.25 us " inch" :4 us " inch" )
391
+ @test x[1 ] == 1 us " inch"
392
+ @test x[2 ] == 1.25 us " inch"
393
+ @test x[end ] == 4 us " inch"
394
+ end
395
+
396
+ @testset " Multiplying ranges with units" begin
397
+ # Test multiplying ranges with units
398
+ x = (1 : 0.25 : 4 )u " inch"
399
+ @test x isa StepRangeLen
400
+ @test first (x) == 1 u " inch"
401
+ @test x[2 ] == 1.25 u " inch"
402
+ @test last (x) == 4 u " inch"
403
+ @test length (x) == 13
404
+
405
+ # Integer range (but real-valued unit)
406
+ x = (1 : 4 )u " inch"
407
+ @test x isa StepRangeLen
408
+ @test first (x) == 1 u " inch"
409
+ @test x[2 ] == 2 u " inch"
410
+ @test last (x) == 4 u " inch"
411
+ @test length (x) == 4
412
+ @test collect (x)[3 ] == 3 u " inch"
413
+
414
+ # Test with floating point range
415
+ x = (1.0 : 0.5 : 3.0 )u " m"
416
+ @test x isa StepRangeLen
417
+ @test first (x) == 1.0 u " m"
418
+ @test x[2 ] == 1.5 u " m"
419
+ @test last (x) == 3.0 u " m"
420
+ @test length (x) == 5
421
+ @test collect (x)[3 ] == 2.0 u " m"
422
+
423
+ x = (0 : 0.1 : 1 )u " m"
424
+ @test length (x) == 11
425
+ @test collect (x)[3 ] == 0.2 u " m"
426
+
427
+ # Test with symbolic units
428
+ x = (1 : 0.25 : 4 )us " inch"
429
+ @test x isa StepRangeLen{<: Quantity{Float64,<:SymbolicDimensions} }
430
+ @test first (x) == us " inch"
431
+ @test x[2 ] == 1.25 us " inch"
432
+ @test last (x) == 4 us " inch"
433
+ @test length (x) == 13
434
+
435
+ # Test that symbolic units preserve their symbolic nature
436
+ x = (0 : 0.1 : 1 )us " km/h"
437
+ @test x isa AbstractRange
438
+ @test first (x) == 0 us " km/h"
439
+ @test x[2 ] == 0.1 us " km/h"
440
+ @test last (x) == 1 us " km/h"
441
+ @test length (x) == 11
442
+
443
+ # Similarly, integers should stay integers:
444
+ x = (1 : 4 )us " inch"
445
+ @test_skip x isa StepRangeLen{<: Quantity{Int64,<:SymbolicDimensions} }
446
+ @test first (x) == us " inch"
447
+ @test x[2 ] == 2 us " inch"
448
+ @test last (x) == 4 us " inch"
449
+ @test length (x) == 4
450
+
451
+ # With RealQuantity:
452
+ @test_skip (1.0 : 4.0 ) * RealQuantity (u " inch" ) isa StepRangeLen{<: RealQuantity{Float64,<:SymbolicDimensions} }
453
+ # TODO : This is not available as TwicePrecision interacts with Real in a way
454
+ # that demands many other functions to be defined.
455
+ end
456
+ end
457
+
361
458
@testset " Alternate dimension construction" begin
362
459
z = Quantity (- 52 , length= 1 ) * Dimensions (mass= 2 )
363
460
z2 = Dimensions (mass= 2 ) * Quantity (- 52 , length= 1 )
0 commit comments