mtsatellite/common/spans_test.go

79 lines
1.4 KiB
Go
Raw Permalink Normal View History

2015-07-26 16:44:51 +02:00
// Copyright 2014, 2015 by Sascha L. Teichmann
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package common
import (
"math/rand"
"testing"
)
const spanItems = 3000
func TestSpans(t *testing.T) {
2024-01-07 03:38:36 +01:00
sp := newSpanPool()
var s *span
for i := 0; i < spanItems; i++ {
2024-01-07 03:38:36 +01:00
s = sp.insert(s, int32(i), 42)
}
2024-01-07 03:38:36 +01:00
if n := s.len(); n != 1 {
t.Errorf("inc: Span length %d expected 1\n", n)
t.Errorf("spans: %s\n", s)
}
2024-01-07 03:38:36 +01:00
sp.freeAll(s)
s = nil
for i := spanItems - 1; i >= 0; i-- {
2024-01-07 03:38:36 +01:00
s = sp.insert(s, int32(i), 42)
}
2024-01-07 03:38:36 +01:00
if n := s.len(); n != 1 {
t.Errorf("dec: Span length %d expected 1\n", n)
t.Errorf("spans: %s\n", s)
}
2024-01-07 03:38:36 +01:00
sp.freeAll(s)
s = nil
for i := 0; i < spanItems/2; i++ {
j := spanItems - 1 - i
2024-01-07 03:38:36 +01:00
s = sp.insert(s, int32(i), 42)
s = sp.insert(s, int32(j), 21)
}
2024-01-07 03:38:36 +01:00
if n := s.len(); n != 2 {
t.Errorf("two: Span length %d expected 2\n", n)
t.Errorf("spans: %s\n", s)
}
2024-01-07 03:38:36 +01:00
sp.freeAll(s)
inp := make([]int32, spanItems)
for i := 0; i < spanItems; i++ {
inp[i] = int32(i)
}
for i := 0; i < spanItems; i++ {
i1 := rand.Int31n(int32(spanItems))
i2 := rand.Int31n(int32(spanItems))
inp[i1], inp[i2] = inp[i2], inp[i1]
}
s = nil
for i := 0; i < spanItems; i++ {
2024-01-07 03:38:36 +01:00
s = sp.insert(s, inp[i], 42)
}
2024-01-07 03:38:36 +01:00
if n := s.len(); n != 1 {
t.Errorf("rand: Span length %d expected 1\n", n)
t.Errorf("spans: %s\n", s)
}
2024-01-07 03:38:36 +01:00
sp.freeAll(s)
}