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>());
        }
Example #2
0
    public void Serializing2()
    {
        // check saving/loading lists of all possible value types
        var testFile = new NbtFile(TestFiles.MakeListTest());

        byte[] buffer    = testFile.SaveToBuffer(NbtCompression.None);
        long   bytesRead = testFile.LoadFromBuffer(buffer, 0, buffer.Length, NbtCompression.None);

        Assert.Equal(bytesRead, buffer.Length);
    }
Example #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>();
        Assert.Equal(bytes,
                     new short[] {
            100, 20, 3
        });
    }