コード例 #1
0
        public void NameValueSectionRoundTrip1EntryTest()
        {
            var ms = new MemoryStream();
            var fio = new FileIO(ms);
            var fw = new FormattedWriter(fio);
            var wc = new WriteContext(fw);

            ISectionFormatter f = new NameValueSectionFormatter();
            wc.Description = new TeaFileDescription();
            wc.Description.NameValues = new NameValueCollection();
            wc.Description.NameValues.Add(new NameValue("someName", 1.23));
            f.Write(wc);

            ms.Position = 0;

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);
            f.Read(rc);

            rc.Description.Should().Not.Be.Null();
            rc.Description.NameValues.Should().Not.Be.Null();
            rc.Description.NameValues.Should().Have.Count.EqualTo(1);
            rc.Description.NameValues.First().Name.Should().Be("someName");
            rc.Description.NameValues.First().GetValue<double>().Should().Be(1.23);
        }
コード例 #2
0
        public void ItemSectionRoundTrip()
        {
            var ms = new MemoryStream();
            var fio = new FileIO(ms);
            var fw = new FormattedWriter(fio);
            var wc = new WriteContext(fw);
            wc.Description = new TeaFileDescription();
            var writeID =
                wc.Description.ItemDescription = ItemDescription.FromAnalysis<Event<OHLCV>>();
            ISectionFormatter f = new ItemSectionFormatter();
            f.Write(wc);

            ms.Position = 0;

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);
            rc.Description.Should().Not.Be.Null();
            f.Read(rc);
            rc.Description.Should().Not.Be.Null();
            var id = rc.Description.ItemDescription;

            id.ItemTypeName.Should().Be(typeof (Event<OHLCV>).GetLanguageName());
            id.ItemSize.Should().Be(wc.Description.ItemDescription.ItemSize);
            id.Fields.Select(ff => ff.Name).Should().Have.SameValuesAs("Time", "Open", "High", "Low", "Close", "Volume");
            id.Fields.Select(ff => ff.Index).Should().Have.SameValuesAs(0, 1, 2, 3, 4, 5);
            id.Fields.Select(ff => ff.FieldType).Should().Have.SameValuesAs(FieldType.Int64, FieldType.Double, FieldType.Double, FieldType.Double, FieldType.Double);
            id.Fields.Select(ff => ff.Offset).Should().Have.SameValuesAs(writeID.Fields.Select(ff => ff.Offset));

            ms.Position.Should().Be(ms.Length); // very important, all bytes must have been read
        }
コード例 #3
0
 public void FormattedReaderExoticCoverageTest()
 {
     var fio = new Mock<IFileIO>();
     var r = new FormattedReader(fio.Object);
     fio.Setup(io => io.ReadBytes(0)); // will cause name to be the empty string
     fio.Setup(io => io.ReadInt32()).Returns((int)NameValue.ValueKind.Invalid);
     Executing.This(() => r.ReadNameValue()).Should().Throw<ArgumentOutOfRangeException>();
 }
コード例 #4
0
        public void FormattedReaderExoticCoverageTest()
        {
            var fio = new Mock <IFileIO>();
            var r   = new FormattedReader(fio.Object);

            fio.Setup(io => io.ReadBytes(0)); // will cause name to be the empty string
            fio.Setup(io => io.ReadInt32()).Returns((int)NameValue.ValueKind.Invalid);
            Executing.This(() => r.ReadNameValue()).Should().Throw <ArgumentOutOfRangeException>();
        }
コード例 #5
0
        public void ReadInt64Test()
        {
            var fio = new Mock<IFileIO>(MockBehavior.Strict);
            fio.Setup(io => io.ReadInt64()).Returns(0x7fcc111122223333);
            var r = new FormattedReader(fio.Object);

            r.ReadInt64().Should().Be(0x7fcc111122223333);

            fio.Verify(f => f.ReadInt64(), Times.Once());
        }
コード例 #6
0
        public void ReadDoubleTest()
        {
            var fio = new Mock<IFileIO>(MockBehavior.Strict);
            fio.Setup(io => io.ReadDouble()).Returns(1.2345);
            var r = new FormattedReader(fio.Object);

            r.ReadDouble().Should().Be(1.2345);

            fio.Verify(f => f.ReadDouble(), Times.Once());
        }
コード例 #7
0
        public void ReadInt64Test()
        {
            var fio = new Mock <IFileIO>(MockBehavior.Strict);

            fio.Setup(io => io.ReadInt64()).Returns(0x7fcc111122223333);
            var r = new FormattedReader(fio.Object);

            r.ReadInt64().Should().Be(0x7fcc111122223333);

            fio.Verify(f => f.ReadInt64(), Times.Once());
        }
コード例 #8
0
        public void ReadDoubleTest()
        {
            var fio = new Mock <IFileIO>(MockBehavior.Strict);

            fio.Setup(io => io.ReadDouble()).Returns(1.2345);
            var r = new FormattedReader(fio.Object);

            r.ReadDouble().Should().Be(1.2345);

            fio.Verify(f => f.ReadDouble(), Times.Once());
        }
コード例 #9
0
        public void ReadTextTest()
        {
            const string text     = "Ce plat est originaire de la Grèce antique, de l'époque de la fondation de Marseille (Massalia) au VIIe siècle av. €";
            var          encoding = new UTF8Encoding(false, true);
            var          bytes    = encoding.GetBytes(text);
            var          fio      = new Mock <IFileIO>(MockBehavior.Strict);

            fio.Setup(io => io.ReadInt32()).Returns(bytes.Length);
            fio.Setup(io => io.ReadBytes(bytes.Length)).Returns(bytes);
            var r = new FormattedReader(fio.Object);

            r.ReadText().Should().Be(text);
        }
        public void NameValueRoundTripTest()
        {
            var stream = new MemoryStream();
            var fio    = new FileIO(stream);
            var w      = new FormattedWriter(fio);

            Guid         g        = new Guid("1116C195-8975-4E73-B777-23E1C548BC71");
            const string testText = "some text with special characters €,@, ʤǤDŽƪҗ∰";

            {
                var nv1 = new NameValue("name1", 1.23);
                var nv2 = new NameValue("name2", 722);
                var nv3 = new NameValue("name3", g);
                var nv4 = new NameValue("name4", testText);

                w.WriteNameValue(nv1);
                w.WriteNameValue(nv2);
                w.WriteNameValue(nv3);
                w.WriteNameValue(nv4);
            }

            stream.Position = 0;

            var r = new FormattedReader(fio);
            {
                var nv1 = r.ReadNameValue();
                nv1.Name.Should().Be("name1");
                nv1.GetValue <double>().Should().Be(1.23);
                nv1.Kind.Should().Be(NameValue.ValueKind.Double);

                var nv2 = r.ReadNameValue();
                nv2.Name.Should().Be("name2");
                nv2.GetValue <int>().Should().Be(722);
                nv2.Kind.Should().Be(NameValue.ValueKind.Int32);

                var nv3 = r.ReadNameValue();
                nv3.Name.Should().Be("name3");
                nv3.GetValue <Guid>().Should().Be(g);
                nv3.Kind.Should().Be(NameValue.ValueKind.Guid);

                var nv4 = r.ReadNameValue();
                nv4.Name.Should().Be("name4");
                nv4.GetValue <string>().Should().Be(testText);
                nv4.Kind.Should().Be(NameValue.ValueKind.Text);
            }
        }
コード例 #11
0
        public void ContentSectionFormatterRoundTrip()
        {
            const string testValue = "Météo pour Paris, France. @€";

            var ms = new MemoryStream();
            var fio = new FileIO(ms);
            var fw = new FormattedWriter(fio);
            var wc = new WriteContext(fw);
            wc.Description = new TeaFileDescription();
            wc.Description.ContentDescription = testValue;
            ISectionFormatter f = new ContentSectionFormatter();
            f.Write(wc);
            ms.Position = 0;
            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);
            f.Read(rc);
            rc.Description.Should().Not.Be.Null();
            rc.Description.ContentDescription.Should().Be(testValue);
        }
コード例 #12
0
        public void ReadHeader()
        {
            FileIO fio = new FileIO(this.stream);
            FormattedReader r = new FormattedReader(fio);
            var rc = HeaderManager.Instance.ReadHeader(r);
            this.description = rc.Description;
            this.itemAreaStart = rc.ItemAreaStart;
            this.itemAreaEnd = rc.ItemAreaEnd;
            if (this.stream.Position != this.itemAreaStart)
            {
                throw new InternalErrorException("file position is not set to begin of item area after reading header.");
            }
            if (this.Description != null && this.Description.Timescale.HasValue)
            {
                var fileTimeScale = this.Description.Timescale.Value;
                if (!fileTimeScale.Equals(Time.Scale))
                {
                    switch (Time.ScaleCollisionBehavior)
                    {
                    case ScaleCollisionBehavior.ThrowException:
                        throw new TimescaleException();

                    case ScaleCollisionBehavior.Ignore:
                        break;

                    case ScaleCollisionBehavior.UseNewScale:
                        Time.Scale = fileTimeScale;
                        break;
                    }
                }
            }
        }
コード例 #13
0
        public void TimeSectionValuesRoundTrip()
        {
            var ms = new MemoryStream();
            var fio = new FileIO(ms);
            var fw = new FormattedWriter(fio);
            var wc = new WriteContext(fw);
            wc.Description = new TeaFileDescription();
            wc.Description.ItemDescription = ItemDescription.FromAnalysis<Event<C>>();
            Time.Scale = Timescale.FromEpoch(33, 77);
            wc.Description.Timescale = Time.Scale;
            ISectionFormatter f = new TimeSectionFormatter();
            f.Write(wc);

            ms.Position = 0;

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);
            rc.Description.Should().Not.Be.Null();
            rc.Description.ItemDescription = wc.Description.ItemDescription;

            f.Read(rc);
            rc.Description.Timescale.Value.Epoch.Should().Be(33);
            rc.Description.Timescale.Value.TicksPerDay.Should().Be(77);

            Executing.This(() => f.Read(rc)).Should().Throw<EndOfStreamException>();
        }
コード例 #14
0
        public void EventTimeAttributeRoundTrip()
        {
            Time.Scale = Timescale.Net;

            var ms = new MemoryStream();
            var fio = new FileIO(ms);
            var fw = new FormattedWriter(fio);
            var wc = new WriteContext(fw);
            wc.Description = new TeaFileDescription();
            wc.Description.ItemDescription = ItemDescription.FromAnalysis<Event<C>>();
            wc.Description.Timescale = Time.Scale;
            ISectionFormatter f = new TimeSectionFormatter();
            f.Write(wc);

            ms.Position = 0;

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);
            rc.Description.Should().Not.Be.Null();
            rc.Description.ItemDescription = wc.Description.ItemDescription;
            rc.Description.ItemDescription.Fields.ForEach(ff => ff.IsEventTime = ff.IsTime = false); // reset flags

            f.Read(rc);

            rc.Description.ItemDescription.Fields.Count(ff => ff.IsTime).Should().Be(1);
            rc.Description.ItemDescription.Fields.Count(ff => ff.IsEventTime).Should().Be(1);
            rc.Description.Timescale.Value.Epoch.Should().Be(0);
            rc.Description.Timescale.Value.TicksPerDay.Should().Be(TimeSpan.TicksPerDay);
        }
コード例 #15
0
        public void FirstTimeFieldIsAutomaticallyEventTime()
        {
            var ms = new MemoryStream();
            var fio = new FileIO(ms);
            var fw = new FormattedWriter(fio);
            var wc = new WriteContext(fw);
            wc.Description = new TeaFileDescription();
            wc.Description.ItemDescription = ItemDescription.FromAnalysis<C2>();
            wc.Description.Timescale = Time.Scale;
            ISectionFormatter f = new TimeSectionFormatter();
            f.Write(wc);

            ms.Position = 0;

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);
            rc.Description.Should().Not.Be.Null();
            rc.Description.ItemDescription = ItemDescription.FromAnalysis<C2>();

            f.Read(rc);

            var fields = rc.Description.ItemDescription.Fields;
            fields[0].IsEventTime.Should().Be.True();
            fields[1].IsEventTime.Should().Be.False();
        }
コード例 #16
0
        public void TimeSectionRoundTripFieldIsNotInItemDescriptionError()
        {
            var ms = new MemoryStream();
            var fio = new FileIO(ms);
            var fw = new FormattedWriter(fio);
            var wc = new WriteContext(fw);
            wc.Description = new TeaFileDescription();
            wc.Description.ItemDescription = ItemDescription.FromAnalysis<Event<C>>();
            wc.Description.Timescale = Time.Scale;
            ISectionFormatter f = new TimeSectionFormatter();
            f.Write(wc);

            ms.Position = 0;

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);
            rc.Description.Should().Not.Be.Null();
            rc.Description.ItemDescription = ItemDescription.FromAnalysis<C2>();

            Executing.This(() => f.Read(rc)).Should().Throw<FileFormatException>();
        }
コード例 #17
0
        public void TimeSectionRoundTrip()
        {
            var ms = new MemoryStream();
            var fio = new FileIO(ms);
            var fw = new FormattedWriter(fio);
            var wc = new WriteContext(fw);
            wc.Description = new TeaFileDescription();
            wc.Description.ItemDescription = ItemDescription.FromAnalysis<Event<C>>();
            wc.Description.Timescale = Time.Scale;
            ISectionFormatter f = new TimeSectionFormatter();
            f.Write(wc);

            ms.Position = 0;
            ms.Length.Should().Be(24); // epoch(8) ticksperday(8) fieldcount(4) + timefieldoffset(4) = 24

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);
            rc.Description.Should().Not.Be.Null();
            rc.Description.ItemDescription = wc.Description.ItemDescription; // this makes the test a bit weaker, but we need some item description here
            f.Read(rc);
            rc.Description.Should().Not.Be.Null();
            rc.Description.Timescale.HasValue.Should().Be.True();
        }
コード例 #18
0
        public void NameValueRoundTripTest()
        {
            var stream = new MemoryStream();
            var fio = new FileIO(stream);
            var w = new FormattedWriter(fio);

            Guid g = new Guid("1116C195-8975-4E73-B777-23E1C548BC71");
            const string testText = "some text with special characters €,@, ʤǤDŽƪҗ∰";
            {
                var nv1 = new NameValue("name1", 1.23);
                var nv2 = new NameValue("name2", 722);
                var nv3 = new NameValue("name3", g);
                var nv4 = new NameValue("name4", testText);

                w.WriteNameValue(nv1);
                w.WriteNameValue(nv2);
                w.WriteNameValue(nv3);
                w.WriteNameValue(nv4);
            }

            stream.Position = 0;

            var r = new FormattedReader(fio);
            {
                var nv1 = r.ReadNameValue();
                nv1.Name.Should().Be("name1");
                nv1.GetValue<double>().Should().Be(1.23);
                nv1.Kind.Should().Be(NameValue.ValueKind.Double);

                var nv2 = r.ReadNameValue();
                nv2.Name.Should().Be("name2");
                nv2.GetValue<int>().Should().Be(722);
                nv2.Kind.Should().Be(NameValue.ValueKind.Int32);

                var nv3 = r.ReadNameValue();
                nv3.Name.Should().Be("name3");
                nv3.GetValue<Guid>().Should().Be(g);
                nv3.Kind.Should().Be(NameValue.ValueKind.Guid);

                var nv4 = r.ReadNameValue();
                nv4.Name.Should().Be("name4");
                nv4.GetValue<string>().Should().Be(testText);
                nv4.Kind.Should().Be(NameValue.ValueKind.Text);
            }
        }
コード例 #19
0
        public void ReadTextTest()
        {
            const string text = "Ce plat est originaire de la Grèce antique, de l'époque de la fondation de Marseille (Massalia) au VIIe siècle av. €";
            var encoding = new UTF8Encoding(false, true);
            var bytes = encoding.GetBytes(text);
            var fio = new Mock<IFileIO>(MockBehavior.Strict);
            fio.Setup(io => io.ReadInt32()).Returns(bytes.Length);
            fio.Setup(io => io.ReadBytes(bytes.Length)).Returns(bytes);
            var r = new FormattedReader(fio.Object);

            r.ReadText().Should().Be(text);
        }
コード例 #20
0
        public void NameValueSectionRoundTrip3EntriesTest()
        {
            var ms = new MemoryStream();
            var fio = new FileIO(ms);
            var fw = new FormattedWriter(fio);
            var wc = new WriteContext(fw);

            ISectionFormatter f = new NameValueSectionFormatter();
            wc.Description = new TeaFileDescription();
            wc.Description.NameValues = new NameValueCollection();
            wc.Description.NameValues.Add(new NameValue("someName", 1.23));
            wc.Description.NameValues.Add(new NameValue("someName2", "second value"));
            wc.Description.NameValues.Add(new NameValue("someName3", 333));
            f.Write(wc);

            ms.Position = 0;

            var fr = new FormattedReader(fio);
            var rc = new ReadContext(fr);
            f.Read(rc);

            rc.Description.Should().Not.Be.Null();
            rc.Description.NameValues.Should().Not.Be.Null();
            rc.Description.NameValues.Should().Have.Count.EqualTo(3);
            rc.Description.NameValues.Select(nv => nv.Name).Should().Have.SameSequenceAs("someName", "someName2", "someName3");
            rc.Description.NameValues.Select(nv => nv.GetValue<object>()).Should().Have.SameSequenceAs(1.23, "second value", 333);
        }