github.com/liquid-dev/text@v0.3.3-liquid/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  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  func lookup(c *ContractTrieSet, nnode int, s []uint8) (i, n int) {
   109  	scan := c.scanner(0, nnode, s)
   110  	scan.scan(0)
   111  	return scan.result()
   112  }
   113  
   114  func TestLookupContraction(t *testing.T) {
   115  	for i, tt := range lookupTests {
   116  		cts := ContractTrieSet(tt.tries)
   117  		for j, lu := range tt.lookup {
   118  			str := lu.str
   119  			for _, s := range []string{str, str + "X"} {
   120  				const msg = `%d:%d: %s of "%s" %v; want %v`
   121  				offset, n := lookup(&cts, tt.n, []byte(s))
   122  				if offset != lu.offset {
   123  					t.Errorf(msg, i, j, "offset", s, offset, lu.offset)
   124  				}
   125  				if n != lu.n {
   126  					t.Errorf(msg, i, j, "bytes consumed", s, n, len(str))
   127  				}
   128  			}
   129  		}
   130  	}
   131  }