2020-06-03 20:12:25 +02:00
|
|
|
package rtptime
|
2020-05-02 15:27:47 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-05-02 18:41:18 +02:00
|
|
|
func TestDuration(t *testing.T) {
|
2020-06-01 01:32:28 +02:00
|
|
|
a := FromDuration(time.Second, 48000)
|
2020-05-02 18:41:18 +02:00
|
|
|
if a != 48000 {
|
|
|
|
t.Errorf("Expected 48000, got %v", a)
|
|
|
|
}
|
|
|
|
|
|
|
|
b := toDuration(48000, 48000)
|
|
|
|
if b != time.Second {
|
|
|
|
t.Errorf("Expected %v, got %v", time.Second, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 15:27:47 +02:00
|
|
|
func differs(a, b, delta uint64) bool {
|
|
|
|
if a < b {
|
|
|
|
a, b = b, a
|
|
|
|
}
|
|
|
|
return a - b >= delta
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMono(t *testing.T) {
|
|
|
|
a := Now(48000)
|
|
|
|
time.Sleep(4 * time.Millisecond)
|
|
|
|
b := Now(48000) - a
|
|
|
|
if differs(b, 4 * 48, 16) {
|
|
|
|
t.Errorf("Expected %v, got %v", 4 * 48, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := Microseconds()
|
|
|
|
time.Sleep(4 * time.Millisecond)
|
|
|
|
d := Microseconds() - c
|
|
|
|
if differs(d, 4000, 1000) {
|
|
|
|
t.Errorf("Expected %v, got %v", 4000, d)
|
|
|
|
}
|
|
|
|
}
|
2020-05-02 18:41:18 +02:00
|
|
|
|
|
|
|
func TestNTP(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
ntp := TimeToNTP(now)
|
|
|
|
now2 := NTPToTime(ntp)
|
|
|
|
ntp2 := TimeToNTP(now2)
|
|
|
|
|
|
|
|
diff1 := now2.Sub(now)
|
|
|
|
if diff1 < 0 {
|
|
|
|
diff1 = -diff1
|
|
|
|
}
|
|
|
|
if diff1 > time.Nanosecond {
|
|
|
|
t.Errorf("Expected %v, got %v (diff=%v)",
|
|
|
|
now, now2, diff1)
|
|
|
|
}
|
|
|
|
|
|
|
|
diff2 := int64(ntp2 - ntp)
|
|
|
|
if diff2 < 0 {
|
|
|
|
diff2 = -diff2
|
|
|
|
}
|
|
|
|
if diff2 > (1 << 8) {
|
|
|
|
t.Errorf("Expected %v, got %v (diff=%v)",
|
|
|
|
ntp, ntp2, float64(diff2) / float64(1<<32))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|