Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

add tests for GList, fix a few things related to GList #601

Merged
merged 2 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/GLib/GLib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ end

import Base: convert, copy, show, size, length, getindex, setindex!, get,
iterate, eltype, isempty, ndims, stride, strides, popfirst!,
empty!, append!, reverse!, pushfirst!, pop!, push!, splice!,
empty!, append!, reverse!, pushfirst!, pop!, push!, splice!, insert!,
sigatomic_begin, sigatomic_end, Sys.WORD_SIZE, unsafe_convert, getproperty,
getindex, setindex!

using Libdl

export GInterface, GType, GObject, GBoxed, @Gtype, @Gabstract, @Giface
export GEnum, GError, GValue, gvalue, make_gvalue, @make_gvalue, g_type
export GList, glist_iter, _GSList, _GList, gobject_ref, gobject_move_ref
export GList, GSList, glist_iter, _GSList, _GList, gobject_ref, gobject_move_ref
export signal_connect, signal_emit, signal_handler_disconnect
export signal_handler_block, signal_handler_unblock
export g_timeout_add, g_idle_add, @idle_add
Expand Down
10 changes: 3 additions & 7 deletions src/GLib/glist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ mutable struct GList{L <: _LList, T} <: AbstractVector{T}
end
end
GList(list::Type{T}) where {T} = GList(convert(Ptr{_GList{T}}, C_NULL), true)
GSList(list::Type{T}) where {T} = GList(convert(Ptr{_GSList{T}}, C_NULL), true)
GList(list::Ptr{L}, transfer_full::Bool = false) where {L <: _LList} = GList{L, eltype(L)}(list, transfer_full)

const LList{L <: _LList} = Union{Ptr{L}, GList{L}}
Expand Down Expand Up @@ -69,13 +70,8 @@ show(io::IO, ::MIME"text/plain", list::GList{L, T}) where {L, T} = show(io, list
show(io::IO, list::GList{L, T}) where {L, T} = print(io, "GList{$L => $T}(length = $(length(list)), transfer_full = $(list.transfer_full))")

unsafe_convert(::Type{Ptr{L}}, list::GList) where {L <: _LList} = list.handle
endof(list::LList) = length(list)
ndims(list::LList) = 1
strides(list::LList) = (1,)
stride(list::LList, k::Integer) = (k > 1 ? length(list) : 1)
size(list::LList) = (length(list),)
isempty(list::LList{L}) where {L} = (unsafe_convert(Ptr{L}, list) == C_NULL)
Base.IteratorSize(::Type{L}) where {L <: LList} = Base.HasLength()

popfirst!(list::GList) = splice!(list, nth_first(list))
pop!(list::GList) = splice!(list, nth_last(list))
Expand Down Expand Up @@ -253,7 +249,7 @@ deref_to(::Type{P}, x::Ptr) where {P <: Ptr} = x
empty!(li::Ptr{_LList{P}}) where {P <: Ptr} = nothing

### Store numbers directly inside the pointer bits (assuming convert(N, x) exists)
ref_to(::Type{N}, x) where {N <: Number} = x
ref_to(::Type{N}, x) where {N <: Number} = Ptr{N}(x)
deref_to(::Type{N}, x::Ptr) where {N <: Number} = x
empty!(li::Ptr{_LList{N}}) where {N <: Number} = nothing

Expand All @@ -270,7 +266,7 @@ function ref_to(::Type{S}, x) where S <: String
s = bytestring(x)
l = sizeof(s)
p = convert(Ptr{UInt8}, g_malloc(l + 1))
unsafe_copy!(p, convert(Ptr{UInt8}, pointer(s)), l)
unsafe_copyto!(p, convert(Ptr{UInt8}, pointer(s)), l)
unsafe_store!(p, '\0', l + 1)
return p
end
Expand Down
201 changes: 201 additions & 0 deletions test/glist.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
using Gtk.GLib
using Gtk
using Gtk.ShortNames
using Test

@testset "glist" begin

@testset "pointers" begin

w = Window("Window", 400, 300)
nb = Notebook()
w = push!(Window("Notebook"),nb)
l = ccall((:gtk_container_get_children,Gtk.libgtk),Ptr{Gtk._GList{Gtk.GtkWidget}},(Ptr{Gtk.GObject},),w)

@test eltype(l)==Gtk.GtkWidget

@test !isempty(l)
@test length(l)==1
@test l[1]==nb

for item in l
@test item==nb
end

end

@testset "string" begin

g = GList(String)

@test ndims(g)==1

@test isempty(g)

for i=1:10
push!(g,string(i))
end

@test !isempty(g)

@test g[1]==string(1)
@test g[2]==string(2)

@test length(g)==10
@test g[end]==string(10)

for (i,item) = enumerate(g)
@test string(i)==item
end

g2=copy(g)

for (i,item) = enumerate(g2)
@test string(i)==item
end

insert!(g,8,string(25))
@test length(g)==11
@test g[8]==string(25)
@test g[9]==string(8)

pushfirst!(g,string(24))
@test length(g)==12
@test g[1]==string(24)
@test g[9]==string(25)

g[8]="hello"
@test g[8]=="hello"
@test g[9]==string(25)

reverse!(g)
@test g[end]==string(24)

g[2]="test"
@test g[2]=="test"

empty!(g)
@test isempty(g)

# repeat above for GSList
g = GSList(String)

@test ndims(g)==1

@test isempty(g)

for i=1:10
push!(g,string(i))
end

@test !isempty(g)

@test g[1]==string(1)
@test g[2]==string(2)

@test length(g)==10
@test g[end]==string(10)

for (i,item) = enumerate(g)
@test string(i)==item
end

g2=copy(g)

for (i,item) = enumerate(g2)
@test string(i)==item
end

insert!(g,8,string(25))
@test length(g)==11
@test g[8]==string(25)
@test g[9]==string(8)

pushfirst!(g,string(24))
@test length(g)==12
@test g[1]==string(24)
@test g[9]==string(25)

g[8]="hello"
@test g[8]=="hello"
@test g[9]==string(25)

reverse!(g)
@test g[end]==string(24)

g[2]="test"
@test g[2]=="test"

empty!(g)
@test isempty(g)

end

@testset "numbers" begin

g = GList(Int64)

@test ndims(g)==1

@test isempty(g)

for i=1:10
push!(g,i)
end

@test !isempty(g)

@test g[1]==1
@test g[2]==2

@test length(g)==10
@test g[end]==10

for (i,item) = enumerate(g)
@test i==item
end

g2=copy(g)

for (i,item) = enumerate(g2)
@test i==item
end

empty!(g)
@test isempty(g)

# repeat above for SList
g = GSList(Int64)

@test ndims(g)==1

@test isempty(g)

for i=1:10
push!(g,i)
end

@test !isempty(g)

@test g[1]==1
@test g[2]==2

@test length(g)==10
@test g[end]==10

for (i,item) = enumerate(g)
@test i==item
end

g2=copy(g)

for (i,item) = enumerate(g2)
@test i==item
end

empty!(g)
@test isempty(g)

end

end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module GtkTestModule
using Test, Gtk

include("glib.jl")
include("glist.jl")
include("gui.jl")
include("list.jl")
include("misc.jl")
Expand Down