storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/words/damerau-levenshtein_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2016, 2017 MinIO, Inc.
     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 words
    18  
    19  import (
    20  	"math"
    21  	"testing"
    22  )
    23  
    24  // Test minimum function which calculates the minimal value in a list of integers
    25  func TestMinimum(t *testing.T) {
    26  	type testCase struct {
    27  		listval  []int
    28  		expected int
    29  	}
    30  	testCases := []testCase{
    31  		{listval: []int{3, 4, 15}, expected: 3},
    32  		{listval: []int{}, expected: math.MaxInt32},
    33  	}
    34  	// Validate all the test cases.
    35  	for i, tt := range testCases {
    36  		val := minimum(tt.listval)
    37  		if val != tt.expected {
    38  			t.Errorf("Test %d:, Expected %d, got %d", i+1, tt.expected, val)
    39  		}
    40  	}
    41  }
    42  
    43  // Test DamerauLevenshtein which calculates the difference distance between two words
    44  func TestDamerauLevenshtein(t *testing.T) {
    45  	type testCase struct {
    46  		word1    string
    47  		word2    string
    48  		distance int
    49  	}
    50  	testCases := []testCase{
    51  		{word1: "", word2: "", distance: 0},
    52  		{word1: "a", word2: "a", distance: 0},
    53  		{word1: "a", word2: "b", distance: 1},
    54  		{word1: "rm", word2: "tm", distance: 1},
    55  		{word1: "version", word2: "evrsion", distance: 1},
    56  		{word1: "version", word2: "bersio", distance: 2},
    57  	}
    58  	// Validate all the test cases.
    59  	for i, tt := range testCases {
    60  		d := DamerauLevenshteinDistance(tt.word1, tt.word2)
    61  		if d != tt.distance {
    62  			t.Errorf("Test %d:, Expected %d, got %d", i+1, tt.distance, d)
    63  		}
    64  	}
    65  }