Ejemplo n.º 1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void shouldPackUnpackBytesHeaderWithCorrectBufferSize() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void ShouldPackUnpackBytesHeaderWithCorrectBufferSize()
        {
            Machine machine = new Machine();

            PackStream.Packer packer = machine.Packer();

            MachineClient client = new MachineClient(8);

            PackStream.Unpacker unpacker = client.Unpacker();

            for (int size = 0; size <= 65536; size++)
            {
                machine.Reset();
                packer.PackBytesHeader(size);
                packer.Flush();

                // Then
                int     bufferSize = ComputeOutputBufferSize(size, false);
                sbyte[] output     = machine.Output();
                assertThat(output.Length, equalTo(bufferSize));

                client.Reset(output);
                int value = unpacker.UnpackBytesHeader();
                assertThat(value, equalTo(size));
            }
        }
Ejemplo n.º 2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void handlesDataCrossingBufferBoundaries() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void HandlesDataCrossingBufferBoundaries()
        {
            // Given
            Machine machine = new Machine();

            PackStream.Packer packer = machine.Packer();
            packer.pack(long.MaxValue);
            packer.pack(long.MaxValue);
            packer.Flush();

            ReadableByteChannel ch = Channels.newChannel(new MemoryStream(machine.Output()));

            PackStream.Unpacker unpacker = new PackStream.Unpacker((new BufferedChannelInput(11)).Reset(ch));

            // Serialized ch will look like, and misalign with the 11-byte unpack buffer:

            // [XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX]
            //  mkr \___________data______________/ mkr \___________data______________/
            // \____________unpack buffer_________________/

            // When
            long first  = unpacker.UnpackLong();
            long second = unpacker.UnpackLong();

            // Then
            assertEquals(long.MaxValue, first);
            assertEquals(long.MaxValue, second);
        }
Ejemplo n.º 3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void testCanPackAndUnpackListStream() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void TestCanPackAndUnpackListStream()
        {
            // Given
            Machine machine = new Machine();

            // When
            PackStream.Packer packer = machine.Packer();
            packer.PackListStreamHeader();
            packer.Pack("eins");
            packer.Pack("zwei");
            packer.Pack("drei");
            packer.PackEndOfStream();
            packer.Flush();
            PackStream.Unpacker unpacker = NewUnpacker(machine.Output());

            // Then

            assertThat(unpacker.UnpackListHeader(), equalTo(PackStream.UNKNOWN_SIZE));

            assertThat(unpacker.UnpackString(), equalTo("eins"));
            assertThat(unpacker.UnpackString(), equalTo("zwei"));
            assertThat(unpacker.UnpackString(), equalTo("drei"));

            unpacker.UnpackEndOfStream();
        }
Ejemplo n.º 4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void testCanPackAndUnpackMapStream() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void TestCanPackAndUnpackMapStream()
        {
            // Given
            Machine machine = new Machine();

            // When
            PackStream.Packer packer = machine.Packer();
            packer.PackMapStreamHeader();
            packer.Pack("one");
            packer.pack(1);
            packer.Pack("two");
            packer.pack(2);
            packer.PackEndOfStream();
            packer.Flush();
            PackStream.Unpacker unpacker = NewUnpacker(machine.Output());

            // Then

            assertThat(unpacker.UnpackMapHeader(), equalTo(PackStream.UNKNOWN_SIZE));

            assertThat(unpacker.UnpackString(), equalTo("one"));
            assertThat(unpacker.UnpackLong(), equalTo(1L));
            assertThat(unpacker.UnpackString(), equalTo("two"));
            assertThat(unpacker.UnpackLong(), equalTo(2L));

            unpacker.UnpackEndOfStream();
        }
Ejemplo n.º 5
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void shouldPackUnpackHeaderWithCorrectBufferSize(PackType type) throws Throwable
        private void ShouldPackUnpackHeaderWithCorrectBufferSize(PackType type)
        {
            Machine machine = new Machine();

            PackStream.Packer packer = machine.Packer();

            MachineClient client = new MachineClient(8);

            PackStream.Unpacker unpacker = client.Unpacker();

            for (int size = 0; size <= 65536; size++)
            {
                machine.Reset();
                switch (type)
                {
                case Org.Neo4j.Bolt.v1.packstream.PackType.Map:
                    packer.PackMapHeader(size);
                    break;

                case Org.Neo4j.Bolt.v1.packstream.PackType.List:
                    packer.PackListHeader(size);
                    break;

                case Org.Neo4j.Bolt.v1.packstream.PackType.String:
                    packer.PackStringHeader(size);
                    break;

                default:
                    throw new System.ArgumentException("Unsupported type: " + type + ".");
                }
                packer.Flush();

                int     bufferSize = ComputeOutputBufferSize(size, true);
                sbyte[] output     = machine.Output();
                assertThat(output.Length, equalTo(bufferSize));

                client.Reset(output);
                int value = 0;
                switch (type)
                {
                case Org.Neo4j.Bolt.v1.packstream.PackType.Map:
                    value = ( int )unpacker.UnpackMapHeader();
                    break;

                case Org.Neo4j.Bolt.v1.packstream.PackType.List:
                    value = ( int )unpacker.UnpackListHeader();
                    break;

                case Org.Neo4j.Bolt.v1.packstream.PackType.String:
                    value = unpacker.UnpackStringHeader();
                    break;

                default:
                    throw new System.ArgumentException("Unsupported type: " + type + ".");
                }

                assertThat(value, equalTo(size));
            }
        }
Ejemplo n.º 6
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void assertPeekType(PackType type, Object value) throws java.io.IOException
        private void AssertPeekType(PackType type, object value)
        {
            // Given
            Machine machine = new Machine();

            PackStream.Packer packer = machine.Packer();
            DoTheThing(packer, value);
            packer.Flush();

            PackStream.Unpacker unpacker = NewUnpacker(machine.Output());

            // When & Then
            assertEquals(type, unpacker.PeekNextType());
        }
Ejemplo n.º 7
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void testCanPackAndUnpackString() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void TestCanPackAndUnpackString()
        {
            // Given
            Machine machine    = new Machine();
            string  abcdefghij = "ABCDEFGHIJ";

            // When
            PackStream.Packer packer = machine.Packer();
            packer.Pack(abcdefghij);
            packer.Flush();

            // Then
            string value = NewUnpacker(machine.Output()).unpackString();

            assertThat(value, equalTo(abcdefghij));
        }
Ejemplo n.º 8
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void doTheThing(PackStream.Packer packer, Object value) throws java.io.IOException
        private void DoTheThing(PackStream.Packer packer, object value)
        {
            if (value is string)
            {
                packer.Pack(( string )value);
            }
            else if (value is long? || value is int?)
            {
                packer.pack((( Number )value).longValue());
            }
            else if (value is double? || value is float?)
            {
                packer.pack((( Number )value).doubleValue());
            }
            else if (value is bool?)
            {
                packer.pack(( bool? )value);
            }
            else if (value is System.Collections.IList)
            {
                System.Collections.IList list = (System.Collections.IList)value;
                packer.PackListHeader(list.Count);
                foreach (object o in list)
                {
                    DoTheThing(packer, o);
                }
            }
            else if (value is System.Collections.IDictionary)
            {
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: java.util.Map<?,?> map = (java.util.Map<?,?>) value;
                IDictionary <object, ?> map = (IDictionary <object, ?>)value;
                packer.PackMapHeader(map.Count);
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: for (java.util.Map.Entry<?,?> o : map.entrySet())
                foreach (KeyValuePair <object, ?> o in map.SetOfKeyValuePairs())
                {
                    DoTheThing(packer, o.Key);
                    DoTheThing(packer, o.Value);
                }
            }
        }
Ejemplo n.º 9
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void testCanPackAndUnpackListOneItemAtATime() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void TestCanPackAndUnpackListOneItemAtATime()
        {
            // Given
            Machine machine = new Machine();

            // When
            PackStream.Packer packer = machine.Packer();
            packer.PackListHeader(3);
            packer.pack(12);
            packer.pack(13);
            packer.pack(14);
            packer.Flush();
            PackStream.Unpacker unpacker = NewUnpacker(machine.Output());

            // Then
            assertThat(unpacker.UnpackListHeader(), equalTo(3L));

            assertThat(unpacker.UnpackLong(), equalTo(12L));
            assertThat(unpacker.UnpackLong(), equalTo(13L));
            assertThat(unpacker.UnpackLong(), equalTo(14L));
        }
Ejemplo n.º 10
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void testCanDoStreamingStructUnpacking() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void TestCanDoStreamingStructUnpacking()
        {
            // Given
            Machine machine = new Machine();

            PackStream.Packer packer = machine.Packer();
            packer.PackStructHeader(4, ( sbyte )'~');
            packer.pack(1);
            packer.pack(2);
            packer.pack(3);
            packer.PackListHeader(2);
            packer.pack(4);
            packer.pack(5);
            packer.Flush();

            // When I unpack this value
            PackStream.Unpacker unpacker = NewUnpacker(machine.Output());

            // Then I can do streaming unpacking
            long size      = unpacker.UnpackStructHeader();
            char signature = unpacker.UnpackStructSignature();
            long a         = unpacker.UnpackLong();
            long b         = unpacker.UnpackLong();
            long c         = unpacker.UnpackLong();

            long innerSize = unpacker.UnpackListHeader();
            long d         = unpacker.UnpackLong();
            long e         = unpacker.UnpackLong();

            // And all the values should be sane
            assertEquals(4, size);
            assertEquals('~', signature);
            assertEquals(2, innerSize);
            assertEquals(1, a);
            assertEquals(2, b);
            assertEquals(3, c);
            assertEquals(4, d);
            assertEquals(5, e);
        }
Ejemplo n.º 11
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void testCanDoStreamingMapUnpacking() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void TestCanDoStreamingMapUnpacking()
        {
            // Given
            Machine machine = new Machine();

            PackStream.Packer packer = machine.Packer();
            packer.PackMapHeader(2);
            packer.Pack("name");
            packer.Pack("Bob");
            packer.Pack("cat_ages");
            packer.PackListHeader(2);
            packer.Pack(4.3);
            packer.Pack(true);
            packer.Flush();

            // When I unpack this value
            PackStream.Unpacker unpacker = NewUnpacker(machine.Output());

            // Then I can do streaming unpacking
            long   size = unpacker.UnpackMapHeader();
            string k1   = unpacker.UnpackString();
            string v1   = unpacker.UnpackString();
            string k2   = unpacker.UnpackString();

            long   innerSize = unpacker.UnpackListHeader();
            double d         = unpacker.UnpackDouble();
            bool   e         = unpacker.UnpackBoolean();

            // And all the values should be sane
            assertEquals(2, size);
            assertEquals(2, innerSize);
            assertEquals("name", k1);
            assertEquals("Bob", v1);
            assertEquals("cat_ages", k2);
            assertEquals(4.3, d, 0.0001);
            assertTrue(e);
        }
Ejemplo n.º 12
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void testCanPackStructIncludingSignature() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void TestCanPackStructIncludingSignature()
        {
            // Given
            Machine machine = new Machine();

            // When
            PackStream.Packer packer = machine.Packer();
            packer.PackStructHeader(3, ( sbyte )'N');
            packer.pack(12);
            packer.PackListHeader(2);
            packer.Pack("Person");
            packer.Pack("Employee");
            packer.PackMapHeader(2);
            packer.Pack("name");
            packer.Pack("Alice");
            packer.Pack("age");
            packer.pack(33);
            packer.Flush();

            // Then
            sbyte[] bytes    = machine.Output();
            sbyte[] expected = new sbyte[] { PackStream.TinyStruct | 3, ( sbyte )'N', 12, PackStream.TinyList | 2, PackStream.TinyString | 6, ( sbyte )'P', ( sbyte )'e', ( sbyte )'r', ( sbyte )'s', ( sbyte )'o', ( sbyte )'n', PackStream.TinyString | 8, ( sbyte )'E', ( sbyte )'m', ( sbyte )'p', ( sbyte )'l', ( sbyte )'o', ( sbyte )'y', ( sbyte )'e', ( sbyte )'e', PackStream.TinyMap | 2, PackStream.TinyString | 4, ( sbyte )'n', ( sbyte )'a', ( sbyte )'m', ( sbyte )'e', PackStream.TinyString | 5, ( sbyte )'A', ( sbyte )'l', ( sbyte )'i', ( sbyte )'c', ( sbyte )'e', PackStream.TinyString | 3, ( sbyte )'a', ( sbyte )'g', ( sbyte )'e', 33 };
            assertThat(bytes, equalTo(expected));
        }
Ejemplo n.º 13
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void testCanPackAndUnpackStruct() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void TestCanPackAndUnpackStruct()
        {
            // Given
            Machine machine = new Machine();

            // When
            PackStream.Packer packer = machine.Packer();
            packer.PackStructHeader(3, ( sbyte )'N');
            packer.pack(12);
            packer.PackListHeader(2);
            packer.Pack("Person");
            packer.Pack("Employee");
            packer.PackMapHeader(2);
            packer.Pack("name");
            packer.Pack("Alice");
            packer.Pack("age");
            packer.pack(33);
            packer.Flush();
            PackStream.Unpacker unpacker = NewUnpacker(machine.Output());

            // Then
            assertThat(unpacker.UnpackStructHeader(), equalTo(3L));
            assertThat(unpacker.UnpackStructSignature(), equalTo('N'));

            assertThat(unpacker.UnpackLong(), equalTo(12L));

            assertThat(unpacker.UnpackListHeader(), equalTo(2L));
            assertThat(unpacker.UnpackString(), equalTo("Person"));
            assertThat(unpacker.UnpackString(), equalTo("Employee"));

            assertThat(unpacker.UnpackMapHeader(), equalTo(2L));
            assertThat(unpacker.UnpackString(), equalTo("name"));
            assertThat(unpacker.UnpackString(), equalTo("Alice"));
            assertThat(unpacker.UnpackString(), equalTo("age"));
            assertThat(unpacker.UnpackLong(), equalTo(33L));
        }
Ejemplo n.º 14
0
 internal Machine(int bufferSize)
 {
     this.OutputConflict = new MemoryStream();
     this.Writable       = Channels.newChannel(this.OutputConflict);
     this.PackerConflict = new PackStream.Packer(new BufferedChannelOutput(this.Writable, bufferSize));
 }