public void OutputPipelineContinuity(
            [Values(1000, 5000, 10000, 15000, 20000)] double sampleRate,
            [Values(0.1, 0.5, 1, 5)] double blockDurationSeconds
            )
        {
            const double epochDuration = 2; //seconds
            var srate = new Measurement((decimal) sampleRate, "Hz");

            var daq = new TestDAQController();
            var outStream = new DAQOutputStream("OUT")
                                {
                                    SampleRate = srate,
                                    MeasurementConversionTarget = "V"
                                };
            var inStream = new DAQInputStream("IN")
                                {
                                    SampleRate = srate,
                                    MeasurementConversionTarget = "V"
                                };

            var controller = new Controller() { Clock = daq, DAQController = daq };

            var dev = new UnitConvertingExternalDevice("dev", "co", controller, new Measurement(0, "V"))
                          {
                              Clock = daq,
                              MeasurementConversionTarget = "V",
                              OutputSampleRate = srate,
                              InputSampleRate = srate
                          };
            dev.BindStream(outStream).BindStream(inStream);

            // Setup Epoch
            var e = new Epoch("OutputPipelineContinuity");

            var nSamples = (int)TimeSpan.FromSeconds(epochDuration).Samples(srate);
            IList<IMeasurement> stimData = (IList<IMeasurement>) Enumerable.Range(0, nSamples)
                                                       .Select(i => new Measurement((decimal) (8 * Math.Sin(((double)i) / (nSamples / 10.0))), "V") as IMeasurement)
                                                       .ToList();
            IOutputData stimOutputData = new OutputData(stimData, srate);

            var stim = new RenderedStimulus((string) "RenderedStimulus", (IDictionary<string, object>) new Dictionary<string, object>(),
                                            stimOutputData);
            e.Stimuli[dev] = stim;
            e.Responses[dev] = new Response();
            e.Backgrounds[dev] = new Background(new Measurement(0, "V"), srate);

            controller.EnqueueEpoch(e);

            var blockSpan = TimeSpan.FromSeconds(blockDurationSeconds);
            foreach (var stimBlock in stim.DataBlocks(blockSpan))
            {
                var cons = stimOutputData.SplitData(blockSpan);
                var expected = cons.Head.Data;
                stimOutputData = cons.Rest;

                Assert.That(stimBlock.Data, Is.EqualTo(expected));
            }
        }
        public void FiresStimulusOutputEvents()
        {
            var c         = new TestDAQController();
            var srate     = new Measurement(10, "Hz");
            var outStream = new DAQOutputStream("out")
            {
                MeasurementConversionTarget = "V",
                SampleRate = srate
            };
            var inStream = new DAQInputStream("in")
            {
                MeasurementConversionTarget = "V",
                SampleRate = srate
            };

            var outputData   = new Dictionary <IDAQOutputStream, Queue <IOutputData> >();
            var dataQueue    = new Queue <IOutputData>();
            var outputIOData = new OutputData(
                Enumerable.Range(0, 100).Select(i => new Measurement(i, "V")),
                new Measurement(10, "Hz")
                );

            dataQueue.Enqueue(outputIOData);

            outputData[outStream] = dataQueue;

            var dev = new TestDevice("test", outputData)
            {
                MeasurementConversionTarget = "V"
            };

            dev.BindStream(outStream);

            bool fired = false;

            c.StimulusOutput += (daq, args) =>
            {
                Assert.That(args.Stream == outStream);
                Assert.That(args.Data != null);
                Assert.That(args.Data.Configuration.Count() > 0);
                fired = true;
            };
            c.ProcessIteration += (daq, args) => ((IDAQController)daq).RequestStop();

            c.AddStreamMapping(outStream, inStream);

            c.Start(false);

            while (c.Running)
            {
                ;
            }

            Assert.That(fired, Is.True.After(1000, 10));
        }
        public void OutputPipelineContinuity(
            [Values(1000, 5000, 10000, 15000, 20000)] double sampleRate,
            [Values(0.1, 0.5, 1, 5)] double blockDurationSeconds
            )
        {

            const double epochDuration = 2; //seconds
            var srate = new Measurement((decimal) sampleRate, "Hz");

            var daq = new TestDAQController();
            var outStream = new DAQOutputStream("OUT")
                                {
                                    SampleRate = srate,
                                    MeasurementConversionTarget = "V"
                                };

            var controller = new Controller() { Clock = daq, DAQController = daq };

            var dev = new UnitConvertingExternalDevice("dev", "co", controller, new Measurement(0, "V"))
                          {
                              Clock = daq,
                              MeasurementConversionTarget = "V"
                          };
            dev.BindStream(outStream);

            // Setup Epoch
            var e = new Epoch("OutputPipelineContinuity");

            var nSamples = (int)TimeSpan.FromSeconds(epochDuration).Samples(srate);
            IList<IMeasurement> stimData = (IList<IMeasurement>) Enumerable.Range(0, nSamples)
                                                       .Select(i => new Measurement((decimal) (8 * Math.Sin(((double)i) / (nSamples / 10.0))), "V") as IMeasurement)
                                                       .ToList();
            IOutputData stimOutputData = new OutputData(stimData, srate);

            var stim = new RenderedStimulus((string) "RenderedStimulus", (IDictionary<string, object>) new Dictionary<string, object>(),
                                            stimOutputData);
            e.Stimuli[dev] = stim;
            e.Responses[dev] = new Response();
            e.Background[dev] = new Epoch.EpochBackground(new Measurement(0, "V"), srate);

            controller.EnqueueEpoch(e);
            controller.NextEpoch();

            var blockSpan = TimeSpan.FromSeconds(blockDurationSeconds);
            foreach (var stimBlock in stim.DataBlocks(blockSpan))
            {
                var cons = stimOutputData.SplitData(blockSpan);
                var expected = cons.Head.Data;
                stimOutputData = cons.Rest;

                Assert.That(stimBlock.Data, Is.EqualTo(expected));
            }

        }
        public void FiresStimulusOutputEvents()
        {
            var c = new TestDAQController();
            var srate = new Measurement(10, "Hz");
            var outStream = new DAQOutputStream("out")
                                {
                                    MeasurementConversionTarget = "V",
                                    SampleRate = srate
                                };
            var inStream = new DAQInputStream("in")
                               {
                                   MeasurementConversionTarget = "V",
                                   SampleRate = srate
                               };

            var outputData = new Dictionary<IDAQOutputStream, Queue<IOutputData>>();
            var dataQueue = new Queue<IOutputData>();
            var outputIOData = new OutputData(
                Enumerable.Range(0, 100).Select(i => new Measurement(i, "V")),
                new Measurement(10, "Hz")
                );

            dataQueue.Enqueue(outputIOData);

            outputData[outStream] = dataQueue;

            var dev = new TestDevice("test", outputData) { MeasurementConversionTarget = "V" };

            dev.BindStream(outStream);

            bool fired = false;
            c.StimulusOutput += (daq, args) =>
                                    {
                                        Assert.That(args.Stream == outStream);
                                        Assert.That(args.Data != null);
                                        Assert.That(args.Data.Configuration.Count() > 0);
                                        fired = true;

                                        ((IDAQController) daq).RequestStop();
                                    };

            c.AddStreamMapping(outStream, inStream);

            c.Start(false);

            while (c.IsRunning)
            {
                Thread.Sleep(1);
            }

            Assert.That(fired, Is.True);
        }
        public void FiresOnStoppedEvent()
        {
            var controller = new TestDAQController();
            controller.Clock = controller;

            bool fired = false;

            controller.Stopped += (c, args) => fired = true;
            controller.ExceptionalStop += (c, ex) => Assert.Fail(ex.Exception.ToString());

            controller.Start(false);
            controller.Stop();

            Assert.IsTrue(fired);
        }
        public void FiresOnStoppedEvent()
        {
            var controller = new TestDAQController();

            controller.Clock = controller;

            bool fired = false;

            controller.Stopped         += (c, args) => fired = true;
            controller.ExceptionalStop += (c, ex) => Assert.Fail(ex.Exception.ToString());

            controller.Start(false);
            controller.Stop();

            Assert.IsTrue(fired);
        }