public int Count() { if (!IsTerminal()) { MakeTerminal(); } // recurse further down and take max value of currently read if (_parentStream != null) { return(Math.Max(_currentlyRead - _offset, _parentStream.Count())); } return(_currentlyRead - _offset); }
public void TestStringStream_Observed_Replay_Mapped_Copy_InterruptedSupply() { ReplaySubject <string> subject = new ReplaySubject <string>(); Stream <string> stream = new Stream <string>(subject.AsObservable()); int mappingsHit = 0; IStream <string> mappedStream = stream.Map(s => { Debug.WriteLine("mapping " + s); mappingsHit++; return("m" + s); }); IStream <string> copyStream = mappedStream.Copy(); for (int i = 0; i < 100; i++) { subject.OnNext(i.ToString()); } for (int i = 0; i < 50; i++) { Assert.AreEqual("m" + i, copyStream.Read()); } Assert.AreEqual(50, mappingsHit); for (int i = 100; i < 200; i++) { subject.OnNext(i.ToString()); } for (int i = 50; i < 200; i++) { Assert.AreEqual("m" + i, copyStream.Read()); } Assert.AreEqual(200, mappingsHit); Assert.AreEqual(200, copyStream.Count()); }
public void TestSensorMultipleStreamCreation() { Sensor <FileInfo> sensor = Sensor <FileInfo> .Create( FileSensor.Create, SensorParams.Create( SensorParams.Keys.Path, "", ResourceLocator.Path(typeof(Resources), "rec-center-hourly-small.Csv"))); HTMSensor <FileInfo> htmSensor = (HTMSensor <FileInfo>)sensor; htmSensor.InitEncoder(GetTestEncoderParams()); // Ensure that the HTMSensor's output stream can be retrieved more than once. IStream <int[]> outputStream = htmSensor.GetOutputStream(); IStream <int[]> outputStream2 = htmSensor.GetOutputStream(); IStream <int[]> outputStream3 = htmSensor.GetOutputStream(); // Check to make sure above multiple retrieval doesn't flag the underlying stream as operated upon Assert.IsFalse(htmSensor.IsTerminal()); Assert.AreEqual(17, outputStream.Count()); //After the above we cannot request a new stream, so this will fail //however, the above streams that were already requested should be unaffected. Assert.IsTrue(htmSensor.IsTerminal(), "Terminal sensor stream expected"); try { //@SuppressWarnings("unused") IStream <int[]> outputStream4 = (Stream <int[]>)htmSensor.GetOutputStream(); Assert.Fail(); } catch (Exception e) { Assert.AreEqual("Stream is already \"terminal\" (operated upon or empty)", e.Message); } //These Streams were created before operating on a stream Assert.AreEqual(17, outputStream2.Count()); Assert.AreEqual(17, outputStream3.Count()); // Verify that different streams are retrieved. Assert.IsFalse(outputStream.GetHashCode() == outputStream2.GetHashCode()); Assert.IsFalse(outputStream2.GetHashCode() == outputStream3.GetHashCode()); }
public void TestStringStream_Observed_Replay_Mapped_Copy() { ReplaySubject <string> subject = new ReplaySubject <string>(); Stream <string> stream = new Stream <string>(subject.AsObservable()); IStream <int[]> mappedStream = stream.Map(s => new int[int.Parse(s)]); IStream <int[]> copyStream = mappedStream.Copy(); subject.OnNext("1"); Assert.IsTrue(new int[1].SequenceEqual(copyStream.Read())); subject.OnNext("2"); Assert.IsTrue(new int[2].SequenceEqual(copyStream.Read())); Assert.AreEqual(2, copyStream.Count()); }
public void TestStringStream_Observed_Replay_Mapped() { ReplaySubject <string> subject = new ReplaySubject <string>(); Stream <string> stream = new Stream <string>(subject.AsObservable()); IStream <int[]> mappedStream = stream.Map(s => new int[int.Parse(s)]); subject.OnNext("1"); Debug.WriteLine("> Reading from mapped stream"); Assert.IsTrue(new int[1].SequenceEqual(mappedStream.Read())); Debug.WriteLine("> Reading from base stream"); Assert.AreEqual(null, stream.Read()); subject.OnNext("2"); Debug.WriteLine("> Reading from base stream"); Assert.AreEqual("2", stream.Read()); Debug.WriteLine("> Reading from mapped stream"); Assert.AreEqual(null, mappedStream.Read()); Assert.AreEqual(2, stream.Count()); Assert.AreEqual(2, mappedStream.Count()); }