github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/misc/amount-convert/rmb_test.go (about)

     1  package amount
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestFenToYuan(t *testing.T) {
     8  
     9  	type arg struct {
    10  		input int64
    11  		want  float64
    12  	}
    13  
    14  	testCases := []arg{
    15  		{
    16  			input: 1,
    17  			want:  0.01,
    18  		},
    19  		{
    20  			input: 0,
    21  			want:  0.00,
    22  		},
    23  		{
    24  			input: 100,
    25  			want:  1.00,
    26  		},
    27  		{
    28  			input: 101,
    29  			want:  1.01,
    30  		},
    31  		{
    32  			input: 105,
    33  			want:  1.05,
    34  		},
    35  		{
    36  			input: 58805,
    37  			want:  588.05,
    38  		}, {
    39  			input: 50005,
    40  			want:  500.05,
    41  		},
    42  	}
    43  
    44  	for _, tc := range testCases {
    45  		output := FenToYuan(tc.input)
    46  		if output != tc.want {
    47  			t.Errorf("input:%d want:%f output:%f", tc.input, tc.want, output)
    48  		} else {
    49  			t.Logf("input:%d want:%f output:%f", tc.input, tc.want, output)
    50  		}
    51  	}
    52  }
    53  
    54  func TestYuanToFen(t *testing.T) {
    55  	type arg struct {
    56  		input float64
    57  		want  int64
    58  	}
    59  
    60  	testCases := []arg{
    61  		{
    62  			input: 0.01,
    63  			want:  1,
    64  		},
    65  		{
    66  			input: 0.00,
    67  			want:  0,
    68  		},
    69  		{
    70  			input: 1.00,
    71  			want:  100,
    72  		},
    73  		{
    74  			input: 1.01,
    75  			want:  101,
    76  		},
    77  		{
    78  			input: 1.05,
    79  			want:  105,
    80  		},
    81  		{
    82  			input: 588.05,
    83  			want:  58805,
    84  		}, {
    85  			input: 500.05,
    86  			want:  50005,
    87  		}, {
    88  			input: 500.55,
    89  			want:  50055,
    90  		}, {
    91  			input: 500.54,
    92  			want:  50054,
    93  		}, {
    94  			input: 500.54,
    95  			want:  50054,
    96  		}, {
    97  			input: 4.19,
    98  			want:  419,
    99  		},
   100  	}
   101  
   102  	for _, tc := range testCases {
   103  		output := YuanToFen(tc.input)
   104  		if output != tc.want {
   105  			t.Errorf("input:%f want:%d output:%d", tc.input, tc.want, output)
   106  		} else {
   107  			t.Logf("input:%f want:%d output:%d", tc.input, tc.want, output)
   108  		}
   109  	}
   110  }
   111  
   112  func TestFloat64ToInt64(t *testing.T) {
   113  	type arg struct {
   114  		input   float64
   115  		decimal int
   116  		want    int64
   117  	}
   118  
   119  	testCases := []arg{
   120  		{
   121  			input:   1.00,
   122  			decimal: 1,
   123  			want:    10,
   124  		},
   125  		{
   126  			input:   0.01,
   127  			decimal: 2,
   128  			want:    1,
   129  		},
   130  		{
   131  			input:   1.01,
   132  			decimal: 3,
   133  			want:    1010,
   134  		},
   135  		{
   136  			input:   1.05,
   137  			decimal: 4,
   138  			want:    10500,
   139  		},
   140  		{
   141  			input:   588.05,
   142  			decimal: 5,
   143  			want:    58805000,
   144  		}, {
   145  			input:   500.05,
   146  			decimal: 6,
   147  			want:    500050000,
   148  		}, {
   149  			input:   500.55,
   150  			decimal: 7,
   151  			want:    5005500000,
   152  		}, {
   153  			input:   500.54,
   154  			decimal: 8,
   155  			want:    50054000000,
   156  		}, {
   157  			input:   500.54,
   158  			decimal: 9,
   159  			want:    500540000000,
   160  		}, {
   161  			input:   4.19,
   162  			decimal: 10,
   163  			want:    41900000000,
   164  		},
   165  	}
   166  
   167  	for _, tc := range testCases {
   168  		output := Float64ToInt64(tc.input, tc.decimal)
   169  		if output != tc.want {
   170  			t.Errorf("input:%f want:%d output:%d", tc.input, tc.want, output)
   171  		} else {
   172  			t.Logf("input:%f want:%d output:%d", tc.input, tc.want, output)
   173  		}
   174  	}
   175  }
   176  
   177  func TestInt64ToFloat64(t *testing.T) {
   178  
   179  	type arg struct {
   180  		input   int64
   181  		decimal int
   182  		want    float64
   183  	}
   184  
   185  	testCases := []arg{
   186  		{
   187  			input:   1,
   188  			decimal: 1,
   189  			want:    0.1,
   190  		},
   191  		{
   192  			input:   0,
   193  			decimal: 2,
   194  			want:    0.00,
   195  		},
   196  		{
   197  			input:   100,
   198  			decimal: 3,
   199  			want:    0.100,
   200  		},
   201  		{
   202  			input:   101,
   203  			decimal: 4,
   204  			want:    0.0101,
   205  		},
   206  		{
   207  			input:   105,
   208  			decimal: 5,
   209  			want:    0.00105,
   210  		},
   211  		{
   212  			input:   58805,
   213  			decimal: 6,
   214  			want:    0.058805,
   215  		}, {
   216  			input:   50005,
   217  			decimal: 7,
   218  			want:    0.0050005,
   219  		},
   220  	}
   221  
   222  	for _, tc := range testCases {
   223  		output := Int64ToFloat64(tc.input, tc.decimal)
   224  		if output != tc.want {
   225  			t.Errorf("input:%d want:%f output:%f", tc.input, tc.want, output)
   226  		} else {
   227  			t.Logf("input:%d want:%f output:%f", tc.input, tc.want, output)
   228  		}
   229  	}
   230  }
   231  
   232  func TestRound(t *testing.T) {
   233  
   234  	type arg struct {
   235  		input    float64
   236  		want     float64
   237  		decimals int
   238  	}
   239  	testCases := []arg{
   240  		{
   241  			input:    1.21,
   242  			want:     1.2,
   243  			decimals: 1,
   244  		},
   245  		{
   246  			input:    1.24,
   247  			want:     1.2,
   248  			decimals: 1,
   249  		},
   250  		{
   251  			input:    1.25,
   252  			want:     1.3,
   253  			decimals: 1,
   254  		},
   255  		{
   256  			input:    1.29,
   257  			want:     1.3,
   258  			decimals: 1,
   259  		},
   260  		{
   261  			input:    1.3,
   262  			want:     1.0,
   263  			decimals: 0,
   264  		},
   265  		{
   266  			input:    1.5,
   267  			want:     2.0,
   268  			decimals: 0,
   269  		},
   270  		{
   271  			input:    1.9,
   272  			want:     2.0,
   273  			decimals: 0,
   274  		},
   275  	}
   276  	for _, tc := range testCases {
   277  		output := Round(tc.input, tc.decimals)
   278  		if output != tc.want {
   279  			t.Errorf("input:%f want:%f output:%f", tc.input, tc.want, output)
   280  		} else {
   281  			t.Logf("input:%f want:%f output:%f", tc.input, tc.want, output)
   282  		}
   283  	}
   284  }
   285  
   286  func TestCountInterestByAnnualizedRate(t *testing.T) {
   287  
   288  	type arg struct {
   289  		capital  float64
   290  		rate     float64
   291  		decimals int
   292  		dateDiff int
   293  		want     float64
   294  	}
   295  	testCases := []arg{
   296  		{
   297  			capital:  100000,
   298  			rate:     0.05,
   299  			decimals: 2,
   300  			dateDiff: 1,
   301  			want:     13.89,
   302  		},
   303  	}
   304  	for _, tc := range testCases {
   305  		output := CountInterestByAnnualizedRate(tc.capital, tc.rate, tc.dateDiff, tc.decimals)
   306  		if output != tc.want {
   307  			t.Errorf("%+v output:%f", tc, output)
   308  		} else {
   309  			t.Logf("%+v output:%f", tc, output)
   310  		}
   311  	}
   312  }