Files
sbox-public/engine/Tests/Sandbox.Test.Unit/Data/code/fastpath/GenericReadonlyStruct.2.cs
Garry Newman d95169d8fa Unit Test Cleanup (#5064)
* Move all unit tests to Engine/Tests
* Fix Margin.EdgeSubtract
* Fix Capsule.Contains
* EnvironmentVariables.Remove if it's null
* Fixed ray trace never returning startedsolid
* Fix HistoryList.Navigate on empty list
* Fix GameObject.WorldPosition accepting NaNs
* Fix flex: initial expanding to the wrong grow/shrink
* Translation.TryConvert shouldn't throw on invalid enum strings
* Skip sound file tests on machines with no audio device
2026-06-12 13:23:50 +01:00

90 lines
1.6 KiB
C#

using System;
using System.IO;
using System.Collections.Generic;
namespace TestPackage;
public static class EnumValues<T> where T : Enum
{
private readonly static T[] s_Values;
public static int Length => s_Values.Length;
public static int IndexOf( T input )
{
var compare = EqualityComparer<T>.Default;
for ( var i = 0; i < s_Values.Length; i++ )
{
if ( compare.Equals( s_Values[i], input ) )
return i + 1;
}
throw new InvalidOperationException();
}
static EnumValues()
{
s_Values = (T[])typeof( T ).GetEnumValues();
}
}
public readonly struct SoundBank<T> where T : Enum
{
// Add support for flags!! - was the whole point of it!!!
private readonly string[] m_Sounds;
public SoundBank()
{
m_Sounds = new string[EnumValues<T>.Length];
}
public SoundBank( IReadOnlyDictionary<T, string> values ) : this()
{
foreach ( var (key, value) in values )
{
Assign( key, value );
}
}
public void Assign( T index, string sound )
{
sound = sound.ToUpper();
m_Sounds[EnumValues<T>.IndexOf( index )] = sound;
}
public string Play( T index )
{
var value = m_Sounds[EnumValues<T>.IndexOf( index )];
return value;
}
}
public enum ExampleEnum
{
Hello,
World,
Testing,
Another
}
public class Program : CompilingTests.IProgram
{
public SoundBank<ExampleEnum> ReadonlyStructField;
public int Main( StringWriter output )
{
ReadonlyStructField = new( new Dictionary<ExampleEnum, string>
{
{ ExampleEnum.Hello, "hello" },
{ ExampleEnum.World, "world" }
} );
output.WriteLine( ReadonlyStructField.Play( ExampleEnum.Hello ) );
output.WriteLine( ReadonlyStructField.Play( ExampleEnum.World ) );
return 0;
}
}