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)
|
|
|
|
}
|
|
|
|
|
2022-04-15 03:35:34 +02:00
|
|
|
b := FromDuration(-time.Second, 48000)
|
|
|
|
if b != -48000 {
|
|
|
|
t.Errorf("Expected -48000, got %v", b)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := ToDuration(48000, 48000)
|
|
|
|
if c != time.Second {
|
|
|
|
t.Errorf("Expected %v, got %v", time.Second, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
d := ToDuration(-48000, 48000)
|
|
|
|
if d != -time.Second {
|
|
|
|
t.Errorf("Expected %v, got %v", -time.Second, d)
|
2020-05-02 18:41:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-22 21:24:36 +02:00
|
|
|
func TestDurationOverflow(t *testing.T) {
|
|
|
|
delta := 10 * time.Minute
|
|
|
|
dj := FromDuration(delta, JiffiesPerSec)
|
|
|
|
var prev int64
|
|
|
|
for d := time.Duration(0); d < time.Duration(1000*time.Hour); d += delta {
|
|
|
|
jiffies := FromDuration(d, JiffiesPerSec)
|
|
|
|
if d != 0 {
|
|
|
|
if jiffies != prev+dj {
|
|
|
|
t.Errorf("%v: %v, %v", d, jiffies, prev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d2 := ToDuration(jiffies, JiffiesPerSec)
|
|
|
|
if d2 != d {
|
|
|
|
t.Errorf("%v != %v (%v)", d2, d, jiffies)
|
|
|
|
}
|
|
|
|
prev = jiffies
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 15:27:47 +02:00
|
|
|
func differs(a, b, delta uint64) bool {
|
|
|
|
if a < b {
|
|
|
|
a, b = b, a
|
|
|
|
}
|
2020-12-19 17:38:47 +01:00
|
|
|
return a-b >= delta
|
2020-05-02 15:27:47 +02:00
|
|
|
}
|
|
|
|
|
2020-06-03 20:18:06 +02:00
|
|
|
func TestTime(t *testing.T) {
|
2020-05-02 15:27:47 +02:00
|
|
|
a := Now(48000)
|
2021-02-14 20:16:27 +01:00
|
|
|
time.Sleep(40 * time.Millisecond)
|
2020-05-02 15:27:47 +02:00
|
|
|
b := Now(48000) - a
|
2021-02-14 20:16:27 +01:00
|
|
|
if differs(b, 40*48, 160) {
|
2020-12-19 17:38:47 +01:00
|
|
|
t.Errorf("Expected %v, got %v", 4*48, b)
|
2020-05-02 15:27:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c := Microseconds()
|
|
|
|
time.Sleep(4 * time.Millisecond)
|
|
|
|
d := Microseconds() - c
|
|
|
|
if differs(d, 4000, 1000) {
|
|
|
|
t.Errorf("Expected %v, got %v", 4000, d)
|
|
|
|
}
|
2020-06-03 20:18:06 +02:00
|
|
|
|
|
|
|
c = Jiffies()
|
2021-08-02 01:59:06 +02:00
|
|
|
time.Sleep(time.Second * 10000000 / JiffiesPerSec)
|
2020-06-03 20:18:06 +02:00
|
|
|
d = Jiffies() - c
|
2021-08-02 01:59:06 +02:00
|
|
|
if differs(d, 10000000, 1000000) {
|
|
|
|
t.Errorf("Expected %v, got %v", 10000000, d)
|
2020-06-03 20:18:06 +02:00
|
|
|
}
|
2020-05-02 15:27:47 +02:00
|
|
|
}
|
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)",
|
2020-12-19 17:38:47 +01:00
|
|
|
ntp, ntp2, float64(diff2)/float64(1<<32))
|
2020-05-02 18:41:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|