예제 #1
0
        // NB: We can not use Decode() since s_FlickrBase58Alphabet is not sorted.
        public static long FromFlickrBase58String(string value)
        {
            Require.NotNull(value, nameof(value));
            Require.True(value.Length <= BASE58_MAX_LENGTH, nameof(value));

            long retval     = 0L;
            long multiplier = 1L;

            for (int i = value.Length - 1; i >= 0; i--)
            {
                int index = Array.IndexOf(s_FlickrBase58Alphabet, value[i]);
                if (index < 0)
                {
                    throw new FormatException(
                              Format.Current(Strings.IllegalCharacter, value[i], i));
                }

                checked
                {
                    retval += multiplier * index;
                    if (i != 0)
                    {
                        multiplier *= BASE58_ALPHABET_LENGTH;
                    }
                }
            }

            return(retval);
        }
예제 #2
0
        public static long FromBase34String(string value)
        {
            Require.NotNull(value, nameof(value));
            Require.True(value.Length <= BASE34_MAX_LENGTH, nameof(value));

            return(Decode(value, s_Base34Alphabet, BASE34_ALPHABET_LENGTH));
        }
예제 #3
0
        public Moneypenny(long amount, Currency currency)
        {
            Require.True(currency.HasFixedDecimalPlaces, nameof(currency));

            Amount   = amount;
            Currency = currency;
        }
예제 #4
0
        public static void True1()
        {
            Action act = () => {
                Require.True(true, "paramName");
                Require.True(true, "paramName", "My message");
            };

            Assert.DoesNotThrow(act);
        }
예제 #5
0
        /// <summary>
        /// Obtains an instance of the <see cref="Currency" /> class associated
        /// with the specified culture.
        /// </summary>
        /// <param name="cultureInfo">A culture info.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="cultureInfo"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="cultureInfo"/> is neutral.</exception>
        /// <exception cref="CurrencyNotFoundException">Thrown if no currency exists for the specified culture.</exception>
        /// <returns>The currency for the specified culture info.</returns>
        public static Currency ForCulture(CultureInfo cultureInfo)
        {
            Require.NotNull(cultureInfo, nameof(cultureInfo));
            Require.True(
                !cultureInfo.IsNeutralCulture,
                nameof(cultureInfo),
                Format.Current(Strings_Money.Argument_NeutralCultureNotSupported, cultureInfo));

            return(ForRegion(new RegionInfo(cultureInfo.Name)));
        }
예제 #6
0
        public static void True2()
        {
            var    paramName = "paramName";
            Action act       = () => Require.True(false, paramName);

            var ex = Record.Exception(act);

            Assert.NotNull(ex);
            Assert.NotNull(ex.Message);
            var argex = Assert.IsType <ArgumentException>(ex);

            Assert.Equal(paramName, argex.ParamName);
        }
예제 #7
0
        public static void True3()
        {
            var    paramName = "paramName";
            var    message   = "My message";
            Action act       = () => Require.True(false, paramName, message);

            var ex = Record.Exception(act);

            Assert.NotNull(ex);
            var argex = Assert.IsType <ArgumentException>(ex);

            Assert.Equal(paramName, argex.ParamName);
            // ArgumentException appends some info to our message.
            Assert.StartsWith(message, ex.Message, StringComparison.OrdinalIgnoreCase);
        }
예제 #8
0
        public static void True_ThrowsArgumentException_ForFalse_1()
        {
            // Arrange
            var    paramName = "paramName";
            Action act       = () => Require.True(false, paramName);

            // Act
            var ex = Record.Exception(act);

            // Assert
            Assert.NotNull(ex);
            Assert.NotNull(ex.Message);
            var argex = Assert.IsType <ArgumentException>(ex);

            Assert.Equal(paramName, argex.ParamName);
        }
예제 #9
0
        /// <summary>
        /// Register a currency not part of ISO 4217.
        /// <para>It can also be useful when the library is not up-to-date with the ISO 4217 list
        /// of active currencies.</para>
        /// </summary>
        /// <remarks>
        /// <para>All currencies registered via this method have the <see cref="CurrencyTypes.UserDefined"/>
        /// type. In particular, they inherit the <see cref="CurrencyTypes.Circulating"/> type; as a
        /// consequence, you can not register a withdrawn currency.</para>
        /// <para>If you have more than one currency to register, you should use
        /// <see cref="RegisterCurrencies(Dictionary{String, Int16?})"/> instead.</para>
        /// <para>This method is thread-safe.</para>
        /// </remarks>
        /// <param name="code">The three letters code.</param>
        /// <param name="minorUnits">The number of minor units; null if the currency does not have
        /// a minor currency unit and <see cref="UnknownMinorUnits"/> if the status is unknown.</param>
        /// <returns>true if the operation succeeded; otherwise, false.</returns>
        /// <exception cref="ArgumentException">Thrown if the candidate currency does not meet
        /// the requirements: a <paramref name="code"/> must be of length 3 and made of ASCII
        /// uppercase letters, and <paramref name="minorUnits"/>, if not null, must be greater than
        /// or equal to zero and lower than or equal to 28.</exception>
        public static bool RegisterCurrency(string code, short?minorUnits)
        {
            // REVIEW: Should we relax the constraints we impose on the code value for user-defined
            // currencies? JavaMoney does it, but I am not very convinced that there are enough
            // good reasons for the complications it will imply. Nevertheless, if we decided
            // to do this, there will a problem with MinorCurrencyCode and we would have to review
            // all guards wherever we accept a code as input.
            Require.NotNull(code, nameof(code));
            // A currency code MUST be composed of exactly 3 letters.
            // A currency code MUST only contain uppercase ASCII letters.
            Require.True(
                ValidateCode(code),
                nameof(code),
                Format.Current(Strings_Money.Argument_InvalidCurrencyCode, code));
            Require.True(
                ValidateMinorUnits(minorUnits),
                nameof(minorUnits),
                Format.Current(Strings_Money.Argument_InvalidCurrencyMinorUnits, minorUnits));

            // Codes and WithdrawnCodes are immutable, so no concurrency problems here.
            if (Codes.ContainsKey(code) || WithdrawnCodes.Contains(code))
            {
                return(false);
            }

            lock (s_UserCodesLock)
            {
                if (!s_UserCodes.ContainsKey(code))
                {
                    s_UserCodes.Add(code, minorUnits);

                    return(true);
                }
            }

            return(false);
        }
예제 #10
0
 public static void True_DoesNotThrow_ForTrue_2() => Require.True(true, "paramName", "My message");
예제 #11
0
 public static void True_DoesNotThrow_ForTrue_1() => Require.True(true, "paramName");
예제 #12
0
 internal void ThrowIfCurrencyMismatch(Money money, string parameterName)
 => Require.True(
     Currency == money.Currency,
     parameterName,
     Format.Current(Strings_Money.CurrencyMismatch, Currency, money.Currency));