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

Commit 5f612ec

Browse files
committed
Merge pull request #2451 from Sridhar-MS/env-tests2
Add missing unit tests for System.Environment class
2 parents 5b273ac + b3ea441 commit 5f612ec

File tree

5 files changed

+186
-0
lines changed

5 files changed

+186
-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
@@ -45,7 +45,11 @@
4545
<Compile Include="System\Convert.ToUInt64.cs" />
4646
<Compile Include="System\Environment.ExpandEnvironmentVariables.cs" />
4747
<Compile Include="System\Environment.GetEnvironmentVariable.cs" />
48+
<Compile Include="System\Environment.NewLine.cs" />
49+
<Compile Include="System\Environment.ProcessorCount.cs" />
4850
<Compile Include="System\Environment.SetEnvironmentVariable.cs" />
51+
<Compile Include="System\Environment.StackTrace.cs" />
52+
<Compile Include="System\Environment.TickCount.cs" />
4953
<Compile Include="System\Math.cs" />
5054
<Compile Include="System\Progress.cs" />
5155
<Compile Include="System\Random.cs" />
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
[PlatformSpecific(PlatformID.Windows)]
13+
[Fact]
14+
public void Windows_NewLineTest()
15+
{
16+
Assert.Equal("\r\n", Environment.NewLine);
17+
}
18+
19+
[PlatformSpecific(PlatformID.AnyUnix)]
20+
[Fact]
21+
public void Unix_NewLineTest()
22+
{
23+
Assert.Equal("\n", Environment.NewLine);
24+
}
25+
}
26+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
[PlatformSpecific(PlatformID.Windows)]
13+
[Fact]
14+
public void Windows_ProcessorCountTest()
15+
{
16+
//arrange
17+
SYSTEM_INFO sysInfo = new SYSTEM_INFO();
18+
GetSystemInfo(ref sysInfo);
19+
int expected = sysInfo.dwNumberOfProcessors;
20+
21+
//act
22+
int actual = Environment.ProcessorCount;
23+
24+
//assert
25+
Assert.True(actual > 0);
26+
Assert.Equal(expected, actual);
27+
}
28+
29+
[PlatformSpecific(PlatformID.AnyUnix)]
30+
[Fact]
31+
public void Unix_ProcessorCountTest()
32+
{
33+
//arrange
34+
int _SC_NPROCESSORS_ONLN = Interop.IsOSX || Interop.IsFreeBSD ? 58 : 84;
35+
int expected = (int)sysconf(_SC_NPROCESSORS_ONLN);
36+
37+
//act
38+
int actual = Environment.ProcessorCount;
39+
40+
//assert
41+
Assert.True(actual > 0);
42+
Assert.Equal(expected, actual);
43+
}
44+
45+
[DllImport("libc")]
46+
private static extern long sysconf(int name);
47+
48+
[DllImport("api-ms-win-core-sysinfo-l1-1-0.dll", SetLastError = true)]
49+
internal static extern void GetSystemInfo(ref SYSTEM_INFO lpSystemInfo);
50+
51+
[StructLayout(LayoutKind.Sequential)]
52+
internal struct SYSTEM_INFO
53+
{
54+
internal int dwOemId; // This is a union of a DWORD and a struct containing 2 WORDs.
55+
internal int dwPageSize;
56+
internal IntPtr lpMinimumApplicationAddress;
57+
internal IntPtr lpMaximumApplicationAddress;
58+
internal IntPtr dwActiveProcessorMask;
59+
internal int dwNumberOfProcessors;
60+
internal int dwProcessorType;
61+
internal int dwAllocationGranularity;
62+
internal short wProcessorLevel;
63+
internal short wProcessorRevision;
64+
}
65+
}
66+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
[MethodImpl(MethodImplOptions.NoInlining)]
64+
public TestClass()
65+
{
66+
EnvironmentStackTrace.StaticFrame(null);
67+
}
68+
}
69+
}
70+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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;
7+
using Xunit;
8+
9+
namespace System.Runtime.Extensions.Tests
10+
{
11+
public class EnvironmentTickCount
12+
{
13+
[Fact]
14+
public void TickCountTest()
15+
{
16+
int start = Environment.TickCount;
17+
Assert.True(SpinWait.SpinUntil(() => Environment.TickCount > start, TimeSpan.FromSeconds(1)));
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)