You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by mh...@apache.org on 2014/08/01 19:35:29 UTC
[2/4] git commit: added parseInt, refactored with tests
added parseInt, refactored with tests
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/90a5211e
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/90a5211e
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/90a5211e
Branch: refs/heads/pcl
Commit: 90a5211e6653b43041ed873a2e914c4ce689d8dd
Parents: 2c22a12
Author: Michael Herndon <mh...@michaelherndon.com>
Authored: Thu Jul 24 16:20:56 2014 -0400
Committer: Michael Herndon <mh...@michaelherndon.com>
Committed: Thu Jul 24 16:20:56 2014 -0400
----------------------------------------------------------------------
src/Lucene.Net.Core/Check.cs | 30 ++++++
src/Lucene.Net.Core/Util/ArrayUtil.cs | 70 ++++++++++++++
.../Lucene.Net.Core.Tests.csproj | 1 +
.../Lucene.Net.Core.Tests.kproj | 1 +
.../Lucene.Net.Core.Tests/Util/TestArrayUtil.cs | 99 ++++++++++++++++++++
.../Lucene.Net.Core.Tests/Util/TestConstants.cs | 3 +
.../Util/LuceneTestCase.cs | 4 +-
7 files changed, 206 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/90a5211e/src/Lucene.Net.Core/Check.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Check.cs b/src/Lucene.Net.Core/Check.cs
index 036d3b0..58b9be7 100644
--- a/src/Lucene.Net.Core/Check.cs
+++ b/src/Lucene.Net.Core/Check.cs
@@ -23,6 +23,7 @@ namespace Lucene.Net
{
using System;
using System.Diagnostics;
+ using System.Reflection;
/// <summary>
/// Checks for code for issues that causes an error. Think of it as the replacement for Guard.
@@ -31,6 +32,35 @@ namespace Lucene.Net
{
[DebuggerStepThrough]
+ public static void Condition(bool condition, string name, string message, params object[] args)
+ {
+ if (condition)
+ {
+ var formattedMessage = message;
+ if (args != null && args.Length > 0)
+ formattedMessage = string.Format(message, args);
+
+ throw new ArgumentException(formattedMessage, name);
+ }
+ }
+
+ [DebuggerStepThrough]
+ public static void Condition<T>(bool condition, string message, params object[] args) where T : Exception
+ {
+ if (condition)
+ {
+ var formattedMessage = message;
+ if (args != null && args.Length > 0)
+ formattedMessage = string.Format(message, args);
+
+ var ctorArgs = new object[] { formattedMessage };
+ var exception = (T)Activator.CreateInstance(typeof(T), ctorArgs);
+
+ throw exception;
+ }
+ }
+
+ [DebuggerStepThrough]
public static void InRangeOfLength(int start, int count, int length)
{
if (start < 0 || start > length || count > length || start > count)
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/90a5211e/src/Lucene.Net.Core/Util/ArrayUtil.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Util/ArrayUtil.cs b/src/Lucene.Net.Core/Util/ArrayUtil.cs
index 0f9fd11..ee734a6 100644
--- a/src/Lucene.Net.Core/Util/ArrayUtil.cs
+++ b/src/Lucene.Net.Core/Util/ArrayUtil.cs
@@ -11,6 +11,76 @@ namespace Lucene.Net
public static class ArrayUtil
{
+ public static int ParseInt(this char[] chars, int offset = 0, int limit = -1, int radix = 10)
+ {
+ Check.NotNull("chars", chars);
+ Check.Condition(chars.Length == 0, "chars", "The parameter, chars, must not be an empty array.");
+ Check.Condition(offset < 0 || offset > chars.Length, "offset",
+ "The parameter, offset ({0}), must be greater than -1 and less than parameter, chars.Length ({1}).", offset, chars.Length);
+
+ if (limit < 0)
+ limit = chars.Length;
+
+ Check.Condition(
+ limit > chars.Length, "limit",
+ "The parameter, limit ({0}), must be less than or equal to the length of the parameter, chars ({1}).",
+ limit, chars.Length);
+
+ const int minRadix = 2;
+ const int maxRadix = 36;
+
+ Check.Condition<ArgumentOutOfRangeException>(
+ radix < minRadix || radix > maxRadix,
+ "The parameter, radix ({0}), must be greater than {1} and less than {2}.", radix, minRadix, maxRadix);
+
+ bool negative = chars[offset] == '-';
+
+ Check.Condition<FormatException>(negative && limit == 1, "A negative sign, '-', cannot be converted into an integer.");
+
+ if (negative == true)
+ {
+ offset++;
+ limit--;
+ }
+
+ return Parse(chars, offset, limit, radix, negative);
+ }
+
+ private static int Parse(char[] chars, int offset, int len, int radix, bool negative)
+ {
+ int max = int.MinValue / radix;
+ int result = 0;
+ for (int i = 0; i < len; i++)
+ {
+ int digit = (int)System.Char.GetNumericValue(chars[i + offset]);
+ if (digit == -1)
+ {
+ throw new System.FormatException("Unable to parse");
+ }
+ if (max > result)
+ {
+ throw new System.FormatException("Unable to parse");
+ }
+ int next = result * radix - digit;
+ if (next > result)
+ {
+ throw new System.FormatException("Unable to parse");
+ }
+ result = next;
+ }
+
+ if (!negative)
+ {
+ result = -result;
+ if (result < 0)
+ {
+ throw new System.FormatException("Unable to parse");
+ }
+ }
+ return result;
+ }
+
+
private class NaturalComparator<T> : IComparer<T> where T : IComparable<T>
{
public int Compare(T x, T y)
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/90a5211e/test/Lucene.Net.Core.Tests/Lucene.Net.Core.Tests.csproj
----------------------------------------------------------------------
diff --git a/test/Lucene.Net.Core.Tests/Lucene.Net.Core.Tests.csproj b/test/Lucene.Net.Core.Tests/Lucene.Net.Core.Tests.csproj
index 29e731e..3e31131 100644
--- a/test/Lucene.Net.Core.Tests/Lucene.Net.Core.Tests.csproj
+++ b/test/Lucene.Net.Core.Tests/Lucene.Net.Core.Tests.csproj
@@ -60,6 +60,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Support\ICloneableTests.cs" />
<Compile Include="Util\BaseSorterTestCase.cs" />
+ <Compile Include="Util\TestArrayUtil.cs" />
<Compile Include="Util\TestBits.cs" />
<Compile Include="Util\TestConstants.cs" />
<Compile Include="Util\TestInPlaceMergeSorter.cs" />
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/90a5211e/test/Lucene.Net.Core.Tests/Lucene.Net.Core.Tests.kproj
----------------------------------------------------------------------
diff --git a/test/Lucene.Net.Core.Tests/Lucene.Net.Core.Tests.kproj b/test/Lucene.Net.Core.Tests/Lucene.Net.Core.Tests.kproj
index a5d68f4..82e6eb5 100644
--- a/test/Lucene.Net.Core.Tests/Lucene.Net.Core.Tests.kproj
+++ b/test/Lucene.Net.Core.Tests/Lucene.Net.Core.Tests.kproj
@@ -54,6 +54,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Support\ICloneableTests.cs" />
<Compile Include="Util\BaseSorterTestCase.cs" />
+ <Compile Include="Util\TestArrayUtil.cs" />
<Compile Include="Util\TestBits.cs" />
<Compile Include="Util\TestConstants.cs" />
<Compile Include="Util\TestInPlaceMergeSorter.cs" />
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/90a5211e/test/Lucene.Net.Core.Tests/Util/TestArrayUtil.cs
----------------------------------------------------------------------
diff --git a/test/Lucene.Net.Core.Tests/Util/TestArrayUtil.cs b/test/Lucene.Net.Core.Tests/Util/TestArrayUtil.cs
new file mode 100644
index 0000000..161d24e
--- /dev/null
+++ b/test/Lucene.Net.Core.Tests/Util/TestArrayUtil.cs
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+namespace Lucene.Net.Util
+{
+ using System;
+ using Lucene.Net.Support;
+
+ public class TestArrayUtil : LuceneTestCase
+ {
+
+ [Test(JavaMethodName = "TestParseInt")]
+ public void ParseInt()
+ {
+ // int result;
+
+ // chars is null
+ Throws<ArgumentNullException>(() =>
+ {
+ ArrayUtil.ParseInt(null);
+ });
+
+ // chars is empty
+ Throws<ArgumentException>(() =>
+ {
+ ArrayUtil.ParseInt("".ToCharArray());
+ });
+
+ // throws when radix is greater than 36
+ Throws<ArgumentOutOfRangeException>(() =>
+ {
+ var data = "-123".ToCharArray();
+ Ok(data.Length > 0, "data.length should be greater than 0");
+ ArrayUtil.ParseInt(data, radix: 37);
+ });
+
+ // throws when radix is less than 2
+ Throws<ArgumentOutOfRangeException>(() =>
+ {
+ ArrayUtil.ParseInt("-123".ToCharArray(), radix: 1);
+ });
+
+ // throws when limit > chars.Length
+ Throws<ArgumentException>(() =>
+ {
+ ArrayUtil.ParseInt("12".ToCharArray(), limit: 20);
+ });
+
+ // throws when offset > chars.length;
+ Throws<ArgumentException>(() =>
+ {
+ ArrayUtil.ParseInt("12".ToCharArray(), offset: 20);
+ });
+
+ // throws when offset < 0.
+ Throws<ArgumentException>(() =>
+ {
+ ArrayUtil.ParseInt("12".ToCharArray(), offset: -1);
+ });
+
+ // throws when only the negative sign is present "-"
+ Throws<FormatException>(() =>
+ {
+ ArrayUtil.ParseInt("-".ToCharArray());
+ });
+
+ int test;
+
+ test = ArrayUtil.ParseInt("1".ToCharArray());
+ Ok(test == 1, test + " does not equal: " + 1);
+
+ test = ArrayUtil.ParseInt("-10000".ToCharArray());
+ Ok(test == -10000, test + " does not equal: " + -10000);
+
+ test = ArrayUtil.ParseInt("1923".ToCharArray());
+ Ok(test == 1923, test + " does not equal: " + 1923);
+
+ test = ArrayUtil.ParseInt("-1".ToCharArray());
+ Ok(test == -1, test + " does not equal: " + -1);
+
+ test = ArrayUtil.ParseInt("foo 1923 bar".ToCharArray(), 4, 4);
+ Ok(test == 1923, test + " does not equal: " + 1923);
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/90a5211e/test/Lucene.Net.Core.Tests/Util/TestConstants.cs
----------------------------------------------------------------------
diff --git a/test/Lucene.Net.Core.Tests/Util/TestConstants.cs b/test/Lucene.Net.Core.Tests/Util/TestConstants.cs
index ffcd3b0..8a3c583 100644
--- a/test/Lucene.Net.Core.Tests/Util/TestConstants.cs
+++ b/test/Lucene.Net.Core.Tests/Util/TestConstants.cs
@@ -43,6 +43,8 @@ namespace Lucene.Net.Util
Ok(Constants.OS_VERSION != null);
Ok(Constants.OS_ARCH != null);
Ok(Constants.LUCENE_VERSION != null);
+
+#if!PORTABLE
Console.WriteLine(" ");
Console.WriteLine("Constants");
Console.WriteLine("---------------------------------------");
@@ -51,6 +53,7 @@ namespace Lucene.Net.Util
Console.WriteLine("OS Arch: {0}", Constants.OS_ARCH);
Console.WriteLine("Lucene Version: {0}", Constants.LUCENE_VERSION);
Console.WriteLine(" ");
+#endif
}
[Test]
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/90a5211e/test/Lucene.Net.TestFramework/Util/LuceneTestCase.cs
----------------------------------------------------------------------
diff --git a/test/Lucene.Net.TestFramework/Util/LuceneTestCase.cs b/test/Lucene.Net.TestFramework/Util/LuceneTestCase.cs
index 1de4ad7..d0045fb 100644
--- a/test/Lucene.Net.TestFramework/Util/LuceneTestCase.cs
+++ b/test/Lucene.Net.TestFramework/Util/LuceneTestCase.cs
@@ -119,9 +119,9 @@ namespace Lucene.Net.Util
}
[DebuggerHidden]
- public static void Throws<T>(Action code) where T : Exception
+ public static T Throws<T>(Action code) where T : Exception
{
- Assert.Throws<T>(code);
+ return Assert.Throws<T>(code);
}
#endif