(t *testing.T)
| 967 | } |
| 968 | |
| 969 | func TestIPCost(t *testing.T) { |
| 970 | ipv4 := "ip('192.168.0.1')" |
| 971 | ipv4BaseEstimatedCost := cost.FixedCostEstimate(2) |
| 972 | ipv4BaseRuntimeCost := uint64(2) |
| 973 | |
| 974 | ipv6 := "ip('2001:db8:3333:4444:5555:6666:7777:8888')" |
| 975 | ipv6BaseEstimatedCost := cost.FixedCostEstimate(4) |
| 976 | ipv6BaseRuntimeCost := uint64(4) |
| 977 | |
| 978 | testCases := []struct { |
| 979 | ops []string |
| 980 | expectEsimatedCost func(cost.CostEstimate) cost.CostEstimate |
| 981 | // expectEsimatedCostV0 is the estimate under cost.ModelVersion0, set only where the |
| 982 | // revision moved it. |
| 983 | expectEsimatedCostV0 func(cost.CostEstimate) cost.CostEstimate |
| 984 | expectRuntimeCost func(uint64) uint64 |
| 985 | }{ |
| 986 | { |
| 987 | // For just parsing the IP, the cost is expected to be the base. |
| 988 | ops: []string{""}, |
| 989 | expectEsimatedCost: func(c cost.CostEstimate) cost.CostEstimate { return c }, |
| 990 | expectRuntimeCost: func(c uint64) uint64 { return c }, |
| 991 | }, |
| 992 | { |
| 993 | ops: []string{".family()", ".isUnspecified()", ".isLoopback()", ".isLinkLocalMulticast()", ".isLinkLocalUnicast()", ".isGlobalUnicast()"}, |
| 994 | // For most other operations, the cost is expected to be the base + 1. |
| 995 | expectEsimatedCost: func(c cost.CostEstimate) cost.CostEstimate { |
| 996 | return cost.RangedCostEstimate(c.Min+1, c.Max+1) |
| 997 | }, |
| 998 | expectRuntimeCost: func(c uint64) uint64 { return c + 1 }, |
| 999 | }, |
| 1000 | { |
| 1001 | ops: []string{" == ip('192.168.0.1')"}, |
| 1002 | // For most other operations, the cost is expected to be the base + 1. |
| 1003 | expectEsimatedCost: func(c cost.CostEstimate) cost.CostEstimate { |
| 1004 | return c.Add(ipv4BaseEstimatedCost).Add(cost.RangedCostEstimate(1, 2)) |
| 1005 | }, |
| 1006 | expectRuntimeCost: func(c uint64) uint64 { return c + ipv4BaseRuntimeCost + 1 }, |
| 1007 | }, |
| 1008 | } |
| 1009 | |
| 1010 | for _, tc := range testCases { |
| 1011 | for _, op := range tc.ops { |
| 1012 | t.Run(ipv4+op, func(t *testing.T) { |
| 1013 | if tc.expectEsimatedCostV0 != nil { |
| 1014 | testCost(t, ipv4+op, tc.expectEsimatedCost(ipv4BaseEstimatedCost), tc.expectRuntimeCost(ipv4BaseRuntimeCost), tc.expectEsimatedCostV0(ipv4BaseEstimatedCost)) |
| 1015 | return |
| 1016 | } |
| 1017 | testCost(t, ipv4+op, tc.expectEsimatedCost(ipv4BaseEstimatedCost), tc.expectRuntimeCost(ipv4BaseRuntimeCost)) |
| 1018 | }) |
| 1019 | |
| 1020 | t.Run(ipv6+op, func(t *testing.T) { |
| 1021 | if tc.expectEsimatedCostV0 != nil { |
| 1022 | testCost(t, ipv6+op, tc.expectEsimatedCost(ipv6BaseEstimatedCost), tc.expectRuntimeCost(ipv6BaseRuntimeCost), tc.expectEsimatedCostV0(ipv6BaseEstimatedCost)) |
| 1023 | return |
| 1024 | } |
| 1025 | testCost(t, ipv6+op, tc.expectEsimatedCost(ipv6BaseEstimatedCost), tc.expectRuntimeCost(ipv6BaseRuntimeCost)) |
| 1026 | }) |
nothing calls this directly
no test coverage detected