1
0
mirror of https://gitee.com/willfree/mlsr.git synced 2026-06-03 15:56:13 +08:00

测试NextHop及NextHopList

This commit is contained in:
free will
2022-06-16 17:36:17 +08:00
parent 8d52bcf7fc
commit 3eb8fb51bb
3 changed files with 315 additions and 11 deletions
+29 -11
View File
@@ -12,6 +12,7 @@ import (
"errors"
"minlib/encoding"
"mlsr/extensions"
"strconv"
)
type NextHopContainer []*NextHop
@@ -25,6 +26,23 @@ type NextHopList struct {
nextHops NextHopContainer
}
//
// ToString
// @Description: 转string。用于测试
// @receiver l
// @return string
//
func (l *NextHopList) ToString() string {
res := "----------------\n"
for i := 0; i < len(l.nextHops); i++ {
res += l.nextHops[i].LogicFaceUri() + " "
res += strconv.FormatUint(l.nextHops[i].RouteCost.RouteCost(), 10) + " "
res += strconv.FormatBool(l.nextHops[i].IsHyperbolic) + " \n"
}
res += "----------------\n"
return res
}
//
// GetNextHops
// @Description: 获取下一跳列表
@@ -44,7 +62,7 @@ func (l *NextHopList) GetNextHops() []*NextHop {
//
func (l *NextHopList) findSameUriNextHop(hop *NextHop) int {
for i := 0; i < len(l.nextHops); i++ {
if l.nextHops[i].LogicFaceUri() == hop.LogicFaceUri(){
if l.nextHops[i].LogicFaceUri() == hop.LogicFaceUri() {
return i
}
}
@@ -61,7 +79,7 @@ func (l *NextHopList) findSameUriNextHop(hop *NextHop) int {
func (l *NextHopList) findSameNextHop(hop *NextHop) int {
for i := 0; i < len(l.nextHops); i++ {
if (l.nextHops[i].LogicFaceUri() == hop.LogicFaceUri()) &&
(l.nextHops[i].RouteCost.RouteCost() == hop.RouteCost.RouteCost()){
(l.nextHops[i].RouteCost.RouteCost() == hop.RouteCost.RouteCost()) {
return i
}
}
@@ -74,11 +92,11 @@ func (l *NextHopList) findSameNextHop(hop *NextHop) int {
// @receiver l
// @param hop
//
func (l *NextHopList) AddNextHop(hop *NextHop) {
func (l *NextHopList) AddNextHop(hop *NextHop) {
index := l.findSameUriNextHop(hop)
if index < 0{
l.nextHops = append(l.nextHops,hop)
}else if l.nextHops[index].RouteCost.RouteCost() != hop.RouteCost.RouteCost(){
if index < 0 {
l.nextHops = append(l.nextHops, hop)
} else if l.nextHops[index].RouteCost.RouteCost() != hop.RouteCost.RouteCost() {
l.nextHops[index].RouteCost.SetRouteCost(hop.RouteCost.RouteCost())
}
}
@@ -92,8 +110,8 @@ func (l *NextHopList) AddNextHop(hop *NextHop) {
func (l *NextHopList) RemoveNextHop(hop *NextHop) {
// 查找该条目索引
index := l.findSameUriNextHop(hop)
if index>0 {
l.nextHops = append(l.nextHops[:index],l.nextHops[index+1:]...)
if index >= 0 {
l.nextHops = append(l.nextHops[:index], l.nextHops[index+1:]...)
}
}
@@ -112,8 +130,8 @@ func (l *NextHopList) Size() int {
// @Description: 清空列表
// @receiver l
//
func (l *NextHopList) Clear() {
l.nextHops=[]*NextHop{}
func (l *NextHopList) Clear() {
l.nextHops = []*NextHop{}
}
//
@@ -188,4 +206,4 @@ func (l *NextHopList) WireDecode(block *encoding.Block) error {
}
l.nextHops = hopContainer
return nil
}
}
+190
View File
@@ -0,0 +1,190 @@
// Package route
// @Author: Wang Feng
// @Description:
// @Version: 0.1.0
// @Date: 2022/6/16 17:08
// @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
//
package route
import (
"fmt"
"minlib/encoding"
"testing"
)
//
// TestNextHopList_AddNextHop
// @Description: 测试构造下一跳列表
// @param t
//
func TestNextHopList_AddNextHop(t *testing.T) {
// 构造三个下一跳
nthop1, _ := NewNextHop("/hop1", 10)
nthop2, _ := NewNextHop("/hop2", 20)
nthop3, _ := NewNextHop("/hop3", 30)
// 构造hoplist
hopList := new(NextHopList)
hopList.AddNextHop(nthop1)
hopList.AddNextHop(nthop2)
hopList.AddNextHop(nthop3)
// 打印长度
fmt.Println(hopList.Size())
// 打印所有下一跳
fmt.Println(hopList.ToString())
}
//
// TestNextHopList_find
// @Description: 测试查找
// @param t
//
func TestNextHopList_find(t *testing.T) {
// 构造三个下一跳
nthop1, _ := NewNextHop("/hop1", 10)
nthop2, _ := NewNextHop("/hop2", 20)
nthop3, _ := NewNextHop("/hop3", 30)
// 构造hoplist
hopList := new(NextHopList)
hopList.AddNextHop(nthop1)
hopList.AddNextHop(nthop2)
hopList.AddNextHop(nthop3)
// 测试查找相同Uri
ntTest, _ := NewNextHop("/hop2", 111)
i1 := hopList.findSameUriNextHop(ntTest)
fmt.Println("第二个下标:", i1)
ntTest, _ = NewNextHop("/hopTest", 111)
i2 := hopList.findSameUriNextHop(ntTest)
fmt.Println("没找到下标:", i2)
// 测试查找完全相同下一跳
ntTest, _ = NewNextHop("/hop2", 20)
i1 = hopList.findSameNextHop(ntTest)
fmt.Println("第二个下标:", i1)
ntTest.SetRouteCost(555)
i2 = hopList.findSameNextHop(ntTest)
fmt.Println("没找到下标:", i2)
}
//
// TestNextHopList_RemoveNextHop
// @Description: 测试删除
// @param t
//
func TestNextHopList_RemoveNextHop(t *testing.T) {
// 构造三个下一跳
nthop1, _ := NewNextHop("/hop1", 10)
nthop2, _ := NewNextHop("/hop2", 20)
nthop3, _ := NewNextHop("/hop3", 30)
// 构造hoplist
hopList := new(NextHopList)
hopList.AddNextHop(nthop1)
hopList.AddNextHop(nthop2)
hopList.AddNextHop(nthop3)
// 打印长度
fmt.Println(hopList.Size())
// 打印所有下一跳
fmt.Println(hopList.ToString())
// 删除
npTest, _ := NewNextHop("/hop1", 10)
hopList.RemoveNextHop(npTest)
// 打印长度
fmt.Println(hopList.Size())
// 打印所有下一跳
fmt.Println(hopList.ToString())
// 删除一个只有uri相同
npTest, _ = NewNextHop("/hop3", 100)
hopList.RemoveNextHop(npTest)
// 打印长度
fmt.Println(hopList.Size())
// 打印所有下一跳
fmt.Println(hopList.ToString())
// 删除一个uri不同
npTest, _ = NewNextHop("/hoppp", 100)
hopList.RemoveNextHop(npTest)
// 打印长度
fmt.Println(hopList.Size())
// 打印所有下一跳
fmt.Println(hopList.ToString())
}
//
// TestNextHopList_WireEncode
// @Description: 测试编码
// @param t
//
func TestNextHopList_WireEncode(t *testing.T) {
// 构造三个下一跳
nthop1, _ := NewNextHop("/hop1", 10)
nthop2, _ := NewNextHop("/hop2", 20)
nthop3, _ := NewNextHop("/hop3", 30)
// 构造hoplist
hopList := new(NextHopList)
hopList.AddNextHop(nthop1)
hopList.AddNextHop(nthop2)
hopList.AddNextHop(nthop3)
// 编码
encoder := encoding.Encoder{}
err := encoder.EncoderReset(encoding.MaxPacketSize, 0)
if err != nil {
fmt.Println("NextHopList编码报错,", err.Error())
return
}
length, err := hopList.WireEncode(&encoder)
if err != nil {
t.Fatal("NextHopList编码失败", err.Error())
}
// 获取编码
buf, err := encoder.GetBuffer()
if err != nil {
t.Fatal("NextHopList获取buffer结果失败", err.Error())
}
// 打印编码结果。如果可以正确解码,则说明编码成功
fmt.Println(length)
for i := 0; i < len(buf); i++ {
fmt.Print(buf[i], ", ")
}
fmt.Println()
}
//
// TestNextHopList_WireDecode
// @Description: 测试解码
// @param t
//
func TestNextHopList_WireDecode(t *testing.T) {
// 由数组构建block
var buf = [58]byte{241, 1, 188, 54, 241, 1, 187, 14, 241, 1, 186,
1, 10, 241, 1, 185, 5, 47, 104, 111, 112, 49, 241, 1, 187, 14,
241, 1, 186, 1, 20, 241, 1, 185, 5, 47, 104, 111, 112, 50, 241,
1, 187, 14,
241, 1, 186, 1, 30, 241, 1, 185, 5, 47, 104, 111, 112, 51}
block, err := encoding.CreateBlockByBuffer(buf[:], true)
if err != nil {
fmt.Println("创建block失败 ", err.Error())
return
}
// 由Block构建结构体,并打印变量
base := new(NextHopList)
err = base.WireDecode(block)
if err != nil {
fmt.Println("线性解码失败 ", err.Error())
return
}
fmt.Println(base.ToString())
}
+96
View File
@@ -0,0 +1,96 @@
// Package route
// @Author: Wang Feng
// @Description:
// @Version: 0.1.0
// @Date: 2022/6/16 16:52
// @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
//
package route
import (
"fmt"
"minlib/encoding"
"testing"
)
//
// TestNewNextHop
// @Description: 测试构造
// @param t
//
func TestNewNextHop(t *testing.T) {
nexthop, err := NewNextHop("/test", 543)
if err == nil {
fmt.Println(nexthop.IsHyperbolic)
fmt.Println(nexthop.RouteCost.RouteCost())
fmt.Println(nexthop.LogicFaceUri())
} else {
fmt.Println("测试NewNextHop报错,", err.Error())
}
}
//
// TestNextHop_WireEncode
// @Description: 测试编码
// @param t
//
func TestNextHop_WireEncode(t *testing.T) {
// 构造
nexthop, err := NewNextHop("/test", 543)
if err != nil {
fmt.Println("NewNextHop报错,", err.Error())
return
}
// 编码
encoder := encoding.Encoder{}
err = encoder.EncoderReset(encoding.MaxPacketSize, 0)
if err != nil {
fmt.Println("NextHop编码报错,", err.Error())
return
}
length, err := nexthop.WireEncode(&encoder)
if err != nil {
t.Fatal("NextHop编码失败", err.Error())
}
// 获取编码
buf, err := encoder.GetBuffer()
if err != nil {
t.Fatal("NextHop获取buffer结果失败", err.Error())
}
// 打印编码结果。如果可以正确解码,则说明编码成功
fmt.Println(length)
for i := 0; i < len(buf); i++ {
fmt.Print(buf[i], ", ")
}
fmt.Println()
}
//
// TestNextHop_WireDecode
// @Description: 测试解码
// @param t
//
func TestNextHop_WireDecode(t *testing.T) {
// 由数组构建block
var buf = [19]byte{241, 1, 187, 15, 241, 1, 186, 2, 2, 31, 241, 1, 185, 5, 47, 116, 101, 115, 116}
block, err := encoding.CreateBlockByBuffer(buf[:], true)
if err != nil {
fmt.Println("创建block失败 ", err.Error())
return
}
// 由Block构建结构体,并打印变量
base := new(NextHop)
err = base.WireDecode(block)
if err != nil {
fmt.Println("线性解码失败 ", err.Error())
return
}
fmt.Println(base.IsHyperbolic)
fmt.Println(base.RouteCost.RouteCost())
fmt.Println(base.LogicFaceUri())
}