public void ConvertStringCustomVsNativeLoadTest()
        {
            // Initialize Data
            var testSamples = new Value128[loadTestsIterationsCount];

            for (var index = 0; index < loadTestsIterationsCount; index++)
            {
                testSamples[index] = (Value128)Guid.NewGuid();
            }

            // Encode
            var customIntermediateResults = new String[loadTestsIterationsCount];

            var nativeIntermediateResults = new String[loadTestsIterationsCount];

            var testResults1 = LoadTest.ExecuteCompare
                               (
                "Value128Converter.TryToBase64String",
                index =>
            {
                var inputValue = testSamples[(Int32)index];

                customIntermediateResults[index] = Value128Converter.TryToBase64String(inputValue, Base64Encoding.Mime).Result;
            },
                "Convert.ToBase64String",
                index =>
            {
                var inputValue = testSamples[(Int32)index];

                nativeIntermediateResults[index] = Convert.ToBase64String((Byte[])inputValue);
            },
                loadTestsIterationsCount
                               );

            Trace.TraceInformation(testResults1.ToString());

            // Decode
            var customActualResults = new Value128[loadTestsIterationsCount];

            var decodedGuids = new Value128[loadTestsIterationsCount];

            var testResults2 = LoadTest.ExecuteCompare
                               (
                "Value128Converter.TryFromBase64String",
                index =>
            {
                var inputValue = customIntermediateResults[index];

                customActualResults[index] = Value128Converter.TryFromBase64String(inputValue, 0, Base64Encoding.Mime).Result;
            },
                "Convert.FromBase64String",
                index =>
            {
                var inputValue = nativeIntermediateResults[index];

                decodedGuids[index] = Value128Converter.TryFromByteArray(Convert.FromBase64String(inputValue), 0).Result;
            }, loadTestsIterationsCount);

            Trace.TraceInformation(testResults2.ToString());
        }
Ejemplo n.º 2
0
        public static IPAddress ToIPAddress(Value128 value)
        {
            // Get value as byte array
            var address = ToByteArray(value);

            // Initialize a new instance of IPAddress
            return(new IPAddress(address));
        }
Ejemplo n.º 3
0
        public static TryResult <Value128> TryFromBase64String(String data, Int32 offset, Base64Encoding base64Encoding)
        {
            // Check input values
            if ((data == null) || (offset > data.Length - base64EncodedSymbolsCount) || (base64Encoding == null))
            {
                return(convertFailResult);
            }

            // Get look-up table
            var lookupTable = base64Encoding.LookupTable;

            var lastIndex = offset + 10;

            var symbol10 = (UInt64)lookupTable[data[lastIndex] & 0x7F];

            var symbol21 = (UInt64)lookupTable[data[offset + 21] & 0x7F];

            // Check symbol
            if ((symbol10 | symbol21) == 0xFF)
            {
                return(convertFailResult);
            }

            // Calculate higher half
            var higherHalf = symbol10 >> 2;

            // Calculate lower half
            var lowerHalf = symbol10 << 62 | symbol21 >> 4;

            // Decode symbols
            for (Int32 indexH = offset, indexL = offset + 11, shiftH = 58, shiftL = 56; indexH < lastIndex; indexH++, indexL++, shiftH -= 6, shiftL -= 6)
            {
                // Get symbols
                var symbolH = data[indexH] & 0x7F;

                var symbolL = data[indexL] & 0x7F;

                // Get symbols values
                var symbolHValue = (UInt64)lookupTable[symbolH];

                var symbolLValue = (UInt64)lookupTable[symbolL];

                // Check symbol
                if ((symbolHValue | symbolLValue) == 0xFF)
                {
                    return(convertFailResult);
                }

                higherHalf |= symbolHValue << shiftH;

                lowerHalf |= symbolLValue << shiftL;
            }

            // Initialize a new instance
            var result = new Value128(higherHalf, lowerHalf);

            return(TryResult <Value128> .CreateSuccess(result));
        }
Ejemplo n.º 4
0
        public static Byte[] ToByteArray(Value128 value)
        {
            var result = new Byte[16];

            Convert(value.HigherHalf, 0, result);

            Convert(value.LowerHalf, 8, result);

            return(result);
        }
Ejemplo n.º 5
0
        public static Guid ToGuid(Value128 value)
        {
            var higherHalf = value.HigherHalf;

            var lowerHalf = value.LowerHalf;

            return(new Guid(new[]
            {
                (Byte)(higherHalf >> 56), (Byte)(higherHalf >> 48), (Byte)(higherHalf >> 40), (Byte)(higherHalf >> 32), (Byte)(higherHalf >> 24), (Byte)(higherHalf >> 16), (Byte)(higherHalf >> 8), (Byte)(higherHalf >> 0), (Byte)(lowerHalf >> 56), (Byte)(lowerHalf >> 48), (Byte)(lowerHalf >> 40), (Byte)(lowerHalf >> 32), (Byte)(lowerHalf >> 24), (Byte)(lowerHalf >> 16), (Byte)(lowerHalf >> 8), (Byte)(lowerHalf >> 0)
            }));
        }
Ejemplo n.º 6
0
        public void Addition()
        {
            for (var index = 0; index < addIterations; index++)
            {
                var operandA = addInputArrayA[index];

                var operandB = addInputArrayB[index];

                var expectedResult = addExpectedResults[index];

                var actualResult = Value128.Add(operandA, operandB);

                Assert.AreEqual(expectedResult, actualResult);
            }
        }
Ejemplo n.º 7
0
        public void Subtraction()
        {
            for (var index = 0; index < subIterations; index++)
            {
                var operandA = subInputArrayA[index];

                var operandB = subInputArrayB[index];

                var expectedResult = subExpectedResults[index];

                var actualResult = Value128.Subtract(operandA, operandB);

                Assert.AreEqual(expectedResult, actualResult);
            }
        }
Ejemplo n.º 8
0
        public static TryResult <Value128> TryFromByteArray(Byte[] data, Int32 offset)
        {
            if ((data == null) || (data.Length - offset < 16))
            {
                return(convertFailResult);
            }

            // Convert higher half
            var higherHalf = Convert(data, offset);

            // Convert lower half
            var lowerHalf = Convert(data, offset + 8);

            // Initialize result
            var result = new Value128(higherHalf, lowerHalf);

            return(TryResult <Value128> .CreateSuccess(result));
        }
Ejemplo n.º 9
0
        public static TryResult <String> TryToBase32String(Value128 data, Base32Encoding base32Encoding)
        {
            if (base32Encoding == null)
            {
                return(encodeFailResult);
            }

            Char[] result;

            // Get padding symbol
            var paddingSymbol = base32Encoding.PaddingSymbol;

            if (paddingSymbol.HasValue)
            {
                result = new Char[32];

                // Set padding
                result[26] = result[27] = result[28] = result[29] = result[30] = result[31] = paddingSymbol.Value;
            }
            else
            {
                result = new Char[26];
            }

            var higherHalf = data.HigherHalf;

            var lowerHalf = data.LowerHalf;

            // Get alphabet
            var alphabet = base32Encoding.Alphabet;

            for (Int32 indexH = 0, indexL = 13, shiftH = 59, shiftL = 58; indexH < 12; indexH++, indexL++, shiftH -= 5, shiftL -= 5)
            {
                result[indexH] = alphabet[(Int32)(higherHalf >> shiftH) & 0x1F];

                result[indexL] = alphabet[(Int32)(lowerHalf >> shiftL) & 0x1F];
            }

            result[12] = alphabet[(Int32)(((higherHalf << 1) & 0x1E) | ((lowerHalf >> 63) & 0x01))];

            result[25] = alphabet[(Int32)((lowerHalf << 2) & 0x1C)];

            return(TryResult <String> .CreateSuccess(new String(result)));
        }
Ejemplo n.º 10
0
        public static TryResult <String> TryToBase64String(Value128 data, Base64Encoding base64Encoding)
        {
            if (base64Encoding == null)
            {
                return(encodeFailResult);
            }

            Char[] result;

            // Get padding symbol
            var paddingSymbol = base64Encoding.PaddingSymbol;

            if (paddingSymbol.HasValue)
            {
                result = new Char[24];

                result[22] = result[23] = paddingSymbol.Value;
            }
            else
            {
                result = new Char[22];
            }

            var higherHalf = data.HigherHalf;

            var lowerHalf = data.LowerHalf;

            // Get alphabet
            var alphabet = base64Encoding.Alphabet;

            for (Int32 indexH = 0, indexL = 11, shiftH = 58, shiftL = 56; indexH < 10; indexH++, indexL++, shiftH -= 6, shiftL -= 6)
            {
                result[indexH] = alphabet[(Int32)(higherHalf >> shiftH) & 0x3F];

                result[indexL] = alphabet[(Int32)(lowerHalf >> shiftL) & 0x3F];
            }

            result[10] = alphabet[(Int32)(((higherHalf << 2) & 0x3C) | ((lowerHalf >> 62) & 0x03))];

            result[21] = alphabet[(Int32)((lowerHalf << 4) & 0x30)];

            return(TryResult <String> .CreateSuccess(new String(result)));
        }