Skip to content

Commit 309c35b

Browse files
committed
Add more tests
1 parent feaa917 commit 309c35b

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

src/NarrowArray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ inner_size(arr::NarrowArray, i::Integer) = size(eltype(parent(arr)), i)
9292
inner_size(arr::NarrowArray{<:Any,N}) where N = ntuple(i -> inner_size(arr, i), Val(N))
9393

9494
Base.size(arr::NarrowArray, i::Integer) = size(parent(arr), i) * size(eltype(parent(arr)), i)
95-
Base.size(arr::NarrowArray{<:Any,N}) where N = ntuple(i -> size(arr, i), Val(N))
95+
Base.size(arr::NarrowArray) = size(parent(arr)) .* inner_size(arr)
9696

9797
Base.IndexStyle(::Type{<:NarrowArray}) = IndexCartesian()
9898
function Base.getindex(arr::NarrowArray{T,N}, i::Vararg{Int,N}) where {T,N}

test/runtests.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,23 @@ bits(x) = x
5656
@test v9 isa NVector{Bool,9}
5757
@test v9.data == (0x55, 0x01)
5858
@test Tuple(v9) == bools9
59+
@test v9[1] === true
60+
@test v9[9] === true
5961
@test reinterpret(UInt16, v9) == 0x0155
6062

63+
f6 = NVector((float6(0x01), float6(0x02), float6(0x03), float6(0x04)))
64+
@test f6 isa NVector{Float6_E3M2FN,4}
65+
@test f6.data == (0x81, 0x30, 0x10)
66+
@test bits.(Tuple(f6)) == (0x01, 0x02, 0x03, 0x04)
67+
@test bits(f6[1]) == 0x01
68+
@test bits(f6[2]) == 0x02
69+
@test bits(f6[3]) == 0x03
70+
@test bits(f6[4]) == 0x04
71+
@test_throws BoundsError f6[5]
72+
6173
m = NMatrix{Bool,2,4}(bools)
6274
@test m isa NMatrix{Bool,2,4}
75+
@test IndexStyle(typeof(m)) === IndexLinear()
6376
@test size(m) == (2, 4)
6477
@test Tuple(m) == bools
6578
@test m[1, 1] === true
@@ -145,7 +158,13 @@ bits(x) = x
145158
@test sprint(show, T3) == "@NarrowTuple{Float4_E2M1FN, UInt8, Float4_E2M1FN}"
146159
@test sprint(show, NarrowTuple) == "NarrowTuple"
147160
@test sprint(show, NarrowTuple{Tuple{UInt8,Bool}}) == "NarrowTuple{Tuple{UInt8, Bool}}"
161+
@test startswith(sprint(show, Union{typeof(nt_bool),T3}), "Union{")
148162
@test contains(sprint(show, methods(show, Tuple{IO,Type{<:NarrowTuple}})), "T<:NarrowTuple")
163+
@test length(typeof(nt_bool)) == 2
164+
@test firstindex(nt_bool) == 1
165+
@test lastindex(nt_bool) == 2
166+
@test convert(Tuple{UInt8,Bool}, nt_bool) == (0x00, true)
167+
@test NarrowTuple{Tuple{UInt8,Bool}}(0x00, true) == nt_bool
149168
@test @NarrowTuple(0x00, true) == nt_bool
150169
@test @NarrowTuple((0x00, true)) == nt_bool
151170

@@ -161,6 +180,8 @@ bits(x) = x
161180
@test BitPacking.bitwidth(BitPacking.OnePad{7}) == 7
162181
@test BitPacking.bitwidth(BitPacking.ZeroPad(7)) == 7
163182
@test BitPacking.bitwidth(BitPacking.OnePad(7)) == 7
183+
@test BitPacking.ZeroPad{7}() == BitPacking.ZeroPad(7)
184+
@test BitPacking.OnePad{7}() == BitPacking.OnePad(7)
164185
@test sprint(show, BitPacking.ZeroPad(7)) == "BitPacking.ZeroPad(7)"
165186
@test sprint(show, BitPacking.OnePad(7)) == "BitPacking.OnePad(7)"
166187

@@ -189,6 +210,8 @@ bits(x) = x
189210
nt_one = Tone(0x00, true)
190211
@test nt_one.data == 0xff00
191212
@test Tuple(nt_one) == (0x00, true, BitPacking.OnePad(7))
213+
@test NarrowTuple((0x00, true, BitPacking.OnePad(7))) == nt_one
214+
@test NarrowTuple{Tuple{UInt8,Bool,BitPacking.OnePad{7}}}(0x00, true, BitPacking.OnePad(7)) == nt_one
192215
@test_throws ArgumentError Tone(0x0100)
193216

194217
Tbytes = @NarrowTuple{Float4_E2M1FN,BitPacking.ZeroPad{5},UInt8}
@@ -256,6 +279,7 @@ bits(x) = x
256279
@test eltype(parent(f4x4_narrow)) <: NVector{Float4_E2M1FN,4}
257280
@test collect(reinterpret(UInt8, f4x4_narrow)) == UInt8[0x21, 0x43]
258281
@test bits.(copy(f4x4_narrow)) == bits.(f4_values)
282+
@test NarrowArray{Float4_E2M1FN,1,4}(f4x4_narrow) === f4x4_narrow
259283
@test NarrowVector{Float4_E2M1FN,4}(f4_narrow) == f4x4_narrow
260284

261285
f4_bits = reinterpret(Bool, f4_narrow)
@@ -269,6 +293,9 @@ bits(x) = x
269293
@test collect(reinterpret(UInt8, host_f4_bits)) == UInt8[0x21, 0x43]
270294

271295
f4_matrix = NarrowArray{Float4_E2M1FN}(reshape(f4_values, 2, 2))
296+
f4_matrix_explicit = NarrowArray{Float4_E2M1FN,2}(reshape(f4_values, 2, 2))
297+
@test f4_matrix_explicit == f4_matrix
298+
@test NarrowArray{Float4_E2M1FN,2}(f4_matrix) === f4_matrix
272299
f4_bytes = reinterpret(UInt8, f4_matrix)
273300
@test size(f4_bytes) == (1, 2)
274301
@test collect(f4_bytes) == UInt8[0x21 0x43]

0 commit comments

Comments
 (0)