Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package router_test
 
import (
    "reflect"
    "testing"
 
    "github.com/v2fly/v2ray-core/v5/app/router"
)
 
func TestWeight(t *testing.T) {
    manager := router.NewWeightManager(
        []*router.StrategyWeight{
            {
                Match: "x5",
                Value: 100,
            },
            {
                Match: "x8",
            },
            {
                Regexp: true,
                Match:  `\bx0+(\.\d+)?\b`,
                Value:  1,
            },
            {
                Regexp: true,
                Match:  `\bx\d+(\.\d+)?\b`,
            },
        },
        1, func(v, w float64) float64 {
            return v * w
        },
    )
    tags := []string{
        "node name, x5, and more",
        "node name, x8",
        "node name, x15",
        "node name, x0100, and more",
        "node name, x10.1",
        "node name, x00.1, and more",
    }
    // test weight
    expected := []float64{100, 8, 15, 100, 10.1, 1}
    actual := make([]float64, 0)
    for _, tag := range tags {
        actual = append(actual, manager.Get(tag))
    }
    if !reflect.DeepEqual(expected, actual) {
        t.Errorf("expected: %v, actual: %v", expected, actual)
    }
    // test scale
    expected2 := []float64{1000, 80, 150, 1000, 101, 10}
    actual2 := make([]float64, 0)
    for _, tag := range tags {
        actual2 = append(actual2, manager.Apply(tag, 10))
    }
    if !reflect.DeepEqual(expected2, actual2) {
        t.Errorf("expected2: %v, actual2: %v", expected2, actual2)
    }
}