TableTests.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. namespace Lua.Tests;
  2. public class TableTests
  3. {
  4. [Test]
  5. public void Test_Indexer()
  6. {
  7. var table = new LuaTable();
  8. table[1] = "foo";
  9. table["bar"] = 2;
  10. table[true] = "baz";
  11. Assert.That(table[1], Is.EqualTo(new LuaValue("foo")));
  12. Assert.That(table["bar"], Is.EqualTo(new LuaValue(2)));
  13. Assert.That(table[true], Is.EqualTo(new LuaValue("baz")));
  14. }
  15. [Test]
  16. public void Test_EnsureCapacity()
  17. {
  18. var table = new LuaTable(2, 2);
  19. table[32] = 10; // hash part
  20. for (int i = 1; i <= 31; i++)
  21. {
  22. table[i] = 10;
  23. }
  24. Assert.That(table[32], Is.EqualTo(new LuaValue(10)));
  25. }
  26. [Test]
  27. public void Test_RemoveAt()
  28. {
  29. var table = new LuaTable();
  30. table[1] = 1;
  31. table[2] = 2;
  32. table[3] = 3;
  33. var value = table.RemoveAt(2);
  34. Assert.That(value, Is.EqualTo(new LuaValue(2)));
  35. Assert.That(table[2], Is.EqualTo(new LuaValue(3)));
  36. }
  37. [Test]
  38. public void Test_TableResize()
  39. {
  40. var table = new LuaTable();
  41. int i = 1;
  42. int count = 10000;
  43. while (count > 0)
  44. {
  45. var key = i;
  46. table[key] = key;
  47. table[key * 2 - key / 2] = key;
  48. i += key;
  49. count--;
  50. }
  51. table[1] = 0;
  52. table[int.MaxValue - 1] = 0;
  53. Assert.That(table[1], Is.EqualTo(new LuaValue(0)));
  54. Assert.That(table[int.MaxValue - 1], Is.EqualTo(new LuaValue(0)));
  55. }
  56. }