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

lduchosal / ipnetwork / 22802766965

07 Mar 2026 04:31PM UTC coverage: 91.853% (-1.6%) from 93.419%
22802766965

Pull #383

travis-pro

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

651 of 721 branches covered (90.29%)

Branch coverage included in aggregate %.

108 of 144 new or added lines in 24 files covered. (75.0%)

1931 of 2090 relevant lines covered (92.39%)

573792.98 hits per line

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

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

5
namespace System.Net;
6

7
using System.Collections.Generic;
8
using System.Diagnostics.CodeAnalysis;
9

10
/// <summary>
11
/// SupernetArray.
12
/// </summary>
13
public sealed partial class IPNetwork2
14
{
15
    /// <summary>
16
    /// Supernet a list of subnet
17
    /// 192.168.0.0/24 + 192.168.1.0/24 = 192.168.0.0/23
18
    /// 192.168.0.0/24 + 192.168.1.0/24 + 192.168.2.0/24 + 192.168.3.0/24 = 192.168.0.0/22.
19
    /// </summary>
20
    /// <param name="ipnetworks">A list of IPNetwork to merge into common supernets.</param>
21
    /// <returns>The result of IPNetwork if merges succeed, the first ipnetwork otherwise.</returns>
22
    public static IPNetwork2[] Supernet(IPNetwork2[] ipnetworks)
23
    {
16 ✔
24
        if (!InternalSupernet(false, ipnetworks, out IPNetwork2[]? supernet))
16 !
NEW
25
        {
×
NEW
26
            throw new ArgumentException("Failed to supernet IP networks.", nameof(ipnetworks));
×
27
        }
28

29
        return supernet;
15 ✔
30
    }
15 ✔
31

32
    /// <summary>
33
    /// Supernet a list of subnet
34
    /// 192.168.0.0/24 + 192.168.1.0/24 = 192.168.0.0/23
35
    /// 192.168.0.0/24 + 192.168.1.0/24 + 192.168.2.0/24 + 192.168.3.0/24 = 192.168.0.0/22.
36
    /// </summary>
37
    /// <param name="ipnetworks">A list of IPNetwork to merge into common supernets.</param>
38
    /// <param name="supernet">The result of IPNetwork merges.</param>
39
    /// <returns>true if ipnetworks was supernetted successfully; otherwise, false.</returns>
40
    public static bool TrySupernet(IPNetwork2[] ipnetworks, [NotNullWhen(true)] out IPNetwork2[]? supernet)
41
    {
7 ✔
42
        bool supernetted = InternalSupernet(true, ipnetworks, out supernet);
7 ✔
43
        return supernetted;
7 ✔
44
    }
7 ✔
45

46
    /// <summary>
47
    /// Attempts to merge an array of adjacent IP networks with equal CIDR values into the smallest possible set of supernets.
48
    /// </summary>
49
    /// <param name="trySupernet">If true, suppresses exceptions on failure; otherwise, throws.</param>
50
    /// <param name="ipnetworks">The array of IP networks to attempt to merge.</param>
51
    /// <param name="supernet">The resulting array of merged supernets if successful; otherwise, the original input.</param>
52
    /// <returns><c>true</c> if supernetting was successful; otherwise, <c>false</c>.</returns>
53
    internal static bool InternalSupernet(bool trySupernet, IPNetwork2[] ipnetworks, [NotNullWhen(true)] out IPNetwork2[]? supernet)
54
    {
23 ✔
55
        // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
56
        if (ipnetworks == null)
23 ✔
57
        {
3 ✔
58
            if (!trySupernet)
3 ✔
59
            {
1 ✔
60
                throw new ArgumentNullException(nameof(ipnetworks));
1 ✔
61
            }
62

63
            supernet = null;
2 ✔
64
            return false;
2 ✔
65
        }
66

67
        if (ipnetworks.Length <= 0)
20 ✔
68
        {
1 ✔
69
            supernet = [];
1 ✔
70
            return true;
1 ✔
71
        }
72

73
        var supernetted = new List<IPNetwork2>();
19 ✔
74
        List<IPNetwork2> ipns = Array2List(ipnetworks);
19 ✔
75
        Stack<IPNetwork2> current = List2Stack(ipns);
19 ✔
76
        int previousCount = 0;
19 ✔
77
        int currentCount = current.Count;
19 ✔
78

79
        while (previousCount != currentCount)
85 ✔
80
        {
66 ✔
81
            supernetted.Clear();
66 ✔
82
            while (current.Count > 1)
132,183 ✔
83
            {
132,117 ✔
84
                IPNetwork2 ipn1 = current.Pop();
132,117 ✔
85
                IPNetwork2 ipn2 = current.Peek();
132,117 ✔
86

87
                if (ipn1.TrySupernet(ipn2, out IPNetwork2? outNetwork))
132,117 ✔
88
                {
66,072 ✔
89
                    current.Pop();
66,072 ✔
90
                    current.Push(outNetwork);
66,072 ✔
91
                }
66,072 ✔
92
                else
93
                {
66,045 ✔
94
                    supernetted.Add(ipn1);
66,045 ✔
95
                }
66,045 ✔
96
            }
132,117 ✔
97

98
            if (current.Count == 1)
66 ✔
99
            {
66 ✔
100
                supernetted.Add(current.Pop());
66 ✔
101
            }
66 ✔
102

103
            previousCount = currentCount;
66 ✔
104
            currentCount = supernetted.Count;
66 ✔
105
            current = List2Stack(supernetted);
66 ✔
106
        }
66 ✔
107

108
        supernet = supernetted.ToArray();
19 ✔
109
        return true;
19 ✔
110
    }
22 ✔
111

112
    private static Stack<IPNetwork2> List2Stack(List<IPNetwork2> list)
113
    {
85 ✔
114
        var stack = new Stack<IPNetwork2>();
85 ✔
115
        list.ForEach(delegate(IPNetwork2 ipn)
85 ✔
116
        {
132,213 ✔
117
            stack.Push(ipn);
132,213 ✔
118
        });
132,298 ✔
119
        return stack;
85 ✔
120
    }
85 ✔
121

122
    private static List<IPNetwork2> Array2List(IPNetwork2[] array)
123
    {
19 ✔
124
        var ipns = new List<IPNetwork2>();
19 ✔
125
        ipns.AddRange(array);
19 ✔
126
        RemoveNull(ipns);
19 ✔
127
        ipns.Sort(delegate(IPNetwork2 ipn1, IPNetwork2 ipn2)
19 ✔
128
        {
850,490 ✔
129
            int networkCompare = ipn1.InternalNetwork.CompareTo(ipn2.InternalNetwork);
850,490 ✔
130
            if (networkCompare == 0)
850,490 ✔
131
            {
1 ✔
132
                int cidrCompare = ipn1.cidr.CompareTo(ipn2.cidr);
1 ✔
133
                return cidrCompare;
1 ✔
134
            }
19 ✔
135

19 ✔
136
            return networkCompare;
850,489 ✔
137
        });
850,509 ✔
138
        ipns.Reverse();
19 ✔
139

140
        return ipns;
19 ✔
141
    }
19 ✔
142

143
    private static void RemoveNull(List<IPNetwork2> ipns)
144
    {
19 ✔
145
        ipns.RemoveAll(delegate(IPNetwork2 ipn)
19 ✔
146
        {
66,106 ✔
147
            if (ipn == null)
66,106 ✔
148
            {
4 ✔
149
                return true;
4 ✔
150
            }
19 ✔
151

19 ✔
152
            return false;
66,102 ✔
153
        });
66,125 ✔
154
    }
19 ✔
155
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc