-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathNativeLibrarySmokeTests.cs
More file actions
151 lines (124 loc) · 4.48 KB
/
NativeLibrarySmokeTests.cs
File metadata and controls
151 lines (124 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System.Runtime.InteropServices;
using OpusSharp.Core;
using Xunit;
namespace OpusSharp.Core.Tests;
public sealed class NativeLibrarySmokeTests
{
public NativeLibrarySmokeTests()
{
TestNativeLibraryBootstrapper.EnsureInitialized();
}
[Fact]
public void Version_ReturnsNonEmptyString()
{
var version = OpusInfo.Version();
Assert.False(string.IsNullOrWhiteSpace(version));
}
[Fact]
public void StringError_ReturnsKnownMessage()
{
var error = OpusInfo.StringError((int)OpusErrorCodes.OPUS_INVALID_PACKET);
Assert.False(string.IsNullOrWhiteSpace(error));
}
[Fact]
public void OpusException_PreservesMessageAndInnerException()
{
var inner = new InvalidOperationException("inner");
var exception = new OpusException("outer", inner);
Assert.Equal("outer", exception.Message);
Assert.Same(inner, exception.InnerException);
}
[Fact]
public void PublicEnumValues_MatchExpectedOpusConstants()
{
Assert.Equal(-4, (int)OpusErrorCodes.OPUS_INVALID_PACKET);
Assert.Equal(2048, (int)OpusPredefinedValues.OPUS_APPLICATION_VOIP);
Assert.Equal(4002, (int)EncoderCTL.OPUS_SET_BITRATE);
}
[Fact]
public void DefaultOpusExceptionCtor_ProducesExceptionInstance()
{
var exception = new OpusException();
Assert.NotNull(exception);
}
private static class TestNativeLibraryBootstrapper
{
private static bool _initialized;
public static void EnsureInitialized()
{
if (_initialized)
{
return;
}
var sourcePath = GetNativeLibraryPath();
var destinationPath = Path.Combine(AppContext.BaseDirectory, Path.GetFileName(sourcePath));
if (!File.Exists(destinationPath))
{
File.Copy(sourcePath, destinationPath, overwrite: true);
}
_initialized = true;
}
private static string GetNativeLibraryPath()
{
var repoRoot = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../../"));
var runtimeFolder = GetRuntimeFolder();
var fileName = GetNativeLibraryFileName();
var path = Path.Combine(repoRoot, "OpusSharp.Natives", "runtimes", runtimeFolder, "native", fileName);
if (!File.Exists(path))
{
throw new FileNotFoundException($"Native opus library was not found at '{path}'.");
}
return path;
}
private static string GetRuntimeFolder()
{
if (OperatingSystem.IsMacOS())
{
return RuntimeInformation.ProcessArchitecture switch
{
Architecture.Arm64 => "osx-arm64",
Architecture.X64 => "osx-x64",
_ => throw new PlatformNotSupportedException("Unsupported macOS architecture.")
};
}
if (OperatingSystem.IsLinux())
{
return RuntimeInformation.ProcessArchitecture switch
{
Architecture.Arm => "linux-arm",
Architecture.Arm64 => "linux-arm64",
Architecture.X64 => "linux-x64",
Architecture.X86 => "linux-x86",
_ => throw new PlatformNotSupportedException("Unsupported Linux architecture.")
};
}
if (OperatingSystem.IsWindows())
{
return RuntimeInformation.ProcessArchitecture switch
{
Architecture.Arm64 => "win-arm64",
Architecture.X64 => "win-x64",
Architecture.X86 => "win-x86",
_ => throw new PlatformNotSupportedException("Unsupported Windows architecture.")
};
}
throw new PlatformNotSupportedException("Unsupported host operating system.");
}
private static string GetNativeLibraryFileName()
{
if (OperatingSystem.IsWindows())
{
return "opus.dll";
}
if (OperatingSystem.IsLinux())
{
return "opus.so";
}
if (OperatingSystem.IsMacOS())
{
return "opus.dylib";
}
throw new PlatformNotSupportedException("Unsupported host operating system.");
}
}
}