Beispiel #1
0
        public void ReadListAsArray()
        {
            NbtCompound intList = TestFiles.MakeListTest();

            var ms = new MemoryStream();

            new NbtFile(intList).SaveToStream(ms, NbtCompression.None);
            ms.Seek(0, SeekOrigin.Begin);
            var reader = new NbtReader(ms);

            // attempt to read value before we're in a list
            Assert.Throws <InvalidOperationException>(() => reader.ReadListAsArray <int>());

            // test byte values
            reader.ReadToFollowing("ByteList");
            byte[] bytes = reader.ReadListAsArray <byte>();
            CollectionAssert.AreEqual(new byte[] { 100, 20, 3 }, bytes);

            // test double values
            reader.ReadToFollowing("DoubleList");
            double[] doubles = reader.ReadListAsArray <double>();
            CollectionAssert.AreEqual(new[] { 1d, 2000d, -3000000d }, doubles);

            // test float values
            reader.ReadToFollowing("FloatList");
            float[] floats = reader.ReadListAsArray <float>();
            CollectionAssert.AreEqual(new[] { 1f, 2000f, -3000000f }, floats);

            // test int values
            reader.ReadToFollowing("IntList");
            int[] ints = reader.ReadListAsArray <int>();
            CollectionAssert.AreEqual(new[] { 1, 2000, -3000000 }, ints);

            // test long values
            reader.ReadToFollowing("LongList");
            long[] longs = reader.ReadListAsArray <long>();
            CollectionAssert.AreEqual(new[] { 1L, 2000L, -3000000L }, longs);

            // test short values
            reader.ReadToFollowing("ShortList");
            short[] shorts = reader.ReadListAsArray <short>();
            CollectionAssert.AreEqual(new short[] { 1, 200, -30000 }, shorts);

            // test short values
            reader.ReadToFollowing("StringList");
            string[] strings = reader.ReadListAsArray <string>();
            CollectionAssert.AreEqual(new[] { "one", "two thousand", "negative three million" }, strings);

            // try reading list of compounds (should fail)
            reader.ReadToFollowing("CompoundList");
            Assert.Throws <InvalidOperationException>(() => reader.ReadListAsArray <NbtCompound>());

            // skip to the end of the stream
            while (reader.ReadToFollowing())
            {
            }
            Assert.Throws <EndOfStreamException>(() => reader.ReadListAsArray <int>());
        }
Beispiel #2
0
        public void ErrorTest()
        {
            var root = new NbtCompound("root");

            byte[] testData = new NbtFile(root).SaveToBuffer(NbtCompression.None);

            // creating NbtReader without a stream, or with a non-readable stream
            Assert.Throws <ArgumentNullException>(() => new NbtReader(null));
            Assert.Throws <ArgumentException>(() => new NbtReader(new NonReadableStream()));

            // corrupt the data
            testData[0] = 123;
            var reader = new NbtReader(new MemoryStream(testData));

            // attempt to use ReadValue when not at value
            Assert.Throws <InvalidOperationException>(() => reader.ReadValue());
            reader.CacheTagValues = true;
            Assert.Throws <InvalidOperationException>(() => reader.ReadValue());

            // attempt to read a corrupt stream
            Assert.Throws <NbtFormatException>(() => reader.ReadToFollowing());

            // make sure we've properly entered the error state
            Assert.IsTrue(reader.IsInErrorState);
            Assert.IsFalse(reader.HasName);
            Assert.Throws <InvalidReaderStateException>(() => reader.ReadToFollowing());
            Assert.Throws <InvalidReaderStateException>(() => reader.ReadListAsArray <int>());
            Assert.Throws <InvalidReaderStateException>(() => reader.ReadToNextSibling());
            Assert.Throws <InvalidReaderStateException>(() => reader.ReadToDescendant("derp"));
            Assert.Throws <InvalidReaderStateException>(() => reader.ReadAsTag());
            Assert.Throws <InvalidReaderStateException>(() => reader.Skip());
        }
Beispiel #3
0
        public void ReadListAsArrayRecast()
        {
            NbtCompound intList = TestFiles.MakeListTest();

            var ms = new MemoryStream();

            new NbtFile(intList).SaveToStream(ms, NbtCompression.None);
            ms.Seek(0, SeekOrigin.Begin);
            var reader = new NbtReader(ms);

            // test bytes as shorts
            reader.ReadToFollowing("ByteList");
            short[] bytes = reader.ReadListAsArray <short>();
            CollectionAssert.AreEqual(bytes,
                                      new short[] {
                100, 20, 3
            });
        }
        public void ErrorTest()
        {
            var root = new NbtCompound("root");
            byte[] testData = new NbtFile(root).SaveToBuffer(NbtCompression.None);

            // creating NbtReader without a stream, or with a non-readable stream
            Assert.Throws<ArgumentNullException>(() => new NbtReader(null));
            Assert.Throws<ArgumentException>(() => new NbtReader(new NonReadableStream()));

            // corrupt the data
            testData[0] = 123;
            var reader = new NbtReader(new MemoryStream(testData));

            // attempt to use ReadValue when not at value
            Assert.Throws<InvalidOperationException>(() => reader.ReadValue());
            reader.CacheTagValues = true;
            Assert.Throws<InvalidOperationException>(() => reader.ReadValue());

            // attempt to read a corrupt stream
            Assert.Throws<NbtFormatException>(() => reader.ReadToFollowing());

            // make sure we've properly entered the error state
            Assert.True(reader.IsInErrorState);
            Assert.False(reader.HasName);
            Assert.Throws<InvalidReaderStateException>(() => reader.ReadToFollowing());
            Assert.Throws<InvalidReaderStateException>(() => reader.ReadListAsArray<int>());
            Assert.Throws<InvalidReaderStateException>(() => reader.ReadToNextSibling());
            Assert.Throws<InvalidReaderStateException>(() => reader.ReadToDescendant("derp"));
            Assert.Throws<InvalidReaderStateException>(() => reader.ReadAsTag());
            Assert.Throws<InvalidReaderStateException>(() => reader.Skip());
        }
        public void ReadListAsArrayRecast()
        {
            NbtCompound intList = TestFiles.MakeListTest();

            var ms = new MemoryStream();
            new NbtFile(intList).SaveToStream(ms, NbtCompression.None);
            ms.Seek(0, SeekOrigin.Begin);
            var reader = new NbtReader(ms);

            // test bytes as shorts
            reader.ReadToFollowing("ByteList");
            short[] bytes = reader.ReadListAsArray<short>();
            Assert.Equal(bytes,
                new short[]
                {
                    100, 20, 3
                });
        }
        public void ReadListAsArray()
        {
            NbtCompound intList = TestFiles.MakeListTest();

            var ms = new MemoryStream();
            new NbtFile(intList).SaveToStream(ms, NbtCompression.None);
            ms.Seek(0, SeekOrigin.Begin);
            var reader = new NbtReader(ms);

            // attempt to read value before we're in a list
            Assert.Throws<InvalidOperationException>(() => reader.ReadListAsArray<int>());

            // test byte values
            reader.ReadToFollowing("ByteList");
            byte[] bytes = reader.ReadListAsArray<byte>();
            Assert.Equal(new byte[] {100, 20, 3}, bytes);

            // test double values
            reader.ReadToFollowing("DoubleList");
            double[] doubles = reader.ReadListAsArray<double>();
            Assert.Equal(doubles, new[] {1d, 2000d, -3000000d});

            // test float values
            reader.ReadToFollowing("FloatList");
            float[] floats = reader.ReadListAsArray<float>();
            Assert.Equal(new[] {1f, 2000f, -3000000f}, floats);

            // test int values
            reader.ReadToFollowing("IntList");
            int[] ints = reader.ReadListAsArray<int>();
            Assert.Equal(new[] {1, 2000, -3000000}, ints);

            // test long values
            reader.ReadToFollowing("LongList");
            long[] longs = reader.ReadListAsArray<long>();
            Assert.Equal(new[] {1L, 2000L, -3000000L}, longs);

            // test short values
            reader.ReadToFollowing("ShortList");
            short[] shorts = reader.ReadListAsArray<short>();
            Assert.Equal(new short[] {1, 200, -30000}, shorts);

            // test short values
            reader.ReadToFollowing("StringList");
            string[] strings = reader.ReadListAsArray<string>();
            Assert.Equal(new[] {"one", "two thousand", "negative three million"}, strings);

            // try reading list of compounds (should fail)
            reader.ReadToFollowing("CompoundList");
            Assert.Throws<InvalidOperationException>(() => reader.ReadListAsArray<NbtCompound>());

            // skip to the end of the stream
            while (reader.ReadToFollowing())
            {
            }
            Assert.Throws<EndOfStreamException>(() => reader.ReadListAsArray<int>());
        }