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

lduchosal / ipnetwork / 22803352265

07 Mar 2026 05:07PM UTC coverage: 91.848% (-1.3%) from 93.118%
22803352265

push

travis-pro

web-flow
Merge pull request #383 from lduchosal/feat/v4-nullable

feat/v4-nullable

651 of 721 branches covered (90.29%)

Branch coverage included in aggregate %.

111 of 147 new or added lines in 26 files covered. (75.51%)

5 existing lines in 3 files now uncovered.

1929 of 2088 relevant lines covered (92.39%)

574342.5 hits per line

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

91.96
/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!
NEW
26
        {
×
NEW
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!
NEW
59
        {
×
NEW
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 (network1 == null)
3✔
79
        {
2✔
80
            throw new ArgumentNullException(nameof(network1));
2✔
81
        }
82

83
        if (network2 == null)
1!
NEW
84
        {
×
NEW
85
            throw new ArgumentNullException(nameof(network2));
×
86
        }
87

88
        return InternalSupernet(true, network1, network2, out supernet);
1✔
89
    }
1✔
90

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

112
            supernet = null;
1✔
113
            return false;
1✔
114
        }
115

116
        if (network2 == null)
132,158✔
117
        {
4✔
118
            if (!trySupernet)
4✔
119
            {
2✔
120
                throw new ArgumentNullException(nameof(network2));
2✔
121
            }
122

123
            supernet = null;
2✔
124
            return false;
2✔
125
        }
126

127
        if (network1.Contains(network2))
132,154✔
128
        {
19✔
129
            supernet = new IPNetwork2(network1.InternalNetwork, network1.family, network1.Cidr);
19✔
130
            return true;
19✔
131
        }
132

133
        if (network2.Contains(network1))
132,135✔
134
        {
3✔
135
            supernet = new IPNetwork2(network2.InternalNetwork, network2.family, network2.Cidr);
3✔
136
            return true;
3✔
137
        }
138

139
        if (network1.cidr != network2.cidr)
132,132✔
140
        {
66,040✔
141
            if (!trySupernet)
66,040✔
142
            {
1✔
143
                throw new ArgumentException("Networks must have the same CIDR.", nameof(network1));
1✔
144
            }
145

146
            supernet = null;
66,039✔
147
            return false;
66,039✔
148
        }
149

150
        IPNetwork2 first = (network1.InternalNetwork < network2.InternalNetwork) ? network1 : network2;
66,092✔
151
        IPNetwork2 last = (network1.InternalNetwork > network2.InternalNetwork) ? network1 : network2;
66,092✔
152

153
        // Starting from here :
154
        // network1 and network2 have the same cidr,
155
        // network1 does not contain network2,
156
        // network2 does not contain network1,
157
        // first is the lower subnet
158
        // last is the higher subnet
159
        if ((first.InternalBroadcast + 1) != last.InternalNetwork)
66,092✔
160
        {
12✔
161
            if (!trySupernet)
12✔
162
            {
1✔
163
                throw new ArgumentOutOfRangeException(nameof(network1));
1✔
164
            }
165

166
            supernet = null;
11✔
167
            return false;
11✔
168
        }
169

170
        BigInteger uintSupernet = first.InternalNetwork;
66,080✔
171
        byte cidrSupernet = (byte)(first.cidr - 1);
66,080✔
172

173
        var networkSupernet = new IPNetwork2(uintSupernet, first.family, cidrSupernet);
66,080✔
174
        if (networkSupernet.InternalNetwork != first.InternalNetwork)
66,080✔
175
        {
8✔
176
            if (!trySupernet)
8✔
177
            {
2✔
178
                throw new ArgumentException("Networks are not contiguous.", nameof(network1));
2✔
179
            }
180

181
            supernet = null;
6✔
182
            return false;
6✔
183
        }
184

185
        supernet = networkSupernet;
66,072✔
186
        return true;
66,072✔
187
    }
132,153✔
188
}
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