Skip to content

Commit

Permalink
Group related tests and benchmarks.
Browse files Browse the repository at this point in the history
  • Loading branch information
bojanz committed Jun 29, 2022
1 parent 97af46f commit af8e718
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 42 deletions.
59 changes: 29 additions & 30 deletions amount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,35 @@ func TestAmount_RoundTo(t *testing.T) {
}
}

func TestAmount_RoundToWithConcurrency(t *testing.T) {
n := 2
roundingModes := []currency.RoundingMode{
currency.RoundHalfUp,
currency.RoundHalfDown,
currency.RoundUp,
currency.RoundDown,
}

for _, roundingMode := range roundingModes {
t.Run(fmt.Sprintf("rounding_mode_%d", roundingMode), func(t *testing.T) {
t.Parallel()

var allDone sync.WaitGroup
allDone.Add(n)

for i := 0; i < n; i++ {
go func() {
defer allDone.Done()
amount, _ := currency.NewAmount("10.99", "EUR")
amount.RoundTo(1, roundingMode)
}()
}

allDone.Wait()
})
}
}

func TestAmount_Cmp(t *testing.T) {
a, _ := currency.NewAmount("3.33", "USD")
b, _ := currency.NewAmount("3.33", "EUR")
Expand Down Expand Up @@ -851,33 +880,3 @@ func TestAmount_Scan(t *testing.T) {
})
}
}

func TestAmount_RoundToWithConcurrency(t *testing.T) {
const n = 2

roundingModes := []currency.RoundingMode{
currency.RoundHalfUp,
currency.RoundHalfDown,
currency.RoundUp,
currency.RoundDown,
}

for _, roundingMode := range roundingModes {
t.Run(fmt.Sprintf("rounding_mode_%d", roundingMode), func(t *testing.T) {
t.Parallel()

var allDone sync.WaitGroup
allDone.Add(n)

for i := 0; i < n; i++ {
go func() {
defer allDone.Done()
amount, _ := currency.NewAmount("10.99", "EUR")
amount.RoundTo(1, roundingMode)
}()
}

allDone.Wait()
})
}
}
23 changes: 11 additions & 12 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,8 @@ func BenchmarkAmount_Round(b *testing.B) {
result = z
}

func BenchmarkAmount_Cmp(b *testing.B) {
x, _ := currency.NewAmount("34.99", "USD")
y, _ := currency.NewAmount("12.99", "USD")

var z int
for n := 0; n < b.N; n++ {
z, _ = x.Cmp(y)
}
cmpResult = z
}

func BenchmarkAmount_RoundTo(b *testing.B) {
x, _ := currency.NewAmount("34.9876", "USD")

roundingModes := []currency.RoundingMode{
currency.RoundHalfUp,
currency.RoundHalfDown,
Expand All @@ -140,3 +128,14 @@ func BenchmarkAmount_RoundTo(b *testing.B) {
})
}
}

func BenchmarkAmount_Cmp(b *testing.B) {
x, _ := currency.NewAmount("34.99", "USD")
y, _ := currency.NewAmount("12.99", "USD")

var z int
for n := 0; n < b.N; n++ {
z, _ = x.Cmp(y)
}
cmpResult = z
}

0 comments on commit af8e718

Please sign in to comment.