Skip to content

Commit

Permalink
split implementations into individual files
Browse files Browse the repository at this point in the history
  • Loading branch information
smdn committed Apr 19, 2022
1 parent 54a127d commit 7faf33b
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 209 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-FileCopyrightText: 2020 smdn <smdn@smdn.jp>
// SPDX-License-Identifier: MIT
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using NUnit.Framework;

namespace Smdn.Test.NUnit.Assertion;

public partial class Assert : global::NUnit.Framework.Assert {
private static TimeSpan MeasureExecutionTime(TestDelegate code)
{
var sw = Stopwatch.StartNew();

code();

return sw.Elapsed;
}

private static TimeSpan MeasureExecutionTime(AsyncTestDelegate code)
{
static async Task<TimeSpan> MeasureCore(AsyncTestDelegate c)
{
var sw = Stopwatch.StartNew();

await c().ConfigureAwait(false);

return sw.Elapsed;
}

return MeasureCore(code).GetAwaiter().GetResult(); // XXX
}

public static void Elapses(TimeSpan expected, TestDelegate code, string message = null)
=> That(MeasureExecutionTime(code), Is.GreaterThanOrEqualTo(expected), message ?? "elapses");

public static void ElapsesAsync(TimeSpan expected, AsyncTestDelegate code, string message = null)
=> That(MeasureExecutionTime(code), Is.GreaterThanOrEqualTo(expected), message ?? "elapses");

public static void NotElapse(TimeSpan expected, TestDelegate code, string message = null)
=> That(MeasureExecutionTime(code), Is.LessThanOrEqualTo(expected), message ?? "not elapse");

public static void NotElapseAsync(TimeSpan expected, AsyncTestDelegate code, string message = null)
=> That(MeasureExecutionTime(code), Is.LessThanOrEqualTo(expected), message ?? "not elapse");

public static void ElapsesInRange(TimeSpan expectedMin, TimeSpan expectedMax, TestDelegate code, string message = null)
=> That(MeasureExecutionTime(code), Is.InRange(expectedMin, expectedMax), message ?? "elapses in range");

public static void ElapsesInRangeAsync(TimeSpan expectedMin, TimeSpan expectedMax, AsyncTestDelegate code, string message = null)
=> That(MeasureExecutionTime(code), Is.InRange(expectedMin, expectedMax), message ?? "elapses in range");
}
43 changes: 0 additions & 43 deletions src/Smdn.Test.NUnit.Utils/Smdn.Test.NUnit.Assertion/Assert.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,11 @@
// SPDX-FileCopyrightText: 2020 smdn <smdn@smdn.jp>
// SPDX-License-Identifier: MIT
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using NUnit.Framework;

namespace Smdn.Test.NUnit.Assertion;

public partial class Assert : global::NUnit.Framework.Assert {
private static TimeSpan MeasureExecutionTime(TestDelegate code)
{
var sw = Stopwatch.StartNew();

code();

return sw.Elapsed;
}

private static TimeSpan MeasureExecutionTime(AsyncTestDelegate code)
{
static async Task<TimeSpan> MeasureCore(AsyncTestDelegate c)
{
var sw = Stopwatch.StartNew();

await c().ConfigureAwait(false);

return sw.Elapsed;
}

return MeasureCore(code).GetAwaiter().GetResult(); // XXX
}

public static void Elapses(TimeSpan expected, TestDelegate code, string message = null)
=> That(MeasureExecutionTime(code), Is.GreaterThanOrEqualTo(expected), message ?? "elapses");

public static void ElapsesAsync(TimeSpan expected, AsyncTestDelegate code, string message = null)
=> That(MeasureExecutionTime(code), Is.GreaterThanOrEqualTo(expected), message ?? "elapses");

public static void NotElapse(TimeSpan expected, TestDelegate code, string message = null)
=> That(MeasureExecutionTime(code), Is.LessThanOrEqualTo(expected), message ?? "not elapse");

public static void NotElapseAsync(TimeSpan expected, AsyncTestDelegate code, string message = null)
=> That(MeasureExecutionTime(code), Is.LessThanOrEqualTo(expected), message ?? "not elapse");

public static void ElapsesInRange(TimeSpan expectedMin, TimeSpan expectedMax, TestDelegate code, string message = null)
=> That(MeasureExecutionTime(code), Is.InRange(expectedMin, expectedMax), message ?? "elapses in range");

public static void ElapsesInRangeAsync(TimeSpan expectedMin, TimeSpan expectedMax, AsyncTestDelegate code, string message = null)
=> That(MeasureExecutionTime(code), Is.InRange(expectedMin, expectedMax), message ?? "elapses in range");

public static TException ThrowsOrAggregates<TException>(TestDelegate code)
where TException : Exception
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// SPDX-FileCopyrightText: 2022 smdn <smdn@smdn.jp>
// SPDX-License-Identifier: MIT
using System;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;

namespace Smdn.Test.NUnit.Assertion;

[TestFixture]
public class AssertExecutionTimeTests {
[SetUp]
public void SetUp()
{
/*
* warm up
*/
Assert.Elapses(TimeSpan.FromMilliseconds(0), static () => Thread.Sleep(0));
Assert.ElapsesAsync(TimeSpan.FromMilliseconds(0), static () => Task.Delay(0));

Assert.NotElapse(TimeSpan.FromMilliseconds(100), static () => Thread.Sleep(0));
Assert.NotElapseAsync(TimeSpan.FromMilliseconds(100), static () => Task.Delay(0));

Assert.ElapsesInRange(TimeSpan.FromMilliseconds(0), TimeSpan.FromMilliseconds(100), static () => Thread.Sleep(10));
Assert.ElapsesInRangeAsync(TimeSpan.FromMilliseconds(0), TimeSpan.FromMilliseconds(100), static () => Task.Delay(10));
}

[Test]
public void Elapses(
[Random(min: 30, max: 40, count: 10)] int milliseconds
)
=> Assert.Elapses(
expected: TimeSpan.FromMilliseconds(20),
code: () => Thread.Sleep(milliseconds)
);

[Test]
public void Elapses_Fail(
[Random(min: 1, max: 10, count: 10)] int milliseconds
)
=> Assert.Throws<AssertionException>(() =>
Assert.Elapses(
expected: TimeSpan.FromMilliseconds(20),
code: () => Thread.Sleep(milliseconds)
)
);

[Test]
public void ElapsesAsync(
[Random(min: 30, max: 40, count: 10)] int milliseconds
)
=> Assert.ElapsesAsync(
expected: TimeSpan.FromMilliseconds(20),
code: () => Task.Delay(milliseconds)
);

[Test]
public void ElapsesAsync_Fail(
[Random(min: 1, max: 10, count: 10)] int milliseconds
)
=> Assert.Throws<AssertionException>(() =>
Assert.ElapsesAsync(
expected: TimeSpan.FromMilliseconds(20),
code: () => Task.Delay(milliseconds)
)
);

[Test]
public void NotElapse(
[Random(min: 1, max: 10, count: 10)] int milliseconds
)
=> Assert.NotElapse(
expected: TimeSpan.FromMilliseconds(20),
code: () => Thread.Sleep(milliseconds)
);

[Test]
public void NotElapse_Fail(
[Random(min: 30, max: 40, count: 10)] int milliseconds
)
=> Assert.Throws<AssertionException>(() =>
Assert.NotElapse(
expected: TimeSpan.FromMilliseconds(20),
code: () => Thread.Sleep(milliseconds)
)
);

[Test]
public void NotElapseAsync(
[Random(min: 1, max: 10, count: 10)] int milliseconds
)
=> Assert.NotElapseAsync(
expected: TimeSpan.FromMilliseconds(20),
code: () => Task.Delay(milliseconds)
);

[Test]
public void NotElapseAsync_Fail(
[Random(min: 30, max: 40, count: 10)] int milliseconds
)
=> Assert.Throws<AssertionException>(() =>
Assert.NotElapseAsync(
expected: TimeSpan.FromMilliseconds(20),
code: () => Task.Delay(milliseconds)
)
);

[Test]
public void ElapsesInRange(
[Random(min: 20, max: 30, count: 10)] int milliseconds
)
=> Assert.ElapsesInRange(
expectedMin: TimeSpan.FromMilliseconds(10),
expectedMax: TimeSpan.FromMilliseconds(40),
code: () => Thread.Sleep(milliseconds)
);

[Test]
public void ElapsesInRange_Fail_LessThanMin(
[Random(min: 0, max: 10, count: 10)] int milliseconds
)
=> Assert.Throws<AssertionException>(() =>
Assert.ElapsesInRange(
expectedMin: TimeSpan.FromMilliseconds(20),
expectedMax: TimeSpan.FromMilliseconds(21),
code: () => Thread.Sleep(milliseconds)
)
);

[Test]
public void ElapsesInRange_Fail_GreaterThanMax(
[Random(min: 30, max: 40, count: 10)] int milliseconds
)
=> Assert.Throws<AssertionException>(() =>
Assert.ElapsesInRange(
expectedMin: TimeSpan.FromMilliseconds(20),
expectedMax: TimeSpan.FromMilliseconds(21),
code: () => Thread.Sleep(milliseconds)
)
);

[Test]
public void ElapsesInRangeAsync(
[Random(min: 20, max: 30, count: 10)] int milliseconds
)
=> Assert.ElapsesInRangeAsync(
expectedMin: TimeSpan.FromMilliseconds(10),
expectedMax: TimeSpan.FromMilliseconds(40),
code: () => Task.Delay(milliseconds)
);

[Test]
public void ElapsesInRangeAsync_Fail_LessThanMin(
[Random(min: 0, max: 10, count: 10)] int milliseconds
)
=> Assert.Throws<AssertionException>(() =>
Assert.ElapsesInRangeAsync(
expectedMin: TimeSpan.FromMilliseconds(20),
expectedMax: TimeSpan.FromMilliseconds(21),
code: () => Task.Delay(milliseconds)
)
);

[Test]
public void ElapsesInRangeAsync_Fail_GreaterThanMax(
[Random(min: 30, max: 40, count: 10)] int milliseconds
)
=> Assert.Throws<AssertionException>(() =>
Assert.ElapsesInRangeAsync(
expectedMin: TimeSpan.FromMilliseconds(20),
expectedMax: TimeSpan.FromMilliseconds(21),
code: () => Task.Delay(milliseconds)
)
);
}
Loading

0 comments on commit 7faf33b

Please sign in to comment.