content: /**
   * Copyright (c) 2016-present, RxJava Contributors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
   * compliance with the License. You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software distributed under the License is
   * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
   * the License for the specific language governing permissions and limitations under the License.
   */
  
  package io.reactivex.interop.internal.operators;
  
  import static io.reactivex.interop.RxJava3Interop.*;
  import static org.junit.Assert.assertEquals;
  import static org.mockito.ArgumentMatchers.any;
  import static org.mockito.Mockito.*;
  
  import java.util.concurrent.atomic.AtomicInteger;
  
  import org.junit.*;
  import org.reactivestreams.Publisher;
  
  import io.reactivex.common.Schedulers;
  import io.reactivex.common.exceptions.TestException;
  import io.reactivex.common.functions.*;
  import io.reactivex.flowable.Flowable;
  import io.reactivex.interop.TestHelper;
  import io.reactivex.observable.*;
  
  public class FlowableReduceTest {
      SingleObserver<Object> singleObserver;
  
      @Before
      public void before() {
          singleObserver = TestHelper.mockSingleObserver();
      }
  
      BiFunction<Integer, Integer, Integer> sum = new BiFunction<Integer, Integer, Integer>() {
          @Override
          public Integer apply(Integer t1, Integer t2) {
              return t1 + t2;
          }
      };
  
      @Test
      public void testAggregateAsIntSum() {
  
          Single<Integer> result = reduce(Flowable.just(1, 2, 3, 4, 5), 0, sum)
                  .map(new Function<Integer, Integer>() {
                      @Override
                      public Integer apply(Integer v) {
                          return v;
                      }
                  });
  
          result.subscribe(singleObserver);
  
          verify(singleObserver).onSuccess(1 + 2 + 3 + 4 + 5);
          verify(singleObserver, never()).onError(any(Throwable.class));
      }
  
      @Test
      public void testAggregateAsIntSumSourceThrows() {
          Single<Integer> result = reduce(Flowable.concat(Flowable.just(1, 2, 3, 4, 5),
                  Flowable.<Integer> error(new TestException()))
                  , 0, sum).map(new Function<Integer, Integer>() {
                      @Override
                      public Integer apply(Integer v) {
                          return v;
                      }
                  });
  
          result.subscribe(singleObserver);
  
          verify(singleObserver, never()).onSuccess(any());
          verify(singleObserver, times(1)).onError(any(TestException.class));
      }
  
      @Test
      public void testAggregateAsIntSumAccumulatorThrows() {
          BiFunction<Integer, Integer, Integer> sumErr = new BiFunction<Integer, Integer, Integer>() {
              @Override
              public Integer apply(Integer t1, Integer t2) {
                  throw new TestException();
              }
          };
  
          Single<Integer> result = reduce(Flowable.just(1, 2, 3, 4, 5)
                  , 0, sumErr).map(new Function<Integer, Integer>() {
                      @Override
                      public Integer apply(Integer v) {
                          return v;
                      }
                  });
  
          result.subscribe(singleObserver);
  
          verify(singleObserver, never()).onSuccess(any());
          verify(singleObserver, times(1)).onError(any(TestException.class));
      }
  
      @Test
      public void testAggregateAsIntSumResultSelectorThrows() {
  
          Function<Integer, Integer> error = new Function<Integer, Integer>() {
  
              @Override
              public Integer apply(Integer t1) {
                  throw new TestException();
              }
          };
  
          Single<Integer> result = reduce(Flowable.just(1, 2, 3, 4, 5)
                  , 0, sum).map(error);
  
          result.subscribe(singleObserver);
  
          verify(singleObserver, never()).onSuccess(any());
          verify(singleObserver, times(1)).onError(any(TestException.class));
      }
  
      @Test
      public void testBackpressureWithNoInitialValue() throws InterruptedException {
          Flowable<Integer> source = Flowable.just(1, 2, 3, 4, 5, 6);
          Maybe<Integer> reduced = reduce(source, sum);
  
          Integer r = reduced.blockingGet();
          assertEquals(21, r.intValue());
      }
  
      @Test
      public void testBackpressureWithInitialValue() throws InterruptedException {
          Flowable<Integer> source = Flowable.just(1, 2, 3, 4, 5, 6);
          Single<Integer> reduced = reduce(source, 0, sum);
  
          Integer r = reduced.blockingGet();
          assertEquals(21, r.intValue());
      }
  
      @Test
      public void dispose() {
          TestHelper.checkDisposed(reduce(Flowable.range(1, 2), sum));
      }
  
      @Test
      public void doubleOnSubscribe() {
          TestHelper.checkDoubleOnSubscribeFlowableToMaybe(new Function<Flowable<Integer>, MaybeSource<Integer>>() {
              @Override
              public MaybeSource<Integer> apply(Flowable<Integer> f) throws Exception {
                  return reduce(f, sum);
              }
          });
      }
  
      @Test
      public void error() {
          reduce(Flowable.<Integer>error(new TestException())
          , sum)
          .test()
          .assertFailure(TestException.class);
      }
  
      @Test
      public void empty() {
          reduce(Flowable.<Integer>empty()
          , sum)
          .test()
          .assertResult();
      }
  
      @Test
      public void badSource() {
          TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() {
              @Override
              public Object apply(Flowable<Integer> f) throws Exception {
                  return reduce(f, sum);
              }
          }, false, 1, 1, 1);
      }
  
      @Test
      public void reducerThrows() {
          reduce(Flowable.just(1, 2)
          , new BiFunction<Integer, Integer, Integer>() {
              @Override
              public Integer apply(Integer a, Integer b) throws Exception {
                  throw new TestException();
              }
          })
          .test()
          .assertFailure(TestException.class);
      }
  
      /**
       * https://gist.github.com/jurna/353a2bd8ff83f0b24f0b5bc772077d61
       */
      @Test
      public void shouldReduceTo10Events() {
          final AtomicInteger count = new AtomicInteger();
  
          Flowable.range(0, 10).flatMap(new Function<Integer, Publisher<String>>() {
              @Override
              public Publisher<String> apply(final Integer x) throws Exception {
                  return toFlowable(reduce(Flowable.range(0, 2)
                      .map(new Function<Integer, String>() {
                      @Override
                      public String apply(Integer y) throws Exception {
                          return blockingOp(x, y);
                      }
                  }).subscribeOn(Schedulers.io())
                  , new BiFunction<String, String, String>() {
                      @Override
                      public String apply(String l, String r) throws Exception {
                          return l + "_" + r;
                      }
                  })
                  .doOnSuccess(new Consumer<String>() {
                      @Override
                      public void accept(String s) throws Exception {
                          count.incrementAndGet();
                          System.out.println("Completed with " + s);}
                  }));
              }
          }
          ).blockingLast();
  
          assertEquals(10, count.get());
      }
  
      static String blockingOp(Integer x, Integer y) {
          try {
              Thread.sleep(100);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
          return "x" + x + "y" + y;
      }
  }
  