Add xunit tests.

This commit is contained in:
Colin McMillen 2020-11-18 17:52:13 -05:00
parent f0e78c34cf
commit db4d18fa80
2 changed files with 36 additions and 12 deletions

View File

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

View File

@ -1,8 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
</PropertyGroup> <GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>
</Project>
<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>