diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4b82ccd --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vs/ +bin/ +obj/ diff --git a/String2IntTest/String2IntTest.sln b/String2IntTest/String2IntTest.sln new file mode 100644 index 0000000..1474107 --- /dev/null +++ b/String2IntTest/String2IntTest.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31624.102 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "String2IntTest", "String2IntTest\String2IntTest.csproj", "{BEEFA1D5-8FBF-491E-849C-AC7FEA2754C3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "String2IntTestTests", "String2IntTestTests\String2IntTestTests.csproj", "{B1E79B52-D62C-4AF6-ADA0-A6793A052EC1}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BEEFA1D5-8FBF-491E-849C-AC7FEA2754C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BEEFA1D5-8FBF-491E-849C-AC7FEA2754C3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BEEFA1D5-8FBF-491E-849C-AC7FEA2754C3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BEEFA1D5-8FBF-491E-849C-AC7FEA2754C3}.Release|Any CPU.Build.0 = Release|Any CPU + {B1E79B52-D62C-4AF6-ADA0-A6793A052EC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B1E79B52-D62C-4AF6-ADA0-A6793A052EC1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B1E79B52-D62C-4AF6-ADA0-A6793A052EC1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B1E79B52-D62C-4AF6-ADA0-A6793A052EC1}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D540A343-1C23-43F6-B22A-0C0F457FE0BC} + EndGlobalSection +EndGlobal diff --git a/String2IntTest/String2IntTest/String2Int.cs b/String2IntTest/String2IntTest/String2Int.cs new file mode 100644 index 0000000..4d00838 --- /dev/null +++ b/String2IntTest/String2IntTest/String2Int.cs @@ -0,0 +1,50 @@ +using System; +/** + * Fillter all integers in given string, build whole integer and return it + * + * Note: The reason it doesn't have Main function is, it is supposed to be a + * converter and used as utility function. + * + * **/ +namespace String2IntTest +{ + public class String2Int + { + int _Integer; + public int GetInteger + { + get { return _Integer; } + } + public void FillterInteger(string strinput) + { + int output = 0 ; + bool IsHasDigit = false; + + if (strinput.Length == 0) + { + throw new ArgumentException("No input data"); + } + + foreach (char i in strinput) + { + if (IsDigit(i)) + { + IsHasDigit = true; + output = (output * 10) + ((int)i - 48); + } + } + + if (!IsHasDigit && output == 0) + { + throw new ArgumentException("Output is null"); + } + + _Integer = output; + } + private bool IsDigit(char c) + { + return (int)c >= 48 && (int)c <= 57 ; + } + } + +} diff --git a/String2IntTest/String2IntTest/String2IntTest.csproj b/String2IntTest/String2IntTest/String2IntTest.csproj new file mode 100644 index 0000000..a462632 --- /dev/null +++ b/String2IntTest/String2IntTest/String2IntTest.csproj @@ -0,0 +1,10 @@ + + + + Library + netcoreapp3.1 + + + + + diff --git a/String2IntTest/String2IntTestTests/String2IntTestTests.csproj b/String2IntTest/String2IntTestTests/String2IntTestTests.csproj new file mode 100644 index 0000000..53bf010 --- /dev/null +++ b/String2IntTest/String2IntTestTests/String2IntTestTests.csproj @@ -0,0 +1,20 @@ + + + + .NETCoreApp,Version=v3.1 + + false + + + + + + + + + + + + + + diff --git a/String2IntTest/String2IntTestTests/String2IntTests.cs b/String2IntTest/String2IntTestTests/String2IntTests.cs new file mode 100644 index 0000000..428e8be --- /dev/null +++ b/String2IntTest/String2IntTestTests/String2IntTests.cs @@ -0,0 +1,72 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using String2IntTest; +using System; +using System.Collections.Generic; +using System.Text; + +namespace String2IntTest.Tests +{ + [TestClass()] + public class String2IntTests + { + [TestMethod()] + public void TestNoInputData() + { + try + { + String2Int strinput = new String2Int(); + strinput.FillterInteger(""); + } + catch (ArgumentException e) + { + StringAssert.Contains(e.Message, "No input data"); + } + + } + + [TestMethod()] + public void TestSpecialCharactersOnly() + { + try + { + String2Int strinput = new String2Int(); + strinput.FillterInteger(" *,"); + } + catch (ArgumentException e) + { + StringAssert.Contains(e.Message, "Output is null"); + } + + } + + + [TestMethod()] + public void TestIntegerOnly() + { + + String2Int strinput = new String2Int(); + strinput.FillterInteger("034599"); + Assert.AreEqual(034599, strinput.GetInteger); + + + } + [TestMethod()] + public void TestSampleInputA() + { + String2Int strinput = new String2Int(); + strinput.FillterInteger("abc573"); + Assert.AreEqual(573, strinput.GetInteger); + + } + + [TestMethod()] + public void TestSampleInputB() + { + String2Int strinput = new String2Int(); + strinput.FillterInteger("a5b7c3"); + Assert.AreEqual(573, strinput.GetInteger); + + } + + } +} \ No newline at end of file