github.com/searKing/golang/go@v1.2.117/container/trie_tree/ternary_search_tree/tst_test.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ternary_search_tree_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/searKing/golang/go/container/traversal"
    11  	"github.com/searKing/golang/go/container/trie_tree/ternary_search_tree"
    12  )
    13  
    14  func TestTernarySearchTree(t *testing.T) {
    15  	tree := ternary_search_tree.New()
    16  	tree.Store("test", 1)
    17  	if tree.Count() != 1 {
    18  		t.Errorf("expecting count 1, actual = %v", tree.Count())
    19  	}
    20  	if tree.Depth() != 4 {
    21  		t.Errorf("expecting depth 4, actual = %v", tree.Depth())
    22  	}
    23  	if !tree.Contains("test") {
    24  		t.Errorf("expecting to find key=test")
    25  	}
    26  	if !tree.ContainsPrefix("tes") {
    27  		t.Errorf("expecting to find key=tes")
    28  	}
    29  
    30  	val, ok := tree.Load("test")
    31  	if !ok {
    32  		t.Errorf("expecting to find key=test")
    33  	}
    34  	if val.(int) != 1 {
    35  		t.Errorf("expecting test's value=1, actual = %v", val)
    36  	}
    37  
    38  	subPrefix, val, ok := tree.Follow("test_hello")
    39  	if !ok {
    40  		t.Errorf("expecting to follow key=test_hello")
    41  	}
    42  	if val.(int) != 1 {
    43  		t.Errorf("expecting test_hello's value=1, actual = %v", val)
    44  	}
    45  	if subPrefix != "test" {
    46  		t.Errorf("expecting test_hello's subprefix=test, actual = %v", subPrefix)
    47  	}
    48  
    49  	tree.Store("test", 11)
    50  	val, ok = tree.Load("test")
    51  	if !ok {
    52  		t.Errorf("expecting to find key=test")
    53  	}
    54  	if val.(int) != 11 {
    55  		t.Errorf("expecting test's value=11, actual = %v", val)
    56  	}
    57  	if tree.Depth() != 4 {
    58  		t.Errorf("expecting depth 4, actual = %v", tree.Depth())
    59  	}
    60  
    61  	tree.Store("testing", 2)
    62  	if tree.Depth() != 7 {
    63  		t.Errorf("expecting depth 7, actual = %v", tree.Depth())
    64  	}
    65  	tree.Store("abcd", 0)
    66  	if tree.Depth() != 7 {
    67  		t.Errorf("expecting depth 7, actual = %v", tree.Depth())
    68  	}
    69  	if tree.Count() != 3 {
    70  		t.Errorf("expecting count 3, actual = %v", tree.Count())
    71  	}
    72  
    73  	found := false
    74  	tree.Traversal(traversal.Preorder, ternary_search_tree.HandlerFunc(
    75  		func(key []byte, val any) bool {
    76  			if string(key) == "test" && val.(int) == 11 {
    77  				found = true
    78  				return false
    79  			}
    80  			return true
    81  		}))
    82  	if !found {
    83  		t.Errorf("expecting iterator to find test")
    84  	}
    85  
    86  	val, ok = tree.Load("testing")
    87  	if !ok {
    88  		t.Errorf("expecting to find key=testing")
    89  	}
    90  	if val.(int) != 2 {
    91  		t.Errorf("expecting testing's value=2")
    92  	}
    93  
    94  	val, ok = tree.Load("abcd")
    95  	if !ok {
    96  		t.Errorf("expecting to find key=abcd")
    97  	}
    98  	if val.(int) != 0 {
    99  		t.Errorf("expecting abcd's value=0")
   100  	}
   101  
   102  	tree.Remove("testing", true)
   103  	tree.Remove("abcd", false)
   104  
   105  	v, ok := tree.Remove("test", false)
   106  	if !ok {
   107  		t.Errorf("expecting test can be found to be removed")
   108  	}
   109  
   110  	if tree.Count() != 0 {
   111  		t.Errorf("expecting count 3, actual = %v", tree.Count())
   112  	}
   113  
   114  	if tree.Depth() != 0 {
   115  		t.Errorf("expecting depth 0, actual = %v", tree.Depth())
   116  	}
   117  
   118  	if tree.Contains("test") {
   119  		t.Errorf("expecting not to find key=test")
   120  	}
   121  	if v.(int) != 11 {
   122  		t.Errorf("expecting test's value=11, actual = %v", val)
   123  	}
   124  }
   125  
   126  func TestTernarySearchTree_String1(t *testing.T) {
   127  
   128  	tree := ternary_search_tree.New()
   129  	tree.Store("abcd", 0)
   130  	tree.Store("abcd1234ABCD", 2)
   131  	tree.Store("abcd1234", 1)
   132  	s := tree.String()
   133  	expect := `abcd:0
   134  abcd1234:1
   135  abcd1234ABCD:2`
   136  	if s != expect {
   137  		t.Errorf("actual:\n%s\nexpect:\n%s", s, expect)
   138  	}
   139  }
   140  
   141  func TestTernarySearchTree_String2(t *testing.T) {
   142  
   143  	tree := ternary_search_tree.New()
   144  	tree.Store("abcd", 0)
   145  	tree.Store("1234", 1)
   146  	s := tree.String()
   147  	expect := `1234:1
   148  abcd:0`
   149  	if s != expect {
   150  		t.Errorf("actual:\n%s\nexpect:\n%s", s, expect)
   151  	}
   152  }