2014-03-02 22:47:28 -08:00
|
|
|
package bandwidth
|
2014-03-02 22:44:09 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func validate(t *testing.T, actual, expected []float64) {
|
|
|
|
if len(actual) != len(expected) {
|
|
|
|
t.Errorf("len is not same: %d expected %d", len(actual), len(expected))
|
|
|
|
}
|
|
|
|
for i, _ := range actual {
|
|
|
|
if actual[i] != expected[i] {
|
|
|
|
t.Errorf("%dth: got %f expected %f", i, actual[i], expected[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmpty(t *testing.T) {
|
|
|
|
bw, err := NewBandwidth([]int{1, 10, 60}, 100*time.Millisecond)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2014-03-08 00:45:55 -08:00
|
|
|
go bw.Run()
|
|
|
|
bw.rxstream = []int{1, 10, 60}
|
|
|
|
validate(t, <-bw.Rx, []float64{})
|
2014-03-02 22:44:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptySeconds(t *testing.T) {
|
|
|
|
_, err := NewBandwidth([]int{}, 100*time.Millisecond)
|
|
|
|
if err == nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 00:45:55 -08:00
|
|
|
|
2014-03-08 18:07:43 -08:00
|
|
|
func TestStreamSize(t *testing.T) {
|
|
|
|
bw, _ := NewBandwidth([]int{1, 2, 5}, 1*time.Second)
|
|
|
|
if len(bw.rxstream) != 5 {
|
|
|
|
t.Errorf("rxstream slice wrong length: %d, expected %d", len(bw.rxstream), 5)
|
|
|
|
}
|
|
|
|
if len(bw.txstream) != 5 {
|
|
|
|
t.Errorf("txstream slice wrong length: %d, expected %d", len(bw.rxstream), 5)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOnes(t *testing.T) {
|
|
|
|
bw, _ := NewBandwidth([]int{1, 2, 5}, 1*time.Second)
|
|
|
|
for i := 0; i < bw.max; i++ {
|
|
|
|
bw.rxstream[i] = 1.0
|
|
|
|
}
|
|
|
|
avgs := bw.averages(bw.rxstream)
|
|
|
|
// try a large range of starting points:
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
bw.timeI = i
|
|
|
|
validate(t, avgs, []float64{1.0, 1.0, 1.0})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestManyOnes(t *testing.T) {
|
|
|
|
bw, _ := NewBandwidth([]int{1, 10, 60}, 1*time.Second)
|
|
|
|
for i := 0; i < bw.max; i++ {
|
|
|
|
bw.rxstream[i] = 1.0
|
|
|
|
}
|
|
|
|
avgs := bw.averages(bw.rxstream)
|
|
|
|
// try a large range of starting points:
|
|
|
|
for i := -70; i < 70; i++ {
|
|
|
|
bw.timeI = i
|
|
|
|
validate(t, avgs, []float64{1.0, 1.0, 1.0})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinear(t *testing.T) {
|
|
|
|
bw, _ := NewBandwidth([]int{1, 10, 60}, 1*time.Second)
|
|
|
|
for i := 0; i < bw.max; i++ {
|
|
|
|
bw.rxstream[i] = i
|
|
|
|
}
|
|
|
|
avgs := bw.averages(bw.rxstream)
|
|
|
|
validate(t, avgs, []float64{0.0, 4.5, 29.5})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInverseLinear(t *testing.T) {
|
|
|
|
bw, _ := NewBandwidth([]int{1, 10, 60}, 1*time.Second)
|
|
|
|
for i := 0; i < bw.max; i++ {
|
|
|
|
bw.rxstream[i] = bw.max - i
|
|
|
|
}
|
|
|
|
avgs := bw.averages(bw.rxstream)
|
|
|
|
validate(t, avgs, []float64{60.0, 55.5, 30.5})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSpecific(t *testing.T) {
|
|
|
|
bw, _ := NewBandwidth([]int{1, 2, 10}, 1*time.Second)
|
|
|
|
bw.rxstream[8] = 1.0
|
|
|
|
var avgs []float64
|
|
|
|
|
|
|
|
bw.timeI = 8
|
|
|
|
avgs = bw.averages(bw.rxstream)
|
|
|
|
validate(t, avgs, []float64{1.0, 0.5, 0.1})
|
|
|
|
|
|
|
|
bw.timeI = 7
|
|
|
|
avgs = bw.averages(bw.rxstream)
|
|
|
|
validate(t, avgs, []float64{0.0, 0.5, 0.1})
|
|
|
|
|
|
|
|
bw.timeI = 9
|
|
|
|
avgs = bw.averages(bw.rxstream)
|
|
|
|
validate(t, avgs, []float64{0.0, 0.0, 0.1})
|
2014-03-08 00:45:55 -08:00
|
|
|
}
|