Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to add benchmarks but something is broken #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 70 additions & 22 deletions time_test.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,88 @@
package cstrftime

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestFormat(t *testing.T) {
tm := time.Date(2000, 2, 1, 12, 30, 0, 0, time.UTC)
tm := time.Date(2000, 2, 1, 16, 30, 22, 123456789, time.UTC)

t.Run("weekday as locale’s abbreviated name", func(t *testing.T) {
assert.Equal(t, "Tue", Format("%a", tm))
})
cases := []struct {
name, format, result string
}{
{"weekday as locale’s abbreviated name", "%a", "Tue"},
{"weekday as locale’s full name", "%A", "Tuesday"},
{"weekday as a decimal number", "%w", "2"},
{"day of the month as a zero-padded decimal number", "%d", "01"},
{"month as locale’s abbreviated name", "%b", "Feb"},
{"month as locale’s full name", "%B", "February"},
{"month as a zero-padded decimal number", "%m", "02"},
{"24 hour Hours", "%H", "16"},
{"12 hour Hours", "%I", "04"},
{"Minutes", "%M", "30"},
{"Seconds", "%S", "22"},
{"seconds with decimals", "%S.%f", "22.123456"},
{"american date", "%m/%d/%Y", "02/01/2000"},
{"proper date", "%Y/%m/%d", "2000/02/01"},
{"dashed date", "%Y-%m-%d", "2000-02-01"},
}

t.Run("weekday as locale’s full name", func(t *testing.T) {
assert.Equal(t, "Tuesday", Format("%A", tm))
})
for _, c := range cases {
c := c

t.Run("weekday as a decimal number", func(t *testing.T) {
assert.Equal(t, "2", Format("%w", tm))
})
t.Run(c.name, func(t *testing.T) {
assert.Equal(t, c.result, Format(c.format, tm))
})
}
}

func BenchmarkFormat(b *testing.B) {
tm := time.Date(2000, time.February, 13, 16, 30, 20, 12345, time.UTC)

cases := []struct {
name, cFormat, goFormat string
}{
{"year", "%Y", "2006"},
{"date", "%Y/%m/%d", "2006/01/02"},
}

for _, c := range cases {
assert.Equal(b, tm.Format(c.goFormat), Format(c.cFormat, tm), c.name)
}

t.Run("day of the month as a zero-padded decimal number", func(t *testing.T) {
assert.Equal(t, "01", Format("%d", tm))
})
for _, c := range cases {
for _, native := range []bool{true, false} {
c := c
native := native

t.Run("month as locale’s abbreviated name", func(t *testing.T) {
assert.Equal(t, "Feb", Format("%b", tm))
})
b.Run(fmt.Sprintf("%s - native(%t)", c.name, native), func(b *testing.B) {
var (
want = tm.Format(c.goFormat)
got string
)

t.Run("month as locale’s full name", func(t *testing.T) {
assert.Equal(t, "February", Format("%B", tm))
})
switch native {
case true:
b.ResetTimer()
for i := 0; i < b.N; i++ {
if got = tm.Format(c.goFormat); want != got {
b.Fatalf("want: %q got: %q", want, got)
}
}

t.Run("month as a zero-padded decimal number", func(t *testing.T) {
assert.Equal(t, "02", Format("%m", tm))
})
case false:
b.ResetTimer()
for i := 0; i < b.N; i++ {
if got = Format(c.cFormat, tm); want != got {
b.Fatalf("want: %q got: %q", want, got)
}
}
}
})
}
}
}