< Summary

Information
Class: IceRpc.Transports.Internal.FlagEnumExtensions
Assembly: IceRpc
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/Transports/Internal/FlagEnumExtensions.cs
Tag: 1321_24790053727
Line coverage
90%
Covered lines: 20
Uncovered lines: 2
Coverable lines: 22
Total lines: 41
Line coverage: 90.9%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage
100%
Covered methods: 4
Fully covered methods: 3
Total methods: 4
Method coverage: 100%
Full method coverage: 75%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TrySetFlag(...)100%11100%
TrySetFlag(...)100%11100%
HasFlag(...)100%11100%
ClearFlag(...)50%2271.42%

File(s)

/home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/Transports/Internal/FlagEnumExtensions.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using System.Runtime.CompilerServices;
 4
 5namespace IceRpc.Transports.Internal;
 6
 7/// <summary>The flag enum extensions help with atomically set/get enumeration flags on an integer value. The
 8/// enumeration underlying type must be an integer as well.</summary>
 9internal static class FlagEnumExtensions
 10{
 11    internal static bool TrySetFlag<T>(this ref int source, T flag) where T : Enum
 9155212    {
 9155213        int flagValue = Unsafe.As<T, int>(ref flag);
 9155214        int previousValue = Interlocked.Or(ref source, flagValue);
 9155215        return previousValue != (previousValue | flagValue);
 9155216    }
 17
 18    internal static bool TrySetFlag<T>(this ref int source, T flag, out int newValue) where T : Enum
 1303119    {
 1303120        int flagValue = Unsafe.As<T, int>(ref flag);
 1303121        int previousValue = Interlocked.Or(ref source, flagValue);
 1303122        newValue = previousValue | flagValue;
 1303123        return previousValue != newValue;
 1303124    }
 25
 26    internal static bool HasFlag<T>(this ref int source, T flag) where T : Enum
 23241127    {
 23241128        int flagValue = Unsafe.As<T, int>(ref flag);
 23241129        return (Volatile.Read(ref source) & flagValue) == flagValue;
 23241130    }
 31
 32    internal static void ClearFlag<T>(this ref int source, T flag) where T : Enum
 8445133    {
 8445134        int flagValue = Unsafe.As<T, int>(ref flag);
 8445135        int previousValue = Interlocked.And(ref source, ~flagValue);
 8445136        if (previousValue == (previousValue & ~flagValue))
 037        {
 038            throw new InvalidOperationException("The state was already cleared.");
 39        }
 8445140    }
 41}