github.com/maruel/nin@v0.0.0-20220112143044-f35891e3ce7e/edit_distance_test.go (about)

     1  // Copyright 2011 Google Inc. All Rights Reserved.
     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 nin
    16  
    17  import "testing"
    18  
    19  func TestEditDistanceTest_TestEmpty(t *testing.T) {
    20  	if 5 != editDistance("", "ninja", true, 0) {
    21  		t.Fatal("expected equal")
    22  	}
    23  	if 5 != editDistance("ninja", "", true, 0) {
    24  		t.Fatal("expected equal")
    25  	}
    26  	if 0 != editDistance("", "", true, 0) {
    27  		t.Fatal("expected equal")
    28  	}
    29  }
    30  
    31  func TestEditDistanceTest_TestMaxDistance(t *testing.T) {
    32  	allowReplacements := true
    33  	for maxDistance := 1; maxDistance < 7; maxDistance++ {
    34  		if maxDistance+1 != editDistance("abcdefghijklmnop", "ponmlkjihgfedcba", allowReplacements, maxDistance) {
    35  			t.Fatal("expected equal")
    36  		}
    37  	}
    38  }
    39  
    40  func TestEditDistanceTest_TestAllowReplacements(t *testing.T) {
    41  	allowReplacements := true
    42  	if 1 != editDistance("ninja", "njnja", allowReplacements, 0) {
    43  		t.Fatal("expected equal")
    44  	}
    45  	if 1 != editDistance("njnja", "ninja", allowReplacements, 0) {
    46  		t.Fatal("expected equal")
    47  	}
    48  
    49  	allowReplacements = false
    50  	if 2 != editDistance("ninja", "njnja", allowReplacements, 0) {
    51  		t.Fatal("expected equal")
    52  	}
    53  	if 2 != editDistance("njnja", "ninja", allowReplacements, 0) {
    54  		t.Fatal("expected equal")
    55  	}
    56  }
    57  
    58  func TestEditDistanceTest_TestBasics(t *testing.T) {
    59  	if 0 != editDistance("browser_tests", "browser_tests", true, 0) {
    60  		t.Fatal("expected equal")
    61  	}
    62  	if 1 != editDistance("browser_test", "browser_tests", true, 0) {
    63  		t.Fatal("expected equal")
    64  	}
    65  	if 1 != editDistance("browser_tests", "browser_test", true, 0) {
    66  		t.Fatal("expected equal")
    67  	}
    68  }