• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lduchosal / ipnetwork / 827

17 Aug 2025 08:49PM UTC coverage: 93.75% (+0.05%) from 93.698%
827

push

appveyor

web-flow
feat/operator-add-int (#365)

* Feat: operator add IPNetwork, int
* Feat: operation -minus
* Fix: edge case with overflow

1965 of 2096 relevant lines covered (93.75%)

668523.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.06
/src/System.Net.IPNetwork/IPNetwork2Operators.cs
1
// <copyright file="IPNetwork2Operators.cs" company="IPNetwork">
2
// Copyright (c) IPNetwork. All rights reserved.
3
// </copyright>
4

5
using System.Collections.Generic;
6

7
namespace System.Net;
8

9
/// <summary>
10
/// Opertors.
11
/// </summary>
12
public sealed partial class IPNetwork2
13
{
14
    /// <summary>
15
    /// Compares two IPNetwork.
16
    /// </summary>
17
    /// <param name="left">left instance.</param>
18
    /// <param name="right">Right instance.</param>
19
    /// <returns>true if left equals right; otherwise, false.</returns>
20
    public static bool operator ==(IPNetwork2 left, IPNetwork2 right)
21
    {
596,608✔
22
        return Equals(left, right);
596,608✔
23
    }
596,608✔
24

25
    /// <summary>
26
    /// Compares two IPNetwork.
27
    /// </summary>
28
    /// <param name="left">left instance.</param>
29
    /// <param name="right">Right instance.</param>
30
    /// <returns>true if left does not equals right; otherwise, false.</returns>
31
    public static bool operator !=(IPNetwork2 left, IPNetwork2 right)
32
    {
132,264✔
33
        return !Equals(left, right);
132,264✔
34
    }
132,264✔
35

36
    /// <summary>
37
    /// Compares two IPNetwork.
38
    /// </summary>
39
    /// <param name="left">left instance.</param>
40
    /// <param name="right">Right instance.</param>
41
    /// <returns>true if left is less than right; otherwise, false.</returns>
42
    public static bool operator <(IPNetwork2 left, IPNetwork2 right)
43
    {
3✔
44
        return Compare(left, right) < 0;
3✔
45
    }
3✔
46

47
    
48
    /// <summary>
49
    /// Compares two IPNetwork.
50
    /// </summary>
51
    /// <param name="left">left instance.</param>
52
    /// <param name="right">Right instance.</param>
53
    /// <returns>true if left is less than right; otherwise, false.</returns>
54
    public static bool operator <=(IPNetwork2 left, IPNetwork2 right)
55
    {
3✔
56
        return Compare(left, right) <= 0;
3✔
57
    }
3✔
58

59
    /// <summary>
60
    /// Compares two IPNetwork.
61
    /// </summary>
62
    /// <param name="left">left instance.</param>
63
    /// <param name="right">Right instance.</param>
64
    /// <returns>true if left is greater than right; otherwise, false.</returns>
65
    public static bool operator >(IPNetwork2 left, IPNetwork2 right)
66
    {
4✔
67
        return Compare(left, right) > 0;
4✔
68
    }
4✔
69
    
70
    /// <summary>
71
    /// Compares two IPNetwork.
72
    /// </summary>
73
    /// <param name="left">left instance.</param>
74
    /// <param name="right">Right instance.</param>
75
    /// <returns>true if left is greater than right; otherwise, false.</returns>
76
    public static bool operator >=(IPNetwork2 left, IPNetwork2 right)
77
    {
5✔
78
        return Compare(left, right) >= 0;
5✔
79
    }
5✔
80
    
81
    /// <summary>
82
    /// Subtract two IPNetwork.
83
    /// </summary>
84
    /// <param name="left">left instance.</param>
85
    /// <param name="right">Right instance.</param>
86
    /// <returns>The symmetric difference (subtraction) of two networks.</returns>
87
    public static List<IPNetwork2> operator -(IPNetwork2 left, IPNetwork2 right)
88
    {
10✔
89
        return left.Subtract(right);
10✔
90
    }
10✔
91
    
92
    /// <summary>
93
    /// Add two IPNetwork.
94
    /// </summary>
95
    /// <param name="left">left instance.</param>
96
    /// <param name="right">Right instance.</param>
97
    /// <returns>Try to supernet two consecutive cidr equal subnet into a single one, otherwise return both netowkrs.</returns>
98
    public static List<IPNetwork2> operator +(IPNetwork2 left, IPNetwork2 right)
99
    {
15✔
100
        if (left.TrySupernet(right, out var result))
15✔
101
        {
12✔
102
            return [result];
12✔
103
        }
104
        return [left, right];
3✔
105
    }
15✔
106
    
107
    /// <summary>
108
    /// Behavior
109
    ///  The addition operator (+) performs the following operations:
110
    ///  Network Expansion: Adds the specified number of IP addresses to the network range
111
    ///  Optimal Grouping: Attempts to create the most efficient network representation
112
    ///  Multiple Networks: When a single contiguous network cannot represent the result, returns multiple networks
113
    ///  CIDR Optimization: Automatically calculates the appropriate subnet mask for the expanded range
114
    /// </summary>
115
    /// <param name="left">left instance.</param>
116
    /// <param name="add">number.</param>
117
    /// <returns>Adds the specified number of IP addresses to the network range.</returns>
118
    public static IEnumerable<IPNetwork2> operator +(IPNetwork2 left, int add)
119
    {
27✔
120
        if (add < 0)
27✔
121
        {
2✔
122
            return left - (add * -1);
2✔
123
        }
124
        if (add == 0)
25✔
125
        {
1✔
126
            return new[] { left };
1✔
127
        }
128
        var start = ToBigInteger(left.First);
24✔
129
        var last = ToBigInteger(left.Last);
24✔
130
        var end = last+ add;
24✔
131

132
        if (end < start)
24✔
133
        {
×
134
            return [];
×
135
        }
136

137
        var startIp = ToIPAddress(start, left.AddressFamily);
24✔
138
        var endIp = ToIPAddress(end, left.AddressFamily);
24✔
139
        
140
        var uintStart = ToBigInteger(startIp);
24✔
141
        var uintEnd = ToBigInteger(endIp);
24✔
142

143
        if (uintEnd <= uintStart)
24✔
144
        {
4✔
145
            throw new OverflowException("IPNetwork overflow");
4✔
146
        }
147
        InternalParseRange(true, startIp, endIp, out IEnumerable<IPNetwork2> networks);
20✔
148
        return networks;
20✔
149
    }
23✔
150
    
151
    /// <summary>
152
    /// Add IPNetwork.
153
    /// </summary>
154
    /// <param name="left">left instance.</param>
155
    /// <param name="subtract">number.</param>
156
    /// <returns>Try to supernet two consecutive cidr equal subnet into a single one, otherwise return both netowkrs.</returns>
157
    public static IEnumerable<IPNetwork2> operator -(IPNetwork2 left, int subtract)
158
    {
19✔
159
        if (subtract < 0)
19✔
160
        {
4✔
161
            return left + (subtract * -1);
4✔
162
        }
163
        if (subtract == 0)
15✔
164
        {
1✔
165
            return new[] { left };
1✔
166
        }
167
        var start = ToBigInteger(left.First);
14✔
168
        var last = ToBigInteger(left.Last);
14✔
169
        var end = last- subtract;
14✔
170

171
        if (end < start)
14✔
172
        {
13✔
173
            return [];
13✔
174
        }
175

176
        var startIp = ToIPAddress(start, left.AddressFamily);
1✔
177
        var endIp = ToIPAddress(end, left.AddressFamily);
1✔
178
        
179
        InternalParseRange(true, startIp, endIp, out IEnumerable<IPNetwork2> networks);
1✔
180
        return networks;
1✔
181
    }
17✔
182
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc