Skip to content

Commit

Permalink
fix Indices.Sort; add test
Browse files Browse the repository at this point in the history
  • Loading branch information
juagargi committed Jun 24, 2020
1 parent 6984151 commit 974cfc2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
7 changes: 5 additions & 2 deletions go/cs/reservation/segment/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,16 @@ func (idxs Indices) Len() int { return len(i
func (idxs Indices) GetIndexNumber(i int) reservation.IndexNumber { return idxs[i].Idx }
func (idxs Indices) GetExpiration(i int) time.Time { return idxs[i].Expiration }

// Sort sorts these Indices.
// Sort sorts these Indices according to their index number modulo 16, e.g. [14, 15, 0, 1].
func (idxs *Indices) Sort() {
if len(*idxs) < 2 {
return
}
sort.Slice(*idxs, func(i, j int) bool {
return (*idxs)[i].Idx < (*idxs)[j].Idx
a, b := (*idxs)[i], (*idxs)[j]
distance := b.Idx.Sub(a.Idx)
return a.Expiration.Before(b.Expiration) ||
(a.Expiration.Equal(b.Expiration) && distance < 3)
})
// find a discontinuity and rotate
i := 1
Expand Down
16 changes: 8 additions & 8 deletions go/cs/reservation/segment/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/stretchr/testify/require"

base "github.com/scionproto/scion/go/cs/reservation"
"github.com/scionproto/scion/go/cs/reservation/segment"
"github.com/scionproto/scion/go/lib/colibri/reservation"
)
Expand Down Expand Up @@ -54,19 +55,18 @@ func TestIndicesSort(t *testing.T) {
func newIndices(idxs ...int) segment.Indices {
indices := make(segment.Indices, len(idxs))
for i, idx := range idxs {
idx := segment.NewIndex(reservation.IndexNumber(idx), time.Unix(1, 0), segment.IndexPending,
reservation.BWCls(1), reservation.BWCls(1), reservation.BWCls(1), &reservation.Token{})
expTime := time.Unix(int64(i/3+1), 0)
idx := segment.NewIndex(reservation.IndexNumber(idx), expTime,
segment.IndexPending, reservation.BWCls(1), reservation.BWCls(1), reservation.BWCls(1),
&reservation.Token{})
indices[i] = *idx
}
return indices
}

func checkIndicesSorted(t *testing.T, idxs segment.Indices) {
t.Helper()
if len(idxs) < 2 {
return
}
for i := 1; i < len(idxs); i++ {
require.Equal(t, idxs[i-1].Idx.Add(1), idxs[i].Idx)
}
// validate according to valid indices criteria
err := base.ValidateIndices(idxs)
require.NoError(t, err)
}
7 changes: 7 additions & 0 deletions go/lib/colibri/reservation/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ func TestIndexNumberArithmetic(t *testing.T) {
require.Equal(t, IndexNumber(0), x)
x = idx.Sub(IndexNumber(2))
require.Equal(t, IndexNumber(15), x)
// distance from 2 to 0 = 0 - 2 = 14 mod 16
x = IndexNumber(2)
distance := IndexNumber(0).Sub(x)
require.Equal(t, IndexNumber(14), distance)
// distance from 2 to 4
distance = IndexNumber(4).Sub(x)
require.Equal(t, IndexNumber(2), distance)
}

func TestValidatePathType(t *testing.T) {
Expand Down

0 comments on commit 974cfc2

Please sign in to comment.