Skip to content
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

チュートリアル C#コーディング問題(xUnit版) #48

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions contents/tutorial/coding-csharp-xunit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
整数 `x` と `y` を引数で受け取り、足し算した値を戻り値として返す `Add(int x, int y)` メソッドを実装してください。


## 例

```cs
Add(2, 3) // 5
Add(15, 8) // 23
Add(65, 94) // 159
```
5 changes: 5 additions & 0 deletions contents/tutorial/coding-csharp-xunit/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"track-metatest-tools": "^0.2.0"
}
}
3 changes: 3 additions & 0 deletions contents/tutorial/coding-csharp-xunit/scripts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/bash
cd /root/src/src/test
dotnet build;
4 changes: 4 additions & 0 deletions contents/tutorial/coding-csharp-xunit/scripts/test_run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/bash
cd /root/src/src/test
dotnet test --logger:xunit;
$(npm bin)/metatest do echo then expand xunit /root/src/src/test/TestResults/TestResults.xml;
7 changes: 7 additions & 0 deletions contents/tutorial/coding-csharp-xunit/solution/App.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class App
{
public static int Add(int x, int y)
{
return x + y;
}
}
7 changes: 7 additions & 0 deletions contents/tutorial/coding-csharp-xunit/src/main/App.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class App
{
public static int Add(int x, int y)
{
return -1;
}
}
11 changes: 11 additions & 0 deletions contents/tutorial/coding-csharp-xunit/src/main/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

class Program
{
static void Main(string[] args)
{
int x = int.Parse(args[0]);
int y = int.Parse(args[1]);
Console.WriteLine(App.Add(x, y));
}
}
12 changes: 12 additions & 0 deletions contents/tutorial/coding-csharp-xunit/src/main/main.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<DOTNET_FRAMEWORK_VERSION Condition=" '$(DOTNET_FRAMEWORK_VERSION)' == '' ">net6.0</DOTNET_FRAMEWORK_VERSION>
<OutputType>Exe</OutputType>
<TargetFramework>$(DOTNET_FRAMEWORK_VERSION)</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
</PropertyGroup>

</Project>
34 changes: 34 additions & 0 deletions contents/tutorial/coding-csharp-xunit/src/test/AppTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Xunit;

public class AppTest
{
[Fact(DisplayName = "[基本実装] 入力が 2 と 3 のとき、期待された出力結果が得られる")]
public void test_2_3()
{
int[] input = new int[2] {2, 3};
__testOutput(input, 5);
}

[Fact(DisplayName = "[基本実装] 入力が 15 と 8 のとき、期待された出力結果が得られる")]
public void test_15_8()
{
int[] input = new int[2] {15, 8};
__testOutput(input, 23);
}

[Fact(DisplayName = "[基本実装] 入力が 65 と 94 のとき、期待された出力結果が得られる")]
public void test_65_94()
{
int[] input = new int[2] {65, 94};
__testOutput(input, 159);
}

private void __testOutput(int[] input, int expected)
{
int x = input[0];
int y = input[1];
int result = Util.RunApp(x, y);

Assert.Equal(expected, result);
}
}
45 changes: 45 additions & 0 deletions contents/tutorial/coding-csharp-xunit/src/test/Util.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Threading;

public class Util
{
private static int TIMEOUT = 5000;
public static int RunApp(int x, int y)
{
AppThread appThread = new AppThread(x, y);
Thread th = new Thread(new ThreadStart(appThread.Run));
try
{
th.Start();
th.Join(TIMEOUT);
Comment on lines +13 to +14
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memo
timeout制御はThreadで実装できました。

}
catch (ThreadInterruptedException)
{
Console.WriteLine("Timeout " + TIMEOUT + "ms");
}
return appThread.GetResult();
}

private class AppThread
{
private readonly int x;
private readonly int y;
private int result = -1;

public AppThread(int x, int y)
{
this.x = x;
this.y = y;
}

public int GetResult()
{
return this.result;
}

public void Run()
{
this.result = App.Add(x, y);
}
}
}
24 changes: 24 additions & 0 deletions contents/tutorial/coding-csharp-xunit/src/test/test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<DOTNET_FRAMEWORK_VERSION Condition=" '$(DOTNET_FRAMEWORK_VERSION)' == '' ">net6.0</DOTNET_FRAMEWORK_VERSION>
<OutputType>Exe</OutputType>
<TargetFramework>$(DOTNET_FRAMEWORK_VERSION)</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="XunitXml.TestLogger" Version="3.0.70" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\main\main.csproj" />
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions contents/tutorial/coding-csharp-xunit/track.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
type: coding
main: src/main/App.cs
editable:
- src/main/App.cs
readonly:
- src/main/Program.cs
- src/test/AppTest.cs
- src/test/Util.cs
- src/main/main.csproj
- src/test/test.csproj
- scripts/*
- package.json
build: sh scripts/build.sh
test: sh scripts/test_run.sh
envConf:
imageName: dotnet
cacheDirs:
- /root/.dotnet
initialize:
commands:
- npm install
solutions:
- src/main/App.cs:solution/App.cs
testcases:
open: 3
secret: 0
evaluationPoint:
基本実装: 基本的なテストケースにおいて、正答が出力できる