2015-07-26 16:44:51 +02:00
|
|
|
// Copyright 2014, 2015 by Sascha L. Teichmann
|
2014-10-21 16:11:20 +02:00
|
|
|
// 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"
|
|
|
|
)
|
|
|
|
|
2014-10-21 18:03:27 +02:00
|
|
|
const spanItems = 3000
|
|
|
|
|
2014-10-21 16:11:20 +02:00
|
|
|
func TestSpans(t *testing.T) {
|
|
|
|
|
2024-01-07 03:38:36 +01:00
|
|
|
sp := newSpanPool()
|
|
|
|
var s *span
|
2014-10-21 18:03:27 +02:00
|
|
|
|
|
|
|
for i := 0; i < spanItems; i++ {
|
2024-01-07 03:38:36 +01:00
|
|
|
s = sp.insert(s, int32(i), 42)
|
2014-10-21 18:03:27 +02:00
|
|
|
}
|
|
|
|
|
2024-01-07 03:38:36 +01:00
|
|
|
if n := s.len(); n != 1 {
|
2014-10-21 18:03:27 +02:00
|
|
|
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)
|
2014-10-21 18:03:27 +02:00
|
|
|
|
|
|
|
s = nil
|
|
|
|
for i := spanItems - 1; i >= 0; i-- {
|
2024-01-07 03:38:36 +01:00
|
|
|
s = sp.insert(s, int32(i), 42)
|
2014-10-21 18:03:27 +02:00
|
|
|
}
|
|
|
|
|
2024-01-07 03:38:36 +01:00
|
|
|
if n := s.len(); n != 1 {
|
2014-10-21 18:03:27 +02:00
|
|
|
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)
|
2014-10-21 18:03:27 +02:00
|
|
|
|
|
|
|
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)
|
2014-10-21 18:03:27 +02:00
|
|
|
}
|
|
|
|
|
2024-01-07 03:38:36 +01:00
|
|
|
if n := s.len(); n != 2 {
|
2014-10-21 18:03:27 +02:00
|
|
|
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)
|
2014-10-21 18:03:27 +02:00
|
|
|
|
|
|
|
inp := make([]int32, spanItems)
|
|
|
|
for i := 0; i < spanItems; i++ {
|
2014-10-21 16:11:20 +02:00
|
|
|
inp[i] = int32(i)
|
|
|
|
}
|
2014-10-21 18:03:27 +02:00
|
|
|
|
|
|
|
for i := 0; i < spanItems; i++ {
|
|
|
|
i1 := rand.Int31n(int32(spanItems))
|
|
|
|
i2 := rand.Int31n(int32(spanItems))
|
2014-10-21 16:11:20 +02:00
|
|
|
inp[i1], inp[i2] = inp[i2], inp[i1]
|
|
|
|
}
|
|
|
|
|
2014-10-21 18:03:27 +02:00
|
|
|
s = nil
|
|
|
|
for i := 0; i < spanItems; i++ {
|
2024-01-07 03:38:36 +01:00
|
|
|
s = sp.insert(s, inp[i], 42)
|
2014-10-21 16:11:20 +02:00
|
|
|
}
|
|
|
|
|
2024-01-07 03:38:36 +01:00
|
|
|
if n := s.len(); n != 1 {
|
2014-10-21 18:03:27 +02:00
|
|
|
t.Errorf("rand: Span length %d expected 1\n", n)
|
2014-10-21 16:11:20 +02:00
|
|
|
t.Errorf("spans: %s\n", s)
|
|
|
|
}
|
|
|
|
|
2024-01-07 03:38:36 +01:00
|
|
|
sp.freeAll(s)
|
2014-10-21 16:11:20 +02:00
|
|
|
}
|