Skip to content

Commit

Permalink
Add colon tests
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed May 6, 2024
1 parent bd1c840 commit acf1a6f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
5 changes: 1 addition & 4 deletions src/atoms/IndexAtom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,7 @@ function Base.getindex(x::AbstractExpr, I::AbstractVector{Bool})
end

# All rows and columns
function Base.getindex(x::AbstractExpr, ::Colon, ::Colon)
rows, cols = size(x)
return getindex(x, 1:rows, 1:cols)
end
Base.getindex(x::AbstractExpr, ::Colon, ::Colon) = x

# All rows for this column(s)
function Base.getindex(x::AbstractExpr, ::Colon, col)
Expand Down
3 changes: 1 addition & 2 deletions src/atoms/VcatAtom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ function Base.getindex(x::VcatAtom, ::Colon, cols::AbstractVector{<:Real})
return x[1:size(x, 1), cols]

Check warning on line 107 in src/atoms/VcatAtom.jl

View check run for this annotation

Codecov / codecov/patch

src/atoms/VcatAtom.jl#L106-L107

Added lines #L106 - L107 were not covered by tests
end

# linear indexing
# very similar to row-indexing above, but with linear indices
# linear indexing: very similar to row-indexing above, but with linear indices
function Base.getindex(x::VcatAtom, inds::AbstractVector{<:Real})
idx = 0
inds = collect(inds)
Expand Down
12 changes: 8 additions & 4 deletions test/test_atoms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,8 @@ function test_IndexAtom()
_test_atom(target) do context
return Variable(2)[:, 1]
end
_test_atom(target) do context
return Variable(2)[:, :]
end
x = Variable(2)
@test x[:, :] === x
target = """
variables: x1, x2, x3
minobjective: [1.0 * x1, 1.0 * x3]
Expand Down Expand Up @@ -814,22 +813,26 @@ function test_VcatAtom()
return
end

function test_getindex_VcatAtom()
function test_VcatAtom_getindex()
x = Variable()
sq = square(x)
for v in [vcat(x, sq, -sq), vcat(vcat(x, sq), -sq)]
@test isequal(v[1], x)
@test isequal(v[2], sq)
@test isequal(v[:, 1], v[1:3, 1])
@test isequal(v[1:3, :], v[1:3, 1:1])
@test v[2:-1:1].children[1] isa Convex.VcatAtom
@test v[2:-1:1].children[1].children == (x, sq)
@test vexity(v[1]) isa Convex.AffineVexity
@test vexity(v[2]) isa Convex.ConvexVexity
@test vexity(v[3]) isa Convex.ConcaveVexity
@test vexity(v[1:2]) isa Convex.ConvexVexity
@test vexity(v[:, 1]) isa Convex.NotDcp
end
x = Variable(2, 2)
sq = square(x)
for v in [vcat(x, sq, -sq), vcat(vcat(x, sq), -sq)]
@test isequal(v[:, :], v)
@test isequal(v[1:2, 1:2], x)
@test v[1, 1:2] isa Convex.IndexAtom
@test v[1:3, 1:2] isa Convex.IndexAtom
Expand All @@ -843,6 +846,7 @@ function test_getindex_VcatAtom()
@test vexity(v[5:6, 1:2]) isa Convex.ConcaveVexity
@test vexity(v[1:3, 1]) isa Convex.ConvexVexity
end
return
end

### exp_+_sdp_cone/LogDetAtom
Expand Down

0 comments on commit acf1a6f

Please sign in to comment.