summaryrefslogtreecommitdiffstats
path: root/spec/eina_hash_spec.rb
blob: 4d1526cc57b3d601cf08da4e3b83e104fa44c6d7 (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
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-
#
require 'efl'
require 'efl/eina_hash'
#
describe Efl::EinaHash do
    #
    before(:all) {
        REinaHash = Efl::EinaHash::REinaHash
        Efl::Eina.init.should == 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 == 0
    }
    #
    it "should append prepend and fetch" do
        h = REinaHash.new
        h.add 'k2', @d2
        h.add 'k1', @d1
        h['k3']=@d3
        h['k0']=@d0
        h['k0'].read_string.should == "D0"
        h['k1'].read_string.should == "D1"
        h['k2'].read_string.should == "D2"
        h['k3'].read_string.should == "D3"
        cpt=0
        h.each { |k,v|
            cpt+=1
            v.read_string.empty?.should be_false
        }
        cpt.should == 4
    end
    #
    it "should be able to convert into ruby Hash from NULL pointer" do
        h = REinaHash.new(FFI::Pointer::NULL).to_h
        h.empty?.should be_true
        h.is_a?(Hash).should be_true
    end
    #
    it "should be able to convert into ruby Hash from empty REinaHash" do
        h = REinaHash.new(FFI::Pointer::NULL).to_h
        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 REinaHash" do
        h = REinaHash.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 == "D0"
        h['k1'].read_string.should == "D1"
        h['k2'].read_string.should == "D2"
        h['k3'].read_string.should == "D3"
        cpt=0
        h.each { |k,v|
            cpt+=1
            v.read_string.empty?.should be_false
            true
        }
        cpt.should == 4
        rh = h.to_h
        rh.length.should == 4
        rh2 = {}
        rh.each { |k,v|
            rh2[k.read_string]=v.read_string
            true
        }
        rh2['k0'].should == 'D0'
        rh2['k1'].should == 'D1'
        rh2['k2'].should == 'D2'
        rh2['k3'].should == 'D3'
    end
    #
    it "should be able to convert into ruby Hash from non empty REinaHash pointer" do
        h = REinaHash.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 == "D0"
        h['k1'].read_string.should == "D1"
        h['k2'].read_string.should == "D2"
        h['k3'].read_string.should == "D3"
        rh = h.to_h
        rh.length.should == 4
    end
    #
    it "should be able to convert into ruby Hash from non empty REinaHash pointer, with key from string" do
        h = REinaHash.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 == "D0"
        h['k1'].read_string.should == "D1"
        h['k2'].read_string.should == "D2"
        h['k3'].read_string.should == "D3"
        rh =  h.to_h_conv
        rh.length.should == 4
        rh['k0'].read_string.should == "D0"
        rh['k1'].read_string.should == "D1"
        rh['k2'].read_string.should == "D2"
        rh['k3'].read_string.should == "D3"
    end
    #
    it "should be able to convert into ruby Hash from non empty REinaHash pointer, with key from string block" do
        h = REinaHash.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 == "D0"
        h['k1'].read_string.should == "D1"
        h['k2'].read_string.should == "D2"
        h['k3'].read_string.should == "D3"
        cpt=0
        rh =  h.to_h_conv { |k| cpt+=1; k.read_string }
        cpt.should == 4
        rh.length.should == 4
        rh['k0'].read_string.should == "D0"
        rh['k1'].read_string.should == "D1"
        rh['k2'].read_string.should == "D2"
        rh['k3'].read_string.should == "D3"
    end
    #
    it "should be able to build REinaHash 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 = REinaHash.new rh
        h[k0].read_string.should == "D0"
        h[k1].read_string.should == "D1"
        h[k2].read_string.should == "D2"
        h[k3].read_string.should == "D3"
    end
    #
    it "alternate constructor should work" do
        cstr_cnt = 0
        h = REinaHash.new { cstr_cnt+=1; Efl::EinaHash.eina_hash_string_superfast_new FFI::Pointer::NULL }
        cstr_cnt.should == 1
    end
end