예제 #1
0
func TestFmtCurrencyParallel(t *testing.T) {
	queue := make(chan fmtNumberData)
	ncpu := runtime.NumCPU()
	prevCPU := runtime.GOMAXPROCS(ncpu)
	defer runtime.GOMAXPROCS(prevCPU)
	wg := new(sync.WaitGroup)

	haveNumber := i18n.NewCurrency(
		i18n.CurrencyFormat("#,##0.000 ¤", testDefaultNumberSymbols),
		i18n.CurrencyFraction(3, 0, 0, 0),
	)

	// spawn workers
	for i := 0; i < ncpu; i++ {
		wg.Add(1)
		go testCurrencyWorker(t, haveNumber, i, queue, wg)
	}

	// master: give work
	for _, test := range genParallelTests(" $") {
		queue <- test
	}
	close(queue)
	wg.Wait()
}
예제 #2
0
func bmFmtCurrency_Non_Singleton(b *testing.B, format, want string, sign int, intgr int64, prec int, frac int64) {
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		haveC := i18n.NewCurrency(
			i18n.CurrencyFormat(format, testDefaultNumberSymbols),
			i18n.CurrencySign(bmCurrencySign),
		)
		var buf bytes.Buffer
		if _, err := haveC.FmtNumber(&buf, sign, intgr, prec, frac); err != nil {
			b.Error(err)
		}
		have := buf.String()
		if have != want {
			b.Errorf("Missmatch have %s vs want %s", have, want)
		}
		benchmarkFmtCurrency = have
	}
}
예제 #3
0
func benchmarkFmtCurrencyFloat64_Non_Singleton(b *testing.B, fl float64, want string) {
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		haveC := i18n.NewCurrency(
			i18n.CurrencyFormat("¤#,##0.00;(¤#,##0.00)", testDefaultNumberSymbols),
			i18n.CurrencySign(bmCurrencySign),
		)
		var buf bytes.Buffer
		if _, err := haveC.FmtFloat64(&buf, fl); err != nil {
			b.Error(err)
		}
		have := buf.String()
		if have != want {
			b.Errorf("Missmatch have %s vs want %s", have, want)
		}
		benchmarkFmtCurrency = have
	}
}
예제 #4
0
	Decimal:                '.',
	Group:                  0,
	List:                   ';',
	PercentSign:            '%',
	CurrencySign:           '¤',
	PlusSign:               '+',
	MinusSign:              '-',
	Exponential:            'E',
	SuperscriptingExponent: '×',
	PerMille:               '‰',
	Infinity:               '∞',
	Nan:                    []byte(`NaN`),
}

var testFmtCur = i18n.NewCurrency(
	i18n.CurrencyFormat("¤ #,##0.00"),
	i18n.CurrencySymbols(testDefaultSymbols),
)
var testFmtNum = i18n.NewNumber(
	i18n.NumberFormat("###0.###"),
	i18n.NumberSymbols(testDefaultSymbols),
)

func TestString(t *testing.T) {
	tests := []struct {
		prec int
		have int64
		want string
	}{
		{0, 13, "$ 13.00"},
		{10, 13, "$ 1.30"},
예제 #5
0
func TestFmtCurrency2(t *testing.T) {

	tests := []struct {
		opts    []i18n.CurrencyOptFunc
		sign    int
		i       int64
		prec    int
		dec     int64
		want    string
		wantErr error
	}{
		{
			[]i18n.CurrencyOptFunc{
				i18n.CurrencyFormat("#,##0.00 ¤"),
				i18n.CurrencyFraction(2, 0, 2, 0), // euro, 2 digits, no rounding
				i18n.CurrencySign([]byte("€")),
			},
			-1, -1234, 2, 6, "-1.234,06 €", nil, // euros with default Symbols
		},
		{
			[]i18n.CurrencyOptFunc{
				i18n.CurrencyFormat("#,##0.00 ¤"),
				i18n.CurrencyFraction(2, 0, 2, 0), // euro, 2 digits, no rounding
				i18n.CurrencySign([]byte("€")),
			},
			-1, -1234, 1, 6, "-1.234,60 €", nil, // euros with default Symbols
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("+#,##0.00 ¤"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("€"))},
			0, 1234, 2, 6, "+1.234,06 €", nil, // number is 1234.06
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("#,##0. ¤"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("€"))},
			1, 1234, 2, 6, "1.234,06 €", nil, // number is 1234.06
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("#,##0. ¤"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("€"))},
			1, 1234, 3, 6, "1.234,01 €", nil, // number is 1234.006
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("#,##0. ¤"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("€"))},
			1, 1234, 3, 345, "1.234,35 €", nil, // number is 1234.345
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("#,##0. ¤"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("€"))},
			1, 1234, 3, 45, "1.234,05 €", nil, // number is 1234.045
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("#,##0. ¤"), i18n.CurrencyFraction(0, 0, 2, 0), i18n.CurrencySign([]byte("€"))},
			1, 123456789, 1, 6, "123.456.790 €", nil, // 123456789.6
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("#,##0. ¤"), i18n.CurrencyFraction(0, 0, 2, 0), i18n.CurrencySign([]byte("€"))},
			1, 123456789, 2, 6, "123.456.789 €", nil, // 123456789.06
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("+0.00 ¤"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("€"))},
			-1, 4, 3, 245, "+4,25 €", nil, // 4.245
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("¤\u00a0#0.00;¤\u00a0-#0.00"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("€"))},
			-1, 4, 2, 245, "€\u00a04,25", i18n.ErrPrecIsTooShort, // 4.245
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("¤\u00a0#0.00;¤\u00a0-#0.00"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("€"))},
			-1, -12345678, 3, 245, "€\u00a0—12345678,25", nil, // 12345678.245
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("¤\u00a0#0.00;¤\u00a0-#0.00"), i18n.CurrencyFraction(-1, -1, -1, -1), i18n.CurrencySign([]byte(""))},
			-1, -12345678, 3, 245, "\uf8ff\u00a0-12345678", nil, // 12345678.245
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("¤\u00a0#0.00;¤\u00a0-#0.00"), i18n.CurrencyFraction(0, 0, 2, 0), i18n.CurrencySign([]byte(""))},
			-1, -12345678, 3, 495, "\uf8ff\u00a0-12345679", nil, // 12345678.495
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("#,##0.00¤;(#,##0.00¤)"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("C"))},
			-1, -12345678, 3, 495, "(12.345.678,50C)", nil, // 12345678.495
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("#,##0.00¤;(#,##0.00¤)"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("C"))},
			0, 0, 3, 495, "", i18n.ErrCannotDetectMinusSign, // 0.495
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("#,##0.00¤;(#,##0.00¤)"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("C"))},
			-1, 0, 3, 495, "(0,50C)", nil, // 0.495
		},
		{
			[]i18n.CurrencyOptFunc{
				i18n.CurrencyFormat("#,##0.00 ¤"),
				i18n.CurrencyFraction(0, 0, 0, 0), // japanese yen, no digits no rounding
				i18n.CurrencySign([]byte("¥JP")),
			},
			1, 1234, 3, 456, "1.235 ¥JP", nil, // yen with default symbols
		},
	}
	var buf bytes.Buffer
	for _, test := range tests {
		haveNumber := i18n.NewCurrency(i18n.CurrencySymbols(testDefCurSym))
		haveNumber.COptions(test.opts...)

		_, err := haveNumber.FmtNumber(&buf, test.sign, test.i, test.prec, test.dec)
		have := buf.String()
		if test.wantErr != nil {
			assert.Error(t, err)
			assert.EqualError(t, err, test.wantErr.Error())
		} else {
			assert.NoError(t, err)

			assert.EqualValues(t, test.want, have, "%v", test)
		}
		buf.Reset()
	}
}
예제 #6
0
func TestFmtCurrency3(t *testing.T) {
	// only to test the default format
	tests := []struct {
		opts    []i18n.CurrencyOptFunc
		sign    int
		i       int64
		prec    int
		dec     int64
		want    string
		wantErr error
	}{
		{
			[]i18n.CurrencyOptFunc{
				i18n.CurrencyFormat("", testDefCurSym),
				i18n.CurrencyFraction(2, 0, 2, 0), // euro, 2 digits, no rounding
				i18n.CurrencySign([]byte("€")),
			},
			-1, -1234, 2, 6, "€\u00a0-1.234,06", nil, // euros with default Symbols, -1234.06
		},
		{
			[]i18n.CurrencyOptFunc{
				i18n.CurrencyFormat("", testDefCurSym),
				i18n.CurrencyFraction(2, 0, 2, 0), // euro, 2 digits, no rounding
				i18n.CurrencySign([]byte("€")),
			},
			-1, -1234, 3, 6, "€\u00a0-1.234,01", nil, // euros with default Symbols, -1234.006
		},
		{
			[]i18n.CurrencyOptFunc{
				i18n.CurrencyFormat("", testDefCurSym),
				i18n.CurrencyFraction(0, 0, 0, 0), // euro, 2 digits, no rounding
				i18n.CurrencySign([]byte("€")),
			},
			1, 1234, 2, 495, "€\u00a0-1.235", i18n.ErrPrecIsTooShort, // euros with default Symbols, -1234.495
		},
		{
			[]i18n.CurrencyOptFunc{
				i18n.CurrencyFormat("", testDefCurSym),
				i18n.CurrencyFraction(0, 0, 0, 0), // euro, 2 digits, no rounding
				i18n.CurrencySign([]byte("€")),
			},
			1, 1234, 2, 44, "€\u00a01.234", nil, // euros with default Symbols, -1234.495
		},
	}

	var buf bytes.Buffer
	for _, test := range tests {
		haveNumber := i18n.NewCurrency(test.opts...)

		_, err := haveNumber.FmtNumber(&buf, test.sign, test.i, test.prec, test.dec)
		have := buf.String()
		if test.wantErr != nil {
			assert.Error(t, err)
			assert.EqualError(t, err, test.wantErr.Error())
		} else {
			assert.NoError(t, err)

			assert.EqualValues(t, test.want, have, "%v", test)
		}
		buf.Reset()
	}
}