Last active
January 2, 2016 01:28
-
-
Save JohanLarsson/8230197 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public struct Code : IEquatable<Code> | |
| { | |
| public Code(ushort major, ushort minor) | |
| : this() | |
| { | |
| Major = major; | |
| Minor = minor; | |
| } | |
| public static Code ErrorCode { get { return new Code(ushort.MaxValue, ushort.MaxValue); } } | |
| public ushort Major { get; private set; } | |
| public ushort Minor { get; private set; } | |
| public static Code Parse(string s) | |
| { | |
| return Parse(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo); | |
| } | |
| public static Code Parse(string s, NumberStyles style) | |
| { | |
| return Parse(s, style, NumberFormatInfo.CurrentInfo); | |
| } | |
| public static Code Parse(string s, IFormatProvider provider) | |
| { | |
| return Parse(s, NumberStyles.Integer, provider); | |
| } | |
| public static Code Parse(string s, NumberStyles style, IFormatProvider provider) | |
| { | |
| if (s == null) | |
| throw new ArgumentNullException("s"); | |
| Code code; | |
| if (TryParse(s, style, provider, out code)) | |
| { | |
| return code; | |
| } | |
| throw new FormatException(); | |
| } | |
| public static bool TryParse(string s, out Code code) | |
| { | |
| return TryParse(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out code); | |
| } | |
| public static bool TryParse(string s, IFormatProvider provider, out Code code) | |
| { | |
| return TryParse(s, NumberStyles.Integer, provider, out code); | |
| } | |
| public static bool TryParse(string s, NumberStyles style, out Code code) | |
| { | |
| return TryParse(s, style, NumberFormatInfo.CurrentInfo, out code); | |
| } | |
| public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out Code code) | |
| { | |
| try | |
| { | |
| if (string.IsNullOrWhiteSpace(s)) | |
| { | |
| code = ErrorCode; | |
| return false; | |
| } | |
| var strings = s.Split(new[] { '.' }); | |
| if (strings.Length == 0 || strings.Length > 2) | |
| { | |
| code = ErrorCode; | |
| return false; | |
| } | |
| ushort major; | |
| if (!ushort.TryParse(strings[0], style, provider, out major)) | |
| { | |
| code = ErrorCode; | |
| return false; | |
| } | |
| ushort minor=0; | |
| if (strings.Length == 2) | |
| { | |
| if (!ushort.TryParse(strings[1], style, provider, out minor)) | |
| { | |
| code = ErrorCode; | |
| return false; | |
| } | |
| } | |
| code = new Code(major, minor); | |
| return true; | |
| } | |
| catch (Exception) | |
| { | |
| code = ErrorCode; | |
| return false; | |
| } | |
| } | |
| public static bool operator ==(Code left, Code right) | |
| { | |
| return left.Equals(right); | |
| } | |
| public static bool operator !=(Code left, Code right) | |
| { | |
| return !left.Equals(right); | |
| } | |
| public override string ToString() | |
| { | |
| return string.Format("{0}{1}", Major.ToString("D2"), (Minor == 0 ? "" : "." + Minor)); | |
| } | |
| public bool Equals(Code other) | |
| { | |
| return Major == other.Major && Minor == other.Minor; | |
| } | |
| public override bool Equals(object obj) | |
| { | |
| if (ReferenceEquals(null, obj)) return false; | |
| return obj is Code && Equals((Code)obj); | |
| } | |
| public override int GetHashCode() | |
| { | |
| return (Major << 16) + Minor; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class CodeTests | |
| { | |
| [Test] | |
| public void CtorTest() | |
| { | |
| var code = new Code(1, 2); | |
| Assert.AreEqual(1, code.Major); | |
| Assert.AreEqual(2, code.Minor); | |
| } | |
| [TestCase("", -1, -1, ExpectedException = typeof (FormatException))] | |
| [TestCase(null, -1, -1, ExpectedException = typeof (ArgumentNullException))] | |
| [TestCase("1", 1, 0)] | |
| [TestCase("95.1", 95, 1)] | |
| public void ParseTests(string s, int major, int minor) | |
| { | |
| var code = Code.Parse(s); | |
| Assert.AreEqual(major, code.Major); | |
| Assert.AreEqual(minor, code.Minor); | |
| } | |
| [TestCase("", -1, -1,false)] | |
| [TestCase(null, -1, -1,false)] | |
| [TestCase("1", 1, 0, true)] | |
| [TestCase("95.1", 95, 1, true)] | |
| public void TryParseTests(string s, int major, int minor,bool expected) | |
| { | |
| Code code; | |
| var tryParse = Code.TryParse(s, out code); | |
| Assert.AreEqual(expected, tryParse); | |
| if (tryParse) | |
| { | |
| Assert.AreEqual(major, code.Major); | |
| Assert.AreEqual(minor, code.Minor); | |
| } | |
| } | |
| [TestCase(0, 0, 0)] | |
| [TestCase(0, 1, 1)] | |
| [TestCase(0,ushort.MaxValue, ushort.MaxValue)] | |
| [TestCase(0, 2, 2)] | |
| [TestCase(1, 0, 65536)] | |
| [TestCase(2, 0, 131072)] | |
| [TestCase(ushort.MaxValue, 0, -65536)] | |
| [TestCase(ushort.MaxValue, ushort.MaxValue, -1)] | |
| public void GetHashCodeTest(int major, int minor, int expected) | |
| { | |
| var code = new Code((ushort)major, (ushort)minor); | |
| Assert.AreEqual(major, code.Major); | |
| Assert.AreEqual(minor, code.Minor); | |
| Assert.AreEqual(expected,code.GetHashCode()); | |
| } | |
| [TestCase(0, 0, "00")] | |
| [TestCase(0, 1, "00.1")] | |
| [TestCase(1, 0, "01")] | |
| [TestCase(1, 1, "01.1")] | |
| [TestCase(259, 1, "259.1")] | |
| public void ToStringTests(int major, int minor, string expected) | |
| { | |
| Code code= new Code((ushort) major,(ushort) minor); | |
| var toString = code.ToString(); | |
| Assert.AreEqual(expected,toString); | |
| var parsed = Code.Parse(toString); | |
| Assert.AreEqual(code, parsed); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment