@@ -17,6 +17,7 @@ using NDTensors.GradedAxes:
1717 AbstractGradedUnitRange,
1818 GradedAxes,
1919 GradedUnitRangeDual,
20+ LabelledUnitRangeDual,
2021 OneToOne,
2122 blocklabels,
2223 blockmergesortperm,
@@ -27,7 +28,8 @@ using NDTensors.GradedAxes:
2728 gradedrange,
2829 isdual,
2930 nondual
30- using NDTensors. LabelledNumbers: LabelledInteger, label, labelled, labelled_isequal
31+ using NDTensors. LabelledNumbers:
32+ LabelledInteger, LabelledUnitRange, label, label_type, labelled, labelled_isequal, unlabel
3133using Test: @test , @test_broken , @testset
3234struct U1
3335 n:: Int
@@ -58,6 +60,92 @@ Base.isless(c1::U1, c2::U1) = c1.n < c2.n
5860 @test blockisequal (ad, a)
5961end
6062
63+ @testset " LabelledUnitRangeDual" begin
64+ la = labelled (1 : 2 , U1 (1 ))
65+ @test la isa LabelledUnitRange
66+ @test label (la) == U1 (1 )
67+ @test blocklabels (la) == [U1 (1 )]
68+ @test unlabel (la) == 1 : 2
69+ @test la == 1 : 2
70+ @test ! isdual (la)
71+ @test labelled_isequal (la, la)
72+ @test space_isequal (la, la)
73+ @test label_type (la) == U1
74+
75+ @test iterate (la) == (1 , 1 )
76+ @test iterate (la) == (1 , 1 )
77+ @test iterate (la, 1 ) == (2 , 2 )
78+ @test isnothing (iterate (la, 2 ))
79+
80+ lad = dual (la)
81+ @test lad isa LabelledUnitRangeDual
82+ @test label (lad) == U1 (- 1 )
83+ @test blocklabels (lad) == [U1 (- 1 )]
84+ @test unlabel (lad) == 1 : 2
85+ @test lad == 1 : 2
86+ @test labelled_isequal (lad, lad)
87+ @test space_isequal (lad, lad)
88+ @test ! labelled_isequal (la, lad)
89+ @test ! space_isequal (la, lad)
90+ @test isdual (lad)
91+ @test nondual (lad) === la
92+ @test dual (lad) === la
93+ @test label_type (lad) == U1
94+
95+ @test iterate (lad) == (1 , 1 )
96+ @test iterate (lad) == (1 , 1 )
97+ @test iterate (lad, 1 ) == (2 , 2 )
98+ @test isnothing (iterate (lad, 2 ))
99+
100+ lad2 = lad[1 : 1 ]
101+ @test lad2 isa LabelledUnitRangeDual
102+ @test label (lad2) == U1 (- 1 )
103+ @test unlabel (lad2) == 1 : 1
104+
105+ laf = flip (la)
106+ @test laf isa LabelledUnitRangeDual
107+ @test label (laf) == U1 (1 )
108+ @test unlabel (laf) == 1 : 2
109+ @test labelled_isequal (la, laf)
110+ @test ! space_isequal (la, laf)
111+
112+ ladf = flip (dual (la))
113+ @test ladf isa LabelledUnitRange
114+ @test label (ladf) == U1 (- 1 )
115+ @test unlabel (ladf) == 1 : 2
116+
117+ lafd = dual (flip (la))
118+ @test lafd isa LabelledUnitRange
119+ @test label (lafd) == U1 (- 1 )
120+ @test unlabel (lafd) == 1 : 2
121+
122+ # check default behavior for objects without dual
123+ la = labelled (1 : 2 , ' x' )
124+ lad = dual (la)
125+ @test lad isa LabelledUnitRangeDual
126+ @test label (lad) == ' x'
127+ @test blocklabels (lad) == [' x' ]
128+ @test unlabel (lad) == 1 : 2
129+ @test lad == 1 : 2
130+ @test labelled_isequal (lad, lad)
131+ @test space_isequal (lad, lad)
132+ @test labelled_isequal (la, lad)
133+ @test ! space_isequal (la, lad)
134+ @test isdual (lad)
135+ @test nondual (lad) === la
136+ @test dual (lad) === la
137+
138+ laf = flip (la)
139+ @test laf isa LabelledUnitRangeDual
140+ @test label (laf) == ' x'
141+ @test unlabel (laf) == 1 : 2
142+
143+ ladf = flip (lad)
144+ @test ladf isa LabelledUnitRange
145+ @test label (ladf) == ' x'
146+ @test unlabel (ladf) == 1 : 2
147+ end
148+
61149@testset " GradedUnitRangeDual" begin
62150 for a in
63151 [gradedrange ([U1 (0 ) => 2 , U1 (1 ) => 3 ]), gradedrange ([U1 (0 ) => 2 , U1 (1 ) => 3 ])[1 : 5 ]]
@@ -124,13 +212,21 @@ end
124212 @test blockmergesortperm (a) == [Block (1 ), Block (2 )]
125213 @test blockmergesortperm (ad) == [Block (1 ), Block (2 )]
126214
127- @test_broken isdual (ad[Block (1 )])
128- @test_broken isdual (ad[Block (1 )[1 : 1 ]])
215+ @test isdual (ad[Block (1 )])
216+ @test isdual (ad[Block (1 )[1 : 1 ]])
217+ @test ad[Block (1 )] isa LabelledUnitRangeDual
218+ @test ad[Block (1 )[1 : 1 ]] isa LabelledUnitRangeDual
219+ @test label (ad[Block (2 )]) == U1 (- 1 )
220+ @test label (ad[Block (2 )[1 : 1 ]]) == U1 (- 1 )
221+
129222 I = mortar ([Block (2 )[1 : 1 ]])
130223 g = ad[I]
131224 @test length (g) == 1
132225 @test label (first (g)) == U1 (- 1 )
133- @test_broken isdual (g[Block (1 )])
226+ @test isdual (g[Block (1 )])
227+
228+ @test isdual (axes (ad[[Block (1 )]], 1 )) # used in view(::BlockSparseVector, [Block(1)])
229+ @test isdual (axes (ad[mortar ([Block (1 )[1 : 1 ]])], 1 )) # used in view(::BlockSparseVector, [Block(1)[1:1]])
134230 end
135231end
136232
0 commit comments