Files
cabletest/phy_test.go
T

117 lines
4.1 KiB
Go

package main
import "testing"
func freshMod(margins [4]float64) phyModView {
return phyModView{fresh: true, link: true, haveSNR: true, margins: margins}
}
func TestPhyDisplayWorstMarginAndFault(t *testing.T) {
cable := cableInfo{
ecd: ecdResult{verdicts: [4]int{pairOK, pairOpen, pairOK, pairOK}, metres: [4]int{45, 12, 41, 46}},
maps: [2]byte{pairIdentityMap, pairIdentityMap},
haveMaps: [2]bool{true, true},
}
a := freshMod([4]float64{5, 5, 2, 0.5})
b := freshMod([4]float64{4, 5, 5, 5})
d := phyDisplayFrom(cable, false, a, b)
if !d.haveSNR || d.worstMargin != 0.5 {
t.Errorf("worstMargin = %v (have %v), want 0.5", d.worstMargin, d.haveSNR)
}
if d.metres != "44" || d.metresClass != clsBad {
t.Errorf("metres = %q class %d, want the healthy mean 44 painted bad", d.metres, d.metresClass)
}
}
func TestPhyDisplaySNREndOwnsMargin(t *testing.T) {
wiitek := freshMod([4]float64{9.2, 7.2, 7.0, 8.7})
fs := phyModView{fresh: true, link: true}
d := phyDisplayFrom(cableInfo{}, false, wiitek, fs)
if !d.haveSNR || d.worstMargin != 7.0 {
t.Errorf("worstMargin = %v (have %v), want the SNR end's 7.0", d.worstMargin, d.haveSNR)
}
d = phyDisplayFrom(cableInfo{}, false, fs, wiitek)
if !d.haveSNR || d.worstMargin != 7.0 {
t.Errorf("swapped ends: worstMargin = %v, want 7.0", d.worstMargin)
}
d = phyDisplayFrom(cableInfo{}, false, fs, fs)
if d.haveSNR {
t.Error("a pair with no SNR end should withhold snr")
}
}
func TestCableSummary(t *testing.T) {
healthy := ecdResult{verdicts: [4]int{pairOK, pairOK, pairOK, pairOK}, metres: [4]int{50, 48, 47, 51}}
for _, c := range []struct {
name string
cable cableInfo
measuring bool
want string
class int
}{
{"clean", cableInfo{ecd: healthy, maps: [2]byte{pairIdentityMap, pairIdentityMap}, haveMaps: [2]bool{true, true}}, false, "49", clsGood},
{"far-end swap", cableInfo{ecd: healthy, maps: [2]byte{pairIdentityMap, 0xE1}, haveMaps: [2]bool{true, true}}, false, "49", clsWarn},
{"one-end map only", cableInfo{ecd: healthy, maps: [2]byte{pairIdentityMap, 0}, haveMaps: [2]bool{true, false}}, false, "49", clsGood},
{"no diag yet", cableInfo{}, false, "-", clsNone},
{"measuring", cableInfo{ecd: healthy, maps: [2]byte{pairIdentityMap, pairIdentityMap}, haveMaps: [2]bool{true, true}}, true, "...", clsNone},
} {
s, cl := cableSummary(c.cable, c.measuring)
if s != c.want || cl != c.class {
t.Errorf("%s = %q class %d, want %q class %d", c.name, s, cl, c.want, c.class)
}
}
}
func TestPhyDisplayStaleGoesDim(t *testing.T) {
cable := cableInfo{
ecd: ecdResult{verdicts: [4]int{pairOK, pairOK, pairOK, pairOK}, metres: [4]int{45, 45, 41, 46}},
maps: [2]byte{pairIdentityMap, pairIdentityMap},
haveMaps: [2]bool{true, true},
}
d := phyDisplayFrom(cable, false, freshMod([4]float64{5, 5, 5, 5}), phyModView{})
if d.haveSNR {
t.Error("one silent module should withhold snr")
}
if d.metres != "44" || d.metresClass != clsGood {
t.Errorf("metres = %q class %d, want the diag verdict kept", d.metres, d.metresClass)
}
}
func TestCableMetresString(t *testing.T) {
c := cableInfo{ecd: ecdResult{
verdicts: [4]int{pairOK, pairOpen, pairOK, pairOK},
metres: [4]int{45, 3, 41, 46},
}}
if got := c.metresString(); got != "44" {
t.Errorf("metres = %q, want 44", got)
}
if got := (cableInfo{}).metresString(); got != "-" {
t.Errorf("no diag = %q, want -", got)
}
}
func TestSNRClass(t *testing.T) {
for _, c := range []struct {
margin float64
want int
}{
{5, clsGood}, {3, clsGood}, {2, clsWarn}, {1, clsWarn}, {0.5, clsBad}, {-2, clsBad},
} {
if got := snrClass(c.margin); got != c.want {
t.Errorf("snrClass(%v) = %d, want %d", c.margin, got, c.want)
}
}
}
func TestVerdictString(t *testing.T) {
ok := ecdResult{verdicts: [4]int{1, 1, 1, 1}, metres: [4]int{45, 45, 41, 46}}
if got := verdictString(ok); got != "all pairs ok, 45/45/41/46 m" {
t.Errorf("healthy = %q", got)
}
bad := ecdResult{verdicts: [4]int{1, 2, 4, 1}, metres: [4]int{45, 12, 30, 46}}
if got := verdictString(bad); got != "B OPEN at 12m, C XTALK at 30m" {
t.Errorf("faulted = %q", got)
}
}