Пример #1
0
        /// <summary>
        /// Checks whether the registry number is valid.
        /// </summary>
        /// <param name="casNumber">the CAS number to validate</param>
        /// <returns>true if a valid CAS number, false otherwise</returns>
        // @cdk.keyword CAS number
        // @cdk.keyword validation
        public static bool IsValid(string casNumber)
        {
            bool overall = true;

            // check format
            var matcher = pattern.Match(casNumber);

            overall = overall && matcher.Success;

            if (matcher.Success)
            {
                // check number
                string part1      = matcher.Groups[1].Value;
                string part2      = matcher.Groups[2].Value;
                string part3      = matcher.Groups[3].Value;
                int    part1value = int.Parse(part1, NumberFormatInfo.InvariantInfo);
                if (part1value < 50)
                {
                    overall = false;
                    // CAS numbers start at 50-00-0
                }
                else
                {
                    int digit = CASNumber.CalculateCheckDigit(part1, part2);
                    overall = overall && (digit == int.Parse(part3, NumberFormatInfo.InvariantInfo));
                }
            }

            return(overall);
        }
Пример #2
0
 public void TestWrongHyphenPositions()
 {
     // invalid format due to invalid hyphen positions
     Assert.IsFalse(CASNumber.IsValid("3-21-40"));
     Assert.IsFalse(CASNumber.IsValid("3-210-4"));
     Assert.IsFalse(CASNumber.IsValid("03-1-4"));
     Assert.IsFalse(CASNumber.IsValid("03-21-"));
 }
Пример #3
0
 public void TestInvalidCharacters()
 {
     // invalid characters
     Assert.IsFalse(CASNumber.IsValid("a-21-4"));
     Assert.IsFalse(CASNumber.IsValid("3-a1-4"));
     Assert.IsFalse(CASNumber.IsValid("3-2a-4"));
     Assert.IsFalse(CASNumber.IsValid("3-21-a"));
     Assert.IsFalse(CASNumber.IsValid("d-cb-a"));
 }
Пример #4
0
 public void TestValidNumbers()
 {
     // valid cas numbers
     Assert.IsTrue(CASNumber.IsValid("50-00-0")); // formaldehyde
     Assert.IsTrue(CASNumber.IsValid("548-00-5"));
     Assert.IsTrue(CASNumber.IsValid("2622-26-6"));
     Assert.IsTrue(CASNumber.IsValid("15299-99-7"));
     Assert.IsTrue(CASNumber.IsValid("673434-32-7"));
 }
Пример #5
0
 public void TestInvalidCheckDigits()
 {
     // invalid R value
     Assert.IsFalse(CASNumber.IsValid("50-00-1"));
     Assert.IsFalse(CASNumber.IsValid("50-00-2"));
     Assert.IsFalse(CASNumber.IsValid("50-00-3"));
     Assert.IsFalse(CASNumber.IsValid("50-00-4"));
     Assert.IsFalse(CASNumber.IsValid("50-00-5"));
     Assert.IsFalse(CASNumber.IsValid("50-00-6"));
     Assert.IsFalse(CASNumber.IsValid("50-00-7"));
     Assert.IsFalse(CASNumber.IsValid("50-00-8"));
     Assert.IsFalse(CASNumber.IsValid("50-00-9"));
 }
Пример #6
0
 public void TestCharacterSet()
 {
     // invalid value even with the '0' unicode character '\u0030'
     Assert.IsFalse(CASNumber.IsValid("\u0030-21-4"));
 }
Пример #7
0
 public void TestSanity()
 {
     // completely stupid value
     Assert.IsFalse(CASNumber.IsValid("0&z003-!0>/-0a"));
 }
Пример #8
0
 public void TestLargerThanFirst()
 {
     // valid format, but wrong number, the first is 50-00-0
     Assert.IsFalse(CASNumber.IsValid("3-21-4"));
 }