add now and nil test to marshal for timestamp

This commit is contained in:
2025-02-03 14:12:10 -01:06
parent e0bc7fd82a
commit 60d3805150
2 changed files with 13 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ package timestamp
import (
_ "embed"
"time"
"golang.org/x/exp/constraints"
)
@@ -18,6 +19,8 @@ type T struct{ N uint64 }
func New[V constraints.Integer](n V) *T { return &T{uint64(n)} }
func Now() *T { return New(time.Now().Unix()) }
func (n *T) Uint64() uint64 { return n.N }
func (n *T) Int64() int64 { return int64(n.N) }
func (n *T) Uint16() uint16 { return uint16(n.N) }
@@ -33,7 +36,11 @@ var powers = []*T{
const zero = '0'
const nine = '9'
func (n *T) Marshal(dst []byte) (b []byte) {
func (n *T) Marshal(dst []byte) (b []byte, err error) {
if n == nil {
err = errorf.E("cannot marshal nil timestamp")
return
}
nn := n.N
b = dst
if n.N == 0 {

View File

@@ -15,7 +15,9 @@ func TestMarshalUnmarshal(t *testing.T) {
var err error
for _ = range 10000000 {
n = New(uint64(frand.Intn(math.MaxInt64)))
b = n.Marshal(b)
if b, err = n.Marshal(b); chk.E(err) {
t.Fatal(err)
}
m := New(0)
if rem, err = m.Unmarshal(b); chk.E(err) {
t.Fatal(err)
@@ -42,7 +44,7 @@ func BenchmarkByteStringToInt64(bb *testing.B) {
bb.ReportAllocs()
for i = 0; i < bb.N; i++ {
n := testInts[i%10000]
b = n.Marshal(b)
b, _ = n.Marshal(b)
b = b[:0]
}
})
@@ -60,7 +62,7 @@ func BenchmarkByteStringToInt64(bb *testing.B) {
m := New(0)
for i = 0; i < bb.N; i++ {
n := testInts[i%10000]
b = m.Marshal(b)
b, _ = m.Marshal(b)
_, _ = n.Unmarshal(b)
b = b[:0]
}