public void TestGenerateAlphabeticReferenceException()
        {
            string reference = Int32Extensions.GenerateAlphabeticReference(-1, true);

            Assert.IsNull(reference,
                          "Not a real assert, just stops the compiler warning about 'reference' being unused.");
        }
        public void TestParseAlphabeticReferenceInvalid()
        {
            int parsed = Int32Extensions.ParseAlphabeticReference("a6c", true);

            Assert.AreEqual(0,
                            parsed,
                            "Not a real assert, just stops the compiler warning about 'parsed' being unused.");
        }
        public void TestParseAlphabeticReferenceNonZeroBased()
        {
            Dictionary <int, string> expectedValues = GetExpectedReferencesNonZeroBased();

            foreach (int integer in expectedValues.Keys)
            {
                Assert.AreEqual(integer,
                                Int32Extensions.ParseAlphabeticReference(expectedValues[integer], false),
                                String.Format("Test parsing of reference for {0}", integer));
            }
        }
        public void TestGenerateAlphabeticReferenceZeroBased()
        {
            Dictionary <int, string> expectedValues = GetExpectedReferencesZeroBased();

            foreach (int integer in expectedValues.Keys)
            {
                Assert.AreEqual(expectedValues[integer],
                                Int32Extensions.GenerateAlphabeticReference(integer, true),
                                String.Format("Test generation of reference for {0}", integer));
            }
        }
Exemple #5
0
        public List <int> getListInt(T i)
        {
            string listString = getFromFileString(i);
            var    result     = new List <int>();

            foreach (string s in listString.Split(LIST_SEPARATOR.ToCharArray()))
            {
                result.Add(Int32Extensions.ParseOrDefault(s, 0));
            }
            return(result);
        }
Exemple #6
0
        /// <summary>
        /// Get index.
        /// </summary>
        /// <param name="bytes">The bytes.</param>
        /// <param name="type">The indexation type.</param>
        /// <returns>The index.</returns>
        private int GetIndex(byte[] bytes, IndexationType type)
        {
            byte prefix         = 0;
            byte firstByteValue = bytes[_currentOffset];

            switch (type)
            {
            case IndexationType.Incremental:
                prefix = (byte)UVarIntPrefix.Incremental;
                break;

            case IndexationType.WithoutIndexation:
                prefix = (byte)UVarIntPrefix.WithoutIndexing;
                break;

            case IndexationType.Indexed:
                prefix = (byte)UVarIntPrefix.Indexed;
                break;

            case IndexationType.EncodingContextUpdate:
                prefix = (byte)UVarIntPrefix.EncodingContextUpdate;
                break;

            case IndexationType.NeverIndexed:
                prefix = (byte)UVarIntPrefix.NeverIndexed;
                break;
            }

            int maxPrefixVal = (1 << prefix) - 1;

            if (firstByteValue < maxPrefixVal)
            {
                _currentOffset++;
                return(firstByteValue);
            }

            int i = 1;

            while (true)
            {
                if ((bytes[_currentOffset + i] & (byte)IndexationType.Indexed) == 0)
                {
                    break;
                }
                i++;
            }

            var numberBytes = new byte[++i];

            Buffer.BlockCopy(bytes, _currentOffset, numberBytes, 0, i);
            _currentOffset += i;

            return(Int32Extensions.FromUVarInt(numberBytes));
        }
Exemple #7
0
        /// <summary>
        /// Decode string.
        /// </summary>
        /// <param name="bytes">The bytes to decode.</param>
        /// <param name="prefix">The prefix.</param>
        /// <returns>The decoded string.</returns>
        private string DecodeString(byte[] bytes, byte prefix)
        {
            int maxPrefixVal = (1 << prefix) - 1;

            // Get first bit. If true => huffman used
            bool isHuffman = (bytes[_currentOffset] & 0x80) != 0;

            int len;

            // throw away huffman's mask
            bytes[_currentOffset] &= 0x7f;

            if ((bytes[_currentOffset]) < maxPrefixVal)
            {
                len = bytes[_currentOffset++];
            }
            else
            {
                int i = 1;
                while (true)
                {
                    if ((bytes[_currentOffset + i] & 0x80) == 0)
                    {
                        break;
                    }
                    i++;
                }

                var numberBytes = new byte[++i];
                Buffer.BlockCopy(bytes, _currentOffset, numberBytes, 0, i);
                _currentOffset += i;

                len = Int32Extensions.FromUVarInt(numberBytes);
            }

            string result = String.Empty;

            if (isHuffman)
            {
                var compressedBytes = new byte[len];
                Buffer.BlockCopy(bytes, _currentOffset, compressedBytes, 0, len);
                var decodedBytes = _huffmanProc.Decompress(compressedBytes);
                result = Encoding.UTF8.GetString(decodedBytes);

                _currentOffset += len;

                return(result);
            }

            result          = Encoding.UTF8.GetString(bytes, _currentOffset, len);
            _currentOffset += len;

            return(result);
        }
Exemple #8
0
        public void UVarIntConversion()
        {
            var test1337 = 1337.ToUVarInt(5);

            Assert.Equal(Int32Extensions.FromUVarInt(test1337), 1337);
            test1337 = 1337.ToUVarInt(3);
            Assert.Equal(Int32Extensions.FromUVarInt(test1337), 1337);
            test1337 = 1337.ToUVarInt(0);
            Assert.Equal(Int32Extensions.FromUVarInt(test1337), 1337);

            var test0 = 0.ToUVarInt(5);

            Assert.Equal(Int32Extensions.FromUVarInt(test0), 0);
            test0 = 0.ToUVarInt(0);
            Assert.Equal(Int32Extensions.FromUVarInt(test0), 0);

            var test0Xfffff = 0xfffff.ToUVarInt(7);

            Assert.Equal(Int32Extensions.FromUVarInt(test0Xfffff), 0xfffff);
            test0Xfffff = 0xfffff.ToUVarInt(4);
            Assert.Equal(Int32Extensions.FromUVarInt(test0Xfffff), 0xfffff);
            test0Xfffff = 0xfffff.ToUVarInt(0);
            Assert.Equal(Int32Extensions.FromUVarInt(test0Xfffff), 0xfffff);
        }
 internal static int WrappedDelta(this ushort a, ushort b)
 {
     return(Int32Extensions.WrappedDelta(a, b, ushort.MaxValue));
 }
Exemple #10
0
        public void Given_ValidValue_ThrowIfLessThanOrEqualTo_ShouldReturn_Result(int value, int comparer, int expected)
        {
            var result = Int32Extensions.ThrowIfLessThanOrEqualTo(value, comparer);

            result.Should().Be(expected);
        }
Exemple #11
0
        public void Given_InvalidValue_ThrowIfLessThanOrEqualTo_ShouldThrow_Exception(int value, int comparer)
        {
            Action action = () => { var result = Int32Extensions.ThrowIfLessThanOrEqualTo(value, comparer); };

            action.ShouldThrow <ArgumentOutOfRangeException>();
        }
Exemple #12
0
        public void Given_Values_IsLessThanOrEqualTo_ShouldReturn_Result(int value, int comparer, bool expected)
        {
            var result = Int32Extensions.IsLessThanOrEqualTo(value, comparer);

            result.Should().Be(expected);
        }