|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using Aurio.Streams; |
| 6 | +using Aurio.Streams.Generic; |
| 7 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 8 | + |
| 9 | +namespace Aurio.UnitTest.Streams.Generic; |
| 10 | + |
| 11 | +[TestClass] |
| 12 | +public class AudioDataStreamTests |
| 13 | +{ |
| 14 | + [TestMethod] |
| 15 | + public void CanReadSeekWriteFlags() |
| 16 | + { |
| 17 | + var samples = new float[] { 0.1f, -0.2f, 0.3f, -0.4f }; |
| 18 | + using var src = new MemorySourceStream(samples, 44100, 1); |
| 19 | + using var stream = new AudioDataStream(src); |
| 20 | + |
| 21 | + Assert.IsTrue(stream.CanRead); |
| 22 | + Assert.IsTrue(stream.CanSeek); |
| 23 | + Assert.IsFalse(stream.CanWrite); |
| 24 | + } |
| 25 | + |
| 26 | + [TestMethod] |
| 27 | + public void LengthAndPositionExposeUnderlyingStream() |
| 28 | + { |
| 29 | + var samples = new float[] { 0.1f, 0.2f, 0.3f, 0.4f }; |
| 30 | + using var src = new MemorySourceStream(samples, 48000, 1); |
| 31 | + using var stream = new AudioDataStream(src); |
| 32 | + |
| 33 | + Assert.AreEqual(src.Length, stream.Length); |
| 34 | + Assert.AreEqual(0L, stream.Position); |
| 35 | + |
| 36 | + stream.Position = 4; // advance by one float sample (4 bytes) |
| 37 | + Assert.AreEqual(4L, stream.Position); |
| 38 | + Assert.AreEqual(4L, src.Position); |
| 39 | + } |
| 40 | + |
| 41 | + [TestMethod] |
| 42 | + public void ReadPassesThroughData() |
| 43 | + { |
| 44 | + // Prepare 4 float samples => 16 bytes |
| 45 | + var input = new float[] { 0.5f, -0.25f, 0.75f, -1.0f }; |
| 46 | + var inputBytes = MemoryMarshal.AsBytes(input.AsSpan()).ToArray(); |
| 47 | + using var src = new MemorySourceStream(input, 44100, 1); |
| 48 | + using var stream = new AudioDataStream(src); |
| 49 | + |
| 50 | + var buffer = new byte[16]; |
| 51 | + var read = stream.Read(buffer, 0, buffer.Length); |
| 52 | + |
| 53 | + CollectionAssert.AreEqual(inputBytes, buffer); |
| 54 | + } |
| 55 | + |
| 56 | + [TestMethod] |
| 57 | + public void SeekChangesPositionCorrectly() |
| 58 | + { |
| 59 | + using var sine = new SineGeneratorStream(44100, 440, TimeSpan.FromSeconds(1)); |
| 60 | + using var stream = new AudioDataStream(sine); |
| 61 | + |
| 62 | + // Seek from begin |
| 63 | + var pos = stream.Seek(100, SeekOrigin.Begin); |
| 64 | + Assert.AreEqual(100L, pos); |
| 65 | + Assert.AreEqual(100L, stream.Position); |
| 66 | + |
| 67 | + // Seek relative to current |
| 68 | + pos = stream.Seek(50, SeekOrigin.Current); |
| 69 | + Assert.AreEqual(150L, pos); |
| 70 | + |
| 71 | + // Seek relative to end (negative offset) |
| 72 | + pos = stream.Seek(-200, SeekOrigin.End); |
| 73 | + Assert.AreEqual(sine.Length - 200, pos); |
| 74 | + } |
| 75 | + |
| 76 | + [TestMethod] |
| 77 | + [ExpectedException(typeof(NotImplementedException))] |
| 78 | + public void SetLengthNotSupported() |
| 79 | + { |
| 80 | + using var src = new MemorySourceStream(new float[] { 0.1f, 0.2f }, 44100, 1); |
| 81 | + using var stream = new AudioDataStream(src); |
| 82 | + |
| 83 | + stream.SetLength(0); |
| 84 | + } |
| 85 | + |
| 86 | + [TestMethod] |
| 87 | + [ExpectedException(typeof(NotImplementedException))] |
| 88 | + public void WriteNotSupported() |
| 89 | + { |
| 90 | + using var src = new MemorySourceStream(new float[] { 0.1f, 0.2f }, 44100, 1); |
| 91 | + using var stream = new AudioDataStream(src); |
| 92 | + |
| 93 | + stream.Write(new byte[4], 0, 4); |
| 94 | + } |
| 95 | +} |
0 commit comments