github.com/pdfcpu/pdfcpu@v0.11.1/pkg/filter/runLengthDecode_test.go (about)

     1  /*
     2  Copyright 2018 The pdfcpu Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package filter
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/hex"
    22  	"testing"
    23  )
    24  
    25  func compare(t *testing.T, a, b []byte) {
    26  
    27  	if len(a) != len(b) {
    28  		t.Errorf("length mismatch %d != %d", len(a), len(b))
    29  		t.Logf("a:\n%s\n", hex.Dump(a))
    30  		t.Logf("b:\n%s\n", hex.Dump(b))
    31  		return
    32  	}
    33  
    34  	for i := 0; i < len(a); i++ {
    35  		if a[i] != b[i] {
    36  			t.Errorf("mismatch at %d(0x%02x), 0x%02x != 0x%02x\n", i, i, a[i], b[i])
    37  			t.Logf("a:\n%s\n", hex.Dump(a))
    38  			t.Logf("b:\n%s\n", hex.Dump(b))
    39  			return
    40  		}
    41  	}
    42  
    43  }
    44  
    45  func TestRunLengthEncoding(t *testing.T) {
    46  
    47  	f := runLengthDecode{baseFilter{}}
    48  
    49  	for _, tt := range []struct {
    50  		raw, enc string
    51  	}{
    52  		{"\x01", "\x00\x01\x80"},
    53  		{"\x01\x01", "\xFF\x01\x80"},
    54  		{"\x00\x00\x02\x02", "\xFF\x00\xFF\x02\x80"},
    55  		{"\x00\x00\x00", "\xFE\x00\x80"},
    56  		{"\x00\x00\x00\x01", "\xFE\x00\x00\x01\x80"},
    57  		{"\x00\x00\x00\x00", "\xFD\x00\x80"},
    58  		{"\x00\x00\x00\x00\x00", "\xFC\x00\x80"},
    59  		{"\x00\x00\x01", "\xFF\x00\x00\x01\x80"},
    60  		{"\x00\x01", "\x01\x00\x01\x80"},
    61  		{"\x00\x01\x02", "\x02\x00\x01\x02\x80"},
    62  		{"\x00\x01\x02\x03", "\x03\x00\x01\x02\x03\x80"},
    63  		{"\x00\x01\x02\x03\x02", "\x04\x00\x01\x02\x03\x02\x80"},
    64  		{"\x00\x01", "\x01\x00\x01\x80"},
    65  		{"\x00\x01\x01", "\x00\x00\xFF\x01\x80"},
    66  		{"\x00\x01\x01\x01", "\x00\x00\xFE\x01\x80"},
    67  		{"\x00\x00\x01\x02\x00\x00", "\xFF\x00\x01\x01\x02\xFF\x00\x80"},
    68  	} {
    69  		var enc bytes.Buffer
    70  		f.encode(&enc, []byte(tt.raw))
    71  		compare(t, enc.Bytes(), []byte(tt.enc))
    72  
    73  		var raw bytes.Buffer
    74  		f.decode(&raw, enc.Bytes(), -1)
    75  		compare(t, raw.Bytes(), []byte(tt.raw))
    76  	}
    77  
    78  }