k8s.io/test-infra/triage@v0.0.0-20240520184403-27c6b4c223d8/summarize/text_test.go (about)

     1  /*
     2  Copyright 2020 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 summarize
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  )
    23  
    24  func TestNormalize(t *testing.T) {
    25  	testCases := []struct {
    26  		name     string
    27  		argument string
    28  		want     string
    29  	}{
    30  		{"Hex strings, letters, version number", "0x1234 a 123.13.45.43 b 2e24e003-9ffd-4e78-852c-9dcb6cbef493-123", "UNIQ1 a UNIQ2 b UNIQ3"},
    31  		{"Date and time", "Mon, 12 January 2017 11:34:35 blah blah", "TIMEblah blah"},
    32  		{"Version number, hex string", "123.45.68.12:345 abcd1234eeee", "UNIQ1 UNIQ2"},
    33  	}
    34  
    35  	for _, tc := range testCases {
    36  		t.Run(tc.name, func(t *testing.T) {
    37  			got := normalize(tc.argument, defaultMaxClusterTextLength)
    38  
    39  			if got != tc.want {
    40  				t.Errorf("normalize(%s) = %s, wanted %s", tc.argument, got, tc.want)
    41  			}
    42  		})
    43  	}
    44  
    45  	// Deal with long strings separately because it requires some setup
    46  	t.Run("Incredibly large string", func(t *testing.T) {
    47  		// Generate an incredibly long string
    48  		var builder strings.Builder
    49  		builder.Grow(10 * 500_000) // Allocate enough memory (10 characters in "foobarbaz ")
    50  
    51  		for i := 0; i < 500_000; i++ {
    52  			builder.WriteString("foobarbaz ")
    53  		}
    54  
    55  		generatedString := builder.String()
    56  		// 10*500 = (number of characters in "foobarbaz ")*(500 repetitions)
    57  		wantString := generatedString[:10*500] + "\n...[truncated]...\n" + generatedString[:10*500]
    58  
    59  		got := normalize(generatedString, defaultMaxClusterTextLength)
    60  
    61  		if got != wantString {
    62  			t.Errorf("normalize(%s) = %s, wanted %s", generatedString, wantString, got)
    63  		}
    64  	})
    65  }
    66  
    67  func TestNgramEditDist(t *testing.T) {
    68  	argument1 := "example text"
    69  	argument2 := "exampl text"
    70  	want := 1
    71  	got := ngramEditDist(argument1, argument2)
    72  
    73  	if got != want {
    74  		t.Errorf("ngramEditDist(%#v, %#v) = %d, wanted %d", argument1, argument2, got, want)
    75  	}
    76  }
    77  
    78  // Ensure stability of ngram count digest
    79  func TestMakeNgramCountsDigest(t *testing.T) {
    80  	want := "eddb950347d1eb05b5d7"
    81  	got := makeNgramCountsDigest("some string")
    82  
    83  	if got != want {
    84  		t.Errorf("makeNgramCountsDigest(%#v) = %#v, wanted %#v", "some string", got, want)
    85  	}
    86  }
    87  
    88  func TestCommonSpans(t *testing.T) {
    89  	testCases := []struct {
    90  		name     string
    91  		argument []string
    92  		want     []int
    93  	}{
    94  		{"Exact match", []string{"an exact match", "an exact match"}, []int{14}},
    95  		{"Replaced word", []string{"some example string", "some other string"}, []int{5, 7, 7}},
    96  		{"Deletion", []string{"a problem with a common set", "a common set"}, []int{2, 7, 1, 4, 13}},
    97  	}
    98  
    99  	for _, tc := range testCases {
   100  		t.Run(tc.name, func(t *testing.T) {
   101  			got := commonSpans(tc.argument)
   102  
   103  			// Check if the int slices are equal
   104  			slicesAreEqual := true
   105  			if len(tc.want) != len(got) {
   106  				slicesAreEqual = false
   107  			} else {
   108  				for i := range tc.want {
   109  					if tc.want[i] != got[i] {
   110  						slicesAreEqual = false
   111  						break
   112  					}
   113  				}
   114  			}
   115  
   116  			if !slicesAreEqual {
   117  				t.Errorf("commonSpans(%#v) = %#v, wanted %#v", tc.argument, got, tc.want)
   118  			}
   119  		})
   120  	}
   121  }