-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
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,47 @@ | ||
using FsCheck; | ||
using FsCheck.Xunit; | ||
using Funcky.Test.TestUtils; | ||
|
||
namespace Funcky.Test; | ||
|
||
public sealed class CycleMaterializedTest | ||
{ | ||
[Fact] | ||
public void IsEnumeratedLazily() | ||
{ | ||
var doNotEnumerate = new FailOnEnumerateCollection<object>(Count: 1); | ||
_ = Sequence.CycleMaterialized(doNotEnumerate); | ||
} | ||
|
||
[Fact] | ||
public void CyclingAnEmptySetThrowsAnException() | ||
=> Assert.Throws<InvalidOperationException>(CycleEmptySequence); | ||
|
||
[Property] | ||
public Property CanProduceArbitraryManyItems(NonEmptySet<int> sequence, PositiveInt arbitraryElements) | ||
{ | ||
var cycleRange = Sequence.CycleMaterialized(sequence.Get.Materialize()); | ||
|
||
return (cycleRange.Take(arbitraryElements.Get).Count() == arbitraryElements.Get) | ||
.ToProperty(); | ||
} | ||
|
||
[Property] | ||
public Property RepeatsTheElementsArbitraryManyTimes(NonEmptySet<int> sequence, PositiveInt arbitraryElements) | ||
{ | ||
var cycleRange = Sequence.CycleMaterialized(sequence.Get.Materialize()); | ||
|
||
return cycleRange | ||
.IsSequenceRepeating(sequence.Get) | ||
.NTimes(arbitraryElements.Get) | ||
.ToProperty(); | ||
} | ||
|
||
private static void CycleEmptySequence() | ||
{ | ||
var cycledRange = Sequence.CycleMaterialized(Array.Empty<int>()); | ||
using var enumerator = cycledRange.GetEnumerator(); | ||
|
||
enumerator.MoveNext(); | ||
} | ||
} |
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
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,43 @@ | ||
using FsCheck; | ||
using FsCheck.Xunit; | ||
using Funcky.Test.TestUtils; | ||
|
||
namespace Funcky.Test; | ||
|
||
public sealed class RepeatMaterializedTest | ||
{ | ||
[Fact] | ||
public void IsEnumeratedLazily() | ||
{ | ||
var doNotEnumerate = new FailOnEnumerateCollection<object>(Count: 0); | ||
_ = Sequence.RepeatMaterialized(doNotEnumerate, 2); | ||
} | ||
|
||
[Property] | ||
public Property ARepeatedEmptySequenceIsStillEmpty(NonNegativeInt count) | ||
{ | ||
var repeated = Sequence.RepeatMaterialized(Array.Empty<object>(), count.Get); | ||
return (!repeated.Any()).ToProperty(); | ||
} | ||
|
||
[Property] | ||
public Property TheLengthOfTheGeneratedSequenceIsCorrect(List<int> list, NonNegativeInt count) | ||
{ | ||
var repeatRange = Sequence.RepeatMaterialized(list, count.Get); | ||
|
||
var materialized = repeatRange.ToList(); | ||
|
||
return (materialized.Count == list.Count * count.Get).ToProperty(); | ||
} | ||
|
||
[Property] | ||
public Property TheSequenceRepeatsTheGivenNumberOfTimes(List<int> list, NonNegativeInt count) | ||
{ | ||
var repeatRange = Sequence.RepeatMaterialized(list, count.Get); | ||
|
||
return repeatRange | ||
.IsSequenceRepeating(list) | ||
.NTimes(count.Get) | ||
.ToProperty(); | ||
} | ||
} |
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
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
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,11 @@ | ||
namespace Funcky; | ||
|
||
public static partial class Sequence | ||
{ | ||
/// <inheritdoc cref="CycleRange{TSource}(IEnumerable{TSource})"/> | ||
[Pure] | ||
public static IEnumerable<TSource> CycleMaterialized<TSource>(IReadOnlyCollection<TSource> source) | ||
=> source.Count > 0 | ||
? Cycle(source).SelectMany(Identity) | ||
: throw new InvalidOperationException("you cannot cycle an empty enumerable"); | ||
} |
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,9 @@ | ||
namespace Funcky; | ||
|
||
public static partial class Sequence | ||
{ | ||
/// <inheritdoc cref="RepeatRange{TSource}(IEnumerable{TSource},int)"/> | ||
[Pure] | ||
public static IEnumerable<TSource> RepeatMaterialized<TSource>(IReadOnlyCollection<TSource> source, int count) | ||
=> Enumerable.Repeat(source, count).SelectMany(Identity); | ||
} |