Skip to content

Commit

Permalink
Enhanced unit-tests to fail on any failure
Browse files Browse the repository at this point in the history
  • Loading branch information
TrekkieCoder committed May 10, 2023
1 parent ded2a59 commit 50e86c1
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func BenchmarkTrie(b *testing.B) {
route := fmt.Sprintf("192.%d.%d.%d/%d", k, j, i, pLen)
res := trieR.AddTrie(route, n)
if res != 0 {
b.Errorf("failed to add %s:%d - (%d)", route, n, res)
b.Fatalf("failed to add %s:%d - (%d)", route, n, res)
trieR.Trie2String(&tk)
}
}
Expand All @@ -60,63 +60,63 @@ func TestTrie(t *testing.T) {
data := 1100
res := trieR.AddTrie(route, data)
if res != 0 {
t.Errorf("failed to add %s:%d", route, data)
t.Fatalf("failed to add %s:%d", route, data)
}

route = "192.168.1.0/15"
data = 100
res = trieR.AddTrie(route, data)
if res != 0 {
t.Errorf("failed to add %s:%d", route, data)
t.Fatalf("failed to add %s:%d", route, data)
}

route = "192.168.1.0/16"
data = 99
res = trieR.AddTrie(route, data)
if res != 0 {
t.Errorf("failed to add %s:%d", route, data)
t.Fatalf("failed to add %s:%d", route, data)
}

route = "192.168.1.0/8"
data = 1
res = trieR.AddTrie(route, data)
if res != 0 {
t.Errorf("failed to add %s:%d", route, data)
t.Fatalf("failed to add %s:%d", route, data)
}

route = "192.168.1.0/16"
data = 1
res = trieR.AddTrie(route, data)
if res == 0 {
t.Errorf("re-added %s:%d", route, data)
t.Fatalf("re-added %s:%d", route, data)
}

route = "0.0.0.0/0"
data = 222
res = trieR.AddTrie(route, data)
if res != 0 {
t.Errorf("failed to add %s:%d", route, data)
t.Fatalf("failed to add %s:%d", route, data)
}

route = "8.8.8.8/32"
data = 1200
res = trieR.AddTrie(route, data)
if res != 0 {
t.Errorf("failed to add %s:%d", route, data)
t.Fatalf("failed to add %s:%d", route, data)
}

route = "10.10.10.10/32"
data = 12
res = trieR.AddTrie(route, data)
if res != 0 {
t.Errorf("failed to add %s:%d", route, data)
t.Fatalf("failed to add %s:%d", route, data)
}

route = "1.1.1.1/32"
data = 1212
res = trieR.AddTrie(route, data)
if res != 0 {
t.Errorf("failed to add %s:%d", route, data)
t.Fatalf("failed to add %s:%d", route, data)
}

// If we need to dump trie elements
Expand All @@ -125,108 +125,108 @@ func TestTrie(t *testing.T) {

ret, ipn, rdata := trieR.FindTrie("192.41.3.1")
if ret != 0 || (*ipn).String() != "192.0.0.0/8" || rdata != 1 {
t.Errorf("failed to find %s", "192.41.3.1")
t.Fatalf("failed to find %s", "192.41.3.1")
}

ret1, ipn, rdata1 := trieR.FindTrie("195.41.3.1")
if ret1 != 0 || (*ipn).String() != "0.0.0.0/0" || rdata1 != 222 {
t.Errorf("failed to find %s", "195.41.3.1")
t.Fatalf("failed to find %s", "195.41.3.1")
}

ret2, ipn, rdata2 := trieR.FindTrie("8.8.8.8")
if ret2 != 0 || (*ipn).String() != "8.8.8.8/32" || rdata2 != 1200 {
t.Errorf("failed to find %d %s %d", ret, "8.8.8.8", rdata)
t.Fatalf("failed to find %d %s %d", ret, "8.8.8.8", rdata)
}

route = "0.0.0.0/0"
res = trieR.DelTrie(route)
if res != 0 {
t.Errorf("failed to delete %s", route)
t.Fatalf("failed to delete %s", route)
}

ret1, _, rdata1 = trieR.FindTrie("195.41.3.1")
if ret1 == 0 {
t.Errorf("failed to find %s", "195.41.3.1")
t.Fatalf("failed to find %s", "195.41.3.1")
}

route = "192.168.1.1/32"
res = trieR.DelTrie(route)
if res != 0 {
t.Errorf("failed to delete %s", route)
t.Fatalf("failed to delete %s", route)
}

route = "192.168.1.0/15"
res = trieR.DelTrie(route)
if res != 0 {
t.Errorf("failed to delete %s", route)
t.Fatalf("failed to delete %s", route)
}

route = "192.168.1.0/16"
res = trieR.DelTrie(route)
if res != 0 {
t.Errorf("failed to delete %s", route)
t.Fatalf("failed to delete %s", route)
}

route = "192.168.1.0/8"
res = trieR.DelTrie(route)
if res != 0 {
t.Errorf("failed to delete %s", route)
t.Fatalf("failed to delete %s", route)
}

route = "0.0.0.0/0"
res = trieR.DelTrie(route)
if res == 0 {
t.Errorf("failed to delete %s", route)
t.Fatalf("failed to delete %s", route)
}

route = "8.8.8.8/32"
res = trieR.DelTrie(route)
if res != 0 {
t.Errorf("failed to delete %s", route)
t.Fatalf("failed to delete %s", route)
}

route = "10.10.10.10/32"
res = trieR.DelTrie(route)
if res != 0 {
t.Errorf("failed to delete %s", route)
t.Fatalf("failed to delete %s", route)
}

route = "1.1.1.1/24"
res = trieR.DelTrie(route)
if res != 0 {
t.Errorf("failed to delete %s", route)
t.Fatalf("failed to delete %s", route)
}

trieR6 := TrieInit(true)
route = "2001:db8::/32"
data = 5100
res = trieR6.AddTrie(route, data)
if res != 0 {
t.Errorf("failed to add %s:%d", route, data)
t.Fatalf("failed to add %s:%d", route, data)
}

route = "2001:db8::1/128"
data = 5200
res = trieR6.AddTrie(route, data)
if res != 0 {
t.Errorf("failed to add %s:%d", route, data)
t.Fatalf("failed to add %s:%d", route, data)
}

ret, ipn, rdata = trieR6.FindTrie("2001:db8::1")
if ret != 0 || (*ipn).String() != "2001:db8::1/128" || rdata != 5200 {
t.Errorf("failed to find %s", "2001:db8::1")
t.Fatalf("failed to find %s", "2001:db8::1")
}

route = "2001:db8::1/128"
res = trieR6.DelTrie(route)
if res != 0 {
t.Errorf("failed to del %s", route)
t.Fatalf("failed to del %s", route)
}

route = "2001:db8::/32"
res = trieR6.DelTrie(route)
if res != 0 {
t.Errorf("failed to delete %s", route)
t.Fatalf("failed to delete %s", route)
}
}

Expand All @@ -239,89 +239,89 @@ func TestCounter(t *testing.T) {

if err == nil {
if i > 9 {
t.Errorf("get Counter unexpected %d:%d", i, idx)
t.Fatalf("get Counter unexpected %d:%d", i, idx)
}
} else {
if i <= 9 {
t.Errorf("failed to get Counter %d:%d", i, idx)
t.Fatalf("failed to get Counter %d:%d", i, idx)
}
}
}

err := cR.PutCounter(5)
if err != nil {
t.Errorf("failed to put valid Counter %d", 5)
t.Fatalf("failed to put valid Counter %d", 5)
}

err = cR.PutCounter(2)
if err != nil {
t.Errorf("failed to put valid Counter %d", 2)
t.Fatalf("failed to put valid Counter %d", 2)
}

err = cR.PutCounter(15)
if err == nil {
t.Errorf("Able to put invalid Counter %d", 15)
t.Fatalf("Able to put invalid Counter %d", 15)
}

err = cR.ReserveCounter(15)
if err == nil {
t.Errorf("Able to put reserve Counter %d:%s", 15, err)
t.Fatalf("Able to put reserve Counter %d:%s", 15, err)
}

var idx uint64
idx, err = cR.GetCounter()
if idx != 5 || err != nil {
t.Errorf("Counter get got %d of expected %d", idx, 5)
t.Fatalf("Counter get got %d of expected %d", idx, 5)
}

idx, err = cR.GetCounter()
if idx != 2 || err != nil {
t.Errorf("Counter get got %d of expected %d", idx, 2)
t.Fatalf("Counter get got %d of expected %d", idx, 2)
}

idx, err = cR.GetCounter()
if idx != ^uint64(0) || err == nil {
t.Errorf("Counter get got %d", idx)
t.Fatalf("Counter get got %d", idx)
}

err = cR.PutCounter(2)
if err != nil {
t.Errorf("failed to put valid Counter %d", 2)
t.Fatalf("failed to put valid Counter %d", 2)
}

err = cR.ReserveCounter(2)
if err != nil {
t.Errorf("failed to reserv valid Counter %d", 2)
t.Fatalf("failed to reserv valid Counter %d", 2)
}

_, err = cR.GetCounter()
if err == nil {
t.Errorf("Counter get passed unexpectedly %s", err)
t.Fatalf("Counter get passed unexpectedly %s", err)
}

err = cR.PutCounter(2)
if err != nil {
t.Errorf("failed to put valid Counter %d", 2)
t.Fatalf("failed to put valid Counter %d", 2)
}

_, err = cR.GetCounter()
if err != nil {
t.Errorf("Counter get failed unexpectedly %s", err)
t.Fatalf("Counter get failed unexpectedly %s", err)
}

cR = NewCounter(0, 5)
err = cR.ReserveCounter(0)
if err != nil {
t.Errorf("failed to reserve valid Counter %d", 0)
t.Fatalf("failed to reserve valid Counter %d", 0)
}

idx, err = cR.GetCounter()
if err != nil {
t.Errorf("failed to get valid Counter %d", 0)
t.Fatalf("failed to get valid Counter %d", 0)
}

if idx == 0 {
t.Errorf("reservation failed Counter %d", 1)
t.Fatalf("reservation failed Counter %d", 1)
}
}

Expand All @@ -330,7 +330,7 @@ func TestIfStat(t *testing.T) {

err := NetGetIfiStats("eth0", &ifs)
if err != 0 {
t.Errorf("Get stats failed for eth0")
t.Fatalf("Get stats failed for eth0")
}
}

Expand Down

0 comments on commit 50e86c1

Please sign in to comment.