storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/trie/trie_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 trie
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  // Simply make sure creating a new tree works.
    24  func TestNewTrie(t *testing.T) {
    25  	trie := NewTrie()
    26  
    27  	if trie.size != 0 {
    28  		t.Errorf("expected size 0, got: %d", trie.size)
    29  	}
    30  }
    31  
    32  // Ensure that we can insert new keys into the tree, then check the size.
    33  func TestInsert(t *testing.T) {
    34  	trie := NewTrie()
    35  
    36  	// We need to have an empty tree to begin with.
    37  	if trie.size != 0 {
    38  		t.Errorf("expected size 0, got: %d", trie.size)
    39  	}
    40  
    41  	trie.Insert("key")
    42  	trie.Insert("keyy")
    43  
    44  	// After inserting, we should have a size of two.
    45  	if trie.size != 2 {
    46  		t.Errorf("expected size 2, got: %d", trie.size)
    47  	}
    48  }
    49  
    50  // Ensure that PrefixMatch gives us the correct two keys in the tree.
    51  func TestPrefixMatch(t *testing.T) {
    52  	trie := NewTrie()
    53  
    54  	// Feed it some fodder: only 'minio' and 'miny-os' should trip the matcher.
    55  	trie.Insert("minio")
    56  	trie.Insert("amazon")
    57  	trie.Insert("cheerio")
    58  	trie.Insert("miny-o's")
    59  
    60  	matches := trie.PrefixMatch("min")
    61  	if len(matches) != 2 {
    62  		t.Errorf("expected two matches, got: %d", len(matches))
    63  	}
    64  
    65  	if matches[0] != "minio" && matches[1] != "minio" {
    66  		t.Errorf("expected one match to be 'minio', got: '%s' and '%s'", matches[0], matches[1])
    67  	}
    68  }