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