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

loresoft / FluentCommand / 10752816192

07 Sep 2024 04:09PM UTC coverage: 53.21%. First build
10752816192

push

github

pwelter34
update to roslyn 4.4.0

1675 of 3740 branches covered (44.79%)

Branch coverage included in aggregate %.

1 of 23 new or added lines in 2 files covered. (4.35%)

4292 of 7474 relevant lines covered (57.43%)

222.61 hits per line

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

58.44
/src/FluentCommand/Internal/HashCode.cs
1
namespace FluentCommand.Internal;
2

3
/// <summary>
4
/// An immutable hash code structure
5
/// </summary>
6
/// <remarks>
7
/// Implements the Jon Skeet suggested implementation of GetHashCode().
8
/// </remarks>
9
public readonly struct HashCode : IFormattable, IEquatable<HashCode>
10
{
11
    /// <summary>
12
    /// The prime multiplier used to combine hash codes.
13
    /// </summary>
14
    public const int Multiplier = 31;
15

16
    private readonly int _hashCode;
17

18
    /// <summary>
19
    /// Initializes a new instance of the <see cref="HashCode"/> struct.
20
    /// </summary>
21
    /// <param name="hashCode">The hash code.</param>
22
    public HashCode(int hashCode)
23
    {
24
        _hashCode = hashCode;
99✔
25
    }
99✔
26

27
    /// <summary>
28
    /// Gets a hash code seed value for combine hash codes values.
29
    /// </summary>
30
    /// <value>
31
    /// The hash code seed value.
32
    /// </value>
33
    public static HashCode Seed => new(17);
41✔
34

35
    /// <summary>
36
    /// Combines this hash code with the hash code of specified <paramref name="value" />.
37
    /// </summary>
38
    /// <typeparam name="TValue">The type of the value.</typeparam>
39
    /// <param name="value">The value to combine hash codes with.</param>
40
    /// <returns>A new hash code combined with this and the values hash codes.</returns>
41
    public HashCode Combine<TValue>(TValue value)
42
    {
43
        var hashCode = value is null ? 0 : EqualityComparer<TValue>.Default.GetHashCode(value);
26!
44
        unchecked
45
        {
46
            hashCode = (_hashCode * Multiplier) + hashCode;
26✔
47
        }
48

49
        return new HashCode(hashCode);
26✔
50
    }
51

52
    /// <summary>
53
    /// Combines this hash code with the hash code of specified <paramref name="value" />.
54
    /// </summary>
55
    /// <param name="value">The value to combine hash codes with.</param>
56
    /// <returns>A new hash code combined with this and the values hash codes.</returns>
57
    public HashCode Combine(string value)
58
    {
59
        // need to handle string values deterministically
60
        var hashCode = HashString(value);
31✔
61
        unchecked
62
        {
63
            hashCode = (_hashCode * Multiplier) + hashCode;
31✔
64
        }
65

66
        return new HashCode(hashCode);
31✔
67
    }
68

69
    /// <summary>
70
    /// Combines this hash code with the hash code of specified <paramref name="value" />.
71
    /// </summary>
72
    /// <param name="value">The value to combine hash codes with.</param>
73
    /// <returns>A new hash code combined with this and the values hash codes.</returns>
74
    public HashCode Combine(object value)
75
    {
76
        // need to handle string values deterministically
77
        return value switch
12✔
78
        {
12✔
79
            string text => Combine(text),
5✔
80
            _ => Combine(value?.GetHashCode() ?? 0),
7!
81
        };
12✔
82
    }
83

84
    /// <summary>
85
    /// Combines this hash code with the hash code of each item specified <paramref name="values" />.
86
    /// </summary>
87
    /// <typeparam name="TValue">The type of the value.</typeparam>
88
    /// <param name="values">The values to combine hash codes with.</param>
89
    /// <returns>A new hash code combined with this and the values hash codes.</returns>
90
    public HashCode CombineAll<TValue>(IEnumerable<TValue> values)
91
    {
92
        if (values == null)
1!
93
            return this;
×
94

95
        var comparer = EqualityComparer<TValue>.Default;
1✔
96
        var current = _hashCode;
1✔
97

98
        foreach (var value in values)
6✔
99
        {
100
            var hashCode = value switch
2!
101
            {
2✔
102
                string text => HashString(text),
×
103
                TValue instance => comparer.GetHashCode(instance),
2✔
104
                _ => 0
×
105
            };
2✔
106

107
            unchecked
108
            {
109
                hashCode = (current * Multiplier) + hashCode;
2✔
110
            }
111

112
            current = hashCode;
2✔
113
        }
114

115
        return new HashCode(current);
1✔
116
    }
117

118

119
    /// <summary>
120
    /// Returns a hash code for this instance.
121
    /// </summary>
122
    /// <returns>
123
    /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
124
    /// </returns>
125
    public override int GetHashCode() => _hashCode;
2✔
126

127

128
    /// <summary>
129
    /// Converts the numeric value of this instance to its equivalent string representation.
130
    /// </summary>
131
    /// <returns>
132
    /// The string representation of the value of this instance.
133
    /// </returns>
134
    public override string ToString() => _hashCode.ToString();
×
135

136
    /// <summary>
137
    /// Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.
138
    /// </summary>
139
    /// <param name="provider">An object that supplies culture-specific formatting information.</param>
140
    /// <returns>
141
    /// The string representation of the value of this instance as specified by provider.
142
    /// </returns>
143
    public string ToString(IFormatProvider provider) => _hashCode.ToString(provider);
×
144

145
    /// <summary>
146
    /// Converts the numeric value of this instance to its equivalent string representation using the specified format.
147
    /// </summary>
148
    /// <param name="format">A standard or custom numeric format string.</param>
149
    /// <returns>
150
    /// The string representation of the value of this instance as specified by format.
151
    /// </returns>
152
    public string ToString(string format) => _hashCode.ToString(format);
×
153

154
    /// <summary>
155
    /// Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.
156
    /// </summary>
157
    /// <param name="format">A standard or custom numeric format string.</param>
158
    /// <param name="provider">An object that supplies culture-specific formatting information.</param>
159
    /// <returns>
160
    /// The string representation of the value of this instance as specified by format and provider.
161
    /// </returns>
162
    public string ToString(string format, IFormatProvider provider) => _hashCode.ToString(format, provider);
6✔
163

164

165
    /// <summary>
166
    /// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
167
    /// </summary>
168
    /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
169
    /// <returns>
170
    ///   <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
171
    /// </returns>
172
    public override bool Equals(object obj) => obj is HashCode code && Equals(code);
×
173

174
    /// <summary>
175
    /// Indicates whether the current object is equal to another object of the same type.
176
    /// </summary>
177
    /// <param name="other">An object to compare with this object.</param>
178
    /// <returns>
179
    /// true if the current object is equal to the <paramref name="other">other</paramref> parameter; otherwise, false.
180
    /// </returns>
181
    public bool Equals(HashCode other) => _hashCode == other._hashCode;
×
182

183

184
    /// <summary>
185
    /// Performs an implicit conversion from <see cref="HashCode"/> to <see cref="int"/>.
186
    /// </summary>
187
    /// <param name="hashCode">The hash code.</param>
188
    /// <returns>
189
    /// The result of the conversion.
190
    /// </returns>
191
    public static implicit operator int(HashCode hashCode) => hashCode._hashCode;
33✔
192

193
    /// <summary>
194
    /// Compares two values to determine equality.
195
    /// </summary>
196
    /// <param name="left">The value to compare with right.</param>
197
    /// <param name="right">The value to compare with left.</param>
198
    /// <returns>
199
    /// true if left is equal to right; otherwise, false.
200
    /// </returns>
201
    public static bool operator ==(HashCode left, HashCode right) => left.Equals(right);
×
202

203
    /// <summary>
204
    /// Compares two values to determine inequality.
205
    /// </summary>
206
    /// <param name="left">The value to compare with right.</param>
207
    /// <param name="right">The value to compare with left.</param>
208
    /// <returns>
209
    /// true if left is not equal to right; otherwise, false.
210
    /// </returns>
211
    public static bool operator !=(HashCode left, HashCode right) => !(left == right);
×
212

213

214
    /// <summary>
215
    /// Deterministic string hash function
216
    /// </summary>
217
    /// <param name="text">The text to hash.</param>
218
    /// <returns>A 32-bit signed integer hash code.</returns>
219
    public static int HashString(string text)
220
    {
221
        if (string.IsNullOrEmpty(text))
31!
222
            return 0;
×
223

224
        int hash = Seed;
31✔
225

226
        unchecked
227
        {
228
            // ReSharper disable once LoopCanBeConvertedToQuery
229
            // ReSharper disable once ForCanBeConvertedToForeach
230
            for (var index = 0; index < text.Length; index++)
2,132✔
231
                hash = (hash * Multiplier) + text[index];
1,035✔
232

233
        }
234

235
        return hash;
31✔
236
    }
237
}
238

239

240
public class MyClass
241
{
NEW
242
    public string Name { get; set; }
×
243

NEW
244
    public Type Type { get; set; }
×
245

246
    public override bool Equals(object obj)
247
    {
NEW
248
        return obj is MyClass @class &&
×
NEW
249
               Name == @class.Name &&
×
NEW
250
               EqualityComparer<Type>.Default.Equals(Type, @class.Type);
×
251
    }
252

253
    public override int GetHashCode()
254
    {
NEW
255
        int hashCode = -243844509;
×
NEW
256
        hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Name);
×
NEW
257
        hashCode = hashCode * -1521134295 + EqualityComparer<Type>.Default.GetHashCode(Type);
×
NEW
258
        return hashCode;
×
259
    }
260
}
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