...
Run Format

Source file src/strconv/decimal_test.go

Documentation: strconv

     1  // Copyright 2009 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 strconv_test
     6  
     7  import (
     8  	. "strconv"
     9  	"testing"
    10  )
    11  
    12  type shiftTest struct {
    13  	i     uint64
    14  	shift int
    15  	out   string
    16  }
    17  
    18  var shifttests = []shiftTest{
    19  	{0, -100, "0"},
    20  	{0, 100, "0"},
    21  	{1, 100, "1267650600228229401496703205376"},
    22  	{1, -100,
    23  		"0.00000000000000000000000000000078886090522101180541" +
    24  			"17285652827862296732064351090230047702789306640625",
    25  	},
    26  	{12345678, 8, "3160493568"},
    27  	{12345678, -8, "48225.3046875"},
    28  	{195312, 9, "99999744"},
    29  	{1953125, 9, "1000000000"},
    30  }
    31  
    32  func TestDecimalShift(t *testing.T) {
    33  	for i := 0; i < len(shifttests); i++ {
    34  		test := &shifttests[i]
    35  		d := NewDecimal(test.i)
    36  		d.Shift(test.shift)
    37  		s := d.String()
    38  		if s != test.out {
    39  			t.Errorf("Decimal %v << %v = %v, want %v",
    40  				test.i, test.shift, s, test.out)
    41  		}
    42  	}
    43  }
    44  
    45  type roundTest struct {
    46  	i               uint64
    47  	nd              int
    48  	down, round, up string
    49  	int             uint64
    50  }
    51  
    52  var roundtests = []roundTest{
    53  	{0, 4, "0", "0", "0", 0},
    54  	{12344999, 4, "12340000", "12340000", "12350000", 12340000},
    55  	{12345000, 4, "12340000", "12340000", "12350000", 12340000},
    56  	{12345001, 4, "12340000", "12350000", "12350000", 12350000},
    57  	{23454999, 4, "23450000", "23450000", "23460000", 23450000},
    58  	{23455000, 4, "23450000", "23460000", "23460000", 23460000},
    59  	{23455001, 4, "23450000", "23460000", "23460000", 23460000},
    60  
    61  	{99994999, 4, "99990000", "99990000", "100000000", 99990000},
    62  	{99995000, 4, "99990000", "100000000", "100000000", 100000000},
    63  	{99999999, 4, "99990000", "100000000", "100000000", 100000000},
    64  
    65  	{12994999, 4, "12990000", "12990000", "13000000", 12990000},
    66  	{12995000, 4, "12990000", "13000000", "13000000", 13000000},
    67  	{12999999, 4, "12990000", "13000000", "13000000", 13000000},
    68  }
    69  
    70  func TestDecimalRound(t *testing.T) {
    71  	for i := 0; i < len(roundtests); i++ {
    72  		test := &roundtests[i]
    73  		d := NewDecimal(test.i)
    74  		d.RoundDown(test.nd)
    75  		s := d.String()
    76  		if s != test.down {
    77  			t.Errorf("Decimal %v RoundDown %d = %v, want %v",
    78  				test.i, test.nd, s, test.down)
    79  		}
    80  		d = NewDecimal(test.i)
    81  		d.Round(test.nd)
    82  		s = d.String()
    83  		if s != test.round {
    84  			t.Errorf("Decimal %v Round %d = %v, want %v",
    85  				test.i, test.nd, s, test.down)
    86  		}
    87  		d = NewDecimal(test.i)
    88  		d.RoundUp(test.nd)
    89  		s = d.String()
    90  		if s != test.up {
    91  			t.Errorf("Decimal %v RoundUp %d = %v, want %v",
    92  				test.i, test.nd, s, test.up)
    93  		}
    94  	}
    95  }
    96  
    97  type roundIntTest struct {
    98  	i     uint64
    99  	shift int
   100  	int   uint64
   101  }
   102  
   103  var roundinttests = []roundIntTest{
   104  	{0, 100, 0},
   105  	{512, -8, 2},
   106  	{513, -8, 2},
   107  	{640, -8, 2},
   108  	{641, -8, 3},
   109  	{384, -8, 2},
   110  	{385, -8, 2},
   111  	{383, -8, 1},
   112  	{1, 100, 1<<64 - 1},
   113  	{1000, 0, 1000},
   114  }
   115  
   116  func TestDecimalRoundedInteger(t *testing.T) {
   117  	for i := 0; i < len(roundinttests); i++ {
   118  		test := roundinttests[i]
   119  		d := NewDecimal(test.i)
   120  		d.Shift(test.shift)
   121  		int := d.RoundedInteger()
   122  		if int != test.int {
   123  			t.Errorf("Decimal %v >> %v RoundedInteger = %v, want %v",
   124  				test.i, test.shift, int, test.int)
   125  		}
   126  	}
   127  }
   128  

View as plain text