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

lduchosal / ipnetwork / 808

17 Aug 2025 08:25AM UTC coverage: 93.223% (-1.0%) from 94.226%
808

push

appveyor

web-flow
Chore: cleanup, breaking changes, enum, tryparse, exception, static ListIPAddress (#363)

* Chore: huge cleanup, enum, tryparse, exception, static ListIPAddress, important changes : IPNetwork comparison and sort order have change to reflect expected behavoir
* Fix: obsolete enums
* Fix: network sorting and member comparison
* Chore: upgrade version number 3.3

1802 of 1933 relevant lines covered (93.22%)

726934.37 hits per line

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

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

5
namespace System.Net;
6

7
using System.Collections;
8
using System.Collections.Generic;
9
using System.Numerics;
10

11
/// <summary>
12
/// Represents a collection of IP addresses within a specific IP network.
13
/// </summary>
14
public class IPAddressCollection : IEnumerable<IPAddress>, IEnumerator<IPAddress>
15
{
16
    private readonly IPNetwork2 ipnetwork;
17
    private readonly Filter filter;
18
    private BigInteger enumerator;
19

20
    /// <summary>
21
    /// Initializes a new instance of the <see cref="IPAddressCollection"/> class.
22
    /// </summary>
23
    /// <param name="ipnetwork">The network.</param>
24
    /// <param name="filter">The filter.</param>
25
    internal IPAddressCollection(IPNetwork2 ipnetwork, Filter filter)
33✔
26
    {
33✔
27
        this.ipnetwork = ipnetwork;
33✔
28
        this.filter = filter;
33✔
29
        this.Reset();
33✔
30
    }
33✔
31

32
    /// <summary>
33
    /// Gets the count of IP addresses within the network.
34
    /// </summary>
35
    /// <value>
36
    /// The count of IP addresses within the network.
37
    /// </value>
38
    public BigInteger Count
39
    {
40
        get
41
        {
293✔
42
            BigInteger count = this.ipnetwork.Total;
293✔
43
            if (this.filter == Filter.Usable)
293✔
44
            {
34✔
45
                count -= 2;
34✔
46
            }
34✔
47

48
            if (count < 0)
293✔
49
            {
1✔
50
                count = 0;
1✔
51
            }
1✔
52

53
            return count;
293✔
54
        }
293✔
55
    }
56

57
    /// <summary>
58
    /// Gets the IP address corresponding to the given index from the IPNetwork collection.
59
    /// </summary>
60
    /// <param name="i">The index of the IP address to retrieve.</param>
61
    /// <returns>The IP address corresponding to the given index.</returns>
62
    /// <exception cref="ArgumentOutOfRangeException">Thrown when the given index is greater than or equal to the Count property of the IPNetwork collection.</exception>
63
    public IPAddress this[BigInteger i]
64
    {
65
        get
66
        {
125✔
67
            if (i >= this.Count)
125✔
68
            {
6✔
69
                throw new ArgumentOutOfRangeException(nameof(i));
6✔
70
            }
71

72
            byte width = this.ipnetwork.AddressFamily == Sockets.AddressFamily.InterNetwork ? (byte)32 : (byte)128;
119✔
73
            IPNetworkCollection ipn = this.ipnetwork.Subnet(width);
119✔
74

75
            BigInteger index = i;
119✔
76
            if (this.filter == Filter.Usable)
119✔
77
            {
18✔
78
                index++;
18✔
79
            }
18✔
80

81
            return ipn[index].Network;
119✔
82
        }
119✔
83
    }
84

85
    /// <summary>
86
    /// Gets the current <see cref="IPAddress"/> from the collection.
87
    /// </summary>
88
    /// <returns>The current <see cref="IPAddress"/>.</returns>
89
    public IPAddress Current
90
    {
91
        get
92
        {
65✔
93
            return this[this.enumerator];
65✔
94
        }
61✔
95
    }
96

97
    /// <inheritdoc/>
98
    object IEnumerator.Current
99
    {
100
        get
101
        {
15✔
102
            return this.Current;
15✔
103
        }
13✔
104
    }
105

106
    /// <inheritdoc />
107
    public bool MoveNext()
108
    {
104✔
109
        this.enumerator++;
104✔
110
        if (this.enumerator >= this.Count)
104✔
111
        {
20✔
112
            return false;
20✔
113
        }
114

115
        return true;
84✔
116
    }
104✔
117

118
    /// <inheritdoc />
119
    public void Reset()
120
    {
44✔
121
        this.enumerator = -1;
44✔
122
    }
44✔
123

124
    /// <inheritdoc />
125
    public void Dispose()
126
    {
26✔
127
        Dispose(true);
26✔
128
        GC.SuppressFinalize(this);
26✔
129
    }
26✔
130

131
    /// <summary>
132
    /// Dispose instance
133
    /// </summary>
134
    /// <param name="disposing"></param>
135
    protected virtual void Dispose(bool disposing)
136
    {
26✔
137
        // Cleanup
138
    }
26✔
139

140
    /// <inheritdoc/>
141
    IEnumerator<IPAddress> IEnumerable<IPAddress>.GetEnumerator()
142
    {
7✔
143
        return new Enumerator(this);
7✔
144
    }
7✔
145

146
    /// <inheritdoc/>
147
    IEnumerator IEnumerable.GetEnumerator()
148
    {
1✔
149
        return new Enumerator(this);
1✔
150
    }
1✔
151

152
    private struct Enumerator : IEnumerator<IPAddress>
153
    {
154
        private readonly IPAddressCollection collection;
155
        private BigInteger enumerator;
156

157
        object IEnumerator.Current
158
        {
159
            get
160
            {
×
161
                return this.Current;
×
162
            }
×
163
        }
164

165
        public IPAddress Current
166
        {
167
            get
168
            {
22✔
169
                return this.collection[this.enumerator];
22✔
170
            }
22✔
171
        }
172

173
        public void Dispose()
174
        {
7✔
175
            // nothing to dispose
176
        }
7✔
177

178
        public bool MoveNext()
179
        {
59✔
180
            this.enumerator++;
59✔
181
            if (this.enumerator >= this.collection.Count)
59✔
182
            {
7✔
183
                return false;
7✔
184
            }
185

186
            return true;
52✔
187
        }
59✔
188

189
        public void Reset()
190
        {
×
191
            this.enumerator = -1;
×
192
        }
×
193

194
        public Enumerator(IPAddressCollection collection)
195
        {
8✔
196
            this.collection = collection;
8✔
197
            this.enumerator = -1;
8✔
198
        }
8✔
199
    }
200
}
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