123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967 |
- using System;
- using NUnit.Framework;
- using Unity.Collections;
- using Unity.Collections.LowLevel.Unsafe;
- using System.Text;
- using Unity.Burst;
- using Unity.Jobs;
-
- namespace Unity.Collections.Tests
- {
- internal class NativeTextTests
- {
- [Test]
- public void NativeTextFixedStringCtors()
- {
- using (NativeText aa = new NativeText(new FixedString32Bytes("test32"), Allocator.Temp))
- {
- Assert.True(aa != new FixedString32Bytes("test"));
- Assert.True(aa.Value == "test32");
- Assert.AreEqual("test32", aa);
- }
-
- using (NativeText aa = new NativeText(new FixedString64Bytes("test64"), Allocator.Temp))
- {
- Assert.True(aa != new FixedString64Bytes("test"));
- Assert.True(aa.Value == "test64");
- Assert.AreEqual("test64", aa);
- }
-
- using (NativeText aa = new NativeText(new FixedString128Bytes("test128"), Allocator.Temp))
- {
- Assert.True(aa != new FixedString128Bytes("test"));
- Assert.True(aa.Value == "test128");
- Assert.AreEqual("test128", aa);
- }
-
- using (NativeText aa = new NativeText(new FixedString512Bytes("test512"), Allocator.Temp))
- {
- Assert.True(aa != new FixedString512Bytes("test"));
- Assert.True(aa.Value == "test512");
- Assert.AreEqual("test512", aa);
- }
-
- using (NativeText aa = new NativeText(new FixedString4096Bytes("test4096"), Allocator.Temp))
- {
- Assert.True(aa != new FixedString4096Bytes("test"));
- Assert.True(aa.Value == "test4096");
- Assert.AreEqual("test4096", aa);
- }
-
- using (NativeText aa = new NativeText("testString", Allocator.Temp))
- {
- var s = "testString";
- Assert.AreEqual(aa, s);
- Assert.True(aa.Value == "testString");
- Assert.AreEqual("testString", aa);
- }
- }
-
- [Test]
- public void NativeTextCorrectLengthAfterClear()
- {
- NativeText aa = new NativeText(4, Allocator.Temp);
- Assert.True(aa.IsCreated);
- Assert.AreEqual(0, aa.Length, "Length after creation is not 0");
- aa.AssertNullTerminated();
-
- aa.Junk();
-
- aa.Clear();
- Assert.AreEqual(0, aa.Length, "Length after clear is not 0");
- aa.AssertNullTerminated();
-
- aa.Dispose();
- }
-
- [Test]
- public void NativeTextFormatExtension1Params()
- {
- NativeText aa = new NativeText(4, Allocator.Temp);
- Assert.True(aa.IsCreated);
- aa.Junk();
- FixedString32Bytes format = "{0}";
- FixedString32Bytes arg0 = "a";
- aa.AppendFormat(format, arg0);
- aa.Add(0x61);
- Assert.AreEqual("aa", aa);
- aa.AssertNullTerminated();
- aa.Dispose();
- }
-
-
- [Test]
- public void NativeTextFormatExtension2Params()
- {
- NativeText aa = new NativeText(4, Allocator.Temp);
- aa.Junk();
- FixedString32Bytes format = "{0} {1}";
- FixedString32Bytes arg0 = "a";
- FixedString32Bytes arg1 = "b";
- aa.AppendFormat(format, arg0, arg1);
- Assert.AreEqual("a b", aa);
- aa.AssertNullTerminated();
- aa.Dispose();
- }
-
-
- [Test]
- public void NativeTextFormatExtension3Params()
- {
- NativeText aa = new NativeText(4, Allocator.Temp);
- aa.Junk();
- FixedString32Bytes format = "{0} {1} {2}";
- FixedString32Bytes arg0 = "a";
- FixedString32Bytes arg1 = "b";
- FixedString32Bytes arg2 = "c";
- aa.AppendFormat(format, arg0, arg1, arg2);
- Assert.AreEqual("a b c", aa);
- aa.AssertNullTerminated();
- aa.Dispose();
- }
-
-
- [Test]
- public void NativeTextFormatExtension4Params()
- {
- NativeText aa = new NativeText(Allocator.Temp);
- aa.Junk();
- FixedString32Bytes format = "{0} {1} {2} {3}";
- FixedString32Bytes arg0 = "a";
- FixedString32Bytes arg1 = "b";
- FixedString32Bytes arg2 = "c";
- FixedString32Bytes arg3 = "d";
- aa.AppendFormat(format, arg0, arg1, arg2, arg3);
- Assert.AreEqual("a b c d", aa);
- aa.AssertNullTerminated();
- aa.Dispose();
- }
-
-
- [Test]
- public void NativeTextFormatExtension5Params()
- {
- NativeText aa = new NativeText(4, Allocator.Temp);
- aa.Junk();
- FixedString32Bytes format = "{0} {1} {2} {3} {4}";
- FixedString32Bytes arg0 = "a";
- FixedString32Bytes arg1 = "b";
- FixedString32Bytes arg2 = "c";
- FixedString32Bytes arg3 = "d";
- FixedString32Bytes arg4 = "e";
- aa.AppendFormat(format, arg0, arg1, arg2, arg3, arg4);
- Assert.AreEqual("a b c d e", aa);
- aa.AssertNullTerminated();
- aa.Dispose();
- }
-
-
- [Test]
- public void NativeTextFormatExtension6Params()
- {
- NativeText aa = new NativeText(4, Allocator.Temp);
- aa.Junk();
- FixedString32Bytes format = "{0} {1} {2} {3} {4} {5}";
- FixedString32Bytes arg0 = "a";
- FixedString32Bytes arg1 = "b";
- FixedString32Bytes arg2 = "c";
- FixedString32Bytes arg3 = "d";
- FixedString32Bytes arg4 = "e";
- FixedString32Bytes arg5 = "f";
- aa.AppendFormat(format, arg0, arg1, arg2, arg3, arg4, arg5);
- Assert.AreEqual("a b c d e f", aa);
- aa.AssertNullTerminated();
- aa.Dispose();
- }
-
-
- [Test]
- public void NativeTextFormatExtension7Params()
- {
- NativeText aa = new NativeText(4, Allocator.Temp);
- aa.Junk();
- FixedString32Bytes format = "{0} {1} {2} {3} {4} {5} {6}";
- FixedString32Bytes arg0 = "a";
- FixedString32Bytes arg1 = "b";
- FixedString32Bytes arg2 = "c";
- FixedString32Bytes arg3 = "d";
- FixedString32Bytes arg4 = "e";
- FixedString32Bytes arg5 = "f";
- FixedString32Bytes arg6 = "g";
- aa.AppendFormat(format, arg0, arg1, arg2, arg3, arg4, arg5, arg6);
- Assert.AreEqual("a b c d e f g", aa);
- aa.AssertNullTerminated();
- aa.Dispose();
- }
-
-
- [Test]
- public void NativeTextFormatExtension8Params()
- {
- NativeText aa = new NativeText(4, Allocator.Temp);
- aa.Junk();
- FixedString128Bytes format = "{0} {1} {2} {3} {4} {5} {6} {7}";
- FixedString32Bytes arg0 = "a";
- FixedString32Bytes arg1 = "b";
- FixedString32Bytes arg2 = "c";
- FixedString32Bytes arg3 = "d";
- FixedString32Bytes arg4 = "e";
- FixedString32Bytes arg5 = "f";
- FixedString32Bytes arg6 = "g";
- FixedString32Bytes arg7 = "h";
- aa.AppendFormat(format, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
- Assert.AreEqual("a b c d e f g h", aa);
- aa.AssertNullTerminated();
- aa.Dispose();
- }
-
-
- [Test]
- public void NativeTextFormatExtension9Params()
- {
- NativeText aa = new NativeText(4, Allocator.Temp);
- aa.Junk();
- FixedString128Bytes format = "{0} {1} {2} {3} {4} {5} {6} {7} {8}";
- FixedString32Bytes arg0 = "a";
- FixedString32Bytes arg1 = "b";
- FixedString32Bytes arg2 = "c";
- FixedString32Bytes arg3 = "d";
- FixedString32Bytes arg4 = "e";
- FixedString32Bytes arg5 = "f";
- FixedString32Bytes arg6 = "g";
- FixedString32Bytes arg7 = "h";
- FixedString32Bytes arg8 = "i";
- aa.AppendFormat(format, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
- Assert.AreEqual("a b c d e f g h i", aa);
- aa.AssertNullTerminated();
- aa.Dispose();
- }
-
-
- [Test]
- public void NativeTextFormatExtension10Params()
- {
- NativeText aa = new NativeText(4, Allocator.Temp);
- aa.Junk();
- FixedString128Bytes format = "{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}";
- FixedString32Bytes arg0 = "a";
- FixedString32Bytes arg1 = "b";
- FixedString32Bytes arg2 = "c";
- FixedString32Bytes arg3 = "d";
- FixedString32Bytes arg4 = "e";
- FixedString32Bytes arg5 = "f";
- FixedString32Bytes arg6 = "g";
- FixedString32Bytes arg7 = "h";
- FixedString32Bytes arg8 = "i";
- FixedString32Bytes arg9 = "j";
- aa.AppendFormat(format, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
- Assert.AreEqual("a b c d e f g h i j", aa);
- aa.AssertNullTerminated();
- aa.Dispose();
- }
-
- [Test]
- public void NativeTextAppendGrows()
- {
- NativeText aa = new NativeText(1, Allocator.Temp);
- var origCapacity = aa.Capacity;
- for (int i = 0; i < origCapacity; ++i)
- aa.Append('a');
- Assert.AreEqual(origCapacity, aa.Capacity);
- aa.Append('b');
- Assert.GreaterOrEqual(aa.Capacity, origCapacity);
- Assert.AreEqual(new String('a', origCapacity) + "b", aa.ToString());
- aa.Dispose();
- }
-
- [Test]
- public void NativeTextAppendString()
- {
- NativeText aa = new NativeText(4, Allocator.Temp);
- aa.Append("aa");
- Assert.AreEqual("aa", aa.ToString());
- aa.Append("bb");
- Assert.AreEqual("aabb", aa.ToString());
- aa.Dispose();
- }
-
-
- [TestCase("Antidisestablishmentarianism")]
- [TestCase("🌹🌻🌷🌿🌵🌾")]
- public void NativeTextCopyFromBytesWorks(String a)
- {
- NativeText aa = new NativeText(4, Allocator.Temp);
- aa.Junk();
- var utf8 = Encoding.UTF8.GetBytes(a);
- unsafe
- {
- fixed (byte* b = utf8)
- aa.Append(b, (ushort) utf8.Length);
- }
-
- Assert.AreEqual(a, aa.ToString());
- aa.AssertNullTerminated();
-
- aa.Append("tail");
- Assert.AreEqual(a + "tail", aa.ToString());
- aa.AssertNullTerminated();
-
- aa.Dispose();
- }
-
- [TestCase("red")]
- [TestCase("紅色", TestName = "{m}(Chinese-Red)")]
- [TestCase("George Washington")]
- [TestCase("村上春樹", TestName = "{m}(HarukiMurakami)")]
- public void NativeTextToStringWorks(String a)
- {
- NativeText aa = new NativeText(4, Allocator.Temp);
- aa.Append(new FixedString128Bytes(a));
- Assert.AreEqual(a, aa.ToString());
- aa.AssertNullTerminated();
- aa.Dispose();
- }
-
- [TestCase("monkey", "monkey")]
- [TestCase("yellow", "green")]
- [TestCase("violet", "紅色", TestName = "{m}(Violet-Chinese-Red")]
- [TestCase("绿色", "蓝色", TestName = "{m}(Chinese-Green-Blue")]
- [TestCase("靛蓝色", "紫罗兰色", TestName = "{m}(Chinese-Indigo-Violet")]
- [TestCase("James Monroe", "John Quincy Adams")]
- [TestCase("Andrew Jackson", "村上春樹", TestName = "{m}(AndrewJackson-HarukiMurakami")]
- [TestCase("三島 由紀夫", "吉本ばなな", TestName = "{m}(MishimaYukio-YoshimotoBanana")]
- public void NativeTextEqualsWorks(String a, String b)
- {
- using (var aa = new NativeText(new FixedString128Bytes(a), Allocator.Temp))
- using (var bb = new NativeText(new FixedString128Bytes(b), Allocator.Temp))
- {
- Assert.AreEqual(aa.Equals(bb), a.Equals(b));
- aa.AssertNullTerminated();
- bb.AssertNullTerminated();
- }
- }
-
- [Test]
- public void NativeTextForEach()
- {
- NativeText actual = new NativeText("A🌕Z🌑D𒁃 𒁃𒁃𒁃𒁃𒁃", Allocator.Temp);
- FixedList128Bytes<uint> expected = default;
- expected.Add('A');
- expected.Add(0x1F315);
- expected.Add('Z');
- expected.Add(0x1F311);
- expected.Add('D');
- expected.Add(0x12043);
- expected.Add(' ');
- expected.Add(0x12043);
- expected.Add(0x12043);
- expected.Add(0x12043);
- expected.Add(0x12043);
- expected.Add(0x12043);
- int index = 0;
- foreach (var rune in actual)
- {
- Assert.AreEqual(expected[index], rune.value);
- ++index;
- }
-
- actual.Dispose();
- }
-
- [Test]
- public void NativeTextNSubstring()
- {
- NativeText a = new NativeText("This is substring.", Allocator.Temp);
-
- #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
- Assert.Throws<ArgumentOutOfRangeException>(() => a.Substring(-8, 9));
- Assert.Throws<ArgumentOutOfRangeException>(() => a.Substring(200, 9));
- Assert.Throws<ArgumentOutOfRangeException>(() => a.Substring(8, -9));
- #endif
-
- {
- NativeText b = a.Substring(8, 9);
- Assert.IsTrue(b.Equals("substring"));
- }
-
- {
- NativeText b = a.Substring(8, 100);
- Assert.IsTrue(b.Equals("substring."));
- }
- }
-
- [Test]
- public void NativeTextIndexOf()
- {
- NativeText a = new NativeText("bookkeeper bookkeeper", Allocator.Temp);
- NativeText b = new NativeText("ookkee", Allocator.Temp);
- Assert.AreEqual(1, a.IndexOf(b));
- Assert.AreEqual(-1, b.IndexOf(a));
- a.Dispose();
- b.Dispose();
- }
-
- [Test]
- public void NativeTextLastIndexOf()
- {
- NativeText a = new NativeText("bookkeeper bookkeeper", Allocator.Temp);
- NativeText b = new NativeText("ookkee", Allocator.Temp);
- Assert.AreEqual(12, a.LastIndexOf(b));
- Assert.AreEqual(-1, b.LastIndexOf(a));
- a.Dispose();
- b.Dispose();
- }
-
- [Test]
- public void NativeTextContains()
- {
- NativeText a = new NativeText("bookkeeper", Allocator.Temp);
- NativeText b = new NativeText("ookkee", Allocator.Temp);
- Assert.AreEqual(true, a.Contains(b));
- a.Dispose();
- b.Dispose();
- }
-
- [Test]
- public void NativeTextComparisons()
- {
- using (var a = new NativeText("apple", Allocator.Temp))
- using (var b = new NativeText("banana", Allocator.Temp))
- {
- Assert.AreEqual(false, a.Equals(b));
- Assert.AreEqual(true, !b.Equals(a));
- }
- }
-
- [Test]
- public void NativeTextCustomAllocatorTest()
- {
- AllocatorManager.Initialize();
- var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
- ref var allocator = ref allocatorHelper.Allocator;
- allocator.Initialize();
-
- using (var container = new NativeText(allocator.Handle))
- {
- }
-
- Assert.IsTrue(allocator.WasUsed);
- allocator.Dispose();
- allocatorHelper.Dispose();
- AllocatorManager.Shutdown();
- }
-
- [BurstCompile]
- struct BurstedCustomAllocatorJob : IJob
- {
- [NativeDisableUnsafePtrRestriction]
- public unsafe CustomAllocatorTests.CountingAllocator* Allocator;
-
- public void Execute()
- {
- unsafe
- {
- using (var container = new NativeText(Allocator->Handle))
- {
- }
- }
- }
- }
-
- [Test]
- public unsafe void NativeTextBurstedCustomAllocatorTest()
- {
- AllocatorManager.Initialize();
- var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
- ref var allocator = ref allocatorHelper.Allocator;
- allocator.Initialize();
-
- var allocatorPtr = (CustomAllocatorTests.CountingAllocator*)UnsafeUtility.AddressOf<CustomAllocatorTests.CountingAllocator>(ref allocator);
- unsafe
- {
- var handle = new BurstedCustomAllocatorJob {Allocator = allocatorPtr}.Schedule();
- handle.Complete();
- }
-
- Assert.IsTrue(allocator.WasUsed);
- allocator.Dispose();
- allocatorHelper.Dispose();
- AllocatorManager.Shutdown();
- }
-
- [Test]
- public void NativeTextIsEmpty()
- {
- var a = new NativeText("", Allocator.Temp);
- Assert.IsTrue(a.IsEmpty);
- a.CopyFrom("hello");
- Assert.IsFalse(a.IsEmpty);
- a.Dispose();
- }
-
- [Test]
- public void NativeTextIsEmptyReturnsTrueForNotConstructed()
- {
- NativeText a = default;
- Assert.IsTrue(a.IsEmpty);
- }
-
- [Test]
- public void NativeTextReadonlyCtor()
- {
- using (NativeText aa = new NativeText(new FixedString32Bytes("test32"), Allocator.Temp))
- {
- var ro = aa.AsReadOnly();
- Assert.True(ro != new FixedString32Bytes("test"));
- Assert.True(ro.Value == "test32");
- Assert.AreEqual("test32", ro);
- }
-
- using (NativeText aa = new NativeText(new FixedString64Bytes("test64"), Allocator.Temp))
- {
- var ro = aa.AsReadOnly();
- Assert.True(ro != new FixedString64Bytes("test"));
- Assert.True(ro.Value == "test64");
- Assert.AreEqual("test64", ro);
- }
-
- using (NativeText aa = new NativeText(new FixedString128Bytes("test128"), Allocator.Temp))
- {
- var ro = aa.AsReadOnly();
- Assert.True(ro != new FixedString128Bytes("test"));
- Assert.True(ro.Value == "test128");
- Assert.AreEqual("test128", ro);
- }
-
- using (NativeText aa = new NativeText(new FixedString512Bytes("test512"), Allocator.Temp))
- {
- var ro = aa.AsReadOnly();
- Assert.True(ro != new FixedString512Bytes("test"));
- Assert.True(ro.Value == "test512");
- Assert.AreEqual("test512", ro);
- }
-
- using (NativeText aa = new NativeText(new FixedString4096Bytes("test4096"), Allocator.Temp))
- {
- var ro = aa.AsReadOnly();
- Assert.True(ro != new FixedString4096Bytes("test"));
- Assert.True(ro.Value == "test4096");
- Assert.AreEqual("test4096", ro);
- }
-
- using (var aa = new NativeText("Hello", Allocator.Temp))
- {
- var ro = aa.AsReadOnly();
-
- Assert.AreEqual(aa.Equals(ro), ro.Equals(aa));
- aa.AssertNullTerminated();
- ro.AssertNullTerminated();
- }
- }
-
- [TestCase("monkey", "monkey")]
- [TestCase("yellow", "green")]
- [TestCase("violet", "紅色", TestName = "{m}(Violet-Chinese-Red")]
- [TestCase("绿色", "蓝色", TestName = "{m}(Chinese-Green-Blue")]
- [TestCase("靛蓝色", "紫罗兰色", TestName = "{m}(Chinese-Indigo-Violet")]
- [TestCase("James Monroe", "John Quincy Adams")]
- [TestCase("Andrew Jackson", "村上春樹", TestName = "{m}(AndrewJackson-HarukiMurakami")]
- [TestCase("三島 由紀夫", "吉本ばなな", TestName = "{m}(MishimaYukio-YoshimotoBanana")]
- public void NativeTextReadOnlyEqualsWorks(String a, String b)
- {
- using (var aa = new NativeText(new FixedString128Bytes(a), Allocator.Temp))
- using (var bb = new NativeText(new FixedString128Bytes(b), Allocator.Temp))
- {
- var aaRO = aa.AsReadOnly();
- var bbRO = bb.AsReadOnly();
- Assert.AreEqual(aaRO.Equals(bbRO), a.Equals(b));
- aaRO.AssertNullTerminated();
- bbRO.AssertNullTerminated();
- }
- }
-
- [Test]
- public void NativeTextReadOnlyIndexOf()
- {
- using (var a = new NativeText("bookkeeper bookkeeper", Allocator.Temp))
- using (var b = new NativeText("ookkee", Allocator.Temp))
- {
- var aRO = a.AsReadOnly();
- var bRO = b.AsReadOnly();
- Assert.AreEqual(1, aRO.IndexOf(bRO));
- Assert.AreEqual(-1, bRO.IndexOf(aRO));
- }
- }
-
- [Test]
- public void NativeTextReadOnlyLastIndexOf()
- {
- using (var a = new NativeText("bookkeeper bookkeeper", Allocator.Temp))
- using (var b = new NativeText("ookkee", Allocator.Temp))
- {
- var aRO = a.AsReadOnly();
- var bRO = b.AsReadOnly();
- Assert.AreEqual(12, aRO.LastIndexOf(bRO));
- Assert.AreEqual(-1, bRO.LastIndexOf(aRO));
- }
- }
-
- [Test]
- public void NativeTextReadOnlyContains()
- {
- using (var a = new NativeText("bookkeeper", Allocator.Temp))
- using (var b = new NativeText("ookkee", Allocator.Temp))
- {
- var aRO = a.AsReadOnly();
- var bRO = b.AsReadOnly();
- Assert.AreEqual(true, aRO.Contains(bRO));
- }
- }
-
- [Test]
- public void NativeTextReadOnlyComparisons()
- {
- using (var a = new NativeText("apple", Allocator.Temp))
- using (var b = new NativeText("banana", Allocator.Temp))
- {
- var aRO = a.AsReadOnly();
- var bRO = b.AsReadOnly();
- Assert.AreEqual(false, aRO.Equals(bRO));
- Assert.AreEqual(true, !bRO.Equals(aRO));
- }
- }
-
- [Test]
- public void NativeTextReadOnlyMakeMoreThanOne()
- {
- using (var a = new NativeText("apple", Allocator.Temp))
- {
- var aRO1 = a.AsReadOnly();
- var aRO2 = a.AsReadOnly();
- Assert.IsTrue(a.Equals(aRO1));
- Assert.IsTrue(aRO1.Equals(aRO2));
- Assert.IsTrue(aRO2.Equals(aRO1));
- }
- }
-
- [Test]
- public void NativeTextReadOnlyIsNotACopy()
- {
- using (var a = new NativeText("apple", Allocator.Temp))
- {
- var aRO = a.AsReadOnly();
- Assert.AreEqual(true, aRO.Equals(a));
-
- unsafe
- {
- Assert.IsTrue(aRO.GetUnsafePtr() == a.GetUnsafePtr());
- }
- }
- }
-
-
- [Test]
- public void NativeTextReadOnlyIsEmpty()
- {
- var a = new NativeText("", Allocator.Temp);
- var aRO = a.AsReadOnly();
- Assert.IsTrue(aRO.IsEmpty);
- a.Dispose();
-
- a = new NativeText("not empty", Allocator.Temp);
- aRO = a.AsReadOnly();
- Assert.IsFalse(aRO.IsEmpty);
- a.Dispose();
- }
-
- [Test]
- public void NativeTextReadOnlyIsEmptyReturnsTrueOrThrowsForNotConstructed()
- {
- NativeText.ReadOnly aRO = default;
- Assert.IsTrue(aRO.IsEmpty);
-
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- NativeText aa = default;
- NativeText.ReadOnly aaRO = aa.AsReadOnly();
- Assert.IsTrue(aaRO.IsEmpty);
- #endif
- }
-
- [Test]
- [TestRequiresDotsDebugOrCollectionChecks]
- public void NativeTextReadOnlyIsNotWritable()
- {
- using (var a = new NativeText("apple", Allocator.Temp))
- {
- var aRO = a.AsReadOnly();
- Assert.AreEqual(true, aRO.Equals(a));
-
- Assert.Throws<NotSupportedException>(() => { aRO.IsEmpty = true; });
- Assert.Throws<NotSupportedException>(() => { aRO.Length = 0; });
- Assert.Throws<NotSupportedException>(() => { aRO.Capacity = 0; });
- Assert.Throws<NotSupportedException>(() => { aRO.ElementAt(0); });
- Assert.Throws<NotSupportedException>(() => { aRO.Clear(); });
- Assert.Throws<NotSupportedException>(() => { aRO.Append("won't work"); });
- FixedString32Bytes format = "{0}";
- FixedString32Bytes arg0 = "a";
- Assert.Throws<NotSupportedException>(() => { aRO.AppendFormat(format, arg0); });
- Assert.Throws<NotSupportedException>(() => { aRO.AppendRawByte(1); });
- Assert.Throws<NotSupportedException>(() => { aRO.TryResize(0); });
- }
- }
-
- [Test]
- public void NativeTextReadOnlyCannotBeUsedAfterSourceIsDisposed()
- {
- AllocatorManager.Initialize();
- using (var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent))
- {
- ref var allocator = ref allocatorHelper.Allocator;
- allocator.Initialize();
-
- var a = new NativeText("Keep it secret, ", allocator.Handle);
- var ro = a.AsReadOnly();
- a.Dispose(); // Invalidate the string we are referring to
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- Assert.Throws<ObjectDisposedException>(() => { UnityEngine.Debug.Log(ro.ToString()); });
- #endif
-
- Assert.IsTrue(allocator.WasUsed);
- allocator.Dispose();
- }
-
- AllocatorManager.Shutdown();
- }
-
- [Test]
- public void NativeTextReadOnlyCannotBeUsedAfterSourceIsChanged()
- {
- AllocatorManager.Initialize();
- using (var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent))
- {
- ref var allocator = ref allocatorHelper.Allocator;
- allocator.Initialize();
-
- var a = new NativeText("Keep it secret, ", allocator.Handle);
- var ro = a.AsReadOnly();
-
- a.Clear(); // Change the string we are referring to
-
- Assert.DoesNotThrow(() => { UnityEngine.Debug.Log(ro.ToString()); });
-
- a.Dispose();
-
- #if ENABLE_UNITY_COLLECTIONS_CHECKS
- Assert.Throws<ObjectDisposedException>(() => { UnityEngine.Debug.Log(ro.ToString()); });
- #endif
-
- Assert.IsTrue(allocator.WasUsed);
- allocator.Dispose();
- }
-
- AllocatorManager.Shutdown();
- }
-
-
- [Test]
- [TestRequiresCollectionChecks]
- public void NativeTextReadOnlyModificationDuringEnumerationThrows()
- {
- AllocatorManager.Initialize();
- using (var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent))
- {
- ref var allocator = ref allocatorHelper.Allocator;
- allocator.Initialize();
-
- var a = new NativeText("Keep it secret, ", allocator.Handle);
- var ro = a.AsReadOnly();
-
- int iterations = 0;
- Assert.Throws<ObjectDisposedException>(() =>
- {
- // Mutate the source string while iterating. Can append once but when we use the iterator
- // again it will now be invalid and should throw
- foreach (var c in ro)
- {
- iterations++;
- a.Append("keep it safe, ");
- }
- });
- Assert.AreEqual(1, iterations);
-
- a.Dispose();
-
- Assert.IsTrue(allocator.WasUsed);
- allocator.Dispose();
- }
-
- AllocatorManager.Shutdown();
- }
-
- struct TestWriteOfTextMappedToReadOnly : IJob
- {
- public NativeText Text;
- public void Execute() { }
- }
-
- struct TestClearTextMappedToReadOnly : IJob
- {
- public NativeText Text;
- public void Execute()
- {
- Text.Clear();
- }
- }
-
- [Test]
- [TestRequiresCollectionChecks]
- public void NativeTextReadOnlyCannotScheduledSourceTextForWrite()
- {
- AllocatorManager.Initialize();
- using (var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent))
- {
- ref var allocator = ref allocatorHelper.Allocator;
- allocator.Initialize();
-
- var a = new NativeText("Keep it secret, ", allocator.Handle);
- var ro = a.AsReadOnly();
- var job = new TestWriteOfTextMappedToReadOnly
- {
- Text = a
- };
-
- var handle = job.Schedule();
-
- Assert.Throws<InvalidOperationException>(() =>
- {
- UnityEngine.Debug.Log(ro.ToString());
- });
-
- Assert.Throws<InvalidOperationException>(() =>
- {
- a.Dispose();
- });
- handle.Complete();
-
- a.Dispose();
-
- Assert.IsTrue(allocator.WasUsed);
- allocator.Dispose();
- }
-
- AllocatorManager.Shutdown();
- }
-
- [Test]
- public void NativeTextReadOnlyCanReadFromSourceTextModifiedInJob()
- {
- AllocatorManager.Initialize();
- using (var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent))
- {
- ref var allocator = ref allocatorHelper.Allocator;
- allocator.Initialize();
-
- var a = new NativeText("Keep it secret, ", allocator.Handle);
- var ro = a.AsReadOnly();
- var job = new TestClearTextMappedToReadOnly
- {
- Text = a
- };
-
- var handle = job.Schedule();
- handle.Complete();
-
- Assert.DoesNotThrow(() =>
- {
- UnityEngine.Debug.Log(ro.ToString());
- });
-
- a.Dispose();
-
- Assert.IsTrue(allocator.WasUsed);
- allocator.Dispose();
- }
-
- AllocatorManager.Shutdown();
- }
-
- struct TestReadOfTextMappedToReadOnly : IJob
- {
- [ReadOnly]
- public NativeText Text;
- public void Execute() { }
- }
-
- [Test]
- public void NativeTextReadOnlyCanScheduledSourceTextForRead()
- {
- AllocatorManager.Initialize();
- using (var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent))
- {
- ref var allocator = ref allocatorHelper.Allocator;
- allocator.Initialize();
-
- var a = new NativeText("Keep it secret, ", allocator.Handle);
- var ro = a.AsReadOnly();
- var job = new TestReadOfTextMappedToReadOnly
- {
- Text = a
- };
-
- Assert.DoesNotThrow(() =>
- {
- job.Schedule().Complete();
- });
-
- a.Dispose();
-
- Assert.IsTrue(allocator.WasUsed);
- allocator.Dispose();
- }
-
- AllocatorManager.Shutdown();
- }
-
- struct TestReadFromReadOnly : IJob
- {
- [ReadOnly] public NativeText.ReadOnly RO;
- public void Execute()
- {
- UnityEngine.Debug.Log($"{RO.Length}");
- }
- }
-
- [Test]
- [TestRequiresCollectionChecks]
- public void NativeTextReadOnlyThrowWhenUsingReadOnlyInJobAfterSourceHasBeenDisposed()
- {
- AllocatorManager.Initialize();
- using (var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent))
- {
- ref var allocator = ref allocatorHelper.Allocator;
- allocator.Initialize();
-
- var a = new NativeText("Keep it secret, ", allocator.Handle);
- var job = new TestReadFromReadOnly
- {
- RO = a.AsReadOnly()
- };
-
- var handle = job.Schedule();
-
- Assert.Throws<InvalidOperationException>(() =>
- {
- a.Dispose();
- });
-
- handle.Complete();
- a.Dispose();
-
- Assert.IsTrue(allocator.WasUsed);
- allocator.Dispose();
- }
-
- AllocatorManager.Shutdown();
- }
- }
- }
|