Files
sbox-public/engine/Sandbox.Test/Network/netDictionary.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

70 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Text.Json;
namespace networking;
[TestClass]
public class netDictionary
{
[TestMethod]
public void AddRemoveAndCount()
{
var dictionary = new NetDictionary<string, int>();
Assert.IsTrue( dictionary.Count == 0 );
dictionary.Add( "foo", 0 );
Assert.IsTrue( dictionary.Count == 1 );
Assert.AreEqual( dictionary["foo"], 0 );
Assert.IsTrue( dictionary.ContainsKey( "foo" ) );
dictionary.Remove( "foo" );
Assert.IsTrue( dictionary.Count == 0 );
}
[TestMethod]
public void Iterate()
{
var dictionary = new NetDictionary<string, int>();
dictionary.Add( "a", 1 );
dictionary.Add( "b", 2 );
dictionary.Add( "c", 3 );
var current = 0;
foreach ( var (k, v) in dictionary )
{
var testKey = string.Empty;
if ( current == 0 )
testKey = "a";
else if ( current == 1 )
testKey = "b";
else if ( current == 2 )
testKey = "c";
Assert.AreEqual( k, testKey );
Assert.AreEqual( v, current + 1 );
current++;
}
Assert.AreEqual( 3, current );
Assert.AreEqual( 1, dictionary["a"] );
Assert.AreEqual( 2, dictionary["b"] );
Assert.AreEqual( 3, dictionary["c"] );
}
[TestMethod]
public void ValidAccess()
{
var dictionary = new NetDictionary<string, int>();
Assert.ThrowsException<KeyNotFoundException>( () =>
{
var _ = dictionary["a"];
} );
}
}