/// <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); }
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-")); }
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")); }
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")); }
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")); }
public void TestCharacterSet() { // invalid value even with the '0' unicode character '\u0030' Assert.IsFalse(CASNumber.IsValid("\u0030-21-4")); }
public void TestSanity() { // completely stupid value Assert.IsFalse(CASNumber.IsValid("0&z003-!0>/-0a")); }
public void TestLargerThanFirst() { // valid format, but wrong number, the first is 50-00-0 Assert.IsFalse(CASNumber.IsValid("3-21-4")); }