| 1 |
use superrust-analyzer cargo zstd 0.13.3 stream/::{copy_encoderust-analyzer cargo zstd 0.13.3 stream/functions/copy_encode()., decode_allrust-analyzer cargo zstd 0.13.3 stream/functions/decode_all()., encode_allrust-analyzer cargo zstd 0.13.3 stream/functions/encode_all().}; |
| 2 |
use superrust-analyzer cargo zstd 0.13.3 stream/::{Decoderrust-analyzer cargo zstd 0.13.3 stream/read/Decoder#, Encoderrust-analyzer cargo zstd 0.13.3 stream/write/Encoder#}; |
| 3 |
|
| 4 |
use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/; |
| 5 |
use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/; |
| 6 |
|
| 7 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 8 |
fn test_end_of_framerust-analyzer cargo zstd 0.13.3 stream/tests/test_end_of_frame().() { |
| 9 |
use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#}; |
| 10 |
|
| 11 |
let mut enc = Encoderrust-analyzer cargo zstd 0.13.3 stream/write/Encoder#::newrust-analyzer cargo zstd 0.13.3 stream/write/impl#[`Encoder<'static, W>`]new().(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(), 1).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 12 |
enc.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(b"foo").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 13 |
let mut compressed = enc.finishrust-analyzer cargo zstd 0.13.3 stream/write/impl#[`Encoder<'a, W>`]finish().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 14 |
|
| 15 |
// Add footer/whatever to underlying storage. |
| 16 |
compressed.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(0); |
| 17 |
|
| 18 |
// Drain zstd stream until end-of-frame. |
| 19 |
let mut dec = Decoderrust-analyzer cargo zstd 0.13.3 stream/read/Decoder#::newrust-analyzer cargo zstd 0.13.3 stream/read/impl#[`Decoder<'static, BufReader<R>>`]new().(&compressed[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().single_framerust-analyzer cargo zstd 0.13.3 stream/read/impl#[`Decoder<'a, R>`]single_frame().(); |
| 20 |
let mut buf = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 21 |
dec.read_to_endrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_to_end().(&mut buf).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 22 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&buf, b"foo", "Error decoding a single frame."); |
| 23 |
} |
| 24 |
|
| 25 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 26 |
fn test_concatenated_framesrust-analyzer cargo zstd 0.13.3 stream/tests/test_concatenated_frames().() { |
| 27 |
let mut buffer = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 28 |
copy_encoderust-analyzer cargo zstd 0.13.3 stream/functions/copy_encode().(&b"foo"[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., &mut buffer, 1).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 29 |
copy_encoderust-analyzer cargo zstd 0.13.3 stream/functions/copy_encode().(&b"bar"[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., &mut buffer, 2).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 30 |
copy_encoderust-analyzer cargo zstd 0.13.3 stream/functions/copy_encode().(&b"baz"[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., &mut buffer, 3).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 31 |
|
| 32 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 33 |
&decode_allrust-analyzer cargo zstd 0.13.3 stream/functions/decode_all().(&buffer[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 34 |
b"foobarbaz", |
| 35 |
"Error decoding concatenated frames." |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 40 |
fn test_flushrust-analyzer cargo zstd 0.13.3 stream/tests/test_flush().() { |
| 41 |
use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#; |
| 42 |
|
| 43 |
let buf = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 44 |
let mut z = Encoderrust-analyzer cargo zstd 0.13.3 stream/write/Encoder#::newrust-analyzer cargo zstd 0.13.3 stream/write/impl#[`Encoder<'static, W>`]new().(buf, 19).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 45 |
|
| 46 |
z.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(b"hello").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 47 |
|
| 48 |
z.flushrust-analyzer cargo zstd 0.13.3 stream/write/impl#[`Encoder<'a, W>`][Write]flush().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); // Might corrupt stream |
| 49 |
let buf = z.finishrust-analyzer cargo zstd 0.13.3 stream/write/impl#[`Encoder<'a, W>`]finish().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 50 |
|
| 51 |
let s = decode_allrust-analyzer cargo zstd 0.13.3 stream/functions/decode_all().(&buf[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 52 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(s, b"hello", "Error decoding after flush."); |
| 53 |
} |
| 54 |
|
| •55 |
/*#[test] |
| 56 |
fn test_try_finish() { |
| 57 |
use std::io::Write; |
| 58 |
let mut z = setup_try_finish(); |
| 59 |
|
| 60 |
z.get_mut().set_ops(iter::repeat(PartialOp::Unlimited)); |
| 61 |
|
| 62 |
// flush() should continue to work even though write() doesn't. |
| 63 |
z.flush().unwrap(); |
| 64 |
|
| 65 |
let buf = match z.try_finish() { |
| 66 |
Ok(buf) => buf.into_inner(), |
| 67 |
Err((_z, e)) => panic!("try_finish failed with {:?}", e), |
| 68 |
}; |
| 69 |
|
| 70 |
// Make sure the multiple try_finish calls didn't screw up the internal |
| 71 |
// buffer and continued to produce valid compressed data. |
| 72 |
assert_eq!(&decode_all(&buf[..]).unwrap(), b"hello", "Error decoding"); |
| 73 |
} |
| 74 |
|
| 75 |
#[test] |
| 76 |
#[should_panic] |
| 77 |
fn test_write_after_try_finish() { |
| 78 |
use std::io::Write; |
| 79 |
let mut z = setup_try_finish(); |
| 80 |
z.write_all(b"hello world").unwrap(); |
| 81 |
} |
| 82 |
|
| 83 |
fn setup_try_finish() -> Encoder<'static, PartialWrite<Vec<u8>>> { |
| 84 |
use std::io::Write; |
| 85 |
|
| 86 |
let buf = |
| 87 |
PartialWrite::new(Vec::new(), iter::repeat(PartialOp::Unlimited)); |
| 88 |
let mut z = Encoder::new(buf, 19).unwrap(); |
| 89 |
|
| 90 |
z.write_all(b"hello").unwrap(); |
| 91 |
|
| 92 |
z.get_mut() |
| 93 |
.set_ops(iter::repeat(PartialOp::Err(io::ErrorKind::WouldBlock))); |
| 94 |
|
| 95 |
let (z, err) = z.try_finish().unwrap_err(); |
| 96 |
assert_eq!( |
| 97 |
err.kind(), |
| 98 |
io::ErrorKind::WouldBlock, |
| 99 |
"expected WouldBlock error" |
| 100 |
); |
| 101 |
|
| 102 |
z |
| 103 |
} |
| 104 |
|
| 105 |
#[test] |
| 106 |
fn test_failing_write() { |
| 107 |
use std::io::Write; |
| 108 |
|
| 109 |
let buf = PartialWrite::new( |
| 110 |
Vec::new(), |
| 111 |
iter::repeat(PartialOp::Err(io::ErrorKind::WouldBlock)), |
| 112 |
); |
| 113 |
let mut z = Encoder::new(buf, 1).unwrap(); |
| 114 |
|
| 115 |
// Fill in enough data to make sure the buffer gets written out. |
| 116 |
let input = vec![b'b'; 128 * 1024]; |
| 117 |
// This should work even though the inner writer rejects writes. |
| 118 |
assert_eq!( |
| 119 |
z.write(&input).unwrap(), |
| 120 |
128 * 1024, |
| 121 |
"did not write all input buffer" |
| 122 |
); |
| 123 |
|
| 124 |
// The next write would fail (the buffer still has some data in it). |
| 125 |
assert_eq!( |
| 126 |
z.write(b"abc").unwrap_err().kind(), |
| 127 |
io::ErrorKind::WouldBlock, |
| 128 |
"expected WouldBlock error" |
| 129 |
); |
| 130 |
|
| 131 |
z.get_mut().set_ops(iter::repeat(PartialOp::Unlimited)); |
| 132 |
|
| 133 |
// This shouldn't have led to any corruption. |
| 134 |
let buf = z.finish().unwrap().into_inner(); |
| 135 |
assert_eq!( |
| 136 |
&decode_all(&buf[..]).unwrap(), |
| 137 |
&input, |
| 138 |
"WouldBlock errors should not corrupt stream" |
| 139 |
); |
| •140 |
}*/ |
| 141 |
|
| 142 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 143 |
fn test_invalid_framerust-analyzer cargo zstd 0.13.3 stream/tests/test_invalid_frame().() { |
| 144 |
use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#; |
| 145 |
|
| 146 |
// I really hope this data is invalid... |
| 147 |
let data = &[1u8, 2u8, 3u8, 4u8, 5u8]; |
| 148 |
let mut dec = Decoderrust-analyzer cargo zstd 0.13.3 stream/read/Decoder#::newrust-analyzer cargo zstd 0.13.3 stream/read/impl#[`Decoder<'static, BufReader<R>>`]new().(&data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 149 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 150 |
dec.read_to_endrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_to_end().(&mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().()).errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]err().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|e| e.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().()), |
| 151 |
Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#), |
| 152 |
"did not encounter expected 'invalid frame' error" |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 157 |
fn test_incomplete_framerust-analyzer cargo zstd 0.13.3 stream/tests/test_incomplete_frame().() { |
| 158 |
use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#}; |
| 159 |
|
| 160 |
let mut enc = Encoderrust-analyzer cargo zstd 0.13.3 stream/write/Encoder#::newrust-analyzer cargo zstd 0.13.3 stream/write/impl#[`Encoder<'static, W>`]new().(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(), 1).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 161 |
enc.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(b"This is a regular string").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 162 |
let mut compressed = enc.finishrust-analyzer cargo zstd 0.13.3 stream/write/impl#[`Encoder<'a, W>`]finish().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 163 |
|
| 164 |
let half_size = compressed.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().2; |
| 165 |
compressed.truncaterust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]truncate().(half_size); |
| 166 |
|
| 167 |
let mut dec = Decoderrust-analyzer cargo zstd 0.13.3 stream/read/Decoder#::newrust-analyzer cargo zstd 0.13.3 stream/read/impl#[`Decoder<'static, BufReader<R>>`]new().(&compressed[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 168 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 169 |
dec.read_to_endrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_to_end().(&mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().()).errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]err().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|e| e.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().()), |
| 170 |
Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::UnexpectedEofrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#UnexpectedEof#), |
| 171 |
"did not encounter expected EOF error" |
| 172 |
); |
| 173 |
} |
| 174 |
|
| •175 |
//#[test] |
| 176 |
#[cfg(has_example_txt_zst)] |
| 177 |
fn test_cli_compatibility() { |
| 178 |
let input = include_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_bytes!!("../../assets/example.txt.zst"); |
| 179 |
|
| 180 |
let output = decode_allrust-analyzer cargo zstd 0.13.3 stream/functions/decode_all().(&input[..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]).unwrap(); |
| 181 |
|
| 182 |
let expected = include_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_bytes!!("../../assets/example.txt"); |
| 183 |
|
| 184 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 185 |
&output[..], |
| 186 |
&expected[..], |
| 187 |
"error decoding cli-compressed data" |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
#[cfg(feature = "legacy")] |
| •192 |
//#[test] |
| 193 |
fn test_legacyrust-analyzer cargo zstd 0.13.3 stream/tests/test_legacy().() { |
| 194 |
use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/; |
| 195 |
use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#; |
| 196 |
|
| 197 |
// Read the content from that file |
| 198 |
let expected = include_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_bytes!!("../../assets/example.txt"); |
| 199 |
|
| 200 |
for version in &[5, 6, 7, 8] { |
| 201 |
let filename = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("assets/example.txt.v{}.zst", version); |
| 202 |
let file = fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(filename).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 203 |
let mut decoder = Decoderrust-analyzer cargo zstd 0.13.3 stream/read/Decoder#::newrust-analyzer cargo zstd 0.13.3 stream/read/impl#[`Decoder<'static, BufReader<R>>`]new().(file).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 204 |
|
| 205 |
let mut buffer = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 206 |
decoder.read_to_endrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_to_end().(&mut buffer).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 207 |
|
| 208 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 209 |
&expected[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., |
| 210 |
&buffer[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()., |
| 211 |
"error decompressing legacy version {}", |
| 212 |
version |
| 213 |
); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
// Check that compressing+decompressing some data gives back the original |
| 218 |
fn test_full_cyclerust-analyzer cargo zstd 0.13.3 stream/tests/test_full_cycle().(input: &[u8], level: i32) { |
| 219 |
craterust-analyzer cargo zstd 0.13.3 crate/::test_cycle_unwraprust-analyzer cargo zstd 0.13.3 test_cycle_unwrap().( |
| 220 |
input, |
| 221 |
|data| encode_allrust-analyzer cargo zstd 0.13.3 stream/functions/encode_all().(data, level), |
| 222 |
|data| decode_allrust-analyzer cargo zstd 0.13.3 stream/functions/decode_all().(data), |
| 223 |
); |
| 224 |
} |
| 225 |
|
| 226 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 227 |
fn test_emptyrust-analyzer cargo zstd 0.13.3 stream/tests/test_empty().() { |
| 228 |
// Test compressing empty data |
| 229 |
for level in 1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#19 { |
| 230 |
test_full_cyclerust-analyzer cargo zstd 0.13.3 stream/tests/test_full_cycle().(b"", level); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 235 |
fn test_ll_sourcerust-analyzer cargo zstd 0.13.3 stream/tests/test_ll_source().() { |
| 236 |
// Where could I find some long text?... |
| •237 |
// Debian: originally it's the prebuilt bindings, but we excluded it. |
| 238 |
let data = include_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_bytes!!("raw.rs"); |
| 239 |
// Test a few compression levels. |
| 240 |
// TODO: check them all? |
| 241 |
for level in 1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#5 { |
| 242 |
// Test compressing actual data |
| 243 |
test_full_cyclerust-analyzer cargo zstd 0.13.3 stream/tests/test_full_cycle().(data, level); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 248 |
fn reader_to_writerrust-analyzer cargo zstd 0.13.3 stream/tests/reader_to_writer().() { |
| 249 |
use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#}; |
| 250 |
|
| 251 |
let clear = include_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_bytes!!("../../assets/example.txt"); |
| 252 |
// Compress using reader |
| 253 |
let mut encoder = superrust-analyzer cargo zstd 0.13.3 stream/::readrust-analyzer cargo zstd 0.13.3 stream/read/::Encoderrust-analyzer cargo zstd 0.13.3 stream/read/Encoder#::newrust-analyzer cargo zstd 0.13.3 stream/read/impl#[`Encoder<'static, BufReader<R>>`]new().(&clear[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., 1).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 254 |
|
| 255 |
let mut compressed_buffer = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 256 |
encoder.read_to_endrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_to_end().(&mut compressed_buffer).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 257 |
|
| 258 |
// eprintln!("Compressed Buffer: {:?}", compressed_buffer); |
| 259 |
|
| 260 |
// Decompress using writer |
| 261 |
let mut decompressed_buffer = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 262 |
let mut decoder = |
| 263 |
superrust-analyzer cargo zstd 0.13.3 stream/::writerust-analyzer cargo zstd 0.13.3 stream/write/::Decoderrust-analyzer cargo zstd 0.13.3 stream/write/Decoder#::newrust-analyzer cargo zstd 0.13.3 stream/write/impl#[`Decoder<'static, W>`]new().(&mut decompressed_buffer).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 264 |
decoder.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(&compressed_buffer[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 265 |
decoder.flushrust-analyzer cargo zstd 0.13.3 stream/write/impl#[`Decoder<'_, W>`][Write]flush().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 266 |
// eprintln!("{:?}", decompressed_buffer); |
| 267 |
|
| 268 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(clear, &decompressed_buffer[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().); |
| 269 |
} |
| 270 |
|
| 271 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 272 |
fn test_finish_empty_encoderrust-analyzer cargo zstd 0.13.3 stream/tests/test_finish_empty_encoder().() { |
| 273 |
use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#; |
| 274 |
let mut enc = Encoderrust-analyzer cargo zstd 0.13.3 stream/write/Encoder#::newrust-analyzer cargo zstd 0.13.3 stream/write/impl#[`Encoder<'static, W>`]new().(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(), 0).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 275 |
enc.do_finishrust-analyzer cargo zstd 0.13.3 stream/write/impl#[`Encoder<'a, W>`]do_finish().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 276 |
enc.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(b"this should not work").unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().(); |
| 277 |
enc.finishrust-analyzer cargo zstd 0.13.3 stream/write/impl#[`Encoder<'a, W>`]finish().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 278 |
} |