github.com/liquid-dev/text@v0.3.3-liquid/internal/colltab/collate_test.go (about)

     1  // Copyright 2014 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_test
     6  
     7  // This file contains tests which need to import package collate, which causes
     8  // an import cycle when done within package colltab itself.
     9  
    10  import (
    11  	"bytes"
    12  	"testing"
    13  	"unicode"
    14  
    15  	"github.com/liquid-dev/text/collate"
    16  	"github.com/liquid-dev/text/language"
    17  	"github.com/liquid-dev/text/unicode/rangetable"
    18  )
    19  
    20  // assigned is used to only test runes that are inside the scope of the Unicode
    21  // version used to generation the collation table.
    22  var assigned = rangetable.Assigned(collate.UnicodeVersion)
    23  
    24  func TestNonDigits(t *testing.T) {
    25  	c := collate.New(language.English, collate.Loose, collate.Numeric)
    26  
    27  	// Verify that all non-digit numbers sort outside of the number range.
    28  	for r, hi := rune(unicode.N.R16[0].Lo), rune(unicode.N.R32[0].Hi); r <= hi; r++ {
    29  		if unicode.In(r, unicode.Nd) || !unicode.In(r, assigned) {
    30  			continue
    31  		}
    32  		if a := string(r); c.CompareString(a, "0") != -1 && c.CompareString(a, "999999") != 1 {
    33  			t.Errorf("%+q non-digit number is collated as digit", a)
    34  		}
    35  	}
    36  }
    37  
    38  func TestNumericCompare(t *testing.T) {
    39  	c := collate.New(language.English, collate.Loose, collate.Numeric)
    40  
    41  	// Iterate over all digits.
    42  	for _, r16 := range unicode.Nd.R16 {
    43  		testDigitCompare(t, c, rune(r16.Lo), rune(r16.Hi))
    44  	}
    45  	for _, r32 := range unicode.Nd.R32 {
    46  		testDigitCompare(t, c, rune(r32.Lo), rune(r32.Hi))
    47  	}
    48  }
    49  
    50  func testDigitCompare(t *testing.T, c *collate.Collator, zero, nine rune) {
    51  	if !unicode.In(zero, assigned) {
    52  		return
    53  	}
    54  	n := int(nine - zero + 1)
    55  	if n%10 != 0 {
    56  		t.Fatalf("len([%+q, %+q]) = %d; want a multiple of 10", zero, nine, n)
    57  	}
    58  	for _, tt := range []struct {
    59  		prefix string
    60  		b      [11]string
    61  	}{
    62  		{
    63  			prefix: "",
    64  			b: [11]string{
    65  				"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
    66  			},
    67  		},
    68  		{
    69  			prefix: "1",
    70  			b: [11]string{
    71  				"10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
    72  			},
    73  		},
    74  		{
    75  			prefix: "0",
    76  			b: [11]string{
    77  				"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10",
    78  			},
    79  		},
    80  		{
    81  			prefix: "00",
    82  			b: [11]string{
    83  				"000", "001", "002", "003", "004", "005", "006", "007", "008", "009", "010",
    84  			},
    85  		},
    86  		{
    87  			prefix: "9",
    88  			b: [11]string{
    89  				"90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100",
    90  			},
    91  		},
    92  	} {
    93  		for k := 0; k <= n; k++ {
    94  			i := k % 10
    95  			a := tt.prefix + string(zero+rune(i))
    96  			for j, b := range tt.b {
    97  				want := 0
    98  				switch {
    99  				case i < j:
   100  					want = -1
   101  				case i > j:
   102  					want = 1
   103  				}
   104  				got := c.CompareString(a, b)
   105  				if got != want {
   106  					t.Errorf("Compare(%+q, %+q) = %d; want %d", a, b, got, want)
   107  					return
   108  				}
   109  			}
   110  		}
   111  	}
   112  }
   113  
   114  func BenchmarkNumericWeighter(b *testing.B) {
   115  	c := collate.New(language.English, collate.Numeric)
   116  	input := bytes.Repeat([]byte("Testing, testing 123..."), 100)
   117  	b.SetBytes(int64(2 * len(input)))
   118  	for i := 0; i < b.N; i++ {
   119  		c.Compare(input, input)
   120  	}
   121  }