kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/util/riegeli/transpose_test.go (about)

     1  /*
     2   * Copyright 2018 The Kythe Authors. All rights reserved.
     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 riegeli
    18  
    19  import (
    20  	"io"
    21  	"testing"
    22  )
    23  
    24  // TODO(schroederc): add more tests once an encoder is available
    25  
    26  // TODO(schroederc): test non-proto records support
    27  // TODO(schroederc): test deprecated protobuf "groups" support
    28  
    29  func TestBackwardWriter(t *testing.T) {
    30  	pieces := []string{"a", "b", "ccc", "dd", "eee"}
    31  
    32  	var w backwardWriter
    33  	var expectedSize int
    34  	for _, p := range pieces {
    35  		w.Push([]byte(p))
    36  		expectedSize += len(p)
    37  		if found := w.Len(); expectedSize != found {
    38  			t.Errorf("Found w.Len() == %d; expected: %d", found, expectedSize)
    39  		}
    40  	}
    41  
    42  	foundBytes := make([]byte, expectedSize)
    43  
    44  	// Read a partial chunk
    45  	if n, err := w.Read(foundBytes[:2]); err != nil || n != 2 {
    46  		t.Fatalf("Failed to read partial chunk: %d %v", n, err)
    47  	} else if found := w.Len(); found != expectedSize-2 {
    48  		t.Errorf("Expected w.Len() == %d; found: %d", expectedSize-2, found)
    49  	}
    50  
    51  	// Read rest
    52  	if n, err := io.ReadFull(&w, foundBytes[2:]); err != nil || n != expectedSize-2 {
    53  		t.Fatalf("Failed to read full buffer: %d %v", n, err)
    54  	} else if found := w.Len(); found != 0 {
    55  		t.Errorf("Expected w.Len() == 0; found: %d", found)
    56  	}
    57  
    58  	var expected string
    59  	for i := len(pieces) - 1; i >= 0; i-- {
    60  		expected += pieces[i]
    61  	}
    62  	if found := string(foundBytes); expected != found {
    63  		t.Fatalf("Found %q; expected %q", found, expected)
    64  	}
    65  }