package model_test import ( "testing" "joylink.club/rtss-core/model" ) func TestLinkPositionInRange(t *testing.T) { // 1. 配置连接关系 turnout := model.NewTurnout("1") ln1 := model.NewLinkNode(turnout) pla := model.NewPipeLink(ln1, model.PipePortA) link1 := model.NewLink(pla, nil) link1.SetLength(1000) linkPos1 := model.NewLinkPosition(link1, 100) // 2. 验证连接关系 inRangeTests := []struct { start int64 end int64 expect bool }{ {start: 0, end: 50, expect: false}, {start: 0, end: 100, expect: true}, {start: 100, end: 101, expect: true}, {start: 100, end: 100, expect: true}, {start: 101, end: 100, expect: true}, {start: 101, end: 200, expect: false}, } for _, test := range inRangeTests { in := linkPos1.IsInRange(test.start, test.end) if in != test.expect { t.Errorf("expect: %v, but got: %v", test.expect, in) } } } func TestLinkPositionAdd(t *testing.T) { // 1. 配置连接关系 turnout := model.NewTurnout("1") ln1 := model.NewLinkNode(turnout) pla := model.NewPipeLink(ln1, model.PipePortA) link1 := model.NewLink(pla, nil) link1.SetLength(1000) // 2. 验证连接关系 addTests := []struct { len uint32 direction model.LinkDirection expect bool expectValue int64 }{ {len: 0, direction: model.LinkDirectionA2B, expect: false, expectValue: 100}, {len: 10, direction: model.LinkDirectionA2B, expect: false, expectValue: 110}, {len: 10, direction: model.LinkDirectionB2A, expect: false, expectValue: 90}, {len: 1000, direction: model.LinkDirectionA2B, expect: true, expectValue: 1100}, {len: 1000, direction: model.LinkDirectionB2A, expect: true, expectValue: -900}, } for _, test := range addTests { linkPos1 := model.NewLinkPosition(link1, 100) overflow := linkPos1.Move(test.len, test.direction) if overflow != test.expect { t.Errorf("expect: %v, but got: %v", test.expect, overflow) } if linkPos1.Position() != test.expectValue { t.Errorf("expect: %v, but got: %v", test.expectValue, linkPos1.Position()) } } }