File: cstruct_spec.rb

package info (click to toggle)
ruby-cstruct 1.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 412 kB
  • sloc: ruby: 1,008; makefile: 7
file content (310 lines) | stat: -rw-r--r-- 5,498 bytes parent folder | download | duplicates (2)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
require 'rspec'
require 'cstruct'

puts "Test CStruct-#{CStruct::VERSION}"

describe 'Normal Member -> Point' do
  subject do

    class Point < CStruct
      int32:x
      int32:y
    end

    point = Point.new
    point.x,point.y = -10,20
    point
  end

  describe '#x' do
    subject { super().x }
    it { is_expected.to eq(-10) }
  end

  describe '#y' do
    subject { super().y }
    it { is_expected.to eq(20) }
  end
end

describe 'Struct Member -> Line' do
  subject do
    class Point < CStruct
      int32:x
      int32:y
    end

    class Line < CStruct
      Point:begin_point
      Point:end_point
    end

    line = Line.new
    line.begin_point.x = 10
    line.begin_point.y = 20
    line
  end

  describe '#begin_point' do
    subject { super().begin_point }
    describe '#x' do
      subject { super().x }
      it { is_expected.to eq(10) }
    end
  end

  describe '#begin_point' do
    subject { super().begin_point }
    describe '#y' do
      subject { super().y }
      it { is_expected.to eq(20) }
    end
  end
end

describe 'Array Member -> Collection' do
  subject do

    class Collection < CStruct
      int32:elements,[8]
    end

    collection = Collection.new

    (0..7).each do |i|
      collection.elements[i] = i  # assign like as C language
    end

    collection
  end

  describe '#elements' do
    subject { super().elements }
    it { is_expected.to eq((0..7).to_a) }
  end
end

describe 'Array Struct Member -> PointArray' do
  it 'PointArray' do
    class Point < CStruct
      double:x
      double:y
    end

    class PointArray < CStruct
      Point:elements,[4]
    end

    array = PointArray.new
    (0..3).each do |i|
      array.elements[i].x = i+0.234
      array.elements[i].y = i+1.667
    end

    (0..3).each do |i|
      expect(array.elements[i].x).to eq(i+0.234)
      expect(array.elements[i].y).to eq(i+1.667)
    end
  end

end


describe 'Inner Struct Member-> T' do
  subject do
    class T < CStruct
      class Inner < CStruct
        int32 :v1
        int32 :v2
      end
      Inner :inner
    end

    t = T.new
    t.inner.v1 = 10
    t.inner.v2 = 20
    t
  end

  describe '#inner' do
    subject { super().inner }
    describe '#v1' do
      subject { super().v1 }
      it { is_expected.to eq(10) }
    end
  end

  describe '#inner' do
    subject { super().inner }
    describe '#v2' do
      subject { super().v2 }
      it { is_expected.to eq(20) }
    end
  end
end

describe 'Anonymous Struct Member -> Window' do
  subject do
    class Window < CStruct
      int32:style
      struct :position do
        int32:x
        int32:y
      end
    end
    window = Window.new
    window.style = 1
    window.position.x = 10
    window.position.y = 20
    window
  end

  describe '#style' do
    subject { super().style }
    it { is_expected.to eq(1)}
  end

  describe '#position' do
    subject { super().position }
    describe '#x' do
      subject { super().x }
      it { is_expected.to eq(10)}
    end
  end

  describe '#position' do
    subject { super().position }
    describe '#y' do
      subject { super().y }
      it { is_expected.to eq(20)}
    end
  end
end

describe 'Anonymous Union Member -> U' do
  subject do
    class U < CStruct
        union:value do
        int32:x
        int32:y
      end
    end

    u = U.new
    u.value.x = 10
    u
  end

  describe '#value' do
    subject { super().value }
    describe '#x' do
      subject { super().x }
      it { is_expected.to eq(10)}
    end
  end
end

describe 'Char Buffer Member -> Addition' do
  subject do
    class Addition < CStruct
      char :description,[32]
    end

    addition = Addition.new
    addition.description = 'Hello Ruby!'
    addition
  end

  describe '#description' do
    subject { super().description }
    describe '#to_cstr' do
      subject { super().to_cstr }
      it {is_expected.to eq('Hello Ruby!')}
    end
  end
end

=begin
describe 'Binary File IO ' do
  subject do

    class Point < CStruct
      int32:x
      int32:y
    end

    class PointF < CStruct
      double:x
      double:y
    end

    class Addition < CStruct
      char :description,[32]
    end

    class IOData < CStruct
      Point:point
      PointF:pointf
      Addition:addition
    end

    write_data = IOData.new
    # write file
    File.open("point.bin","wb")do |f|
        write_data.point   = Point.new {|st| st.x = 100; st.y =200 }
        write_data.point_f = PointF.new{|st| st.x = 20.65; st.y =70.86 }

        write_data.addition = Addition.new
        write_data.addition.description= "Hello Ruby!"

        f.write write_data.data
    end

    read_data = IOData.new

    #read file
    File.open("point.bin","rb")do |f|
      read_data   << f.read(IODtata.size)

      puts point.x
      puts point.y
      puts point_f.x
      puts point_f.y
      puts addition.description.to_cstr
    end
    read_data
  end

  its(:'point.x') {should == 200}
end
=end
describe 'Namespace ' do
 it 'NS1 NS2' do
    module NS1    #namespace
        class A < CStruct
            uint32:handle
        end

        module NS2
            class B < CStruct
                A:a # directly use A
            end
        end

        class C < CStruct
          A     :a
          NS2_B :b  # Meaning of the 'NS2_B' is NS2::B
        end
    end

    class D < CStruct
        NS1_NS2_B:b # Meaning of the 'NS1_NS2_B' is NS1::NS2::B
    end

    v = D.new
    v.b.a.handle = 120
    expect(v.b.a.handle).to eq(120)

  end
end