-
Notifications
You must be signed in to change notification settings - Fork 0
Homework 10 - [MyLinq]
#6
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
| // <copyright file="GlobalSuppressions.cs" company="Ilya Krivtsov"> | ||
| // Copyright (c) Ilya Krivtsov. All rights reserved. | ||
| // </copyright> | ||
|
|
||
| // This file is used by Code Analysis to maintain SuppressMessage | ||
| // attributes that are applied to this project. | ||
| // Project-level suppressions either have no target or are given | ||
| // a specific target and scoped to a namespace, type, member, etc. | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| [assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:Elements should be documented", Justification = "This is tests project")] |
This file contains hidden or 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,28 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <LangVersion>latest</LangVersion> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
|
|
||
| <IsTestProject>true</IsTestProject> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="coverlet.collector" Version="6.0.2" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" /> | ||
| <PackageReference Include="NUnit" Version="4.2.2" /> | ||
| <PackageReference Include="NUnit.Analyzers" Version="4.4.0" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="4.6.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Using Include="NUnit.Framework" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="../MyLinq/MyLinq.csproj"/> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or 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,94 @@ | ||
| // <copyright file="MyLinqTests.cs" company="Ilya Krivtsov"> | ||
| // Copyright (c) Ilya Krivtsov. All rights reserved. | ||
| // </copyright> | ||
|
|
||
| namespace MyLinq.Tests; | ||
|
|
||
| public class MyLinqTests | ||
| { | ||
| [Test] | ||
| public void GetPrimes_Yields_PrimeNumbers() | ||
| { | ||
| int iterations = 100; | ||
|
|
||
| foreach (var (index, prime) in MyLinq.GetPrimes().Index()) | ||
| { | ||
| if (index >= iterations) | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| for (uint i = 2; i < prime; i++) | ||
| { | ||
| Assert.That(prime % i, Is.Not.EqualTo(0)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void Take_ReturnsSequence_With_First_N_Elements() | ||
| { | ||
| int takeCount = 47; | ||
| Assert.That( | ||
| MyLinq.Take(Enumerable.Range(0, 100), takeCount) | ||
| .SequenceEqual(Enumerable.Range(0, takeCount)), | ||
| Is.True); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Take_ReturnsSameSequence_If_CountIsGreaterThanOrEqualTo_SourceCount() | ||
| { | ||
| int count = 100; | ||
| var range = Enumerable.Range(0, count); | ||
| Assert.Multiple(() => | ||
| { | ||
| Assert.That(MyLinq.Take(range, count).SequenceEqual(range), Is.True); | ||
| Assert.That(MyLinq.Take(range, count + 1).SequenceEqual(range), Is.True); | ||
| }); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Take_ReturnsEmptySequence_If_CountIs_LessThanOrEqualTo_Zero() | ||
| { | ||
| var range = Enumerable.Range(0, 100); | ||
| Assert.Multiple(() => | ||
| { | ||
| Assert.That(MyLinq.Take(range, 0).Any(), Is.False); | ||
| Assert.That(MyLinq.Take(range, -1).Any(), Is.False); | ||
| }); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Skip_ReturnsSequence_Without_First_N_Elements() | ||
| { | ||
| int takeCount = 53; | ||
| Assert.That( | ||
| MyLinq.Skip(Enumerable.Range(0, 100), takeCount) | ||
| .SequenceEqual(Enumerable.Range(takeCount, 100 - takeCount)), | ||
| Is.True); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Skip_ReturnsSameSequence_If_CountIsLessThanOrEqualTo_Zero() | ||
| { | ||
| int count = 100; | ||
| var range = Enumerable.Range(0, count); | ||
| Assert.Multiple(() => | ||
| { | ||
| Assert.That(MyLinq.Skip(range, 0).SequenceEqual(range), Is.True); | ||
| Assert.That(MyLinq.Skip(range, -1).SequenceEqual(range), Is.True); | ||
| }); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Skip_ReturnsEmptySequence_If_CountIsGreaterThanOrEqualTo_SourceCount() | ||
| { | ||
| int count = 100; | ||
| var range = Enumerable.Range(0, count); | ||
| Assert.Multiple(() => | ||
| { | ||
| Assert.That(MyLinq.Skip(range, count).Any(), Is.False); | ||
| Assert.That(MyLinq.Skip(range, count + 1).Any(), Is.False); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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,53 @@ | ||
|
|
||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 17 | ||
| VisualStudioVersion = 17.0.31903.59 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyLinq", "MyLinq\MyLinq.csproj", "{29852F1B-EA67-4A5F-921D-52E576AB1D21}" | ||
| EndProject | ||
| Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "MyLinq.Tests", "MyLinq.Tests", "{57E4A64D-212E-B59A-67DA-DA99F8ED86A3}" | ||
| EndProject | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyLinq.Tests", "MyLinq.Tests\MyLinq.Tests.csproj", "{69B64B70-96AF-4C17-8F62-95FCA2604676}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Debug|x64 = Debug|x64 | ||
| Debug|x86 = Debug|x86 | ||
| Release|Any CPU = Release|Any CPU | ||
| Release|x64 = Release|x64 | ||
| Release|x86 = Release|x86 | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {29852F1B-EA67-4A5F-921D-52E576AB1D21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {29852F1B-EA67-4A5F-921D-52E576AB1D21}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {29852F1B-EA67-4A5F-921D-52E576AB1D21}.Debug|x64.ActiveCfg = Debug|Any CPU | ||
| {29852F1B-EA67-4A5F-921D-52E576AB1D21}.Debug|x64.Build.0 = Debug|Any CPU | ||
| {29852F1B-EA67-4A5F-921D-52E576AB1D21}.Debug|x86.ActiveCfg = Debug|Any CPU | ||
| {29852F1B-EA67-4A5F-921D-52E576AB1D21}.Debug|x86.Build.0 = Debug|Any CPU | ||
| {29852F1B-EA67-4A5F-921D-52E576AB1D21}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {29852F1B-EA67-4A5F-921D-52E576AB1D21}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {29852F1B-EA67-4A5F-921D-52E576AB1D21}.Release|x64.ActiveCfg = Release|Any CPU | ||
| {29852F1B-EA67-4A5F-921D-52E576AB1D21}.Release|x64.Build.0 = Release|Any CPU | ||
| {29852F1B-EA67-4A5F-921D-52E576AB1D21}.Release|x86.ActiveCfg = Release|Any CPU | ||
| {29852F1B-EA67-4A5F-921D-52E576AB1D21}.Release|x86.Build.0 = Release|Any CPU | ||
| {69B64B70-96AF-4C17-8F62-95FCA2604676}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {69B64B70-96AF-4C17-8F62-95FCA2604676}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {69B64B70-96AF-4C17-8F62-95FCA2604676}.Debug|x64.ActiveCfg = Debug|Any CPU | ||
| {69B64B70-96AF-4C17-8F62-95FCA2604676}.Debug|x64.Build.0 = Debug|Any CPU | ||
| {69B64B70-96AF-4C17-8F62-95FCA2604676}.Debug|x86.ActiveCfg = Debug|Any CPU | ||
| {69B64B70-96AF-4C17-8F62-95FCA2604676}.Debug|x86.Build.0 = Debug|Any CPU | ||
| {69B64B70-96AF-4C17-8F62-95FCA2604676}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {69B64B70-96AF-4C17-8F62-95FCA2604676}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {69B64B70-96AF-4C17-8F62-95FCA2604676}.Release|x64.ActiveCfg = Release|Any CPU | ||
| {69B64B70-96AF-4C17-8F62-95FCA2604676}.Release|x64.Build.0 = Release|Any CPU | ||
| {69B64B70-96AF-4C17-8F62-95FCA2604676}.Release|x86.ActiveCfg = Release|Any CPU | ||
| {69B64B70-96AF-4C17-8F62-95FCA2604676}.Release|x86.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(NestedProjects) = preSolution | ||
| {69B64B70-96AF-4C17-8F62-95FCA2604676} = {57E4A64D-212E-B59A-67DA-DA99F8ED86A3} | ||
| EndGlobalSection | ||
| EndGlobal |
This file contains hidden or 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,83 @@ | ||
| // <copyright file="MyLinq.cs" company="Ilya Krivtsov"> | ||
| // Copyright (c) Ilya Krivtsov. All rights reserved. | ||
| // </copyright> | ||
|
|
||
| namespace MyLinq; | ||
|
|
||
| /// <summary> | ||
| /// Utility class with GetPrimes, Take and Skip functions. | ||
| /// </summary> | ||
| public static class MyLinq | ||
| { | ||
| /// <summary> | ||
| /// Practically infinite sequence of prime numbers. | ||
| /// </summary> | ||
| /// <returns>Sequence of prime numbers.</returns> | ||
| public static IEnumerable<ulong> GetPrimes() | ||
| { | ||
| yield return 2; | ||
| yield return 3; | ||
| ulong lastPrime = 3; | ||
| while (true) | ||
| { | ||
| lastPrime += 2; | ||
| bool isPrime = true; | ||
| for (ulong i = 2; i * i <= lastPrime; i++) | ||
| { | ||
| if (lastPrime % i == 0) | ||
| { | ||
| isPrime = false; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (isPrime) | ||
| { | ||
| yield return lastPrime; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sequence with first <paramref name="count"/> items from <paramref name="sequence"/>. | ||
| /// </summary> | ||
| /// <typeparam name="T">Item type.</typeparam> | ||
| /// <param name="sequence">Sequence to take first <paramref name="count"/> items from.</param> | ||
| /// <param name="count">How many items to take.</param> | ||
| /// <returns>Subsequence of <paramref name="sequence"/>.</returns> | ||
| public static IEnumerable<T> Take<T>(this IEnumerable<T> sequence, int count) | ||
| { | ||
| int index = 0; | ||
ilya-krivtsov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| foreach (var item in sequence) | ||
| { | ||
| if (index >= count) | ||
| { | ||
| yield break; | ||
| } | ||
|
|
||
| yield return item; | ||
| index++; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sequence without first <paramref name="count"/> items from <paramref name="sequence"/>. | ||
| /// </summary> | ||
| /// <typeparam name="T">Item type.</typeparam> | ||
| /// <param name="sequence">Sequence to skip first <paramref name="count"/> items from.</param> | ||
| /// <param name="count">How many items to skip.</param> | ||
| /// <returns>Subsequence of <paramref name="sequence"/>.</returns> | ||
| public static IEnumerable<T> Skip<T>(this IEnumerable<T> sequence, int count) | ||
| { | ||
| int index = 0; | ||
| foreach (var item in sequence) | ||
| { | ||
| if (index >= count) | ||
| { | ||
| yield return item; | ||
| } | ||
|
|
||
| index++; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,10 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Library</OutputType> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.