コード例 #1
0
ファイル: encoding_test.go プロジェクト: levcom/csfw
func TestScan(t *testing.T) {

	tests := []struct {
		src     interface{}
		want    string
		wantErr error
	}{
		{nil, `NaN`, nil},
		{[]byte{0x39, 0x39, 0x39, 0x2e, 0x30, 0x30, 0x30, 0x30}, `999.0000`, nil},
		{[]byte{0x37, 0x30, 0x35, 0x2e, 0x39, 0x39, 0x33, 0x33}, `705.9933`, nil},
		{[]byte{0x37, 0x30, 0x35, 0x2e, 0x39, 0x39, 0x33, 0x33}, `705.9933`, nil},
		{[]byte{0x37, 0x30, 0x35, 0x2e, 0x39, 0x39, 0x33, 0x33}, `705.9933`, nil},
		{[]byte{0x37, 0x30, 0x35, 0x2e, 0x39, 0x39, 0x33, 0x33}, `705.9933`, nil},
		{[]byte{0x37, 0x30, 0x35, 0x2e, 0x19, 0x39, 0x33, 0x13}, `0.0000`, strconv.ErrSyntax},
		{[]byte{0x37, 0x33}, `73.0000`, nil},
		{[]byte{0x37, 0x38}, `78.0000`, nil},
		{[]byte{0x37, 0x34}, `74.0000`, nil},
		{[]byte{0x37, 0x37}, `77.0000`, nil},
		{[]byte{0xa7, 0x3e}, `0.0000`, strconv.ErrSyntax},
		{int(33), `0.0000`, errors.New("Unsupported Type int for value. Supported: []byte")},
	}

	var buf bytes.Buffer
	for _, test := range tests {
		var c money.Money
		err := c.Scan(test.src)
		c.FmtCur = testFmtCur
		c.FmtNum = testFmtNum

		if test.wantErr != nil {
			assert.Error(t, err, "%v", test)
			assert.Contains(t, err.Error(), test.wantErr.Error())
		} else {
			assert.NoError(t, err, "%v", test)
			assert.EqualValues(t, test.want, string(c.Ftoa()), "%v", test)

			if _, err := c.NumberWriter(&buf); err != nil {
				t.Error(err)
			}
			buf.WriteString("; ")
		}
	}

	want := `NaN; 999.000; 705.993; 705.993; 705.993; 705.993; 73.000; 78.000; 74.000; 77.000; `
	have := buf.String()
	if want != have {
		t.Errorf("\nHave: %s\n\nWant: %s\n", have, want)
	}
}
コード例 #2
0
ファイル: encoding_bm_test.go プロジェクト: levcom/csfw
// Benchmark_MoneyScan	 3000000	       504 ns/op	     136 B/op	       2 allocs/op => Go 1.4.2
// Benchmark_MoneyScan 	 5000000	       386 ns/op	     144 B/op	       2 allocs/op => Go 1.5.0
func Benchmark_MoneyScan(b *testing.B) {
	var d interface{}
	d = []byte{0x37, 0x30, 0x35, 0x2e, 0x39, 0x39, 0x33, 0x33}
	var want float64 = 705.993300
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		var c money.Money
		c.Scan(d)
		benchmarkMoneyScan = c.Getf()
		if benchmarkMoneyScan != want {
			b.Errorf("Have: %f\nWant: %f", benchmarkMoneyScan, want)
		}
	}
}