Exemple #1
0
        public void object_pool_work()
        {
            using (var mem = new MemoryStream())
            {
                var sA = new String('A', 100);
                var sB = new String('B', 100);
                using (var w = new CKBinaryWriter(mem, Encoding.UTF8, true))
                {
                    var pool = new CKBinaryWriter.ObjectPool <string>(w, StringComparer.InvariantCultureIgnoreCase);

                    var p = mem.Position;
                    p.Should().Be(0);

                    pool.MustWrite(sA).Should().BeTrue();
                    w.Write(sA);
                    pool.MustWrite(sB).Should().BeTrue();
                    w.Write(sB);

                    var delta = mem.Position - p;
                    p = mem.Position;
                    delta.Should().Be(1 + 1 + sA.Length + 1 + 1 + sB.Length, "Marker byte + small length + UTF8 ascii string");

                    for (int i = 0; i < 50; ++i)
                    {
                        pool.MustWrite(sA).Should().BeFalse();
                        pool.MustWrite(sB).Should().BeFalse();
                        pool.MustWrite(sA.ToLowerInvariant()).Should().BeFalse();
                        pool.MustWrite(sB.ToLowerInvariant()).Should().BeFalse();
                    }
                    delta = mem.Position - p;
                    delta.Should().Be(50 * 4 * (1 + 1), "Marker byte + NonNegativeSmallInt32 that is actuall one byte...");
                }
                mem.Position = 0;
                using (var r = new CKBinaryReader(mem, Encoding.UTF8, true))
                {
                    var    pool = new CKBinaryReader.ObjectPool <string>(r);
                    string rA   = pool.TryRead(out rA).SetReadResult(r.ReadString());
                    rA.Should().Be(sA);
                    string rB = pool.Read((state, reader) => reader.ReadString());
                    rB.Should().Be(sB);
                    for (int i = 0; i < 50; ++i)
                    {
                        pool.TryRead(out var rA2).Success.Should().BeTrue();
                        rA2.Should().Be(rA);
                        pool.Read((state, reader) => reader.ReadString()).Should().Be(rB);
                        pool.Read((state, reader) => reader.ReadString()).Should().Be(rA);
                        pool.Read((state, reader) => reader.ReadString()).Should().Be(rB);
                    }
                }
            }
        }
Exemple #2
0
        public void object_pool_with_write_marker()
        {
            using (var mem = new MemoryStream())
            {
                // Same string but in two different instances: the PureObjectRefEqualityComparer
                // does its job.
                var o1 = new String('B', 100);
                var o2 = new String('B', 100);
                using (var w = new CKBinaryWriter(mem, Encoding.UTF8, true))
                {
                    var pool = new CKBinaryWriter.ObjectPool <string>(w, PureObjectRefEqualityComparer <string> .Default);
                    pool.MustWrite(o1, 3).Should().BeTrue();
                    w.Write(o1);
                    pool.MustWrite(o2, 255).Should().BeTrue();
                    w.Write(o2);
                }
                mem.Position = 0;
                using (var r = new CKBinaryReader(mem, Encoding.UTF8, true))
                {
                    var pool   = new CKBinaryReader.ObjectPool <string>(r);
                    var state1 = pool.TryRead(out var s1);
                    s1.Should().BeNull();
                    state1.Success.Should().BeFalse();
                    state1.WriteMarker.Should().Be(3);
                    s1 = state1.SetReadResult(r.ReadString());

                    var state2 = pool.TryRead(out var s2);
                    s2.Should().BeNull();
                    state2.Success.Should().BeFalse();
                    state2.WriteMarker.Should().Be(255);
                    s2 = state2.SetReadResult(r.ReadString());

                    s1.Should().Be(o1).And.Be(o2);
                    s2.Should().Be(o1).And.Be(o2);
                }
            }
        }
 public SerializerContext(ICKBinaryWriter writer, int version)
 {
     (Writer = writer).WriteNonNegativeSmallInt32(version);
     TraitContextPool = new CKBinaryWriter.ObjectPool <CKTraitContext>(Writer, PureObjectRefEqualityComparer <CKTraitContext> .Default);
     TraitPool        = new CKBinaryWriter.ObjectPool <CKTrait>(Writer, PureObjectRefEqualityComparer <CKTrait> .Default);
 }