Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added first cut of prime tester

  • Loading branch information...
commit 2013f397b10063cda93f081a78af9120369c7f8d 1 parent f2af448
@FlukeFan authored
View
37 Source/RemoteJs.Demo.Test/Domain/PrimeTest.cs
@@ -0,0 +1,37 @@
+using System;
+using NUnit.Framework;
+using RemoteJs.Demo.Domain;
+
+namespace RemoteJs.Demo.Test.Domain
+{
+ [TestFixture]
+ public class PrimeTest
+ {
+ [Test]
+ public void IsPrime()
+ {
+ Assert.That(Prime.IsPrime(1));
+ Assert.That(Prime.IsPrime(2));
+ Assert.That(Prime.IsPrime(3));
+ Assert.That(Prime.IsPrime(5));
+ Assert.That(Prime.IsPrime(7));
+ Assert.That(Prime.IsPrime(11));
+ Assert.That(Prime.IsPrime(13));
+ }
+
+ [Test]
+ public void IsNotPrime()
+ {
+ Assert.That(!Prime.IsPrime(4));
+ Assert.That(!Prime.IsPrime(9));
+ Assert.That(!Prime.IsPrime(28));
+ Assert.That(!Prime.IsPrime(99999));
+ }
+
+ [Test]
+ public void OnlySmallNumbers()
+ {
+ Assert.Throws<Exception>(() => Prime.IsPrime(99999 + 1));
+ }
+ }
+}
View
1  Source/RemoteJs.Demo.Test/RemoteJs.Demo.Test.csproj
@@ -56,6 +56,7 @@
<Compile Include="Controllers\DemosControllerTest.cs" />
<Compile Include="Controllers\HomeControllerTest.cs" />
<Compile Include="Domain\AccountTest.cs" />
+ <Compile Include="Domain\PrimeTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Util\TestTemplateBaseTest.cs" />
<Compile Include="Util\TestTemplateBase.cs" />
View
47 Source/RemoteJs.Demo/Domain/Prime.cs
@@ -0,0 +1,47 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+
+namespace RemoteJs.Demo.Domain
+{
+ public class Prime
+ {
+ private static uint _maxAllowed = 99999;
+ private static List<uint> _primes = new List<uint> { 2 };
+ private static uint _next = 2;
+
+ public static bool IsPrime(uint number)
+ {
+ if (number == 0)
+ return false;
+
+ if (number == 1)
+ return true;
+
+ if (number > _maxAllowed)
+ throw new Exception(string.Format("Cannot calculate for larger than {0}", _maxAllowed));
+
+ lock (_primes)
+ {
+ while (_next < number)
+ {
+ _next++;
+ bool isPrime = true;
+
+ foreach (var prime in _primes)
+ if (_next % prime == 0)
+ {
+ isPrime = false;
+ break;
+ }
+
+ if (isPrime)
+ _primes.Add(_next);
+ }
+
+ return _primes.Contains(number);
+ }
+ }
+ }
+}
View
1  Source/RemoteJs.Demo/RemoteJs.Demo.csproj
@@ -63,6 +63,7 @@
<Compile Include="Controllers\DemosController.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Domain\Account.cs" />
+ <Compile Include="Domain\Prime.cs" />
<Compile Include="Domain\Util\Command.cs" />
<Compile Include="Domain\Util\CommandExecutor.cs" />
<Compile Include="Domain\Util\DomainException.cs" />
Please sign in to comment.
Something went wrong with that request. Please try again.