github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/text/internal/colltab/contract_test.go (about)

     1  // Copyright 2012 The Go Authors. 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 colltab
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  type lookupStrings struct {
    12  	str    string
    13  	offset int
    14  	n      int // bytes consumed from input
    15  }
    16  
    17  var lookupTests = []struct {
    18  	lookup []lookupStrings
    19  	n      int
    20  	tries  contractTrieSet
    21  }{
    22  	{
    23  		[]lookupStrings{
    24  			{"abc", 1, 3},
    25  			{"a", 0, 0},
    26  			{"b", 0, 0},
    27  			{"c", 0, 0},
    28  			{"d", 0, 0},
    29  		},
    30  		1,
    31  		contractTrieSet{
    32  			{'a', 0, 1, 0xFF},
    33  			{'b', 0, 1, 0xFF},
    34  			{'c', 'c', 0, 1},
    35  		},
    36  	},
    37  	{
    38  		[]lookupStrings{
    39  			{"abc", 1, 3},
    40  			{"abd", 2, 3},
    41  			{"abe", 3, 3},
    42  			{"a", 0, 0},
    43  			{"ab", 0, 0},
    44  			{"d", 0, 0},
    45  			{"f", 0, 0},
    46  		},
    47  		1,
    48  		contractTrieSet{
    49  			{'a', 0, 1, 0xFF},
    50  			{'b', 0, 1, 0xFF},
    51  			{'c', 'e', 0, 1},
    52  		},
    53  	},
    54  	{
    55  		[]lookupStrings{
    56  			{"abc", 1, 3},
    57  			{"ab", 2, 2},
    58  			{"a", 3, 1},
    59  			{"abcd", 1, 3},
    60  			{"abe", 2, 2},
    61  		},
    62  		1,
    63  		contractTrieSet{
    64  			{'a', 0, 1, 3},
    65  			{'b', 0, 1, 2},
    66  			{'c', 'c', 0, 1},
    67  		},
    68  	},
    69  	{
    70  		[]lookupStrings{
    71  			{"abc", 1, 3},
    72  			{"abd", 2, 3},
    73  			{"ab", 3, 2},
    74  			{"ac", 4, 2},
    75  			{"a", 5, 1},
    76  			{"b", 6, 1},
    77  			{"ba", 6, 1},
    78  		},
    79  		2,
    80  		contractTrieSet{
    81  			{'b', 'b', 0, 6},
    82  			{'a', 0, 2, 5},
    83  			{'c', 'c', 0, 4},
    84  			{'b', 0, 1, 3},
    85  			{'c', 'd', 0, 1},
    86  		},
    87  	},
    88  	{
    89  		[]lookupStrings{
    90  			{"bcde", 2, 4},
    91  			{"bc", 7, 2},
    92  			{"ab", 6, 2},
    93  			{"bcd", 5, 3},
    94  			{"abcd", 1, 4},
    95  			{"abc", 4, 3},
    96  			{"bcdf", 3, 4},
    97  		},
    98  		2,
    99  		contractTrieSet{
   100  			{'b', 3, 1, 0xFF},
   101  			{'a', 0, 1, 0xFF},
   102  			{'b', 0, 1, 6},
   103  			{'c', 0, 1, 4},
   104  			{'d', 'd', 0, 1},
   105  			{'c', 0, 1, 7},
   106  			{'d', 0, 1, 5},
   107  			{'e', 'f', 0, 2},
   108  		},
   109  	},
   110  }
   111  
   112  func lookup(c *contractTrieSet, nnode int, s []uint8) (i, n int) {
   113  	scan := c.scanner(0, nnode, s)
   114  	scan.scan(0)
   115  	return scan.result()
   116  }
   117  
   118  func TestLookupContraction(t *testing.T) {
   119  	for i, tt := range lookupTests {
   120  		cts := contractTrieSet(tt.tries)
   121  		for j, lu := range tt.lookup {
   122  			str := lu.str
   123  			for _, s := range []string{str, str + "X"} {
   124  				const msg = "%d:%d: %s of %q %v; want %v"
   125  				offset, n := lookup(&cts, tt.n, []byte(s))
   126  				if offset != lu.offset {
   127  					t.Errorf(msg, i, j, "offset", s, offset, lu.offset)
   128  				}
   129  				if n != lu.n {
   130  					t.Errorf(msg, i, j, "bytes consumed", s, n, len(str))
   131  				}
   132  			}
   133  		}
   134  	}
   135  }