using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Numerics; namespace R3.Tests.OperatorTests; public class SumTest { [Fact] public async Task Empty() { (await Observable.Empty().SumAsync()).Should().Be(0); (await Observable.Empty().SumAsync()).Value.Should().Be(0); } [Fact] public async Task One() { (await Observable.Return(999).SumAsync()).Should().Be(999); (await Observable.Return(new TestNumber(999)).SumAsync()).Value.Should().Be(999); } [Fact] public async Task Error() { var error = Observable.Range(0, 10).Select(x => { if (x == 3) throw new Exception("foo"); return x; }); await Assert.ThrowsAsync(async () => await error.SumAsync()); await Assert.ThrowsAsync(async () => await error.OnErrorResumeAsFailure().SumAsync()); } [Fact] public async Task MultipleValues() { var source = new int[] { 1, 10, 1, 3, 4, 6, 7, 4 }.ToObservable(); (await source.SumAsync()).Should().Be(36); var source2 = source.Select(x => new TestNumber(x)); (await source2.SumAsync()).Value.Should().Be(36); } [Fact] public async Task WithSelector() { var source = new[] { 1, 10, 1, 3, 4, 6, 7, 4 }.ToObservable(); (await source.SumAsync(x => x * 10f)).Should().Be(360f); // (await source.SumAsync(x => new TestNumber(x))).Value.Should().Be(36); } [Fact] public async Task WithSelectorError() { var error = Observable.Range(1, 10).Select(x => { if (x == 3) throw new Exception("foo"); return x; }); await Assert.ThrowsAsync(async () => await error.SumAsync(x => x)); await Assert.ThrowsAsync(async () => await error.OnErrorResumeAsFailure().SumAsync(x => x)); await Assert.ThrowsAsync(async () => await Observable.Range(0, 10).SumAsync(x => throw new Exception("bra"))); } record struct TestNumber(int Value) : INumber { public static TestNumber One => throw new NotImplementedException(); public static int Radix => throw new NotImplementedException(); public static TestNumber Zero => new TestNumber(0); public static TestNumber AdditiveIdentity => throw new NotImplementedException(); public static TestNumber MultiplicativeIdentity => throw new NotImplementedException(); static TestNumber INumberBase.One => throw new NotImplementedException(); static int INumberBase.Radix => throw new NotImplementedException(); static TestNumber INumberBase.Zero => new TestNumber(0); static TestNumber IAdditiveIdentity.AdditiveIdentity => throw new NotImplementedException(); static TestNumber IMultiplicativeIdentity.MultiplicativeIdentity => throw new NotImplementedException(); public static TestNumber Abs(TestNumber value) { throw new NotImplementedException(); } public static bool IsCanonical(TestNumber value) { throw new NotImplementedException(); } public static bool IsComplexNumber(TestNumber value) { throw new NotImplementedException(); } public static bool IsEvenInteger(TestNumber value) { throw new NotImplementedException(); } public static bool IsFinite(TestNumber value) { throw new NotImplementedException(); } public static bool IsImaginaryNumber(TestNumber value) { throw new NotImplementedException(); } public static bool IsInfinity(TestNumber value) { throw new NotImplementedException(); } public static bool IsInteger(TestNumber value) { throw new NotImplementedException(); } public static bool IsNaN(TestNumber value) { throw new NotImplementedException(); } public static bool IsNegative(TestNumber value) { throw new NotImplementedException(); } public static bool IsNegativeInfinity(TestNumber value) { throw new NotImplementedException(); } public static bool IsNormal(TestNumber value) { throw new NotImplementedException(); } public static bool IsOddInteger(TestNumber value) { throw new NotImplementedException(); } public static bool IsPositive(TestNumber value) { throw new NotImplementedException(); } public static bool IsPositiveInfinity(TestNumber value) { throw new NotImplementedException(); } public static bool IsRealNumber(TestNumber value) { throw new NotImplementedException(); } public static bool IsSubnormal(TestNumber value) { throw new NotImplementedException(); } public static bool IsZero(TestNumber value) { throw new NotImplementedException(); } public static TestNumber MaxMagnitude(TestNumber x, TestNumber y) { throw new NotImplementedException(); } public static TestNumber MaxMagnitudeNumber(TestNumber x, TestNumber y) { throw new NotImplementedException(); } public static TestNumber MinMagnitude(TestNumber x, TestNumber y) { throw new NotImplementedException(); } public static TestNumber MinMagnitudeNumber(TestNumber x, TestNumber y) { throw new NotImplementedException(); } public static TestNumber Parse(ReadOnlySpan s, NumberStyles style, IFormatProvider? provider) { throw new NotImplementedException(); } public static TestNumber Parse(string s, NumberStyles style, IFormatProvider? provider) { throw new NotImplementedException(); } public static TestNumber Parse(ReadOnlySpan s, IFormatProvider? provider) { throw new NotImplementedException(); } public static TestNumber Parse(string s, IFormatProvider? provider) { throw new NotImplementedException(); } public static bool TryParse(ReadOnlySpan s, NumberStyles style, IFormatProvider? provider, [MaybeNullWhen(false)] out TestNumber result) { throw new NotImplementedException(); } public static bool TryParse([NotNullWhen(true)] string? s, NumberStyles style, IFormatProvider? provider, [MaybeNullWhen(false)] out TestNumber result) { throw new NotImplementedException(); } public static bool TryParse(ReadOnlySpan s, IFormatProvider? provider, [MaybeNullWhen(false)] out TestNumber result) { throw new NotImplementedException(); } public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out TestNumber result) { throw new NotImplementedException(); } static TestNumber INumberBase.Abs(TestNumber value) { throw new NotImplementedException(); } static bool INumberBase.IsCanonical(TestNumber value) { throw new NotImplementedException(); } static bool INumberBase.IsComplexNumber(TestNumber value) { throw new NotImplementedException(); } static bool INumberBase.IsEvenInteger(TestNumber value) { throw new NotImplementedException(); } static bool INumberBase.IsFinite(TestNumber value) { throw new NotImplementedException(); } static bool INumberBase.IsImaginaryNumber(TestNumber value) { throw new NotImplementedException(); } static bool INumberBase.IsInfinity(TestNumber value) { throw new NotImplementedException(); } static bool INumberBase.IsInteger(TestNumber value) { throw new NotImplementedException(); } static bool INumberBase.IsNaN(TestNumber value) { throw new NotImplementedException(); } static bool INumberBase.IsNegative(TestNumber value) { throw new NotImplementedException(); } static bool INumberBase.IsNegativeInfinity(TestNumber value) { throw new NotImplementedException(); } static bool INumberBase.IsNormal(TestNumber value) { throw new NotImplementedException(); } static bool INumberBase.IsOddInteger(TestNumber value) { throw new NotImplementedException(); } static bool INumberBase.IsPositive(TestNumber value) { throw new NotImplementedException(); } static bool INumberBase.IsPositiveInfinity(TestNumber value) { throw new NotImplementedException(); } static bool INumberBase.IsRealNumber(TestNumber value) { throw new NotImplementedException(); } static bool INumberBase.IsSubnormal(TestNumber value) { throw new NotImplementedException(); } static bool INumberBase.IsZero(TestNumber value) { throw new NotImplementedException(); } static TestNumber INumberBase.MaxMagnitude(TestNumber x, TestNumber y) { throw new NotImplementedException(); } static TestNumber INumberBase.MaxMagnitudeNumber(TestNumber x, TestNumber y) { throw new NotImplementedException(); } static TestNumber INumberBase.MinMagnitude(TestNumber x, TestNumber y) { throw new NotImplementedException(); } static TestNumber INumberBase.MinMagnitudeNumber(TestNumber x, TestNumber y) { throw new NotImplementedException(); } static TestNumber INumberBase.Parse(ReadOnlySpan s, NumberStyles style, IFormatProvider? provider) { throw new NotImplementedException(); } static TestNumber INumberBase.Parse(string s, NumberStyles style, IFormatProvider? provider) { throw new NotImplementedException(); } static TestNumber ISpanParsable.Parse(ReadOnlySpan s, IFormatProvider? provider) { throw new NotImplementedException(); } static TestNumber IParsable.Parse(string s, IFormatProvider? provider) { throw new NotImplementedException(); } static bool INumberBase.TryConvertFromChecked(TOther value, out TestNumber result) { throw new NotImplementedException(); } static bool INumberBase.TryConvertFromSaturating(TOther value, out TestNumber result) { throw new NotImplementedException(); } static bool INumberBase.TryConvertFromTruncating(TOther value, out TestNumber result) { throw new NotImplementedException(); } static bool INumberBase.TryConvertToChecked(TestNumber value, out TOther result) { throw new NotImplementedException(); } static bool INumberBase.TryConvertToSaturating(TestNumber value, out TOther result) { throw new NotImplementedException(); } static bool INumberBase.TryConvertToTruncating(TestNumber value, out TOther result) { throw new NotImplementedException(); } static bool INumberBase.TryParse(ReadOnlySpan s, NumberStyles style, IFormatProvider? provider, out TestNumber result) { throw new NotImplementedException(); } static bool INumberBase.TryParse(string? s, NumberStyles style, IFormatProvider? provider, out TestNumber result) { throw new NotImplementedException(); } static bool ISpanParsable.TryParse(ReadOnlySpan s, IFormatProvider? provider, out TestNumber result) { throw new NotImplementedException(); } static bool IParsable.TryParse(string? s, IFormatProvider? provider, out TestNumber result) { throw new NotImplementedException(); } public int CompareTo(object? obj) { throw new NotImplementedException(); } public int CompareTo(TestNumber other) { throw new NotImplementedException(); } public string ToString(string? format, IFormatProvider? formatProvider) { throw new NotImplementedException(); } public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) { throw new NotImplementedException(); } int IComparable.CompareTo(object? obj) { throw new NotImplementedException(); } int IComparable.CompareTo(TestNumber other) { throw new NotImplementedException(); } bool IEquatable.Equals(TestNumber other) { throw new NotImplementedException(); } string IFormattable.ToString(string? format, IFormatProvider? formatProvider) { throw new NotImplementedException(); } bool ISpanFormattable.TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) { throw new NotImplementedException(); } public static TestNumber operator +(TestNumber value) { throw new NotImplementedException(); } static TestNumber IUnaryPlusOperators.operator +(TestNumber value) { throw new NotImplementedException(); } public static TestNumber operator +(TestNumber left, TestNumber right) => new(left.Value + right.Value); static TestNumber IAdditionOperators.operator +(TestNumber left, TestNumber right) { return new(left.Value + right.Value); } public static TestNumber operator -(TestNumber value) { throw new NotImplementedException(); } static TestNumber IUnaryNegationOperators.operator -(TestNumber value) { throw new NotImplementedException(); } public static TestNumber operator -(TestNumber left, TestNumber right) { throw new NotImplementedException(); } static TestNumber ISubtractionOperators.operator -(TestNumber left, TestNumber right) { throw new NotImplementedException(); } public static TestNumber operator ++(TestNumber value) { throw new NotImplementedException(); } static TestNumber IIncrementOperators.operator ++(TestNumber value) { throw new NotImplementedException(); } public static TestNumber operator --(TestNumber value) { throw new NotImplementedException(); } static TestNumber IDecrementOperators.operator --(TestNumber value) { throw new NotImplementedException(); } public static TestNumber operator *(TestNumber left, TestNumber right) { throw new NotImplementedException(); } static TestNumber IMultiplyOperators.operator *(TestNumber left, TestNumber right) { throw new NotImplementedException(); } public static TestNumber operator /(TestNumber left, TestNumber right) { throw new NotImplementedException(); } static TestNumber IDivisionOperators.operator /(TestNumber left, TestNumber right) { throw new NotImplementedException(); } public static TestNumber operator %(TestNumber left, TestNumber right) { throw new NotImplementedException(); } static TestNumber IModulusOperators.operator %(TestNumber left, TestNumber right) { throw new NotImplementedException(); } static bool IEqualityOperators.operator ==(TestNumber left, TestNumber right) { throw new NotImplementedException(); } static bool IEqualityOperators.operator !=(TestNumber left, TestNumber right) { throw new NotImplementedException(); } public static bool operator <(TestNumber left, TestNumber right) { throw new NotImplementedException(); } static bool IComparisonOperators.operator <(TestNumber left, TestNumber right) { throw new NotImplementedException(); } public static bool operator >(TestNumber left, TestNumber right) { throw new NotImplementedException(); } static bool IComparisonOperators.operator >(TestNumber left, TestNumber right) { throw new NotImplementedException(); } public static bool operator <=(TestNumber left, TestNumber right) { throw new NotImplementedException(); } static bool IComparisonOperators.operator <=(TestNumber left, TestNumber right) { throw new NotImplementedException(); } public static bool operator >=(TestNumber left, TestNumber right) { throw new NotImplementedException(); } static bool IComparisonOperators.operator >=(TestNumber left, TestNumber right) { throw new NotImplementedException(); } } }