Skip to content

Commit df8713a

Browse files
authored
Merge pull request #106 from SpringMT/add-decompress-with-pos-tests
Add tests for decompress_with_pos method
2 parents b402a77 + 97c7d07 commit df8713a

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

ext/zstdruby/streaming_decompress.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ rb_streaming_decompress_decompress(VALUE obj, VALUE src)
114114
}
115115

116116
static VALUE
117-
rb_streaming_decompress_decompress2(VALUE obj, VALUE src)
117+
rb_streaming_decompress_decompress_with_pos(VALUE obj, VALUE src)
118118
{
119119
StringValue(src);
120120
const char* input_data = RSTRING_PTR(src);
@@ -131,7 +131,7 @@ rb_streaming_decompress_decompress2(VALUE obj, VALUE src)
131131
rb_raise(rb_eRuntimeError, "decompress error error code: %s", ZSTD_getErrorName(ret));
132132
}
133133
rb_str_cat(result, output.dst, output.pos);
134-
return rb_ary_new_from_args(3, UINT2NUM(ret), result, ULONG2NUM(input.pos));
134+
return rb_ary_new_from_args(2, result, ULONG2NUM(input.pos));
135135
}
136136

137137
extern VALUE rb_mZstd, cStreamingDecompress;
@@ -142,5 +142,5 @@ zstd_ruby_streaming_decompress_init(void)
142142
rb_define_alloc_func(cStreamingDecompress, rb_streaming_decompress_allocate);
143143
rb_define_method(cStreamingDecompress, "initialize", rb_streaming_decompress_initialize, -1);
144144
rb_define_method(cStreamingDecompress, "decompress", rb_streaming_decompress_decompress, 1);
145-
rb_define_method(cStreamingDecompress, "decompress2", rb_streaming_decompress_decompress2, 1);
145+
rb_define_method(cStreamingDecompress, "decompress_with_pos", rb_streaming_decompress_decompress_with_pos, 1);
146146
}

spec/zstd-ruby-streaming-decompress_spec.rb

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,71 @@
1717
end
1818
end
1919

20+
describe 'decompress_with_pos' do
21+
it 'should return decompressed data and consumed input position' do
22+
str = "hello world test data"
23+
cstr = Zstd.compress(str)
24+
stream = Zstd::StreamingDecompress.new
25+
26+
# Test with partial input
27+
result_array = stream.decompress_with_pos(cstr[0, 10])
28+
expect(result_array).to be_an(Array)
29+
expect(result_array.length).to eq(2)
30+
31+
decompressed_data = result_array[0]
32+
consumed_bytes = result_array[1]
33+
34+
expect(decompressed_data).to be_a(String)
35+
expect(consumed_bytes).to be_a(Integer)
36+
expect(consumed_bytes).to be > 0
37+
expect(consumed_bytes).to be <= 10
38+
end
39+
40+
it 'should work with complete compressed data' do
41+
str = "foo bar buzz"
42+
cstr = Zstd.compress(str)
43+
stream = Zstd::StreamingDecompress.new
44+
45+
result_array = stream.decompress_with_pos(cstr)
46+
decompressed_data = result_array[0]
47+
consumed_bytes = result_array[1]
48+
49+
expect(decompressed_data).to eq(str)
50+
expect(consumed_bytes).to eq(cstr.length)
51+
end
52+
53+
it 'should work with multiple calls' do
54+
str = "test data for multiple calls"
55+
cstr = Zstd.compress(str)
56+
stream = Zstd::StreamingDecompress.new
57+
58+
result = ''
59+
total_consumed = 0
60+
chunk_size = 5
61+
62+
while total_consumed < cstr.length
63+
remaining_data = cstr[total_consumed..-1]
64+
chunk = remaining_data[0, chunk_size]
65+
66+
result_array = stream.decompress_with_pos(chunk)
67+
decompressed_chunk = result_array[0]
68+
consumed_bytes = result_array[1]
69+
70+
result << decompressed_chunk
71+
total_consumed += consumed_bytes
72+
73+
expect(consumed_bytes).to be > 0
74+
expect(consumed_bytes).to be <= chunk.length
75+
76+
# If we consumed less than the chunk size, we might be done or need more data
77+
break if consumed_bytes < chunk.length && total_consumed == cstr.length
78+
end
79+
80+
expect(result).to eq(str)
81+
expect(total_consumed).to eq(cstr.length)
82+
end
83+
end
84+
2085
describe 'streaming decompress + GC.compact' do
2186
it 'shoud work' do
2287
# str = SecureRandom.hex(150)
@@ -109,4 +174,3 @@
109174
end
110175
end
111176
end
112-

0 commit comments

Comments
 (0)