github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/golang/text/internal/number/plural_test.go (about)

     1  // Copyright 2016 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 number
     6  
     7  import (
     8  	"strconv"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/insionng/yougam/libraries/x/text/language"
    13  )
    14  
    15  func TestOrdinal(t *testing.T) {
    16  	testPlurals(t, &ordinalData, ordinalTests)
    17  }
    18  
    19  func TestCardinal(t *testing.T) {
    20  	testPlurals(t, &cardinalData, cardinalTests)
    21  }
    22  
    23  func testPlurals(t *testing.T, p *pluralRules, testCases []pluralTest) {
    24  	for _, tc := range testCases {
    25  		for _, loc := range strings.Split(tc.locales, " ") {
    26  			langIndex, _ := language.CompactIndex(language.MustParse(loc))
    27  			// Test integers
    28  			for _, s := range tc.integer {
    29  				a := strings.Split(s, "~")
    30  				from := parseUint(t, a[0])
    31  				to := from
    32  				if len(a) > 1 {
    33  					to = parseUint(t, a[1])
    34  				}
    35  				for n := from; n <= to; n++ {
    36  					if f := matchPlural(p, langIndex, n, 0, 0); f != tc.form {
    37  						t.Errorf("%s:int(%d) = %v; want %v", loc, n, f, tc.form)
    38  					}
    39  				}
    40  			}
    41  			// Test decimals
    42  			for _, s := range tc.decimal {
    43  				a := strings.Split(s, "~")
    44  				from, scale := parseFixedPoint(t, a[0])
    45  				to := from
    46  				if len(a) > 1 {
    47  					var toScale int
    48  					if to, toScale = parseFixedPoint(t, a[1]); toScale != scale {
    49  						t.Fatalf("%s:%s: non-matching scales %d versus %d", loc, s, scale, toScale)
    50  					}
    51  				}
    52  				m := 1
    53  				for i := 0; i < scale; i++ {
    54  					m *= 10
    55  				}
    56  				for n := from; n <= to; n++ {
    57  					if f := matchPlural(p, langIndex, n/m, n%m, scale); f != tc.form {
    58  						t.Errorf("%[1]s:dec(%[2]d.%0[4]*[3]d) = %[5]v; want %[6]v", loc, n/m, n%m, scale, f, tc.form)
    59  					}
    60  				}
    61  			}
    62  		}
    63  	}
    64  }
    65  
    66  func parseUint(t *testing.T, s string) int {
    67  	val, err := strconv.ParseUint(s, 10, 32)
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	return int(val)
    72  }
    73  
    74  func parseFixedPoint(t *testing.T, s string) (val, scale int) {
    75  	p := strings.Index(s, ".")
    76  	s = strings.Replace(s, ".", "", 1)
    77  	v, err := strconv.ParseUint(s, 10, 32)
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  	return int(v), len(s) - p
    82  }
    83  
    84  func BenchmarkPluralSimpleCases(b *testing.B) {
    85  	p := &cardinalData
    86  	en, _ := language.CompactIndex(language.English)
    87  	zh, _ := language.CompactIndex(language.Chinese)
    88  	for i := 0; i < b.N; i++ {
    89  		matchPlural(p, en, 0, 0, 0)  // 0
    90  		matchPlural(p, en, 1, 0, 0)  // 1
    91  		matchPlural(p, en, 2, 12, 3) // 2.120
    92  		matchPlural(p, zh, 0, 0, 0)  // 0
    93  		matchPlural(p, zh, 1, 0, 0)  // 1
    94  		matchPlural(p, zh, 2, 12, 3) // 2.120
    95  	}
    96  }
    97  
    98  func BenchmarkPluralComplexCases(b *testing.B) {
    99  	p := &cardinalData
   100  	ar, _ := language.CompactIndex(language.Arabic)
   101  	lv, _ := language.CompactIndex(language.Latvian)
   102  	for i := 0; i < b.N; i++ {
   103  		matchPlural(p, lv, 0, 19, 2)    // 0.19
   104  		matchPlural(p, lv, 11, 0, 3)    // 11.000
   105  		matchPlural(p, lv, 100, 123, 4) // 0.1230
   106  		matchPlural(p, ar, 0, 0, 0)     // 0
   107  		matchPlural(p, ar, 110, 0, 0)   // 110
   108  		matchPlural(p, ar, 99, 99, 2)   // 99.99
   109  	}
   110  }