< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.ServiceModelDictionary
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/ServiceModelDictionary.cs
Line coverage
51%
Covered lines: 28
Uncovered lines: 26
Coverable lines: 54
Total lines: 141
Line coverage: 51.8%
Branch coverage
44%
Covered branches: 15
Total branches: 34
Branch coverage: 44.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%11100%
.ctor(...)100%11100%
CreateString(...)100%11100%
TryLookup(...)0%880%
TryLookup(...)85.71%141488.88%
TryLookup(...)25%121227.77%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/ServiceModelDictionary.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.Collections.Generic;
 5using System.Xml;
 6
 7namespace CoreWCF
 8{
 9    internal class ServiceModelDictionary : IXmlDictionary
 10    {
 1311        public static readonly ServiceModelDictionary Version1 = new ServiceModelDictionary(new ServiceModelStringsVersi
 12        private readonly ServiceModelStrings _strings;
 13        private readonly int _count;
 14        private XmlDictionaryString[] _dictionaryStrings1;
 15        private XmlDictionaryString[] _dictionaryStrings2;
 16        private Dictionary<string, int> _dictionary;
 17        private XmlDictionaryString[] _versionedDictionaryStrings;
 18
 1319        public ServiceModelDictionary(ServiceModelStrings strings)
 20        {
 1321            _strings = strings;
 1322            _count = strings.Count;
 1323        }
 24
 25        public static ServiceModelDictionary CurrentVersion
 26        {
 27            get
 28            {
 5745129                return Version1;
 30            }
 31        }
 32
 33        public XmlDictionaryString CreateString(string value, int key)
 34        {
 3577435            return new XmlDictionaryString(this, value, key);
 36        }
 37
 38        public bool TryLookup(string key, out XmlDictionaryString value)
 39        {
 040            if (key == null)
 41            {
 042                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(key));
 43            }
 44
 045            if (_dictionary == null)
 46            {
 047                Dictionary<string, int> dictionary = new Dictionary<string, int>(_count);
 048                for (int i = 0; i < _count; i++)
 49                {
 050                    dictionary.Add(_strings[i], i);
 51                }
 52
 053                _dictionary = dictionary;
 54            }
 055            if (_dictionary.TryGetValue(key, out int id))
 56            {
 057                return TryLookup(id, out value);
 58            }
 59
 060            value = null;
 061            return false;
 62        }
 63
 64        public bool TryLookup(int key, out XmlDictionaryString value)
 65        {
 66            const int keyThreshold = 32;
 1011767            if (key < 0 || key >= _count)
 68            {
 069                value = null;
 070                return false;
 71            }
 72            XmlDictionaryString s;
 1011773            if (key < keyThreshold)
 74            {
 865475                if (_dictionaryStrings1 == null)
 76                {
 577                    _dictionaryStrings1 = new XmlDictionaryString[keyThreshold];
 78                }
 79
 865480                s = _dictionaryStrings1[key];
 865481                if (s == null)
 82                {
 5483                    s = CreateString(_strings[key], key);
 5484                    _dictionaryStrings1[key] = s;
 85                }
 86            }
 87            else
 88            {
 146389                if (_dictionaryStrings2 == null)
 90                {
 391                    _dictionaryStrings2 = new XmlDictionaryString[_count - keyThreshold];
 92                }
 93
 146394                s = _dictionaryStrings2[key - keyThreshold];
 146395                if (s == null)
 96                {
 6297                    s = CreateString(_strings[key], key);
 6298                    _dictionaryStrings2[key - keyThreshold] = s;
 99                }
 100            }
 10117101            value = s;
 10117102            return true;
 103        }
 104
 105        public bool TryLookup(XmlDictionaryString key, out XmlDictionaryString value)
 106        {
 607107            if (key == null)
 108            {
 0109                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(key));
 110            }
 111
 607112            if (key.Dictionary == this)
 113            {
 0114                value = key;
 0115                return true;
 116            }
 607117            if (key.Dictionary == CurrentVersion)
 118            {
 0119                if (_versionedDictionaryStrings == null)
 120                {
 0121                    _versionedDictionaryStrings = new XmlDictionaryString[CurrentVersion._count];
 122                }
 123
 0124                XmlDictionaryString s = _versionedDictionaryStrings[key.Key];
 0125                if (s == null)
 126                {
 0127                    if (!TryLookup(key.Value, out s))
 128                    {
 0129                        value = null;
 0130                        return false;
 131                    }
 0132                    _versionedDictionaryStrings[key.Key] = s;
 133                }
 0134                value = s;
 0135                return true;
 136            }
 607137            value = null;
 607138            return false;
 139        }
 140    }
 141}