Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CI workflow #1

Merged
merged 2 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: CI
on:
workflow_dispatch:
pull_request:

jobs:
test:
name: Run Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- uses: actions/setup-dotnet@v1
with:
dotnet-version: '8.0.x'

- name: Run Tests
run: dotnet test --configuration Release
working-directory: ./Tests

benchmark:
name: Run Benchmarks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- uses: actions/setup-dotnet@v1
with:
dotnet-version: '8.0.x'

- name: Run benchmark
working-directory: ./Benchmarks
run: dotnet run -c Release --exporters json --filter '*' --memory --join

- name: Get benchmark file name
id: benchmarkfilename
working-directory: ./Benchmarks/BenchmarkDotNet.Artifacts/results
run: |
filePath=$(find . -type f -name 'BenchmarkRun-joined-*-report-full-compressed.json' | rev | cut -d '/' -f1 | rev)
echo $filePath
echo "::set-output name=file::$filePath"

- name: Store benchmark result
uses: rhysd/github-action-benchmark@v1
with:
name: Benchmark.Net Benchmark
tool: 'benchmarkdotnet'
output-file-path: Benchmarks/BenchmarkDotNet.Artifacts/results/${{ steps.benchmarkfilename.outputs.file }}
alert-threshold: '200%'
fail-on-alert: true

- name: Upload Artifacts
uses: actions/upload-artifact@v2
with:
name: Benchmark
path: Benchmark/BenchmarkDotNet.Artifacts/results/
52 changes: 52 additions & 0 deletions .github/workflows/update-benchmarks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Update Benchmarks
on:
workflow_dispatch:
push:
branches:
- main

permissions:
contents: write
deployments: write

jobs:
benchmark:
name: Run Benchmarks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- uses: actions/setup-dotnet@v1
with:
dotnet-version: '8.0.x'

- name: Run benchmark
working-directory: ./Benchmarks
run: dotnet run -c Release --exporters json --filter '*' --memory --join

- name: Get benchmark file name
id: benchmarkfilename
working-directory: ./Benchmarks/BenchmarkDotNet.Artifacts/results
run: |
filePath=$(find . -type f -name 'BenchmarkRun-joined-*-report-full-compressed.json' | rev | cut -d '/' -f1 | rev)
echo $filePath
echo "::set-output name=file::$filePath"

- name: Store benchmark result
uses: rhysd/github-action-benchmark@v1
with:
name: Benchmark.Net Benchmark
tool: 'benchmarkdotnet'
output-file-path: Benchmarks/BenchmarkDotNet.Artifacts/results/${{ steps.benchmarkfilename.outputs.file }}
github-token: ${{ secrets.GITHUB_TOKEN }}
auto-push: true
alert-threshold: '200%'
comment-on-alert: true
fail-on-alert: true
alert-comment-cc-users: '@timmoth'

- name: Upload Artifacts
uses: actions/upload-artifact@v2
with:
name: Benchmark
path: Benchmark/BenchmarkDotNet.Artifacts/results/
71 changes: 71 additions & 0 deletions Benchmarks/BubbleSortBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using BenchmarkDotNet.Attributes;
using DsaDotnet;

namespace Benchmarks;

public class BubbleSortBenchmarks
{
private int[] _ascendingOrder = null!;
private int[] _descendingOrder = null!;
private int[] _randomOrder = null!;

[Params(100, 1000, 10000)] public int N { get; set; }

[IterationSetup]
public void Setup()
{
_ascendingOrder = GenerateAscendingArray(N);
_randomOrder = GenerateRandomArray(N);
_descendingOrder = GenerateDescendingArray(N);
}

[Benchmark]
public void Bubble_Ascending()
{
_ascendingOrder.BubbleSortInPlace();
}

[Benchmark]
public void Bubble_Random()
{
_randomOrder.BubbleSortInPlace();
}

[Benchmark]
public void Bubble_Descending()
{
_descendingOrder.BubbleSortInPlace();
}

[IterationCleanup]
public void Cleanup()
{
_randomOrder = null!;
_ascendingOrder = null!;
_descendingOrder = null!;
}

private static int[] GenerateAscendingArray(int size)
{
var array = new int[size];
for (var i = 0; i < size; i++) array[i] = i;

return array;
}

private static int[] GenerateRandomArray(int size)
{
var random = new Random(0);
var array = new int[size];
for (var i = 0; i < size; i++) array[i] = random.Next();
return array;
}

private static int[] GenerateDescendingArray(int size)
{
var array = new int[size];
for (var i = 0; i < size; i++) array[i] = size - i;

return array;
}
}
24 changes: 4 additions & 20 deletions Benchmarks/FibonacciBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,11 @@ namespace Benchmarks;

public class FibonacciBenchmarks
{
[Benchmark]
public ulong Fib10()
{
return Fibonacci.Compute(10);
}

[Benchmark]
public ulong Fib100()
{
return Fibonacci.Compute(100);
}

[Benchmark]
public ulong Fib1000()
{
return Fibonacci.Compute(1000);
}
[Params(10, 100, 1000, 10000)] public int N { get; set; }

[Benchmark]
public ulong Fib10000()
public ulong Fib()
{
return Fibonacci.Compute(10000);
return Fibonacci.Compute(N);
}
}
}
2 changes: 1 addition & 1 deletion Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Benchmarks;

internal class Program
{
static void Main(string[] args)
private static void Main(string[] args)
{
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
}
Expand Down
47 changes: 25 additions & 22 deletions DsaDotnet/Fibonacci.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
using System.Numerics;

namespace DsaDotnet
namespace DsaDotnet;

public class Fibonacci
{
public class Fibonacci
public static ulong Compute(int input)
{
public static ulong Compute(uint n)
if (input <= 1)
{
if (n <= 1)
{
return n;
}
if (input < 0) throw new ArgumentException("Cannot calculate the fibonacci of a negative number");

ulong a = 0, b = 1;
for (var i = 31 - BitOperations.LeadingZeroCount(n); i >= 0; i--)
{
var c = a * ((b << 1) - a);
var d = a * a + b * b;
a = c;
b = d;
if ((n & (1 << i)) == 0)
continue;
var temp = a + b;
a = b;
b = temp;
}
return a;
return (ulong)input;
}

var n = (uint)input;
ulong a = 0, b = 1;
for (var i = 31 - BitOperations.LeadingZeroCount(n); i >= 0; i--)
{
var c = a * ((b << 1) - a);
var d = a * a + b * b;
a = c;
b = d;
if ((n & (1 << i)) == 0)
continue;
var temp = a + b;
a = b;
b = temp;
}

return a;
}
}
}
40 changes: 40 additions & 0 deletions DsaDotnet/Sorting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace DsaDotnet;

public static class Sorting
{
public static IEnumerable<T> BubbleSort<T>(this IEnumerable<T> source, IComparer<T>? comparer = null)
{
if (source == null)
throw new ArgumentNullException(nameof(source));

var elementArray = source.ToArray();
elementArray.BubbleSortInPlace(comparer);
return elementArray;
}

public static void BubbleSortInPlace<T>(this T[] source, IComparer<T>? comparer = null)
{
if (source == null)
throw new ArgumentNullException(nameof(source));

comparer ??= Comparer<T>.Default;

var n = source.Length;
bool swapped;

do
{
swapped = false;
for (var i = 0; i < n - 1; i++)
{
if (comparer.Compare(source[i], source[i + 1]) <= 0)
continue;

(source[i], source[i + 1]) = (source[i + 1], source[i]);
swapped = true;
}

n--;
} while (swapped);
}
}
22 changes: 22 additions & 0 deletions Tests/BubbleSortTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using DsaDotnet;
using FluentAssertions;

namespace Tests;

public class BubbleSortTests
{
[Theory]
[InlineData(new[] { 1, 2, 3, 4, 5 }, new[] { 1, 2, 3, 4, 5 })]
[InlineData(new[] { 5, 4, 3, 2, 1 }, new[] { 1, 2, 3, 4, 5 })]
[InlineData(new[] { 5, 3, 7, 2, 8, 10, 1, 6, 4, 9 }, new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 })]
public void BubbleSort_Returns_SortedCollection(int[] input, int[] expected)
{
// Arrange

// Act
var result = input.BubbleSort();

// Assert
result.Should().Equal(expected);
}
}
39 changes: 19 additions & 20 deletions Tests/FibonacciTests.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
using DsaDotnet;
using FluentAssertions;

namespace Tests
namespace Tests;

public class FibonacciTests
{
public class FibonacciTests
[Theory]
[InlineData(0, 0)]
[InlineData(1, 1)]
[InlineData(2, 1)]
[InlineData(3, 2)]
[InlineData(4, 3)]
[InlineData(5, 5)]
[InlineData(10, 55)]
[InlineData(15, 610)]
[InlineData(20, 6765)]
public void Fibonacci_Returns_CorrectValue(int n, uint expected)
{
[Theory]
[InlineData(0, 0)]
[InlineData(1, 1)]
[InlineData(2, 1)]
[InlineData(3, 2)]
[InlineData(4, 3)]
[InlineData(5, 5)]
[InlineData(10, 55)]
[InlineData(15, 610)]
[InlineData(20, 6765)]
public void Fibonacci_Returns_CorrectValue(uint n, uint expected)
{
// Arrange
// Arrange

// Act
var result = Fibonacci.Compute(n);
// Act
var result = Fibonacci.Compute(n);

// Assert
result.Should().Be(expected);
}
// Assert
result.Should().Be(expected);
}
}
Loading