1 /* 2 * Copyright 2022 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.google.android.libraries.mobiledatadownload.internal.util; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import com.google.common.collect.ImmutableList; 21 import com.google.common.collect.Ordering; 22 import java.util.Comparator; 23 import org.junit.Test; 24 import org.junit.runner.RunWith; 25 import org.robolectric.RobolectricTestRunner; 26 27 @RunWith(RobolectricTestRunner.class) 28 public final class EitherTest { 29 30 // ImmutableList ensures the Either List isn't modified by sortedEquals. 31 private static final ImmutableList<String> LIST = ImmutableList.of("a", "b", "c"); 32 private static final ImmutableList<String> JUMBLED_LIST = ImmutableList.of("c", "a", "b"); 33 private static final ImmutableList<String> OTHER_LIST = ImmutableList.of("c"); 34 35 private static final Comparator<String> COMPARATOR = Ordering.natural(); 36 37 @Test sortedEquals_comparesSortedList()38 public void sortedEquals_comparesSortedList() throws Exception { 39 assertThat(Either.sortedEquals(Either.makeLeft(LIST), Either.makeLeft(LIST), COMPARATOR)) 40 .isTrue(); 41 assertThat( 42 Either.sortedEquals(Either.makeLeft(LIST), Either.makeLeft(JUMBLED_LIST), COMPARATOR)) 43 .isTrue(); 44 45 assertThat(Either.sortedEquals(Either.makeLeft(LIST), Either.makeLeft(OTHER_LIST), COMPARATOR)) 46 .isFalse(); 47 } 48 49 @Test sortedEquals_handlesNull()50 public void sortedEquals_handlesNull() throws Exception { 51 assertThat(Either.sortedEquals(Either.makeLeft(LIST), null, COMPARATOR)).isFalse(); 52 assertThat(Either.sortedEquals(Either.makeLeft(LIST), Either.makeLeft(null), COMPARATOR)) 53 .isFalse(); 54 55 assertThat(Either.sortedEquals(Either.makeLeft(null), Either.makeLeft(null), COMPARATOR)) 56 .isTrue(); 57 assertThat(Either.sortedEquals(null, null, COMPARATOR)).isTrue(); 58 } 59 } 60