Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

FixedStringSizedTests.tt 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <#/*THIS IS A T4 FILE - see t4_text_templating.md for what it is and how to run codegen*/#>
  2. <#@ template debug="True" #>
  3. <#@ output extension=".gen.cs" encoding="utf-8"#>
  4. <#@ assembly name="System.Core" #>
  5. //------------------------------------------------------------------------------
  6. // <auto-generated>
  7. // This code was generated by a tool.
  8. //
  9. // TextTransform Samples/Packages/com.unity.collections/Unity.Collections.Tests/FixedStringSizedTests.tt
  10. //
  11. // Changes to this file may cause incorrect behavior and will be lost if
  12. // the code is regenerated.
  13. // </auto-generated>
  14. //------------------------------------------------------------------------------
  15. #if !UNITY_DOTSRUNTIME
  16. using System;
  17. using NUnit.Framework;
  18. using Unity.Collections;
  19. using Unity.Collections.Tests;
  20. #if !UNITY_DOTSRUNTIME // DOTS-Runtime doesn't support UnityEngine
  21. using UnityEngine;
  22. #endif
  23. namespace FixedStringTests
  24. {
  25. internal class FixedStringSizedTests
  26. {
  27. <#
  28. var SIZES = new int[] {32,64,128,512,4096};
  29. foreach(var BYTES in SIZES)
  30. {
  31. // 2 bytes ushort + 1 byte space for null termination
  32. var ALMOST_TOO_BIG = new String('o', BYTES - 3);
  33. var TOO_BIG = new String('o', BYTES - 2);
  34. #>
  35. #if !UNITY_DOTSRUNTIME // DOTS-Runtime doesn't support UnityEngine
  36. class ScriptableObjectFixedString<#=BYTES#>Bytes : UnityEngine.ScriptableObject
  37. {
  38. public FixedString<#=BYTES#>Bytes String;
  39. }
  40. [Test]
  41. public void FixedString<#=BYTES#>BytesSerializes()
  42. {
  43. var a = UnityEngine.ScriptableObject.CreateInstance<ScriptableObjectFixedString<#=BYTES#>Bytes>();
  44. a.String = "Hello World";
  45. var b = UnityEngine.Object.Instantiate(a);
  46. Assert.AreEqual(a.String, b.String);
  47. }
  48. #endif
  49. [TestCase("<#=ALMOST_TOO_BIG#>", FormatError.None, TestName="FixedString<#=BYTES#>AtMaximumSizeWorks_Almost")]
  50. [TestCase("<#=TOO_BIG#>", FormatError.Overflow, TestName="FixedString<#=BYTES#>AtMaximumSizeWorks_Over")]
  51. public void FixedString<#=BYTES#>BytesAtMaximumSizeWorks(String a, FormatError expectedError)
  52. {
  53. FixedString<#=BYTES#>Bytes aa = default;
  54. aa.Junk();
  55. var error = aa.Append(a);
  56. Assert.AreEqual(expectedError, error);
  57. if (expectedError == FormatError.None)
  58. aa.AssertNullTerminated();
  59. else
  60. Assert.AreEqual(0, aa.Length);
  61. }
  62. [Test]
  63. public unsafe void FixedString<#=BYTES#>BytesToFixedList()
  64. {
  65. FixedString<#=BYTES#>Bytes a = default;
  66. a.Junk();
  67. a.Append("0123");
  68. ref var aList = ref a.AsFixedList();
  69. Assert.IsFalse(aList.IsEmpty);
  70. Assert.AreEqual(4, aList.Length);
  71. Assert.AreEqual(a.Capacity + 1, aList.Capacity);
  72. Assert.AreEqual('0', aList[0]);
  73. Assert.AreEqual('1', aList[1]);
  74. Assert.AreEqual('2', aList[2]);
  75. Assert.AreEqual('3', aList[3]);
  76. aList.Add((byte)'4');
  77. Assert.AreEqual("01234", a);
  78. // because we modified it as a FixedList, it will not be null terminated!
  79. Assert.AreNotEqual(0, a.GetUnsafePtr()[a.Length]);
  80. }
  81. [TestCase("red")]
  82. [TestCase("紅色", TestName="{m}(Chinese-Red)")]
  83. [TestCase("George Washington")]
  84. [TestCase("村上春樹", TestName="{m}(HarukiMurakami)")]
  85. public unsafe void FixedString<#=BYTES#>BytesEqualsStringNoGC(string a)
  86. {
  87. FixedString<#=BYTES#>Bytes b = a;
  88. GCAllocRecorder.ValidateNoGCAllocs(() =>
  89. {
  90. b.Equals(a);
  91. });
  92. }
  93. <#
  94. foreach(var OTHERBYTES in SIZES)
  95. {
  96. if (OTHERBYTES != BYTES)
  97. {
  98. #>
  99. [TestCase("red")]
  100. [TestCase("紅色", TestName="{m}(Chinese-Red)")]
  101. [TestCase("George Washington")]
  102. [TestCase("村上春樹", TestName="{m}(HarukiMurakami)")]
  103. public void FixedString<#=BYTES#>BytesToFixedString<#=OTHERBYTES#>Works(String a)
  104. {
  105. var b = new FixedString<#=BYTES#>Bytes(a);
  106. var c = new FixedString<#=OTHERBYTES#>Bytes(b);
  107. String d = c.ToString();
  108. Assert.AreEqual(a, d);
  109. }
  110. <#
  111. }
  112. }
  113. }
  114. #>
  115. }
  116. }
  117. #endif