Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

FixedStringSizedTests.tt 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. using System;
  16. using NUnit.Framework;
  17. using Unity.Collections;
  18. using Unity.Collections.Tests;
  19. using UnityEngine;
  20. namespace Unity.Collections.Tests
  21. {
  22. internal class FixedStringSizedTests
  23. {
  24. <#
  25. var SIZES = new int[] {32,64,128,512,4096};
  26. foreach(var BYTES in SIZES)
  27. {
  28. // 2 bytes ushort + 1 byte space for null termination
  29. var ALMOST_TOO_BIG = new String('o', BYTES - 3);
  30. var TOO_BIG = new String('o', BYTES - 2);
  31. #>
  32. class ScriptableObjectFixedString<#=BYTES#>Bytes : UnityEngine.ScriptableObject
  33. {
  34. public FixedString<#=BYTES#>Bytes String;
  35. }
  36. [Test]
  37. public void FixedString<#=BYTES#>BytesSerializes()
  38. {
  39. var a = UnityEngine.ScriptableObject.CreateInstance<ScriptableObjectFixedString<#=BYTES#>Bytes>();
  40. a.String = "Hello World";
  41. var b = UnityEngine.Object.Instantiate(a);
  42. Assert.AreEqual(a.String, b.String);
  43. }
  44. [TestCase("<#=ALMOST_TOO_BIG#>", FormatError.None, TestName="FixedString<#=BYTES#>AtMaximumSizeWorks_Almost")]
  45. [TestCase("<#=TOO_BIG#>", FormatError.Overflow, TestName="FixedString<#=BYTES#>AtMaximumSizeWorks_Over")]
  46. public void FixedString<#=BYTES#>BytesAtMaximumSizeWorks(String a, FormatError expectedError)
  47. {
  48. FixedString<#=BYTES#>Bytes aa = default;
  49. aa.Junk();
  50. var error = aa.Append(a);
  51. Assert.AreEqual(expectedError, error);
  52. if (expectedError == FormatError.None)
  53. aa.AssertNullTerminated();
  54. else
  55. Assert.AreEqual(0, aa.Length);
  56. }
  57. [Test]
  58. public unsafe void FixedString<#=BYTES#>BytesToFixedList()
  59. {
  60. FixedString<#=BYTES#>Bytes a = default;
  61. a.Junk();
  62. a.Append("0123");
  63. ref var aList = ref a.AsFixedList();
  64. Assert.IsFalse(aList.IsEmpty);
  65. Assert.AreEqual(4, aList.Length);
  66. Assert.AreEqual(a.Capacity + 1, aList.Capacity);
  67. Assert.AreEqual('0', aList[0]);
  68. Assert.AreEqual('1', aList[1]);
  69. Assert.AreEqual('2', aList[2]);
  70. Assert.AreEqual('3', aList[3]);
  71. aList.Add((byte)'4');
  72. Assert.AreEqual("01234", a);
  73. // because we modified it as a FixedList, it will not be null terminated!
  74. Assert.AreNotEqual(0, a.GetUnsafePtr()[a.Length]);
  75. }
  76. [TestCase("red")]
  77. [TestCase("紅色", TestName="{m}(Chinese-Red)")]
  78. [TestCase("George Washington")]
  79. [TestCase("村上春樹", TestName="{m}(HarukiMurakami)")]
  80. public unsafe void FixedString<#=BYTES#>BytesEqualsStringNoGC(string a)
  81. {
  82. FixedString<#=BYTES#>Bytes b = a;
  83. GCAllocRecorder.ValidateNoGCAllocs(() =>
  84. {
  85. b.Equals(a);
  86. });
  87. }
  88. <#
  89. foreach(var OTHERBYTES in SIZES)
  90. {
  91. if (OTHERBYTES != BYTES)
  92. {
  93. #>
  94. [TestCase("red")]
  95. [TestCase("紅色", TestName="{m}(Chinese-Red)")]
  96. [TestCase("George Washington")]
  97. [TestCase("村上春樹", TestName="{m}(HarukiMurakami)")]
  98. public void FixedString<#=BYTES#>BytesToFixedString<#=OTHERBYTES#>Works(String a)
  99. {
  100. var b = new FixedString<#=BYTES#>Bytes(a);
  101. var c = new FixedString<#=OTHERBYTES#>Bytes(b);
  102. String d = c.ToString();
  103. Assert.AreEqual(a, d);
  104. }
  105. <#
  106. }
  107. }
  108. }
  109. #>
  110. }
  111. }