Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 28bf4a6

Browse files
committed
Add missing unit tests for System.Environment class
The following pulbic APIs from System.Enironment did not have any unit test coverage - Environment.NewLine.cs - Environment.ProcessorCount.cs - Environment.TickCount.cs - Environment.StackTrace.cs
1 parent 6804045 commit 28bf4a6

File tree

5 files changed

+196
-0
lines changed

5 files changed

+196
-0
lines changed

src/System.Runtime.Extensions/tests/System.Runtime.Extensions.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
</PropertyGroup>
1919
<ItemGroup>
2020
<Compile Include="System\Diagnostics\Stopwatch.cs" />
21+
<Compile Include="System\Environment.NewLine.cs" />
22+
<Compile Include="System\Environment.ProcessorCount.cs" />
23+
<Compile Include="System\Environment.StackTrace.cs" />
2124
<Compile Include="System\Runtime\Versioning\FrameworkName.cs" />
2225
<Compile Include="System\IO\Path.cs" />
2326
<Compile Include="System\IO\Path.Combine.cs" />
@@ -56,6 +59,7 @@
5659
</ItemGroup>
5760
<ItemGroup>
5861
<None Include="project.json" />
62+
<Compile Include="System\Environment.TickCount.cs" />
5963
</ItemGroup>
6064
<ItemGroup>
6165
<ProjectReference Include="..\src\System.Runtime.Extensions.CoreCLR.csproj">
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
using Xunit;
7+
8+
namespace System.Runtime.Extensions.Tests
9+
{
10+
public class EnvironmentNewLine
11+
{
12+
[Fact]
13+
public void NewLineTest()
14+
{
15+
//arrange
16+
string expectedNewLine = Interop.IsWindows ? "\r\n" : "\n";
17+
18+
//act
19+
string actualNewLine = Environment.NewLine;
20+
21+
//assert
22+
Assert.Equal(expectedNewLine, actualNewLine);
23+
}
24+
}
25+
26+
27+
28+
29+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
using Xunit;
7+
8+
namespace System.Runtime.Extensions.Tests
9+
{
10+
public class EnvironmentProcessorCount
11+
{
12+
[Fact]
13+
public void ProcessorCountTest()
14+
{
15+
//arrange
16+
int expected;
17+
18+
if(Interop.IsWindows)
19+
{
20+
SYSTEM_INFO sysInfo = new SYSTEM_INFO();
21+
SYSTEM_INFO.GetSystemInfo(ref sysInfo);
22+
expected = sysInfo.dwNumberOfProcessors;
23+
}
24+
else
25+
{
26+
int _SC_NPROCESSORS_ONLN = Interop.IsOSX ? 58 : 84;
27+
expected = (int)sysconf(_SC_NPROCESSORS_ONLN);
28+
}
29+
30+
//act
31+
int actual = Environment.ProcessorCount;
32+
33+
//assert
34+
Assert.True(actual > 0);
35+
Assert.Equal(expected, actual);
36+
}
37+
38+
[DllImport("libc")]
39+
private static extern long sysconf(int name);
40+
41+
[StructLayout(LayoutKind.Sequential)]
42+
internal struct SYSTEM_INFO
43+
{
44+
internal int dwOemId; // This is a union of a DWORD and a struct containing 2 WORDs.
45+
internal int dwPageSize;
46+
internal IntPtr lpMinimumApplicationAddress;
47+
internal IntPtr lpMaximumApplicationAddress;
48+
internal IntPtr dwActiveProcessorMask;
49+
internal int dwNumberOfProcessors;
50+
internal int dwProcessorType;
51+
internal int dwAllocationGranularity;
52+
internal short wProcessorLevel;
53+
internal short wProcessorRevision;
54+
[DllImport("api-ms-win-core-sysinfo-l1-1-0.dll", SetLastError = true)]
55+
internal static extern void GetSystemInfo(ref SYSTEM_INFO lpSystemInfo);
56+
}
57+
58+
59+
}
60+
61+
62+
63+
64+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using Xunit;
11+
12+
namespace System.Runtime.Extensions.Tests
13+
{
14+
public class EnvironmentStackTrace
15+
{
16+
static string s_stackTrace;
17+
18+
[Fact]
19+
public void StackTraceTest()
20+
{
21+
//arrange
22+
List<string> knownFrames = new List<string>()
23+
{
24+
"System.Runtime.Extensions.Tests.EnvironmentStackTrace.StaticFrame(Object obj)",
25+
"System.Runtime.Extensions.Tests.EnvironmentStackTrace.TestClass..ctor()",
26+
"System.Runtime.Extensions.Tests.EnvironmentStackTrace.GenericFrame[T1,T2](T1 t1, T2 t2)",
27+
"System.Runtime.Extensions.Tests.EnvironmentStackTrace.TestFrame()"
28+
};
29+
30+
//act
31+
Task.Run(() => TestFrame()).Wait();
32+
33+
//assert
34+
int index = 0;
35+
foreach(string frame in knownFrames)
36+
{
37+
index = s_stackTrace.IndexOf(frame, index);
38+
Assert.True(index > -1);
39+
index += frame.Length;
40+
}
41+
}
42+
43+
[MethodImpl(MethodImplOptions.NoInlining)]
44+
public void TestFrame()
45+
{
46+
GenericFrame<DateTime, StringBuilder>(DateTime.Now, null);
47+
}
48+
49+
[MethodImpl(MethodImplOptions.NoInlining)]
50+
public void GenericFrame<T1, T2>(T1 t1, T2 t2)
51+
{
52+
var test = new TestClass();
53+
}
54+
55+
[MethodImpl(MethodImplOptions.NoInlining)]
56+
public static void StaticFrame(object obj)
57+
{
58+
s_stackTrace = Environment.StackTrace;
59+
}
60+
61+
class TestClass
62+
{
63+
public TestClass()
64+
{
65+
EnvironmentStackTrace.StaticFrame(null);
66+
}
67+
}
68+
}
69+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
using System.Threading.Tasks;
7+
using Xunit;
8+
9+
namespace System.Runtime.Extensions.Tests
10+
{
11+
public class EnvironmentTickCount
12+
{
13+
[Fact]
14+
public void TickCountTest()
15+
{
16+
//arrange
17+
const int milliSeconds = 1000;
18+
int start = Environment.TickCount;
19+
20+
//act
21+
Task.Delay(milliSeconds).Wait();
22+
int end = Environment.TickCount;
23+
24+
//assert
25+
Console.WriteLine("Start - " + start);
26+
Console.WriteLine("End - " + end);
27+
Assert.True(end - start >= milliSeconds);
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)