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

     1  // Copyright 2015 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  	"reflect"
     9  	"testing"
    10  	"unsafe"
    11  )
    12  
    13  var testCases = []struct {
    14  	pat  string
    15  	want *Format
    16  }{{
    17  	"#",
    18  	&Format{
    19  		FormatWidth: 1,
    20  		// TODO: Should MinIntegerDigits be 1?
    21  	},
    22  }, {
    23  	"0",
    24  	&Format{
    25  		FormatWidth:      1,
    26  		MinIntegerDigits: 1,
    27  	},
    28  }, {
    29  	"0000",
    30  	&Format{
    31  		FormatWidth:      4,
    32  		MinIntegerDigits: 4,
    33  	},
    34  }, {
    35  	".#",
    36  	&Format{
    37  		FormatWidth:       2,
    38  		MaxFractionDigits: 1,
    39  	},
    40  }, {
    41  	"#0.###",
    42  	&Format{
    43  		FormatWidth:       6,
    44  		MinIntegerDigits:  1,
    45  		MaxFractionDigits: 3,
    46  	},
    47  }, {
    48  	"#0.######",
    49  	&Format{
    50  		FormatWidth:       9,
    51  		MinIntegerDigits:  1,
    52  		MaxFractionDigits: 6,
    53  	},
    54  }, {
    55  	"#,##0.###",
    56  	&Format{
    57  		FormatWidth:       9,
    58  		GroupingSize:      [2]uint8{3, 0},
    59  		MinIntegerDigits:  1,
    60  		MaxFractionDigits: 3,
    61  	},
    62  }, {
    63  	"#,##,##0.###",
    64  	&Format{
    65  		FormatWidth:       12,
    66  		GroupingSize:      [2]uint8{3, 2},
    67  		MinIntegerDigits:  1,
    68  		MaxFractionDigits: 3,
    69  	},
    70  }, {
    71  	// Ignore additional separators.
    72  	"#,####,##,##0.###",
    73  	&Format{
    74  		FormatWidth:       17,
    75  		GroupingSize:      [2]uint8{3, 2},
    76  		MinIntegerDigits:  1,
    77  		MaxFractionDigits: 3,
    78  	},
    79  }, {
    80  	"#E0",
    81  	&Format{
    82  		FormatWidth:       3,
    83  		MaxIntegerDigits:  1,
    84  		MinExponentDigits: 1,
    85  	},
    86  }, {
    87  	"0E0",
    88  	&Format{
    89  		FormatWidth:       3,
    90  		MinIntegerDigits:  1,
    91  		MinExponentDigits: 1,
    92  	},
    93  }, {
    94  	"##00.0#E0",
    95  	&Format{
    96  		FormatWidth:       9,
    97  		MinIntegerDigits:  2,
    98  		MaxIntegerDigits:  4,
    99  		MinFractionDigits: 1,
   100  		MaxFractionDigits: 2,
   101  		MinExponentDigits: 1,
   102  	},
   103  }, {
   104  	"#00.0E+0",
   105  	&Format{
   106  		FormatWidth:       8,
   107  		Flags:             AlwaysExpSign,
   108  		MinIntegerDigits:  2,
   109  		MaxIntegerDigits:  3,
   110  		MinFractionDigits: 1,
   111  		MaxFractionDigits: 1,
   112  		MinExponentDigits: 1,
   113  	},
   114  }, {
   115  	"0.0E++0",
   116  	nil,
   117  }, {
   118  	"#0E+",
   119  	nil,
   120  }, {
   121  	// significant digits
   122  	"@",
   123  	&Format{
   124  		FormatWidth:          1,
   125  		MinSignificantDigits: 1,
   126  		MaxSignificantDigits: 1,
   127  	},
   128  }, {
   129  	// significant digits
   130  	"@@@@",
   131  	&Format{
   132  		FormatWidth:          4,
   133  		MinSignificantDigits: 4,
   134  		MaxSignificantDigits: 4,
   135  	},
   136  }, {
   137  	"@###",
   138  	&Format{
   139  		FormatWidth:          4,
   140  		MinSignificantDigits: 1,
   141  		MaxSignificantDigits: 4,
   142  	},
   143  }, {
   144  	// Exponents in significant digits mode gets normalized.
   145  	"@@E0",
   146  	&Format{
   147  		FormatWidth:       4,
   148  		MinIntegerDigits:  1,
   149  		MaxIntegerDigits:  1,
   150  		MinFractionDigits: 1,
   151  		MaxFractionDigits: 1,
   152  		MinExponentDigits: 1,
   153  	},
   154  }, {
   155  	"@###E00",
   156  	&Format{
   157  		FormatWidth:       7,
   158  		MinIntegerDigits:  1,
   159  		MaxIntegerDigits:  1,
   160  		MinFractionDigits: 0,
   161  		MaxFractionDigits: 3,
   162  		MinExponentDigits: 2,
   163  	},
   164  }, {
   165  	// The significant digits mode does not allow fractions.
   166  	"@###.#E0",
   167  	nil,
   168  }, {
   169  	//alternative negative pattern
   170  	"#0.###;(#0.###)",
   171  	&Format{
   172  		Affix:             "\x00\x00\x01(\x01)",
   173  		NegOffset:         2,
   174  		FormatWidth:       6,
   175  		MinIntegerDigits:  1,
   176  		MaxFractionDigits: 3,
   177  	},
   178  }, {
   179  	// Rounding increments
   180  	"1.05",
   181  	&Format{
   182  		RoundIncrement:    105,
   183  		FormatWidth:       4,
   184  		MinIntegerDigits:  1,
   185  		MinFractionDigits: 2,
   186  		MaxFractionDigits: 2,
   187  	},
   188  }, {
   189  	"0.0%",
   190  	&Format{
   191  		Affix:             "\x00\x01%",
   192  		Multiplier:        100,
   193  		FormatWidth:       4,
   194  		MinIntegerDigits:  1,
   195  		MinFractionDigits: 1,
   196  		MaxFractionDigits: 1,
   197  	},
   198  }, {
   199  	"0.0‰",
   200  	&Format{
   201  		Affix:             "\x00\x03‰",
   202  		Multiplier:        1000,
   203  		FormatWidth:       4,
   204  		MinIntegerDigits:  1,
   205  		MinFractionDigits: 1,
   206  		MaxFractionDigits: 1,
   207  	},
   208  }, {
   209  	"#,##0.00¤",
   210  	&Format{
   211  		Affix:             "\x00\x02¤",
   212  		FormatWidth:       9,
   213  		GroupingSize:      [2]uint8{3, 0},
   214  		MinIntegerDigits:  1,
   215  		MinFractionDigits: 2,
   216  		MaxFractionDigits: 2,
   217  	},
   218  }, {
   219  	"#,##0.00 ¤;(#,##0.00 ¤)",
   220  	&Format{Affix: "\x00\x04\u00a0¤\x01(\x05\u00a0¤)",
   221  		NegOffset:         6,
   222  		Multiplier:        0,
   223  		FormatWidth:       10,
   224  		GroupingSize:      [2]uint8{3, 0},
   225  		MinIntegerDigits:  1,
   226  		MinFractionDigits: 2,
   227  		MaxFractionDigits: 2,
   228  	},
   229  }, {
   230  	// padding
   231  	"*x#",
   232  	&Format{
   233  		PadRune:     'x',
   234  		FormatWidth: 1,
   235  	},
   236  }, {
   237  	// padding
   238  	"#*x",
   239  	&Format{
   240  		PadRune:     'x',
   241  		FormatWidth: 1,
   242  		Flags:       PadBeforeSuffix,
   243  	},
   244  }, {
   245  	"*xpre#suf",
   246  	&Format{
   247  		Affix:       "\x03pre\x03suf",
   248  		PadRune:     'x',
   249  		FormatWidth: 7,
   250  	},
   251  }, {
   252  	"pre*x#suf",
   253  	&Format{
   254  		Affix:       "\x03pre\x03suf",
   255  		PadRune:     'x',
   256  		FormatWidth: 7,
   257  		Flags:       PadAfterPrefix,
   258  	},
   259  }, {
   260  	"pre#*xsuf",
   261  	&Format{
   262  		Affix:       "\x03pre\x03suf",
   263  		PadRune:     'x',
   264  		FormatWidth: 7,
   265  		Flags:       PadBeforeSuffix,
   266  	},
   267  }, {
   268  	"pre#suf*x",
   269  	&Format{
   270  		Affix:       "\x03pre\x03suf",
   271  		PadRune:     'x',
   272  		FormatWidth: 7,
   273  		Flags:       PadAfterSuffix,
   274  	},
   275  }, {
   276  	// no duplicate padding
   277  	"*xpre#suf*x", nil,
   278  }, {
   279  	// no duplicate padding
   280  	"*xpre#suf*x", nil,
   281  }}
   282  
   283  func TestParsePattern(t *testing.T) {
   284  	for i, tc := range testCases {
   285  		f, err := ParsePattern(tc.pat)
   286  		if !reflect.DeepEqual(f, tc.want) {
   287  			t.Errorf("%d:%s:\ngot %#v;\nwant %#v", i, tc.pat, f, tc.want)
   288  		}
   289  		if got, want := err != nil, tc.want == nil; got != want {
   290  			t.Errorf("%d:%s:error: got %v; want %v", i, tc.pat, err, want)
   291  		}
   292  	}
   293  }
   294  
   295  func TestPatternSize(t *testing.T) {
   296  	if sz := unsafe.Sizeof(Format{}); sz > 48 {
   297  		t.Errorf("got %d; want 48", sz)
   298  	}
   299  
   300  }