Skip to content

Commit cd4f70d

Browse files
committed
Fixed nullable warnings and created a project folder for dotnet tests
1 parent 8d9f99d commit cd4f70d

File tree

7 files changed

+101
-3
lines changed

7 files changed

+101
-3
lines changed

csharp/Clockhands.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
public class AntiparallelClockHandsComputer
3+
{
4+
public static void Main()
5+
{
6+
for (var i = 0; i < 11; i++)
7+
{
8+
var t = (43200 * i + 21600) / 11;
9+
(var h, var m, var s) = (t / 3600, t / 60 % 60, t % 60);
10+
Console.WriteLine("{0:00}:{1:00}:{2:00}", (h != 0 ? h : 12), m, s);
11+
}
12+
}
13+
}

csharp/ReifiedExample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
public class Box<T>
22
{
3-
public T Value { get; set; }
3+
public T? Value { get; set; }
44
}
55

66
public class ReifiedExample

csharp/TraditionalWordCounter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ static void Main(string[] args)
1010
{
1111
var counts = new SortedDictionary<string, int>();
1212
var wordRegex = new Regex(@"[\p{L}']+");
13-
string line;
13+
string? line;
1414
while ((line = Console.ReadLine()?.ToLower()) != null)
1515
{
1616
foreach (Match match in wordRegex.Matches(line))

csharp/WordCounter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static void Main(string[] args)
2121
}
2222
static IEnumerable<string> StandardInputLines()
2323
{
24-
string line;
24+
string? line;
2525
while ((line = Console.ReadLine()) != null)
2626
{
2727
yield return line;

csharp/project/Program.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text.RegularExpressions;
4+
using System.IO;
5+
using System.Linq;
6+
7+
namespace Examples
8+
{
9+
static class WordCounter
10+
{
11+
static void Main(string[] args)
12+
{
13+
Regex wordRegex = new Regex(@"[\p{L}']+");
14+
StandardInputLines()
15+
.SelectMany(line => wordRegex.Matches(line).Cast<Match>())
16+
.Select(x => x.Value.ToLower())
17+
.GroupBy(x => x)
18+
.OrderBy(x => x.Key)
19+
.ToList()
20+
.ForEach(p => Console.WriteLine("{0} {1}", p.Key, p.Count()));
21+
}
22+
static IEnumerable<string> StandardInputLines()
23+
{
24+
string? line;
25+
while ((line = Console.ReadLine()) != null)
26+
{
27+
yield return line;
28+
}
29+
}
30+
}
31+
}

csharp/project/project.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

csharp/test.ps1

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
function Assert-MatchTests {
2+
param (
3+
[Parameter(Mandatory = $true, ValueFromPipeline)] $TestResult
4+
)
5+
6+
if ($TestResult) {
7+
$TestResult
8+
Write-Error "Output does not match expected results."
9+
}
10+
}
11+
12+
function Initialize-File {
13+
param (
14+
[Parameter(Mandatory = $true, ValueFromPipeline)] $FileName
15+
)
16+
17+
Get-Content "$PSScriptRoot\$FileName" > "$PSScriptRoot\project\Program.cs"
18+
}
19+
20+
# File path for the project folder.
21+
$project = "$PSScriptRoot\project"
22+
23+
$Error.clear()
24+
Initialize-File("Greeter.cs") && dotnet run --project $project &&
25+
Initialize-File("ReifiedExample.cs") && dotnet run --project $project &&
26+
Initialize-File("TraditionalWordCounter.cs") && Get-Content "$PSScriptRoot\..\test\wordcount_ascii_input" |
27+
dotnet run --project $project |
28+
Compare-Object (Get-Content "$PSScriptRoot\..\test\wordcount_ascii_expected") |
29+
Assert-MatchTests &&
30+
Initialize-File("Tripler.cs") && dotnet run --project $project |
31+
Compare-Object (Get-Content "$PSScriptRoot\..\test\triple_expected") |
32+
Assert-MatchTests &&
33+
Initialize-File("WordCounter.cs") && Get-Content "$PSScriptRoot\..\test\wordcount_ascii_input" |
34+
dotnet run --project $project |
35+
Compare-Object (Get-Content "$PSScriptRoot\..\test\wordcount_ascii_expected") &&
36+
# Assert-MatchTests &&
37+
ForEach-Object 'foo'
38+
39+
if ($Error -or !$?) {
40+
"*** C# TESTS FAILED ***"
41+
}
42+
else {
43+
"C# TESTS PASSED"
44+
}

0 commit comments

Comments
 (0)