Beispiel #1
0
        public void AddEntriesFrom_NonPackedInt32()
        {
            uint nonPackedTag = WireFormat.MakeTag(10, WireFormat.WireType.Varint);
            var  stream       = new MemoryStream();
            var  output       = new CodedOutputStream(stream);

            output.WriteTag(nonPackedTag);
            output.WriteInt32(10);
            output.WriteTag(nonPackedTag);
            output.WriteInt32(999);
            output.WriteTag(nonPackedTag);
            output.WriteInt32(-1000); // Just for variety...
            output.Flush();
            stream.Position = 0;

            // Deliberately "expecting" a packed tag, but we detect that the data is
            // actually not packed.
            uint packedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var  field     = new RepeatedField <int>();
            var  input     = new CodedInputStream(stream);

            input.AssertNextTag(nonPackedTag);
            field.AddEntriesFrom(input, FieldCodec.ForInt32(packedTag));
            CollectionAssert.AreEqual(new[] { 10, 999, -1000 }, field);
            Assert.IsTrue(input.IsAtEnd);
        }
        public void AddEntriesFrom_PackedInt32()
        {
            uint packedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var  stream    = new MemoryStream();
            var  output    = CodedOutputStream.CreateInstance(stream);
            var  length    = CodedOutputStream.ComputeInt32Size(10)
                             + CodedOutputStream.ComputeInt32Size(999)
                             + CodedOutputStream.ComputeInt32Size(-1000);

            output.WriteTag(packedTag);
            output.WriteRawVarint32((uint)length);
            output.WriteInt32(10);
            output.WriteInt32(999);
            output.WriteInt32(-1000);
            output.Flush();
            stream.Position = 0;

            // Deliberately "expecting" a non-packed tag, but we detect that the data is
            // actually packed.
            uint nonPackedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var  field        = new RepeatedField <int>();
            var  input        = CodedInputStream.CreateInstance(stream);

            input.AssertNextTag(packedTag);
            field.AddEntriesFrom(input, FieldCodec.ForInt32(nonPackedTag));
            CollectionAssert.AreEqual(new[] { 10, 999, -1000 }, field);
            Assert.IsTrue(input.IsAtEnd);
        }
Beispiel #3
0
                static Location()
                {
                    // Note: this type is marked as 'beforefieldinit'.
                    while (true)
                    {
IL_7D:
                        uint arg_61_0 = 3312851954u;
                        while (true)
                        {
                            uint num;
                            switch ((num = (arg_61_0 ^ 3421084927u)) % 4u)
                            {
                            case 0u:
                                SourceCodeInfo.Types.Location._repeated_span_codec = FieldCodec.ForInt32(18u);
                                SourceCodeInfo.Types.Location._repeated_leadingDetachedComments_codec = FieldCodec.ForString(50u);
                                arg_61_0 = (num * 3524145418u ^ 1786628465u);
                                continue;

                            case 1u:
                                SourceCodeInfo.Types.Location._repeated_path_codec = FieldCodec.ForInt32(10u);
                                arg_61_0 = (num * 3025576672u ^ 2268289631u);
                                continue;

                            case 3u:
                                goto IL_7D;
                            }
                            return;
                        }
                    }
                }
Beispiel #4
0
        public void CalculateSize_VariableSizeNonPacked()
        {
            var list = new RepeatedField <int> {
                1, 500, 1
            };
            var tag = WireFormat.MakeTag(1, WireFormat.WireType.Varint);

            // 2 bytes for the first entry, 3 bytes for the second, 2 bytes for the third
            Assert.AreEqual(7, list.CalculateSize(FieldCodec.ForInt32(tag)));
        }
Beispiel #5
0
        public void CalculateSize_VariableSizePacked()
        {
            var list = new RepeatedField <int> {
                1, 500, 1
            };
            var tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);

            // 1 byte for the tag, 1 byte for the length,
            // 1 byte for the first entry, 2 bytes for the second, 1 byte for the third
            Assert.AreEqual(6, list.CalculateSize(FieldCodec.ForInt32(tag)));
        }
Beispiel #6
0
        private async Task <FileWeightsReply> GetFileWeightsInternal(GetFileWeightsRequest request)
        {
            var reply = new FileWeightsReply();

            if (string.IsNullOrWhiteSpace(request.RootName))
            {
                reply.Error = Error.NoSuchEntity;
                return(reply);
            }
            var path   = PathSep + request.RootName;
            var folder = await _service.All().Where(k => k.Name == request.RootName && k.Path == path && k.Type == (int)FileType.Folder)
                         .FirstOrDefaultAsync();

            if (folder == null)
            {
                reply.Error = Error.NoSuchEntity;
                return(reply);
            }
            var pathStart = folder.Path + PathSep;
            var weights   = await _service.All().Where(p => p.Type == (int)FileType.Normal && p.Path.StartsWith(pathStart)).Select(f => new { f.Name, f.Weight })
                            .ToArrayAsync();

            foreach (var w in weights)
            {
                reply.Weights.Add(w.Name.ToString(), w.Weight ?? 0);
            }
            var codec = new MapField <string, int> .Codec(FieldCodec.ForString(0), FieldCodec.ForInt32(1), 0);

            var version = await _contentService.Add(_cachesGroup, fs =>
            {
                using (var s = new CodedOutputStream(fs))
                {
                    reply.WriteTo(s);
                }

                return(Task.FromResult(true));
            }, "bin");

            await _cache.SetStringAsync(FileWeightsVersionCacheKey(request.RootName), version);

            return(reply);
        }
Beispiel #7
0
        public void WriteTo_NonPackedInt32()
        {
            uint tag   = WireFormat.MakeTag(10, WireFormat.WireType.Varint);
            var  field = new RepeatedField <int> {
                10, 1000, 1000000
            };
            var stream = new MemoryStream();
            var output = new CodedOutputStream(stream);

            field.WriteTo(output, FieldCodec.ForInt32(tag));
            output.Flush();
            stream.Position = 0;

            var input = new CodedInputStream(stream);

            input.AssertNextTag(tag);
            Assert.AreEqual(10, input.ReadInt32());
            input.AssertNextTag(tag);
            Assert.AreEqual(1000, input.ReadInt32());
            input.AssertNextTag(tag);
            Assert.AreEqual(1000000, input.ReadInt32());
            Assert.IsTrue(input.IsAtEnd);
        }
        public void WriteTo_PackedInt32()
        {
            uint tag   = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var  field = new RepeatedField <int> {
                10, 1000, 1000000
            };
            var stream = new MemoryStream();
            var output = CodedOutputStream.CreateInstance(stream);

            field.WriteTo(output, FieldCodec.ForInt32(tag));
            output.Flush();
            stream.Position = 0;

            var input = CodedInputStream.CreateInstance(stream);

            input.AssertNextTag(tag);
            var length = input.ReadLength();

            Assert.AreEqual(10, input.ReadInt32());
            Assert.AreEqual(1000, input.ReadInt32());
            Assert.AreEqual(1000000, input.ReadInt32());
            Assert.IsTrue(input.IsAtEnd);
            Assert.AreEqual(1 + CodedOutputStream.ComputeLengthSize(length) + length, stream.Length);
        }
Beispiel #9
0
 /// <inheritdoc/>
 public override FieldCodec <int> CreateFieldCodec(int fieldNumber)
 {
     return(FieldCodec.ForInt32(WireFormat.MakeTag(fieldNumber, WireType)));
 }