summaryrefslogtreecommitdiffstats
path: root/spec/eina/eina_hash_spec.rb
blob: 3cd55040e305c239213a72b8977665fcbb7e7ec9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-
#
require 'efl/eina/eina_hash'
#
describe Efl::Eina::EinaHash do
    #
    before(:all) {
        EinaHash = Efl::Eina::EinaHash
        Efl::Eina.init.should eql 1
        @d0 = ::FFI::MemoryPointer.from_string "D0"
        @d1 = ::FFI::MemoryPointer.from_string "D1"
        @d2 = ::FFI::MemoryPointer.from_string "D2"
        @d3 = ::FFI::MemoryPointer.from_string "D3"
    }
    after(:all) {
        Efl::Eina.shutdown.should eql 0
    }
    #
    it "should append prepend and fetch" do
        h = EinaHash.new
        h.add 'k2', @d2
        h.add 'k1', @d1
        h['k3']=@d3
        h['k0']=@d0
        h['k0'].read_string.should eql "D0"
        h['k1'].read_string.should eql "D1"
        h['k2'].read_string.should eql "D2"
        h['k3'].read_string.should eql "D3"
        cpt=0
        h.each { |k,v|
            cpt+=1
            v.read_string.empty?.should be_false
        }
        cpt.should eql 4
        h.free
    end
    #
    it "should be able to convert into ruby Hash from NULL pointer" do
        h = Hash.from_eina_hash ::FFI::Pointer::NULL
        h.empty?.should be_true
        h.is_a?(Hash).should be_true
    end
    #
    it "should be able to convert into ruby Hash from empty EinaHash" do
        h = Hash.from_eina_hash EinaHash.new
        h.empty?.should be_true
        h.is_a?(Hash).should be_true
    end
    #
    it "should be able to convert into ruby Hash from empty EinaHash pointer" do
        h = Hash.from_eina_hash EinaHash.new.ptr
        h.empty?.should be_true
        h.is_a?(Hash).should be_true
    end
    #
    it "should be able to convert into ruby Hash from non empty EinaHash" do
        h = EinaHash.new
        d0 = ::FFI::MemoryPointer.from_string "D0"
        d1 = ::FFI::MemoryPointer.from_string "D1"
        d2 = ::FFI::MemoryPointer.from_string "D2"
        d3 = ::FFI::MemoryPointer.from_string "D3"
        h.add 'k2', d2
        h.add 'k1', d1
        h['k3']=d3
        h["k0"]=d0
        h["k0"].read_string.should eql "D0"
        h['k1'].read_string.should eql "D1"
        h['k2'].read_string.should eql "D2"
        h['k3'].read_string.should eql "D3"
        cpt=0
        h.each { |k,v|
            cpt+=1
            v.read_string.empty?.should be_false
            true
        }
        cpt.should eql 4
        rh =  Hash.from_eina_hash h
        rh.length.should eql 4
        rh2 = {}
        rh.each { |k,v|
            rh2[k.read_string]=v.read_string
            true
        }
        rh2['k0'].should eql 'D0'
        rh2['k1'].should eql 'D1'
        rh2['k2'].should eql 'D2'
        rh2['k3'].should eql 'D3'
        h.free
    end
    #
    it "should be able to convert into ruby Hash from non empty EinaHash pointer" do
        h = EinaHash.new
        d0 = ::FFI::MemoryPointer.from_string "D0"
        d1 = ::FFI::MemoryPointer.from_string "D1"
        d2 = ::FFI::MemoryPointer.from_string "D2"
        d3 = ::FFI::MemoryPointer.from_string "D3"
        h.add 'k2', d2
        h.add 'k1', d1
        h['k3']=d3
        h['k0']=d0
        h['k0'].read_string.should eql "D0"
        h['k1'].read_string.should eql "D1"
        h['k2'].read_string.should eql "D2"
        h['k3'].read_string.should eql "D3"
        rh =  Hash.from_eina_hash h.ptr
        rh.length.should eql 4
        h.free
    end
    #
    it "should be able to convert into ruby Hash from non empty EinaHash pointer, with key from string" do
        h = EinaHash.new
        d0 = ::FFI::MemoryPointer.from_string "D0"
        d1 = ::FFI::MemoryPointer.from_string "D1"
        d2 = ::FFI::MemoryPointer.from_string "D2"
        d3 = ::FFI::MemoryPointer.from_string "D3"
        h.add 'k2', d2
        h.add 'k1', d1
        h['k3']=d3
        h['k0']=d0
        h['k0'].read_string.should eql "D0"
        h['k1'].read_string.should eql "D1"
        h['k2'].read_string.should eql "D2"
        h['k3'].read_string.should eql "D3"
        rh =  h.to_h_conv
        rh.length.should eql 4
        rh['k0'].read_string.should eql "D0"
        rh['k1'].read_string.should eql "D1"
        rh['k2'].read_string.should eql "D2"
        rh['k3'].read_string.should eql "D3"
        h.free
    end
    #
    it "should be able to convert into ruby Hash from non empty EinaHash pointer, with key from string block" do
        h = EinaHash.new
        d0 = ::FFI::MemoryPointer.from_string "D0"
        d1 = ::FFI::MemoryPointer.from_string "D1"
        d2 = ::FFI::MemoryPointer.from_string "D2"
        d3 = ::FFI::MemoryPointer.from_string "D3"
        h.add 'k2', d2
        h.add 'k1', d1
        h['k3']=d3
        h['k0']=d0
        h['k0'].read_string.should eql "D0"
        h['k1'].read_string.should eql "D1"
        h['k2'].read_string.should eql "D2"
        h['k3'].read_string.should eql "D3"
        cpt=0
        rh =  h.to_h_conv { |k| cpt+=1; k.read_string }
        cpt.should eql 4
        rh.length.should eql 4
        rh['k0'].read_string.should eql "D0"
        rh['k1'].read_string.should eql "D1"
        rh['k2'].read_string.should eql "D2"
        rh['k3'].read_string.should eql "D3"
        h.free
    end
    #
    it "should be able to build from ruby Hash" do
        rh = {}
        k0 = ::FFI::MemoryPointer.from_string "0"
        k1 = ::FFI::MemoryPointer.from_string "1"
        k2 = ::FFI::MemoryPointer.from_string "2"
        k3 = ::FFI::MemoryPointer.from_string "3"
        d0 = ::FFI::MemoryPointer.from_string "D0"
        d1 = ::FFI::MemoryPointer.from_string "D1"
        d2 = ::FFI::MemoryPointer.from_string "D2"
        d3 = ::FFI::MemoryPointer.from_string "D3"
        rh[k0]=d0
        rh[k1]=d1
        rh[k2]=d2
        rh[k3]=d3
        h = EinaHash.new rh
        h[k0].read_string.should eql "D0"
        h[k1].read_string.should eql "D1"
        h[k2].read_string.should eql "D2"
        h[k3].read_string.should eql "D3"
        h.free
    end
    #
    it "alternate constructor should work" do
        cstr_cnt = 0
        h = EinaHash.new { cstr_cnt+=1; Efl::FFI.eina_hash_string_superfast_new ::FFI::Pointer::NULL }
        cstr_cnt.should eql 1
    end
end