trpc.group/trpc-go/trpc-go@v1.0.3/internal/dat/dat_test.go (about) 1 // 2 // 3 // Tencent is pleased to support the open source community by making tRPC available. 4 // 5 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 6 // All rights reserved. 7 // 8 // If you have downloaded a copy of the tRPC source code from Tencent, 9 // please note that tRPC source code is licensed under the Apache 2.0 License, 10 // A copy of the Apache 2.0 License is included in this file. 11 // 12 // 13 14 package dat_test 15 16 import ( 17 "testing" 18 19 "trpc.group/trpc-go/trpc-go/internal/dat" 20 ) 21 22 var fps = [][]string{ 23 {"baz"}, 24 {"foobar", "foo"}, 25 {"foobar", "bar"}, 26 {"foobar", "baz", "baz"}, 27 {"foo", "bar", "baz", "qux"}, 28 } 29 30 func TestBuild(t *testing.T) { 31 if got, err := dat.Build(fps); err != nil || got == nil { 32 t.Errorf("Build(%v) (got, error) = %v, %v, (want, wantErr) = (not nil, nil)", fps, got, err) 33 } 34 } 35 36 func TestCommonPrefixSearch(t *testing.T) { 37 trie := mustBuild(t, fps) 38 for _, tt := range []struct { 39 name string 40 input []string 41 want bool 42 }{ 43 { 44 name: "fail-1", 45 input: []string{"foobar", "baz"}, 46 want: false, 47 }, 48 { 49 name: "fail-2", 50 input: []string{"bar1"}, 51 want: false, 52 }, 53 { 54 name: "fail-3", 55 input: []string{}, 56 want: false, 57 }, 58 { 59 name: "fail-4", 60 input: []string{"foobar"}, 61 want: false, 62 }, 63 { 64 name: "success-1", 65 input: []string{"foobar", "foo"}, 66 want: true, 67 }, 68 { 69 name: "success-2", 70 input: []string{"foo", "bar", "baz", "qux"}, 71 want: true, 72 }, 73 { 74 name: "success-3", 75 input: []string{"foo", "bar", "baz", "qux", "any"}, 76 want: true, 77 }, 78 } { 79 t.Run(tt.name, func(t *testing.T) { 80 if got := trie.CommonPrefixSearch(tt.input); got != tt.want { 81 t.Errorf("dat.CommonPrefixSearch(%v) got = %v, want %v", tt.input, got, tt.want) 82 } 83 }) 84 } 85 } 86 87 func mustBuild(t *testing.T, fps [][]string) *dat.DoubleArrayTrie { 88 t.Helper() 89 trie, err := dat.Build(fps) 90 if err != nil { 91 t.Fatalf("could not build DoubleArrayTrie under test: %v", err) 92 } 93 return trie 94 }