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

lduchosal / ipnetwork / 752

05 Apr 2025 05:56AM UTC coverage: 93.417% (-0.2%) from 93.641%
752

push

appveyor

web-flow
Update README.md

1561 of 1671 relevant lines covered (93.42%)

825349.07 hits per line

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

92.21
/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 different filters for a collection of items.
13
/// </summary>
14
public enum FilterEnum
15
{
16
    /// <summary>
17
    /// Every IPAdresses are returned
18
    /// </summary>
19
    All,
20

21
    /// <summary>
22
    /// Returns only usable IPAdresses
23
    /// </summary>
24
    Usable,
25
}
26

27
/// <summary>
28
/// Represents a collection of IP addresses within a specific IP network.
29
/// </summary>
30
public class IPAddressCollection : IEnumerable<IPAddress>, IEnumerator<IPAddress>
31
{
32
    private readonly IPNetwork2 ipnetwork;
33
    private readonly FilterEnum filter;
34
    private BigInteger enumerator;
35

36
    /// <summary>
37
    /// Initializes a new instance of the <see cref="IPAddressCollection"/> class.
38
    /// </summary>
39
    /// <param name="ipnetwork">The network.</param>
40
    /// <param name="filter">The filter.</param>
41
    internal IPAddressCollection(IPNetwork2 ipnetwork, FilterEnum filter)
32✔
42
    {
32✔
43
        this.ipnetwork = ipnetwork;
32✔
44
        this.filter = filter;
32✔
45
        this.Reset();
32✔
46
    }
32✔
47

48
    /// <summary>
49
    /// Gets the count of IP addresses within the network.
50
    /// </summary>
51
    /// <value>
52
    /// The count of IP addresses within the network.
53
    /// </value>
54
    public BigInteger Count
55
    {
56
        get
57
        {
266✔
58
            BigInteger count = this.ipnetwork.Total;
266✔
59
            if (this.filter == FilterEnum.Usable)
266✔
60
            {
27✔
61
                count -= 2;
27✔
62
            }
27✔
63

64
            if (count < 0)
266✔
65
            {
1✔
66
                count = 0;
1✔
67
            }
1✔
68

69
            return count;
266✔
70
        }
266✔
71
    }
72

73
    /// <summary>
74
    /// Gets the IP address corresponding to the given index from the IPNetwork collection.
75
    /// </summary>
76
    /// <param name="i">The index of the IP address to retrieve.</param>
77
    /// <returns>The IP address corresponding to the given index.</returns>
78
    /// <exception cref="ArgumentOutOfRangeException">Thrown when the given index is greater than or equal to the Count property of the IPNetwork collection.</exception>
79
    public IPAddress this[BigInteger i]
80
    {
81
        get
82
        {
132✔
83
            if (i >= this.Count)
132✔
84
            {
6✔
85
                throw new ArgumentOutOfRangeException(nameof(i));
6✔
86
            }
87

88
            byte width = this.ipnetwork.AddressFamily == Sockets.AddressFamily.InterNetwork ? (byte)32 : (byte)128;
126✔
89
            IPNetworkCollection ipn = this.ipnetwork.Subnet(width);
126✔
90

91
            BigInteger index = i;
126✔
92
            if (this.filter == FilterEnum.Usable)
126✔
93
            {
18✔
94
                index++;
18✔
95
            }
18✔
96

97
            return ipn[index].Network;
126✔
98
        }
126✔
99
    }
100

101
    /// <summary>
102
    /// Gets the current <see cref="IPAddress"/> from the collection.
103
    /// </summary>
104
    /// <returns>The current <see cref="IPAddress"/>.</returns>
105
    public IPAddress Current
106
    {
107
        get
108
        {
64✔
109
            return this[this.enumerator];
64✔
110
        }
60✔
111
    }
112

113
    /// <inheritdoc/>
114
    object IEnumerator.Current
115
    {
116
        get
117
        {
14✔
118
            return this.Current;
14✔
119
        }
12✔
120
    }
121

122
    /// <inheritdoc />
123
    public bool MoveNext()
124
    {
104✔
125
        this.enumerator++;
104✔
126
        if (this.enumerator >= this.Count)
104✔
127
        {
20✔
128
            return false;
20✔
129
        }
130

131
        return true;
84✔
132
    }
104✔
133

134
    /// <inheritdoc />
135
    public void Reset()
136
    {
42✔
137
        this.enumerator = -1;
42✔
138
    }
42✔
139

140
    /// <inheritdoc />
141
    public void Dispose()
142
    {
25✔
143
        // nothing to dispose
144
    }
25✔
145

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

152
    /// <inheritdoc/>
153
    IEnumerator IEnumerable.GetEnumerator()
154
    {
1✔
155
        return new Enumerator(this);
1✔
156
    }
1✔
157

158
    private struct Enumerator : IEnumerator<IPAddress>
159
    {
160
        private readonly IPAddressCollection collection;
161
        private BigInteger enumerator;
162

163
        object IEnumerator.Current
164
        {
165
            get
166
            {
×
167
                return this.Current;
×
168
            }
×
169
        }
170

171
        public IPAddress Current
172
        {
173
            get
174
            {
22✔
175
                return this.collection[this.enumerator];
22✔
176
            }
22✔
177
        }
178

179
        public void Dispose()
180
        {
3✔
181
            // nothing to dispose
182
        }
3✔
183

184
        public bool MoveNext()
185
        {
25✔
186
            this.enumerator++;
25✔
187
            if (this.enumerator >= this.collection.Count)
25✔
188
            {
3✔
189
                return false;
3✔
190
            }
191

192
            return true;
22✔
193
        }
25✔
194

195
        public void Reset()
196
        {
×
197
            this.enumerator = -1;
×
198
        }
×
199

200
        public Enumerator(IPAddressCollection collection)
201
        {
4✔
202
            this.collection = collection;
4✔
203
            this.enumerator = -1;
4✔
204
        }
4✔
205
    }
206
}
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