github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/models/common_test.go (about)

     1  // Copyright 2023 iLogtail Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package models
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  	"unsafe"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func Test_Tags_SortTo(t *testing.T) {
    26  	tests := []struct {
    27  		caseName      string
    28  		tags          map[string]string
    29  		buf           []KeyValue[string]
    30  		want          []KeyValue[string]
    31  		wantOriginBuf bool
    32  	}{
    33  		{
    34  			caseName: "empty tags empty buf",
    35  			tags:     nil,
    36  			buf:      nil,
    37  			want:     nil,
    38  		},
    39  		{
    40  			caseName: "empty tags none empty buf",
    41  			tags:     nil,
    42  			buf:      []KeyValue[string]{{Key: "a", Value: "b"}},
    43  			want:     []KeyValue[string]{},
    44  		},
    45  		{
    46  			caseName: "none empty tags empty buf",
    47  			tags:     map[string]string{"b": "b", "a": "a"},
    48  			buf:      nil,
    49  			want:     []KeyValue[string]{{Key: "a", Value: "a"}, {Key: "b", Value: "b"}},
    50  		},
    51  		{
    52  			caseName:      "none empty tags none empty buf enough len",
    53  			tags:          map[string]string{"b": "b", "a": "a"},
    54  			buf:           []KeyValue[string]{{Key: "c", Value: "c"}, {Key: "c", Value: "c"}, {Key: "c", Value: "c"}},
    55  			want:          []KeyValue[string]{{Key: "a", Value: "a"}, {Key: "b", Value: "b"}},
    56  			wantOriginBuf: true,
    57  		},
    58  		{
    59  			caseName: "none empty tags none empty buf not enough len",
    60  			tags:     map[string]string{"b": "b", "a": "a"},
    61  			buf:      []KeyValue[string]{{Key: "c", Value: "c"}},
    62  			want:     []KeyValue[string]{{Key: "a", Value: "a"}, {Key: "b", Value: "b"}},
    63  		},
    64  	}
    65  
    66  	for _, cases := range tests {
    67  		tags := NewTagsWithMap(cases.tags)
    68  		result := tags.SortTo(cases.buf)
    69  		assert.ElementsMatchf(t, result, cases.want, cases.caseName)
    70  		if cases.wantOriginBuf {
    71  			assert.Equalf(t, (*reflect.SliceHeader)(unsafe.Pointer(&cases.buf)).Data, (*reflect.SliceHeader)(unsafe.Pointer(&result)).Data, cases.caseName)
    72  		}
    73  	}
    74  }