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

lduchosal / ipnetwork / 881

07 Mar 2026 04:55PM UTC coverage: 92.371% (-1.0%) from 93.419%
881

Pull #383

appveyor

web-flow
Merge 72bf930e1 into 4178c9692
Pull Request #383: feat/v4-nullable

1901 of 2058 relevant lines covered (92.37%)

580908.3 hits per line

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

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

5
namespace System.Net;
6

7
using System.Diagnostics.CodeAnalysis;
8
using System.Numerics;
9

10
/// <summary>
11
/// supernet.
12
/// </summary>
13
public sealed partial class IPNetwork2
14
{
15
    /// <summary>
16
    /// Supernet two consecutive cidr equal subnet into a single one
17
    /// 192.168.0.0/24 + 192.168.1.0/24 = 192.168.0.0/23
18
    /// 10.1.0.0/16 + 10.0.0.0/16 = 10.0.0.0/15
19
    /// 192.168.0.0/24 + 192.168.0.0/25 = 192.168.0.0/24.
20
    /// </summary>
21
    /// <param name="network2">The network to supernet with.</param>
22
    /// <returns>A super netted IP Network.</returns>
23
    public IPNetwork2 Supernet(IPNetwork2 network2)
24
    {
10✔
25
        if (!InternalSupernet(false, this, network2, out IPNetwork2? supernet))
10✔
26
        {
×
27
            throw new ArgumentException("Failed to supernet networks.", nameof(network2));
×
28
        }
29

30
        return supernet;
4✔
31
    }
4✔
32

33
    /// <summary>
34
    /// Try to supernet two consecutive cidr equal subnet into a single one
35
    /// 192.168.0.0/24 + 192.168.1.0/24 = 192.168.0.0/23
36
    /// 10.1.0.0/16 + 10.0.0.0/16 = 10.0.0.0/15
37
    /// 192.168.0.0/24 + 192.168.0.0/25 = 192.168.0.0/24.
38
    /// </summary>
39
    /// <param name="network2">The network to supernet with.</param>
40
    /// <param name="supernet">The resulting IPNetwork.</param>
41
    /// <returns>true if network2 was super netted successfully; otherwise, false.</returns>
42
    public bool TrySupernet(IPNetwork2 network2, [NotNullWhen(true)] out IPNetwork2? supernet)
43
    {
132,146✔
44
        return InternalSupernet(true, this, network2, out supernet);
132,146✔
45
    }
132,146✔
46

47
    /// <summary>
48
    /// Supernet two consecutive cidr equal subnet into a single one
49
    /// 192.168.0.0/24 + 192.168.1.0/24 = 192.168.0.0/23
50
    /// 10.1.0.0/16 + 10.0.0.0/16 = 10.0.0.0/15
51
    /// 192.168.0.0/24 + 192.168.0.0/25 = 192.168.0.0/24.
52
    /// </summary>
53
    /// <param name="network1">The first network.</param>
54
    /// <param name="network2">The second network.</param>
55
    /// <returns>A super netted IP Network.</returns>
56
    public static IPNetwork2 Supernet(IPNetwork2 network1, IPNetwork2 network2)
57
    {
1✔
58
        if (!InternalSupernet(false, network1, network2, out IPNetwork2? supernet))
1✔
59
        {
×
60
            throw new ArgumentException("Failed to supernet networks.", nameof(network2));
×
61
        }
62

63
        return supernet;
1✔
64
    }
1✔
65

66
    /// <summary>
67
    /// Try to supernet two consecutive cidr equal subnet into a single one
68
    /// 192.168.0.0/24 + 192.168.1.0/24 = 192.168.0.0/23
69
    /// 10.1.0.0/16 + 10.0.0.0/16 = 10.0.0.0/15
70
    /// 192.168.0.0/24 + 192.168.0.0/25 = 192.168.0.0/24.
71
    /// </summary>
72
    /// <param name="network1">The first network.</param>
73
    /// <param name="network2">The second network.</param>
74
    /// <param name="supernet">The resulting IPNetwork.</param>
75
    /// <returns>true if network1 and network2 were super netted successfully; otherwise, false.</returns>
76
    public static bool TrySupernet(IPNetwork2 network1, IPNetwork2 network2, [NotNullWhen(true)] out IPNetwork2? supernet)
77
    {
3✔
78
#if NETSTANDARD2_1
79
        if (network1 == null)
80
        {
81
            throw new ArgumentNullException(nameof(network1));
82
        }
83
#else
84
        ArgumentNullException.ThrowIfNull(network1);
3✔
85
#endif
86

87
#if NETSTANDARD2_1
88
        if (network2 == null)
89
        {
90
            throw new ArgumentNullException(nameof(network2));
91
        }
92
#else
93
        ArgumentNullException.ThrowIfNull(network2);
1✔
94
#endif
95

96
        return InternalSupernet(true, network1, network2, out supernet);
1✔
97
    }
1✔
98

99
    /// <summary>
100
    /// Attempts to merge two adjacent IP networks with equal CIDR values into a single supernet.
101
    /// </summary>
102
    /// <param name="trySupernet">If true, suppresses exceptions on failure; otherwise, throws.</param>
103
    /// <param name="network1">The first IP network.</param>
104
    /// <param name="network2">The second IP network.</param>
105
    /// <param name="supernet">The resulting supernet if the merge is successful; otherwise, null.</param>
106
    internal static bool InternalSupernet(
107
        bool trySupernet,
108
        IPNetwork2 network1,
109
        IPNetwork2 network2,
110
        [NotNullWhen(true)] out IPNetwork2? supernet)
111
    {
132,160✔
112
        // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
113
        if (network1 == null)
132,160✔
114
        {
2✔
115
            if (!trySupernet)
2✔
116
            {
1✔
117
                throw new ArgumentNullException(nameof(network1));
1✔
118
            }
119

120
            supernet = null;
1✔
121
            return false;
1✔
122
        }
123

124
        if (network2 == null)
132,158✔
125
        {
4✔
126
            if (!trySupernet)
4✔
127
            {
2✔
128
                throw new ArgumentNullException(nameof(network2));
2✔
129
            }
130

131
            supernet = null;
2✔
132
            return false;
2✔
133
        }
134

135
        if (network1.Contains(network2))
132,154✔
136
        {
19✔
137
            supernet = new IPNetwork2(network1.InternalNetwork, network1.family, network1.Cidr);
19✔
138
            return true;
19✔
139
        }
140

141
        if (network2.Contains(network1))
132,135✔
142
        {
3✔
143
            supernet = new IPNetwork2(network2.InternalNetwork, network2.family, network2.Cidr);
3✔
144
            return true;
3✔
145
        }
146

147
        if (network1.cidr != network2.cidr)
132,132✔
148
        {
66,040✔
149
            if (!trySupernet)
66,040✔
150
            {
1✔
151
                throw new ArgumentException("Networks must have the same CIDR.", nameof(network1));
1✔
152
            }
153

154
            supernet = null;
66,039✔
155
            return false;
66,039✔
156
        }
157

158
        IPNetwork2 first = (network1.InternalNetwork < network2.InternalNetwork) ? network1 : network2;
66,092✔
159
        IPNetwork2 last = (network1.InternalNetwork > network2.InternalNetwork) ? network1 : network2;
66,092✔
160

161
        // Starting from here :
162
        // network1 and network2 have the same cidr,
163
        // network1 does not contain network2,
164
        // network2 does not contain network1,
165
        // first is the lower subnet
166
        // last is the higher subnet
167
        if ((first.InternalBroadcast + 1) != last.InternalNetwork)
66,092✔
168
        {
12✔
169
            if (!trySupernet)
12✔
170
            {
1✔
171
                throw new ArgumentOutOfRangeException(nameof(network1));
1✔
172
            }
173

174
            supernet = null;
11✔
175
            return false;
11✔
176
        }
177

178
        BigInteger uintSupernet = first.InternalNetwork;
66,080✔
179
        byte cidrSupernet = (byte)(first.cidr - 1);
66,080✔
180

181
        var networkSupernet = new IPNetwork2(uintSupernet, first.family, cidrSupernet);
66,080✔
182
        if (networkSupernet.InternalNetwork != first.InternalNetwork)
66,080✔
183
        {
8✔
184
            if (!trySupernet)
8✔
185
            {
2✔
186
                throw new ArgumentException("Networks are not contiguous.", nameof(network1));
2✔
187
            }
188

189
            supernet = null;
6✔
190
            return false;
6✔
191
        }
192

193
        supernet = networkSupernet;
66,072✔
194
        return true;
66,072✔
195
    }
132,153✔
196
}
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

© 2026 Coveralls, Inc