< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.MostlySingletonList<T>
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/MostlySingletonList.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 46
Coverable lines: 46
Total lines: 133
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 26
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Add(...)0%440%
Compare(...)0%220%
Contains(...)100%110%
EnsureValidSingletonIndex(...)0%440%
MatchesSingleton(...)0%220%
IndexOf(...)0%440%
Remove(...)0%660%
RemoveAt(...)0%220%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/MostlySingletonList.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Collections.Generic;
 6
 7namespace CoreWCF.IdentityModel
 8{
 9    internal struct MostlySingletonList<T> where T : class
 10    {
 11        private T _singleton;
 12        private List<T> _list;
 13
 14        public T this[int index]
 15        {
 16            get
 17            {
 018                if (_list == null)
 19                {
 020                    EnsureValidSingletonIndex(index);
 021                    return _singleton;
 22                }
 23                else
 24                {
 025                    return _list[index];
 26                }
 27            }
 28        }
 29
 030        public int Count { get; private set; }
 31
 32        public void Add(T item)
 33        {
 034            if (_list == null)
 35            {
 036                if (Count == 0)
 37                {
 038                    _singleton = item;
 039                    Count = 1;
 040                    return;
 41                }
 042                _list = new List<T>
 043                {
 044                    _singleton
 045                };
 046                _singleton = null;
 47            }
 048            _list.Add(item);
 049            Count++;
 050        }
 51
 52        private static bool Compare(T x, T y)
 53        {
 054            return x == null ? y == null : x.Equals(y);
 55        }
 56
 57        public bool Contains(T item)
 58        {
 059            return IndexOf(item) >= 0;
 60        }
 61
 62        private void EnsureValidSingletonIndex(int index)
 63        {
 064            if (Count != 1)
 65            {
 066                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.Fo
 67            }
 68
 069            if (index != 0)
 70            {
 071                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(index),
 72            }
 073        }
 74
 75        private bool MatchesSingleton(T item)
 76        {
 077            return Count == 1 && Compare(_singleton, item);
 78        }
 79
 80        public int IndexOf(T item)
 81        {
 082            if (_list == null)
 83            {
 084                return MatchesSingleton(item) ? 0 : -1;
 85            }
 86            else
 87            {
 088                return _list.IndexOf(item);
 89            }
 90        }
 91
 92        public bool Remove(T item)
 93        {
 094            if (_list == null)
 95            {
 096                if (MatchesSingleton(item))
 97                {
 098                    _singleton = null;
 099                    Count = 0;
 0100                    return true;
 101                }
 102                else
 103                {
 0104                    return false;
 105                }
 106            }
 107            else
 108            {
 0109                bool result = _list.Remove(item);
 0110                if (result)
 111                {
 0112                    Count--;
 113                }
 0114                return result;
 115            }
 116        }
 117
 118        public void RemoveAt(int index)
 119        {
 0120            if (_list == null)
 121            {
 0122                EnsureValidSingletonIndex(index);
 0123                _singleton = null;
 0124                Count = 0;
 125            }
 126            else
 127            {
 0128                _list.RemoveAt(index);
 0129                Count--;
 130            }
 0131        }
 132    }
 133}