Browse Source

Add xunit tests.

main
Colin McMillen 11 months ago
parent
commit
db4d18fa80
  1. 25
      Program.cs
  2. 23
      euler.csproj

25
Program.cs

@ -1,11 +1,13 @@
using System;
using System;
using System.Collections.Generic;
using static System.Console;
using Xunit;
namespace Euler {
class Program {
public class Program {
[Fact]
static long Problem1() {
long sum = 0;
for (long i = 1; i < 1000; i++) {
@ -13,9 +15,11 @@ namespace Euler {
sum += i;
}
}
Assert.Equal(233168, sum);
return sum;
}
[Fact]
static long Problem2() {
long max = 4_000_000;
@ -35,6 +39,7 @@ namespace Euler {
}
}
Assert.Equal(4613732, sum);
return sum;
}
@ -71,6 +76,7 @@ namespace Euler {
}
}
[Fact]
static long Problem3() {
long target = 600_851_475_143;
long targetSqrt = (long) Math.Sqrt(target);
@ -81,6 +87,7 @@ namespace Euler {
highestPrimeFactor = i;
}
}
Assert.Equal(6857, highestPrimeFactor);
return highestPrimeFactor;
}
@ -94,6 +101,7 @@ namespace Euler {
return true;
}
[Fact]
static long Problem4() {
long largest = 0;
for (long i = 999; i >= 100; i--) {
@ -107,9 +115,11 @@ namespace Euler {
}
}
}
Assert.Equal(906609, largest);
return largest;
}
[Fact]
static long Problem5() {
for (long test = 20; ; test += 20) {
for (int i = 2; i <= 20; i++) {
@ -117,12 +127,14 @@ namespace Euler {
break;
}
if (i == 20) {
Assert.Equal(232792560, test);
return test;
}
}
}
}
[Fact]
static long Problem6() {
long sum = 0;
long sumSq = 0;
@ -130,12 +142,17 @@ namespace Euler {
sum += i;
sumSq += i * i;
}
return sum * sum - sumSq;
long result = sum * sum - sumSq;
Assert.Equal(25164150, result);
return result;
}
[Fact]
static long Problem7() {
List<long> primes = FirstNPrimes(10001);
return primes[primes.Count - 1];
long result = primes[primes.Count - 1];
Assert.Equal(104743, result);
return result;
}
static void Main(string[] args) {

23
euler.csproj

@ -1,8 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
</ItemGroup>
</Project>
Loading…
Cancel
Save