-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split implementations into individual files
- Loading branch information
Showing
4 changed files
with
226 additions
and
209 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/Smdn.Test.NUnit.Utils/Smdn.Test.NUnit.Assertion/Assert.ExecutionTime.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
src/Smdn.Test.NUnit.Utils/Smdn.Test.NUnit.Assertion/Assert.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
tests/Smdn.Test.NUnit.Utils/Smdn.Test.NUnit.Assertion/Assert.ExecutionTime.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
); | ||
} |
Oops, something went wrong.