File: lzma_decoder_test.go

package info (click to toggle)
golang-github-kjk-lzma 1.0.0-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, bullseye-backports, sid, trixie
  • size: 492 kB
  • sloc: makefile: 4
file content (55 lines) | stat: -rw-r--r-- 1,409 bytes parent folder | download | duplicates (4)
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
// Copyright (c) 2010, Andrei Vieru. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package lzma

import (
	"bytes"
	"io"
	"log"
	"testing"
)

func TestDecoder(t *testing.T) {
	b := new(bytes.Buffer)
	for _, tt := range lzmaTests {
		in := bytes.NewBuffer(tt.lzma)
		r := NewReader(in)
		defer r.Close()
		b.Reset()
		n, err := io.Copy(b, r)
		if err != tt.err {
			t.Errorf("%s: io.Copy: %v, want %v", tt.descr, err, tt.err)
		}
		if err == nil { // if err != nil, there is little chance that data is decoded correctly, if at all
			s := b.String()
			if s != tt.raw {
				t.Errorf("%s: got %d-byte %q, want %d-byte %q", tt.descr, n, s, len(tt.raw), tt.raw)
			}
		}
	}
}

func BenchmarkDecoder(b *testing.B) {
	b.StopTimer()
	buf := new(bytes.Buffer)
	for i := 0; i < b.N; i++ {
		buf.Reset()
		in := bytes.NewBuffer(bench.lzma)
		b.StartTimer()
		// timer starts before this contructor because variable "in" already
		// contains data, so the decoding start rigth away
		r := NewReader(in)
		n, err := io.Copy(buf, r)
		b.StopTimer()
		if err != nil {
			log.Fatalf("%v", err)
		}
		b.SetBytes(n)
		r.Close()
	}
	if bytes.Equal(buf.Bytes(), bench.raw) == false { // check only after last iteration
		log.Fatalf("%s: got %d-byte %q, want %d-byte %q", bench.descr, len(buf.Bytes()), buf.String(), len(bench.raw), bench.raw)
	}
}