github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/text/collate/colltab/colelem_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  	"unicode"
    10  )
    11  
    12  type ceTest struct {
    13  	f   func(inout []int) (Elem, ceType)
    14  	arg []int
    15  }
    16  
    17  func makeCE(weights []int) Elem {
    18  	ce, _ := MakeElem(weights[0], weights[1], weights[2], uint8(weights[3]))
    19  	return ce
    20  }
    21  
    22  func makeContractIndex(index, n, offset int) Elem {
    23  	const (
    24  		contractID            = 0xC0000000
    25  		maxNBits              = 4
    26  		maxTrieIndexBits      = 12
    27  		maxContractOffsetBits = 13
    28  	)
    29  	ce := Elem(contractID)
    30  	ce += Elem(offset << (maxNBits + maxTrieIndexBits))
    31  	ce += Elem(index << maxNBits)
    32  	ce += Elem(n)
    33  	return ce
    34  }
    35  
    36  func makeExpandIndex(index int) Elem {
    37  	const expandID = 0xE0000000
    38  	return expandID + Elem(index)
    39  }
    40  
    41  func makeDecompose(t1, t2 int) Elem {
    42  	const decompID = 0xF0000000
    43  	return Elem(t2<<8+t1) + decompID
    44  }
    45  
    46  func normalCE(inout []int) (ce Elem, t ceType) {
    47  	ce = makeCE(inout)
    48  	inout[0] = ce.Primary()
    49  	inout[1] = ce.Secondary()
    50  	inout[2] = int(ce.Tertiary())
    51  	inout[3] = int(ce.CCC())
    52  	return ce, ceNormal
    53  }
    54  
    55  func expandCE(inout []int) (ce Elem, t ceType) {
    56  	ce = makeExpandIndex(inout[0])
    57  	inout[0] = splitExpandIndex(ce)
    58  	return ce, ceExpansionIndex
    59  }
    60  
    61  func contractCE(inout []int) (ce Elem, t ceType) {
    62  	ce = makeContractIndex(inout[0], inout[1], inout[2])
    63  	i, n, o := splitContractIndex(ce)
    64  	inout[0], inout[1], inout[2] = i, n, o
    65  	return ce, ceContractionIndex
    66  }
    67  
    68  func decompCE(inout []int) (ce Elem, t ceType) {
    69  	ce = makeDecompose(inout[0], inout[1])
    70  	t1, t2 := splitDecompose(ce)
    71  	inout[0], inout[1] = int(t1), int(t2)
    72  	return ce, ceDecompose
    73  }
    74  
    75  var ceTests = []ceTest{
    76  	{normalCE, []int{0, 0, 0, 0}},
    77  	{normalCE, []int{0, 30, 3, 0}},
    78  	{normalCE, []int{0, 30, 3, 0xFF}},
    79  	{normalCE, []int{100, defaultSecondary, defaultTertiary, 0}},
    80  	{normalCE, []int{100, defaultSecondary, defaultTertiary, 0xFF}},
    81  	{normalCE, []int{100, defaultSecondary, 3, 0}},
    82  	{normalCE, []int{0x123, defaultSecondary, 8, 0xFF}},
    83  
    84  	{contractCE, []int{0, 0, 0}},
    85  	{contractCE, []int{1, 1, 1}},
    86  	{contractCE, []int{1, (1 << maxNBits) - 1, 1}},
    87  	{contractCE, []int{(1 << maxTrieIndexBits) - 1, 1, 1}},
    88  	{contractCE, []int{1, 1, (1 << maxContractOffsetBits) - 1}},
    89  
    90  	{expandCE, []int{0}},
    91  	{expandCE, []int{5}},
    92  	{expandCE, []int{(1 << maxExpandIndexBits) - 1}},
    93  
    94  	{decompCE, []int{0, 0}},
    95  	{decompCE, []int{1, 1}},
    96  	{decompCE, []int{0x1F, 0x1F}},
    97  }
    98  
    99  func TestColElem(t *testing.T) {
   100  	for i, tt := range ceTests {
   101  		inout := make([]int, len(tt.arg))
   102  		copy(inout, tt.arg)
   103  		ce, typ := tt.f(inout)
   104  		if ce.ctype() != typ {
   105  			t.Errorf("%d: type is %d; want %d (ColElem: %X)", i, ce.ctype(), typ, ce)
   106  		}
   107  		for j, a := range tt.arg {
   108  			if inout[j] != a {
   109  				t.Errorf("%d: argument %d is %X; want %X (ColElem: %X)", i, j, inout[j], a, ce)
   110  			}
   111  		}
   112  	}
   113  }
   114  
   115  type implicitTest struct {
   116  	r rune
   117  	p int
   118  }
   119  
   120  var implicitTests = []implicitTest{
   121  	{0x33FF, 0x533FF},
   122  	{0x3400, 0x23400},
   123  	{0x4DC0, 0x54DC0},
   124  	{0x4DFF, 0x54DFF},
   125  	{0x4E00, 0x14E00},
   126  	{0x9FCB, 0x19FCB},
   127  	{0xA000, 0x5A000},
   128  	{0xF8FF, 0x5F8FF},
   129  	{0xF900, 0x1F900},
   130  	{0xFA23, 0x1FA23},
   131  	{0xFAD9, 0x1FAD9},
   132  	{0xFB00, 0x5FB00},
   133  	{0x20000, 0x40000},
   134  	{0x2B81C, 0x4B81C},
   135  	{unicode.MaxRune, 0x15FFFF}, // maximum primary value
   136  }
   137  
   138  func TestImplicit(t *testing.T) {
   139  	for _, tt := range implicitTests {
   140  		if p := implicitPrimary(tt.r); p != tt.p {
   141  			t.Errorf("%U: was %X; want %X", tt.r, p, tt.p)
   142  		}
   143  	}
   144  }
   145  
   146  func TestUpdateTertiary(t *testing.T) {
   147  	tests := []struct {
   148  		in, out Elem
   149  		t       uint8
   150  	}{
   151  		{0x4000FE20, 0x0000FE8A, 0x0A},
   152  		{0x4000FE21, 0x0000FEAA, 0x0A},
   153  		{0x0000FE8B, 0x0000FE83, 0x03},
   154  		{0x82FF0188, 0x9BFF0188, 0x1B},
   155  		{0xAFF0CC02, 0xAFF0CC1B, 0x1B},
   156  	}
   157  	for i, tt := range tests {
   158  		if out := tt.in.updateTertiary(tt.t); out != tt.out {
   159  			t.Errorf("%d: was %X; want %X", i, out, tt.out)
   160  		}
   161  	}
   162  }