...

Source file src/k8s.io/apimachinery/pkg/runtime/splice_test.go

Documentation: k8s.io/apimachinery/pkg/runtime

     1  /*
     2  Copyright 2023 The Kubernetes 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 runtime_test
    18  
    19  import (
    20  	"bytes"
    21  	"testing"
    22  
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  )
    25  
    26  func TestSpliceBuffer(t *testing.T) {
    27  	testBytes0 := []byte{0x01, 0x02, 0x03, 0x04}
    28  	testBytes1 := []byte{0x04, 0x03, 0x02, 0x02}
    29  
    30  	testCases := []struct {
    31  		name string
    32  		run  func(sb runtime.Splice, buf *bytes.Buffer)
    33  	}{
    34  		{
    35  			name: "Basic Write",
    36  			run: func(sb runtime.Splice, buf *bytes.Buffer) {
    37  				sb.Write(testBytes0)
    38  				buf.Write(testBytes0)
    39  			},
    40  		},
    41  		{
    42  			name: "Multiple Writes",
    43  			run: func(sb runtime.Splice, buf *bytes.Buffer) {
    44  				for _, b := range testBytes0 {
    45  					sb.Write([]byte{b})
    46  					buf.Write([]byte{b})
    47  				}
    48  			},
    49  		},
    50  		{
    51  			name: "Write and Reset",
    52  			run: func(sb runtime.Splice, buf *bytes.Buffer) {
    53  				sb.Write(testBytes0)
    54  				buf.Write(testBytes0)
    55  
    56  				sb.Reset()
    57  				buf.Reset()
    58  			},
    59  		},
    60  		{
    61  			name: "Write/Splice",
    62  			run: func(sb runtime.Splice, buf *bytes.Buffer) {
    63  				sb.Splice(testBytes0)
    64  				buf.Write(testBytes0)
    65  			},
    66  		},
    67  		{
    68  			name: "Write/Splice and Reset",
    69  			run: func(sb runtime.Splice, buf *bytes.Buffer) {
    70  				sb.Splice(testBytes0)
    71  				buf.Write(testBytes0)
    72  
    73  				sb.Reset()
    74  				buf.Reset()
    75  			},
    76  		},
    77  		{
    78  			name: "Write/Splice, Reset, Write/Splice",
    79  			run: func(sb runtime.Splice, buf *bytes.Buffer) {
    80  				sb.Splice(testBytes0)
    81  				buf.Write(testBytes0)
    82  
    83  				sb.Reset()
    84  				buf.Reset()
    85  
    86  				sb.Splice(testBytes1)
    87  				buf.Write(testBytes1)
    88  			},
    89  		},
    90  		{
    91  			name: "Write, Reset, Splice",
    92  			run: func(sb runtime.Splice, buf *bytes.Buffer) {
    93  				sb.Write(testBytes0)
    94  				buf.Write(testBytes0)
    95  
    96  				sb.Reset()
    97  				buf.Reset()
    98  
    99  				sb.Splice(testBytes1)
   100  				buf.Write(testBytes1)
   101  			},
   102  		},
   103  	}
   104  
   105  	for _, tt := range testCases {
   106  		t.Run(tt.name, func(t *testing.T) {
   107  			sb := runtime.NewSpliceBuffer()
   108  			buf := &bytes.Buffer{}
   109  			tt.run(sb, buf)
   110  
   111  			if sb.Bytes() == nil {
   112  				t.Errorf("Unexpected nil")
   113  			}
   114  			if string(sb.Bytes()) != string(buf.Bytes()) {
   115  				t.Errorf("Expected sb.Bytes() == %q, buf.Bytes() == %q", sb.Bytes(), buf.Bytes())
   116  			}
   117  		})
   118  
   119  	}
   120  
   121  }
   122  

View as plain text