public void ReadValueAsTest() { byte[] testData = new NbtFile(TestFiles.MakeValueTest()).SaveToBuffer(NbtCompression.None); var reader = new NbtReader(new MemoryStream(testData)); Assert.IsTrue(reader.ReadToFollowing()); // root Assert.IsTrue(reader.ReadToFollowing()); // byte Assert.AreEqual(1, reader.ReadValueAs <byte>()); Assert.IsTrue(reader.ReadToFollowing()); // short Assert.AreEqual(2, reader.ReadValueAs <short>()); Assert.IsTrue(reader.ReadToFollowing()); // int Assert.AreEqual(3, reader.ReadValueAs <int>()); Assert.IsTrue(reader.ReadToFollowing()); // long Assert.AreEqual(4L, reader.ReadValueAs <long>()); Assert.IsTrue(reader.ReadToFollowing()); // float Assert.AreEqual(5f, reader.ReadValueAs <float>()); Assert.IsTrue(reader.ReadToFollowing()); // double Assert.AreEqual(6d, reader.ReadValueAs <double>()); Assert.IsTrue(reader.ReadToFollowing()); // byteArray CollectionAssert.AreEqual(new byte[] { 10, 11, 12 }, reader.ReadValueAs <byte[]>()); Assert.IsTrue(reader.ReadToFollowing()); // intArray CollectionAssert.AreEqual(new[] { 20, 21, 22 }, reader.ReadValueAs <int[]>()); Assert.IsTrue(reader.ReadToFollowing()); // string Assert.AreEqual("123", reader.ReadValueAs <string>()); }
public void ReadValueTest() { byte[] testData = new NbtFile(TestFiles.MakeValueTest()).SaveToBuffer(NbtCompression.None); var reader = new NbtReader(new MemoryStream(testData)); Assert.IsTrue(reader.ReadToFollowing()); // root Assert.IsTrue(reader.ReadToFollowing()); // byte Assert.AreEqual(1, reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // short Assert.AreEqual(2, reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // int Assert.AreEqual(3, reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // long Assert.AreEqual(4L, reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // float Assert.AreEqual(5f, reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // double Assert.AreEqual(6d, reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // byteArray CollectionAssert.AreEqual(new byte[] { 10, 11, 12 }, (byte[])reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // intArray CollectionAssert.AreEqual(new[] { 20, 21, 22 }, (int[])reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // string Assert.AreEqual("123", reader.ReadValue()); // Skip to the very end and make sure that we can't read any more values reader.ReadToFollowing(); Assert.Throws <EndOfStreamException>(() => reader.ReadValue()); }
public void PartialReadTest() { TestFiles.RelocateTestFiles(); // read the whole thing as one tag, one byte at a time TestFiles.AssertValueTest(PartialReadTestInternal(new NbtFile(TestFiles.MakeValueTest()))); TestFiles.AssertNbtSmallFile(PartialReadTestInternal(TestFiles.MakeSmallFile())); TestFiles.AssertNbtBigFile(PartialReadTestInternal(new NbtFile(TestFiles.Big))); }
public void PartialBatchReadTest() { TestFiles.RelocateTestFiles(); // read the whole thing as one tag, in batches of 4 bytes // Verifies fix for https://github.com/fragmer/fNbt/issues/26 TestFiles.AssertValueTest(PartialReadTestInternal(new NbtFile(TestFiles.MakeValueTest()), 4)); TestFiles.AssertNbtSmallFile(PartialReadTestInternal(TestFiles.MakeSmallFile(), 4)); TestFiles.AssertNbtBigFile(PartialReadTestInternal(new NbtFile(TestFiles.Big), 4)); }
public void SkippingValuesInCompoundTest() { NbtCompound root = TestFiles.MakeValueTest(); NbtCompound nestedComp = TestFiles.MakeValueTest(); nestedComp.Name = "NestedComp"; root.Add(nestedComp); var file = new NbtFile(root); byte[] savedFile = file.SaveToBuffer(NbtCompression.None); file.LoadFromBuffer(savedFile, 0, savedFile.Length, NbtCompression.None, tag => false); Assert.AreEqual(0, file.RootTag.Count); }
public void ReadAsTagTest3() { // read values as tags byte[] testData = new NbtFile(TestFiles.MakeValueTest()).SaveToBuffer(NbtCompression.None); var reader = new NbtReader(new MemoryStream(testData)); var root = new NbtCompound("root"); // skip root reader.ReadToFollowing(); reader.ReadToFollowing(); while (!reader.IsAtStreamEnd) { root.Add(reader.ReadAsTag()); } TestFiles.AssertValueTest(new NbtFile(root)); }
public void ReadAsTagTest2() { // read the whole thing as one tag byte[] testData = new NbtFile(TestFiles.MakeValueTest()).SaveToBuffer(NbtCompression.None); { var reader = new NbtReader(new MemoryStream(testData)); var root = (NbtCompound)reader.ReadAsTag(); TestFiles.AssertValueTest(new NbtFile(root)); } { // Try the same thing but with end tag skipping disabled var reader = new NbtReader(new MemoryStream(testData)) { SkipEndTags = false }; var root = (NbtCompound)reader.ReadAsTag(); TestFiles.AssertValueTest(new NbtFile(root)); } }
public void WriteTagTest() { using (var ms = new MemoryStream()) { var writer = new NbtWriter(ms, "root"); { foreach (NbtTag tag in TestFiles.MakeValueTest().Tags) { writer.WriteTag(tag); } writer.EndCompound(); Assert.IsTrue(writer.IsDone); writer.Finish(); } ms.Position = 0; var file = new NbtFile(); long bytesRead = file.LoadFromBuffer(ms.ToArray(), 0, (int)ms.Length, NbtCompression.None); Assert.AreEqual(bytesRead, ms.Length); TestFiles.AssertValueTest(file); } }
public void CacheTagValuesTest() { byte[] testData = new NbtFile(TestFiles.MakeValueTest()).SaveToBuffer(NbtCompression.None); var reader = new NbtReader(new MemoryStream(testData)); Assert.IsFalse(reader.CacheTagValues); reader.CacheTagValues = true; Assert.IsTrue(reader.ReadToFollowing()); // root Assert.IsTrue(reader.ReadToFollowing()); // byte Assert.AreEqual(1, reader.ReadValue()); Assert.AreEqual(1, reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // short Assert.AreEqual(2, reader.ReadValue()); Assert.AreEqual(2, reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // int Assert.AreEqual(3, reader.ReadValue()); Assert.AreEqual(3, reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // long Assert.AreEqual(4L, reader.ReadValue()); Assert.AreEqual(4L, reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // float Assert.AreEqual(5f, reader.ReadValue()); Assert.AreEqual(5f, reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // double Assert.AreEqual(6d, reader.ReadValue()); Assert.AreEqual(6d, reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // byteArray CollectionAssert.AreEqual(new byte[] { 10, 11, 12 }, (byte[])reader.ReadValue()); CollectionAssert.AreEqual(new byte[] { 10, 11, 12 }, (byte[])reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // intArray CollectionAssert.AreEqual(new[] { 20, 21, 22 }, (int[])reader.ReadValue()); CollectionAssert.AreEqual(new[] { 20, 21, 22 }, (int[])reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // string Assert.AreEqual("123", reader.ReadValue()); Assert.AreEqual("123", reader.ReadValue()); }