|
| 1 | +// Copyright (c) 2019+ Klaus Post. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package s2 |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "io" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +func TestLeadingSkippableBlock(t *testing.T) { |
| 14 | + var buf bytes.Buffer |
| 15 | + w := NewWriter(&buf) |
| 16 | + if err := w.AddSkippableBlock(0x80, []byte("skippable block")); err != nil { |
| 17 | + t.Fatalf("w.AddSkippableBlock: %v", err) |
| 18 | + } |
| 19 | + if _, err := w.Write([]byte("some data")); err != nil { |
| 20 | + t.Fatalf("w.Write: %v", err) |
| 21 | + } |
| 22 | + if err := w.Close(); err != nil { |
| 23 | + t.Fatalf("w.Close: %v", err) |
| 24 | + } |
| 25 | + r := NewReader(&buf) |
| 26 | + var sb []byte |
| 27 | + r.SkippableCB(0x80, func(sr io.Reader) error { |
| 28 | + var err error |
| 29 | + sb, err = io.ReadAll(sr) |
| 30 | + return err |
| 31 | + }) |
| 32 | + if _, err := r.Read([]byte{}); err != nil { |
| 33 | + t.Errorf("empty read failed: %v", err) |
| 34 | + } |
| 35 | + if !bytes.Equal(sb, []byte("skippable block")) { |
| 36 | + t.Errorf("didn't get correct data from skippable block: %q", string(sb)) |
| 37 | + } |
| 38 | + data, err := io.ReadAll(r) |
| 39 | + if err != nil { |
| 40 | + t.Fatalf("r.Read: %v", err) |
| 41 | + } |
| 42 | + if !bytes.Equal(data, []byte("some data")) { |
| 43 | + t.Errorf("didn't get correct compressed data: %q", string(data)) |
| 44 | + } |
| 45 | +} |
0 commit comments