github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/text/collate/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  type LookupTest struct {
    18  	lookup []lookupStrings
    19  	n      int
    20  	tries  contractTrieSet
    21  }
    22  
    23  var lookupTests = []LookupTest{
    24  	{[]lookupStrings{
    25  		{"abc", 1, 3},
    26  		{"a", 0, 0},
    27  		{"b", 0, 0},
    28  		{"c", 0, 0},
    29  		{"d", 0, 0},
    30  	},
    31  		1,
    32  		contractTrieSet{
    33  			{'a', 0, 1, 0xFF},
    34  			{'b', 0, 1, 0xFF},
    35  			{'c', 'c', 0, 1},
    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  	{[]lookupStrings{
    55  		{"abc", 1, 3},
    56  		{"ab", 2, 2},
    57  		{"a", 3, 1},
    58  		{"abcd", 1, 3},
    59  		{"abe", 2, 2},
    60  	},
    61  		1,
    62  		contractTrieSet{
    63  			{'a', 0, 1, 3},
    64  			{'b', 0, 1, 2},
    65  			{'c', 'c', 0, 1},
    66  		},
    67  	},
    68  	{[]lookupStrings{
    69  		{"abc", 1, 3},
    70  		{"abd", 2, 3},
    71  		{"ab", 3, 2},
    72  		{"ac", 4, 2},
    73  		{"a", 5, 1},
    74  		{"b", 6, 1},
    75  		{"ba", 6, 1},
    76  	},
    77  		2,
    78  		contractTrieSet{
    79  			{'b', 'b', 0, 6},
    80  			{'a', 0, 2, 5},
    81  			{'c', 'c', 0, 4},
    82  			{'b', 0, 1, 3},
    83  			{'c', 'd', 0, 1},
    84  		},
    85  	},
    86  	{[]lookupStrings{
    87  		{"bcde", 2, 4},
    88  		{"bc", 7, 2},
    89  		{"ab", 6, 2},
    90  		{"bcd", 5, 3},
    91  		{"abcd", 1, 4},
    92  		{"abc", 4, 3},
    93  		{"bcdf", 3, 4},
    94  	},
    95  		2,
    96  		contractTrieSet{
    97  			{'b', 3, 1, 0xFF},
    98  			{'a', 0, 1, 0xFF},
    99  			{'b', 0, 1, 6},
   100  			{'c', 0, 1, 4},
   101  			{'d', 'd', 0, 1},
   102  			{'c', 0, 1, 7},
   103  			{'d', 0, 1, 5},
   104  			{'e', 'f', 0, 2},
   105  		},
   106  	},
   107  }
   108  
   109  func lookup(c *contractTrieSet, nnode int, s []uint8) (i, n int) {
   110  	scan := c.scanner(0, nnode, s)
   111  	scan.scan(0)
   112  	return scan.result()
   113  }
   114  
   115  func TestLookupContraction(t *testing.T) {
   116  	for i, tt := range lookupTests {
   117  		cts := contractTrieSet(tt.tries)
   118  		for j, lu := range tt.lookup {
   119  			str := lu.str
   120  			for _, s := range []string{str, str + "X"} {
   121  				const msg = `%d:%d: %s of "%s" %v; want %v`
   122  				offset, n := lookup(&cts, tt.n, []byte(s))
   123  				if offset != lu.offset {
   124  					t.Errorf(msg, i, j, "offset", s, offset, lu.offset)
   125  				}
   126  				if n != lu.n {
   127  					t.Errorf(msg, i, j, "bytes consumed", s, n, len(str))
   128  				}
   129  			}
   130  		}
   131  	}
   132  }