diff --git a/std/encoding/name_pattern.go b/std/encoding/name_pattern.go index 4311b28b..1384978e 100644 --- a/std/encoding/name_pattern.go +++ b/std/encoding/name_pattern.go @@ -5,8 +5,6 @@ import ( "hash" "io" "strings" - - "github.com/named-data/ndnd/std/utils" ) type Name []Component @@ -204,7 +202,7 @@ func (n Name) Append(rest ...Component) Name { } func (n Name) Compare(rhs Name) int { - for i := 0; i < utils.Min(len(n), len(rhs)); i++ { + for i := 0; i < min(len(n), len(rhs)); i++ { if ret := n[i].Compare(rhs[i]); ret != 0 { return ret } @@ -220,7 +218,7 @@ func (n Name) Compare(rhs Name) int { } func (n NamePattern) Compare(rhs NamePattern) int { - for i := 0; i < utils.Min(len(n), len(rhs)); i++ { + for i := 0; i < min(len(n), len(rhs)); i++ { if ret := n[i].Compare(rhs[i]); ret != 0 { return ret } diff --git a/std/schema/rdr/rdr.go b/std/schema/rdr/rdr.go index f008106d..2119579b 100644 --- a/std/schema/rdr/rdr.go +++ b/std/schema/rdr/rdr.go @@ -82,7 +82,7 @@ func (n *SegmentedNode) Provide(mNode schema.MatchedNode, content enc.Wire, need pktContent := enc.Wire{} remSize := n.SegmentSize for remSize > 0 && wireIdx < len(content) && bufferIdx < len(content[wireIdx]) { - curSize := int(utils.Min(uint64(len(content[wireIdx])-bufferIdx), remSize)) + curSize := int(min(uint64(len(content[wireIdx])-bufferIdx), remSize)) pktContent = append(pktContent, content[wireIdx][bufferIdx:bufferIdx+curSize]) bufferIdx += curSize remSize -= uint64(curSize) diff --git a/std/schema/svs/sync.go b/std/schema/svs/sync.go index b2625026..e7170202 100644 --- a/std/schema/svs/sync.go +++ b/std/schema/svs/sync.go @@ -222,7 +222,7 @@ func (n *SvsNode) aggregate(remoteSv *spec_svs.StateVector) { SeqNo: cur.SeqNo, }) } else { - n.aggSv.Entries[li].SeqNo = utils.Max(n.aggSv.Entries[li].SeqNo, cur.SeqNo) + n.aggSv.Entries[li].SeqNo = max(n.aggSv.Entries[li].SeqNo, cur.SeqNo) } } } @@ -312,7 +312,7 @@ func (n *SvsNode) onAttach(event *schema.Event) any { n.state = SyncSteady n.missChan = make(chan MissingData, n.ChannelSize) // The first sync Interest should be sent out ASAP - n.cancelSyncTimer = n.timer.Schedule(utils.Min(n.getSyncIntv(), 100*time.Millisecond), n.onSyncTimer) + n.cancelSyncTimer = n.timer.Schedule(min(n.getSyncIntv(), 100*time.Millisecond), n.onSyncTimer) n.stopChan = make(chan struct{}, 1) if len(n.OnMissingData.Val()) > 0 { diff --git a/std/utils/utils.go b/std/utils/utils.go index 7f8c78c9..4f3c22c5 100644 --- a/std/utils/utils.go +++ b/std/utils/utils.go @@ -21,28 +21,14 @@ func ConvIntPtr[A, B constraints.Integer](a *A) *B { } } -func Min[T constraints.Ordered](a, b T) T { - if a < b { - return a - } - return b -} - -func Max[T constraints.Ordered](a, b T) T { - if a > b { - return a - } - return b -} - func MakeTimestamp(t time.Time) uint64 { return uint64(t.UnixNano() / int64(time.Millisecond)) } func ConvertNonce(nonce []byte) *uint64 { - ret := uint64(0) - for _, v := range nonce { - ret = ret*256 + uint64(v) + x := uint64(0) + for _, b := range nonce { + x = (x << 8) | uint64(b) } - return &ret + return &x } diff --git a/std/utils/utils_test.go b/std/utils/utils_test.go new file mode 100644 index 00000000..986a86e4 --- /dev/null +++ b/std/utils/utils_test.go @@ -0,0 +1,33 @@ +package utils_test + +import ( + "testing" + "time" + + "github.com/named-data/ndnd/std/utils" + "github.com/stretchr/testify/require" +) + +func TestIdPtr(t *testing.T) { + utils.SetTestingT(t) + + p := utils.IdPtr(uint64(42)) + require.Equal(t, uint64(42), *p) +} + +func TestMakeTimestamp(t *testing.T) { + utils.SetTestingT(t) + + date := time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC) + require.Equal(t, uint64(1609459200000), utils.MakeTimestamp(date)) +} + +func TestConvertNonce(t *testing.T) { + utils.SetTestingT(t) + + nonce := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06} + require.Equal(t, uint64(0x010203040506), *utils.ConvertNonce(nonce)) + + nonce = []byte{0x42, 0x1C, 0xE1, 0x4B, 0x99, 0xFA, 0xA3} + require.Equal(t, uint64(0x421ce14b99faa3), *utils.ConvertNonce(nonce)) +}