File: byte_format_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (280 lines) | stat: -rw-r--r-- 8,449 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
require "spec"

private def assert_bytes(io, *bytes)
  io.rewind
  bytes.each do |byte|
    io.read_byte.should eq(byte)
  end
  io.read_byte.should be_nil
end

private def assert_bytes_reversed(io, *bytes)
  assert_bytes io, *bytes.reverse
end

private def new_string_io(*bytes)
  io = IO::Memory.new
  bytes.each { |byte| io.write_byte byte.to_u8 }
  io.rewind
  io
end

describe IO::ByteFormat do
  describe "little endian" do
    describe "encode" do
      describe "to io" do
        it "writes int8" do
          io = IO::Memory.new
          io.write_bytes 0x12_i8, IO::ByteFormat::LittleEndian
          assert_bytes io, 0x12
        end

        it "writes int16" do
          io = IO::Memory.new
          io.write_bytes 0x1234_i16, IO::ByteFormat::LittleEndian
          assert_bytes io, 0x34, 0x12
        end

        it "writes uint16" do
          io = IO::Memory.new
          io.write_bytes 0x1234_u16, IO::ByteFormat::LittleEndian
          assert_bytes io, 0x34, 0x12
        end

        it "writes int32" do
          io = IO::Memory.new
          io.write_bytes 0x12345678_i32, IO::ByteFormat::LittleEndian
          assert_bytes io, 0x78, 0x56, 0x34, 0x12
        end

        it "writes int64" do
          io = IO::Memory.new
          io.write_bytes 0x123456789ABCDEF0_i64, IO::ByteFormat::LittleEndian
          assert_bytes io, 0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12
        end

        it "writes float32" do
          io = IO::Memory.new
          io.write_bytes 1.234_f32, IO::ByteFormat::LittleEndian
          assert_bytes io, 0xB6, 0xF3, 0x9D, 0x3F
        end

        it "writes float64" do
          io = IO::Memory.new
          io.write_bytes 1.234, IO::ByteFormat::LittleEndian
          assert_bytes io, 0x58, 0x39, 0xB4, 0xC8, 0x76, 0xBE, 0xF3, 0x3F
        end
      end

      describe "to slice" do
        it "writes int8" do
          bytes = Bytes[0]
          IO::ByteFormat::LittleEndian.encode(0x12_i8, bytes)
          bytes.should eq(Bytes[0x12_i8])
        end

        it "writes int16" do
          bytes = Bytes[0, 0]
          IO::ByteFormat::LittleEndian.encode(0x1234_i16, bytes)
          bytes.should eq(Bytes[0x34, 0x12])
        end

        it "writes int16 to larger slice" do
          bytes = Bytes[0, 0, 0, 0]
          IO::ByteFormat::LittleEndian.encode(0x1234_i16, bytes)
          bytes.should eq(Bytes[0x34, 0x12, 0, 0])
        end
      end
    end

    describe "decode" do
      describe "from io" do
        it "reads int8" do
          io = new_string_io(0x12)
          int = io.read_bytes Int8, IO::ByteFormat::LittleEndian
          int.should eq(0x12_i8)
        end

        it "reads int16" do
          io = new_string_io(0x34, 0x12)
          int = io.read_bytes Int16, IO::ByteFormat::LittleEndian
          int.should eq(0x1234_i16)
        end

        it "reads unt16" do
          io = new_string_io(0x34, 0x12)
          int = io.read_bytes UInt16, IO::ByteFormat::LittleEndian
          int.should eq(0x1234_u16)
        end

        it "reads int32" do
          io = new_string_io(0x78, 0x56, 0x34, 0x12)
          int = io.read_bytes Int32, IO::ByteFormat::LittleEndian
          int.should eq(0x12345678_i32)
        end

        it "reads int64" do
          io = new_string_io(0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12)
          int = io.read_bytes Int64, IO::ByteFormat::LittleEndian
          int.should eq(0x123456789ABCDEF0_i64)
        end

        it "reads float32" do
          io = new_string_io(0xB6, 0xF3, 0x9D, 0x3F)
          float = io.read_bytes Float32, IO::ByteFormat::LittleEndian
          float.should be_close(1.234, 0.0001)
        end

        it "reads float64" do
          io = new_string_io(0x58, 0x39, 0xB4, 0xC8, 0x76, 0xBE, 0xF3, 0x3F)
          float = io.read_bytes Float64, IO::ByteFormat::LittleEndian
          float.should be_close(1.234, 0.0001)
        end
      end

      describe "from slice" do
        it "reads int8" do
          bytes = Bytes[0x12]
          int = IO::ByteFormat::LittleEndian.decode(Int8, bytes)
          int.should eq(0x12_i8)
        end

        it "reads int16" do
          bytes = Bytes[0x34, 0x12]
          int = IO::ByteFormat::LittleEndian.decode(Int16, bytes)
          int.should eq(0x1234_i16)
        end

        it "reads int16 from larger slice" do
          bytes = Bytes[0x34, 0x12, 0, 0]
          int = IO::ByteFormat::LittleEndian.decode(Int16, bytes)
          int.should eq(0x1234_i16)
        end

        it "reads float32" do
          bytes = Bytes[0xB6, 0xF3, 0x9D, 0x3F]
          float = IO::ByteFormat::LittleEndian.decode(Float32, bytes)
          float.should be_close(1.234, 0.0001)
        end

        it "reads float64" do
          bytes = Bytes[0x58, 0x39, 0xB4, 0xC8, 0x76, 0xBE, 0xF3, 0x3F]
          float = IO::ByteFormat::LittleEndian.decode(Float64, bytes)
          float.should be_close(1.234, 0.0001)
        end
      end
    end
  end

  describe "big endian" do
    describe "encode" do
      it "writes int8" do
        io = IO::Memory.new
        io.write_bytes 0x12_i8, IO::ByteFormat::BigEndian
        assert_bytes io, 0x12
      end

      it "writes int16" do
        io = IO::Memory.new
        io.write_bytes 0x1234_i16, IO::ByteFormat::BigEndian
        assert_bytes_reversed io, 0x34, 0x12
      end

      it "writes int32" do
        io = IO::Memory.new
        io.write_bytes 0x12345678_i32, IO::ByteFormat::BigEndian
        assert_bytes_reversed io, 0x78, 0x56, 0x34, 0x12
      end

      it "writes int64" do
        io = IO::Memory.new
        io.write_bytes 0x123456789ABCDEF0_i64, IO::ByteFormat::BigEndian
        assert_bytes_reversed io, 0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12
      end

      it "writes float32" do
        io = IO::Memory.new
        io.write_bytes 1.234_f32, IO::ByteFormat::BigEndian
        assert_bytes_reversed io, 0xB6, 0xF3, 0x9D, 0x3F
      end

      it "writes float64" do
        io = IO::Memory.new
        io.write_bytes 1.234, IO::ByteFormat::BigEndian
        assert_bytes_reversed io, 0x58, 0x39, 0xB4, 0xC8, 0x76, 0xBE, 0xF3, 0x3F
      end
    end

    describe "decode" do
      describe "from io" do
        it "reads int8" do
          io = new_string_io(0x12)
          int = io.read_bytes Int8, IO::ByteFormat::BigEndian
          int.should eq(0x12_i8)
        end

        it "reads int16" do
          io = new_string_io(0x12, 0x34)
          int = io.read_bytes Int16, IO::ByteFormat::BigEndian
          int.should eq(0x1234_i16)
        end

        it "reads unt16" do
          io = new_string_io(0x12, 0x34)
          int = io.read_bytes UInt16, IO::ByteFormat::BigEndian
          int.should eq(0x1234_u16)
        end

        it "reads int32" do
          io = new_string_io(0x12, 0x34, 0x56, 0x78)
          int = io.read_bytes Int32, IO::ByteFormat::BigEndian
          int.should eq(0x12345678_i32)
        end

        it "reads int64" do
          io = new_string_io(0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0)
          int = io.read_bytes Int64, IO::ByteFormat::BigEndian
          int.should eq(0x123456789ABCDEF0_i64)
        end

        it "reads float32" do
          io = new_string_io(0x3F, 0x9D, 0xF3, 0xB6)
          float = io.read_bytes Float32, IO::ByteFormat::BigEndian
          float.should be_close(1.234, 0.0001)
        end

        it "reads float64" do
          io = new_string_io(0x3F, 0xF3, 0xBE, 0x76, 0xC8, 0xB4, 0x39, 0x58)
          float = io.read_bytes Float64, IO::ByteFormat::BigEndian
          float.should be_close(1.234, 0.0001)
        end
      end

      describe "from slice" do
        it "reads int8" do
          bytes = Bytes[0x12]
          int = IO::ByteFormat::BigEndian.decode(Int8, bytes)
          int.should eq(0x12_i8)
        end

        it "reads int16" do
          bytes = Bytes[0x12, 0x34]
          int = IO::ByteFormat::BigEndian.decode(Int16, bytes)
          int.should eq(0x1234_i16)
        end

        it "reads float32" do
          bytes = Bytes[0x3F, 0x9D, 0xF3, 0xB6]
          float = IO::ByteFormat::BigEndian.decode(Float32, bytes)
          float.should be_close(1.234, 0.0001)
        end

        it "reads float64" do
          bytes = Bytes[0x3F, 0xF3, 0xBE, 0x76, 0xC8, 0xB4, 0x39, 0x58]
          float = IO::ByteFormat::BigEndian.decode(Float64, bytes)
          float.should be_close(1.234, 0.0001)
        end
      end
    end
  end
end