< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.ServiceModelDictionary
Assembly: CoreWCF.Http
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Http/src/CoreWCF/ServiceModelDictionary.cs
Line coverage
9%
Covered lines: 5
Uncovered lines: 49
Coverable lines: 54
Total lines: 133
Line coverage: 9.2%
Branch coverage
0%
Covered branches: 0
Total branches: 34
Branch coverage: 0%
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%110%
TryLookup(...)0%880%
TryLookup(...)0%14140%
TryLookup(...)0%12120%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Http/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;
 5using System.Collections.Generic;
 6using System.Xml;
 7
 8namespace CoreWCF
 9{
 10    internal class ServiceModelDictionary : IXmlDictionary
 11    {
 112        public static readonly ServiceModelDictionary Version1 = new ServiceModelDictionary(new ServiceModelStringsVersi
 13        private readonly ServiceModelStrings _strings;
 14        private readonly int _count;
 15        private XmlDictionaryString[] _dictionaryStrings1;
 16        private XmlDictionaryString[] _dictionaryStrings2;
 17        private Dictionary<string, int> _dictionary;
 18        private XmlDictionaryString[] _versionedDictionaryStrings;
 19
 120        public ServiceModelDictionary(ServiceModelStrings strings)
 21        {
 122            _strings = strings;
 123            _count = strings.Count;
 124        }
 25
 026        public static ServiceModelDictionary CurrentVersion => Version1;
 27
 028        public XmlDictionaryString CreateString(string value, int key) => new XmlDictionaryString(this, value, key);
 29
 30        public bool TryLookup(string key, out XmlDictionaryString value)
 31        {
 032            if (key == null)
 33            {
 034                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(key)));
 35            }
 36
 037            if (_dictionary == null)
 38            {
 039                Dictionary<string, int> dictionary = new Dictionary<string, int>(_count);
 040                for (int i = 0; i < _count; i++)
 41                {
 042                    dictionary.Add(_strings[i], i);
 43                }
 44
 045                _dictionary = dictionary;
 46            }
 047            if (_dictionary.TryGetValue(key, out int id))
 48            {
 049                return TryLookup(id, out value);
 50            }
 51
 052            value = null;
 053            return false;
 54        }
 55
 56        public bool TryLookup(int key, out XmlDictionaryString value)
 57        {
 58            const int keyThreshold = 32;
 059            if (key < 0 || key >= _count)
 60            {
 061                value = null;
 062                return false;
 63            }
 64            XmlDictionaryString s;
 065            if (key < keyThreshold)
 66            {
 067                if (_dictionaryStrings1 == null)
 68                {
 069                    _dictionaryStrings1 = new XmlDictionaryString[keyThreshold];
 70                }
 71
 072                s = _dictionaryStrings1[key];
 073                if (s == null)
 74                {
 075                    s = CreateString(_strings[key], key);
 076                    _dictionaryStrings1[key] = s;
 77                }
 78            }
 79            else
 80            {
 081                if (_dictionaryStrings2 == null)
 82                {
 083                    _dictionaryStrings2 = new XmlDictionaryString[_count - keyThreshold];
 84                }
 85
 086                s = _dictionaryStrings2[key - keyThreshold];
 087                if (s == null)
 88                {
 089                    s = CreateString(_strings[key], key);
 090                    _dictionaryStrings2[key - keyThreshold] = s;
 91                }
 92            }
 093            value = s;
 094            return true;
 95        }
 96
 97        public bool TryLookup(XmlDictionaryString key, out XmlDictionaryString value)
 98        {
 099            if (key == null)
 100            {
 0101                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(key)));
 102            }
 103
 0104            if (key.Dictionary == this)
 105            {
 0106                value = key;
 0107                return true;
 108            }
 0109            if (key.Dictionary == CurrentVersion)
 110            {
 0111                if (_versionedDictionaryStrings == null)
 112                {
 0113                    _versionedDictionaryStrings = new XmlDictionaryString[CurrentVersion._count];
 114                }
 115
 0116                XmlDictionaryString s = _versionedDictionaryStrings[key.Key];
 0117                if (s == null)
 118                {
 0119                    if (!TryLookup(key.Value, out s))
 120                    {
 0121                        value = null;
 0122                        return false;
 123                    }
 0124                    _versionedDictionaryStrings[key.Key] = s;
 125                }
 0126                value = s;
 0127                return true;
 128            }
 0129            value = null;
 0130            return false;
 131        }
 132    }
 133}