Skip to content

Commit

Permalink
Migrate the primitives tests to Truth.
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 441320218
  • Loading branch information
eamonnmcmanus authored and Google Java Core Libraries committed Apr 13, 2022
1 parent da1aa61 commit 9f8615e
Show file tree
Hide file tree
Showing 33 changed files with 3,271 additions and 3,023 deletions.
224 changes: 113 additions & 111 deletions android/guava-tests/test/com/google/common/primitives/BooleansTest.java

Large diffs are not rendered by default.

188 changes: 95 additions & 93 deletions android/guava-tests/test/com/google/common/primitives/BytesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.common.primitives;

import static com.google.common.truth.Truth.assertThat;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.testing.Helpers;
Expand All @@ -41,97 +43,97 @@ public class BytesTest extends TestCase {

public void testHashCode() {
for (byte value : VALUES) {
assertEquals(((Byte) value).hashCode(), Bytes.hashCode(value));
assertThat(Bytes.hashCode(value)).isEqualTo(((Byte) value).hashCode());
}
}

public void testContains() {
assertFalse(Bytes.contains(EMPTY, (byte) 1));
assertFalse(Bytes.contains(ARRAY1, (byte) 2));
assertFalse(Bytes.contains(ARRAY234, (byte) 1));
assertTrue(Bytes.contains(new byte[] {(byte) -1}, (byte) -1));
assertTrue(Bytes.contains(ARRAY234, (byte) 2));
assertTrue(Bytes.contains(ARRAY234, (byte) 3));
assertTrue(Bytes.contains(ARRAY234, (byte) 4));
assertThat(Bytes.contains(EMPTY, (byte) 1)).isFalse();
assertThat(Bytes.contains(ARRAY1, (byte) 2)).isFalse();
assertThat(Bytes.contains(ARRAY234, (byte) 1)).isFalse();
assertThat(Bytes.contains(new byte[] {(byte) -1}, (byte) -1)).isTrue();
assertThat(Bytes.contains(ARRAY234, (byte) 2)).isTrue();
assertThat(Bytes.contains(ARRAY234, (byte) 3)).isTrue();
assertThat(Bytes.contains(ARRAY234, (byte) 4)).isTrue();
}

public void testIndexOf() {
assertEquals(-1, Bytes.indexOf(EMPTY, (byte) 1));
assertEquals(-1, Bytes.indexOf(ARRAY1, (byte) 2));
assertEquals(-1, Bytes.indexOf(ARRAY234, (byte) 1));
assertEquals(0, Bytes.indexOf(new byte[] {(byte) -1}, (byte) -1));
assertEquals(0, Bytes.indexOf(ARRAY234, (byte) 2));
assertEquals(1, Bytes.indexOf(ARRAY234, (byte) 3));
assertEquals(2, Bytes.indexOf(ARRAY234, (byte) 4));
assertEquals(1, Bytes.indexOf(new byte[] {(byte) 2, (byte) 3, (byte) 2, (byte) 3}, (byte) 3));
assertThat(Bytes.indexOf(EMPTY, (byte) 1)).isEqualTo(-1);
assertThat(Bytes.indexOf(ARRAY1, (byte) 2)).isEqualTo(-1);
assertThat(Bytes.indexOf(ARRAY234, (byte) 1)).isEqualTo(-1);
assertThat(Bytes.indexOf(new byte[] {(byte) -1}, (byte) -1)).isEqualTo(0);
assertThat(Bytes.indexOf(ARRAY234, (byte) 2)).isEqualTo(0);
assertThat(Bytes.indexOf(ARRAY234, (byte) 3)).isEqualTo(1);
assertThat(Bytes.indexOf(ARRAY234, (byte) 4)).isEqualTo(2);
assertThat(Bytes.indexOf(new byte[] {(byte) 2, (byte) 3, (byte) 2, (byte) 3}, (byte) 3))
.isEqualTo(1);
}

public void testIndexOf_arrayTarget() {
assertEquals(0, Bytes.indexOf(EMPTY, EMPTY));
assertEquals(0, Bytes.indexOf(ARRAY234, EMPTY));
assertEquals(-1, Bytes.indexOf(EMPTY, ARRAY234));
assertEquals(-1, Bytes.indexOf(ARRAY234, ARRAY1));
assertEquals(-1, Bytes.indexOf(ARRAY1, ARRAY234));
assertEquals(0, Bytes.indexOf(ARRAY1, ARRAY1));
assertEquals(0, Bytes.indexOf(ARRAY234, ARRAY234));
assertEquals(0, Bytes.indexOf(ARRAY234, new byte[] {(byte) 2, (byte) 3}));
assertEquals(1, Bytes.indexOf(ARRAY234, new byte[] {(byte) 3, (byte) 4}));
assertEquals(1, Bytes.indexOf(ARRAY234, new byte[] {(byte) 3}));
assertEquals(2, Bytes.indexOf(ARRAY234, new byte[] {(byte) 4}));
assertEquals(
1,
Bytes.indexOf(
new byte[] {(byte) 2, (byte) 3, (byte) 3, (byte) 3, (byte) 3}, new byte[] {(byte) 3}));
assertEquals(
2,
Bytes.indexOf(
new byte[] {(byte) 2, (byte) 3, (byte) 2, (byte) 3, (byte) 4, (byte) 2, (byte) 3},
new byte[] {(byte) 2, (byte) 3, (byte) 4}));
assertEquals(
1,
Bytes.indexOf(
new byte[] {(byte) 2, (byte) 2, (byte) 3, (byte) 4, (byte) 2, (byte) 3, (byte) 4},
new byte[] {(byte) 2, (byte) 3, (byte) 4}));
assertEquals(
-1,
Bytes.indexOf(
new byte[] {(byte) 4, (byte) 3, (byte) 2}, new byte[] {(byte) 2, (byte) 3, (byte) 4}));
assertThat(Bytes.indexOf(EMPTY, EMPTY)).isEqualTo(0);
assertThat(Bytes.indexOf(ARRAY234, EMPTY)).isEqualTo(0);
assertThat(Bytes.indexOf(EMPTY, ARRAY234)).isEqualTo(-1);
assertThat(Bytes.indexOf(ARRAY234, ARRAY1)).isEqualTo(-1);
assertThat(Bytes.indexOf(ARRAY1, ARRAY234)).isEqualTo(-1);
assertThat(Bytes.indexOf(ARRAY1, ARRAY1)).isEqualTo(0);
assertThat(Bytes.indexOf(ARRAY234, ARRAY234)).isEqualTo(0);
assertThat(Bytes.indexOf(ARRAY234, new byte[] {(byte) 2, (byte) 3})).isEqualTo(0);
assertThat(Bytes.indexOf(ARRAY234, new byte[] {(byte) 3, (byte) 4})).isEqualTo(1);
assertThat(Bytes.indexOf(ARRAY234, new byte[] {(byte) 3})).isEqualTo(1);
assertThat(Bytes.indexOf(ARRAY234, new byte[] {(byte) 4})).isEqualTo(2);
assertThat(
Bytes.indexOf(
new byte[] {(byte) 2, (byte) 3, (byte) 3, (byte) 3, (byte) 3},
new byte[] {(byte) 3}))
.isEqualTo(1);
assertThat(
Bytes.indexOf(
new byte[] {(byte) 2, (byte) 3, (byte) 2, (byte) 3, (byte) 4, (byte) 2, (byte) 3},
new byte[] {(byte) 2, (byte) 3, (byte) 4}))
.isEqualTo(2);
assertThat(
Bytes.indexOf(
new byte[] {(byte) 2, (byte) 2, (byte) 3, (byte) 4, (byte) 2, (byte) 3, (byte) 4},
new byte[] {(byte) 2, (byte) 3, (byte) 4}))
.isEqualTo(1);
assertThat(
Bytes.indexOf(
new byte[] {(byte) 4, (byte) 3, (byte) 2},
new byte[] {(byte) 2, (byte) 3, (byte) 4}))
.isEqualTo(-1);
}

public void testLastIndexOf() {
assertEquals(-1, Bytes.lastIndexOf(EMPTY, (byte) 1));
assertEquals(-1, Bytes.lastIndexOf(ARRAY1, (byte) 2));
assertEquals(-1, Bytes.lastIndexOf(ARRAY234, (byte) 1));
assertEquals(0, Bytes.lastIndexOf(new byte[] {(byte) -1}, (byte) -1));
assertEquals(0, Bytes.lastIndexOf(ARRAY234, (byte) 2));
assertEquals(1, Bytes.lastIndexOf(ARRAY234, (byte) 3));
assertEquals(2, Bytes.lastIndexOf(ARRAY234, (byte) 4));
assertEquals(
3, Bytes.lastIndexOf(new byte[] {(byte) 2, (byte) 3, (byte) 2, (byte) 3}, (byte) 3));
assertThat(Bytes.lastIndexOf(EMPTY, (byte) 1)).isEqualTo(-1);
assertThat(Bytes.lastIndexOf(ARRAY1, (byte) 2)).isEqualTo(-1);
assertThat(Bytes.lastIndexOf(ARRAY234, (byte) 1)).isEqualTo(-1);
assertThat(Bytes.lastIndexOf(new byte[] {(byte) -1}, (byte) -1)).isEqualTo(0);
assertThat(Bytes.lastIndexOf(ARRAY234, (byte) 2)).isEqualTo(0);
assertThat(Bytes.lastIndexOf(ARRAY234, (byte) 3)).isEqualTo(1);
assertThat(Bytes.lastIndexOf(ARRAY234, (byte) 4)).isEqualTo(2);
assertThat(Bytes.lastIndexOf(new byte[] {(byte) 2, (byte) 3, (byte) 2, (byte) 3}, (byte) 3))
.isEqualTo(3);
}

public void testConcat() {
assertTrue(Arrays.equals(EMPTY, Bytes.concat()));
assertTrue(Arrays.equals(EMPTY, Bytes.concat(EMPTY)));
assertTrue(Arrays.equals(EMPTY, Bytes.concat(EMPTY, EMPTY, EMPTY)));
assertTrue(Arrays.equals(ARRAY1, Bytes.concat(ARRAY1)));
assertNotSame(ARRAY1, Bytes.concat(ARRAY1));
assertTrue(Arrays.equals(ARRAY1, Bytes.concat(EMPTY, ARRAY1, EMPTY)));
assertTrue(
Arrays.equals(
new byte[] {(byte) 1, (byte) 1, (byte) 1}, Bytes.concat(ARRAY1, ARRAY1, ARRAY1)));
assertTrue(
Arrays.equals(
new byte[] {(byte) 1, (byte) 2, (byte) 3, (byte) 4}, Bytes.concat(ARRAY1, ARRAY234)));
assertThat(Bytes.concat()).isEqualTo(EMPTY);
assertThat(Bytes.concat(EMPTY)).isEqualTo(EMPTY);
assertThat(Bytes.concat(EMPTY, EMPTY, EMPTY)).isEqualTo(EMPTY);
assertThat(Bytes.concat(ARRAY1)).isEqualTo(ARRAY1);
assertThat(Bytes.concat(ARRAY1)).isNotSameInstanceAs(ARRAY1);
assertThat(Bytes.concat(EMPTY, ARRAY1, EMPTY)).isEqualTo(ARRAY1);
assertThat(Bytes.concat(ARRAY1, ARRAY1, ARRAY1))
.isEqualTo(new byte[] {(byte) 1, (byte) 1, (byte) 1});
assertThat(Bytes.concat(ARRAY1, ARRAY234))
.isEqualTo(new byte[] {(byte) 1, (byte) 2, (byte) 3, (byte) 4});
}

public void testEnsureCapacity() {
assertSame(EMPTY, Bytes.ensureCapacity(EMPTY, 0, 1));
assertSame(ARRAY1, Bytes.ensureCapacity(ARRAY1, 0, 1));
assertSame(ARRAY1, Bytes.ensureCapacity(ARRAY1, 1, 1));
assertTrue(
Arrays.equals(
new byte[] {(byte) 1, (byte) 0, (byte) 0}, Bytes.ensureCapacity(ARRAY1, 2, 1)));
assertThat(Bytes.ensureCapacity(EMPTY, 0, 1)).isSameInstanceAs(EMPTY);
assertThat(Bytes.ensureCapacity(ARRAY1, 0, 1)).isSameInstanceAs(ARRAY1);
assertThat(Bytes.ensureCapacity(ARRAY1, 1, 1)).isSameInstanceAs(ARRAY1);
assertThat(Bytes.ensureCapacity(ARRAY1, 2, 1))
.isEqualTo(new byte[] {(byte) 1, (byte) 0, (byte) 0});
}

public void testEnsureCapacity_fail() {
Expand All @@ -151,17 +153,17 @@ public void testEnsureCapacity_fail() {
public void testToArray() {
// need explicit type parameter to avoid javac warning!?
List<Byte> none = Arrays.<Byte>asList();
assertTrue(Arrays.equals(EMPTY, Bytes.toArray(none)));
assertThat(Bytes.toArray(none)).isEqualTo(EMPTY);

List<Byte> one = Arrays.asList((byte) 1);
assertTrue(Arrays.equals(ARRAY1, Bytes.toArray(one)));
assertThat(Bytes.toArray(one)).isEqualTo(ARRAY1);

byte[] array = {(byte) 0, (byte) 1, (byte) 0x55};

List<Byte> three = Arrays.asList((byte) 0, (byte) 1, (byte) 0x55);
assertTrue(Arrays.equals(array, Bytes.toArray(three)));
assertThat(Bytes.toArray(three)).isEqualTo(array);

assertTrue(Arrays.equals(array, Bytes.toArray(Bytes.asList(array))));
assertThat(Bytes.toArray(Bytes.asList(array))).isEqualTo(array);
}

public void testToArray_threadSafe() {
Expand All @@ -171,9 +173,9 @@ public void testToArray_threadSafe() {
Collection<Byte> misleadingSize = Helpers.misleadingSizeCollection(delta);
misleadingSize.addAll(list);
byte[] arr = Bytes.toArray(misleadingSize);
assertEquals(i, arr.length);
assertThat(arr).hasLength(i);
for (int j = 0; j < i; j++) {
assertEquals(VALUES[j], arr[j]);
assertThat(arr[j]).isEqualTo(VALUES[j]);
}
}
}
Expand All @@ -198,21 +200,21 @@ public void testToArray_withConversion() {
List<Long> longs = Arrays.asList((long) 0, (long) 1, (long) 2);
List<Double> doubles = Arrays.asList((double) 0, (double) 1, (double) 2);

assertTrue(Arrays.equals(array, Bytes.toArray(bytes)));
assertTrue(Arrays.equals(array, Bytes.toArray(shorts)));
assertTrue(Arrays.equals(array, Bytes.toArray(ints)));
assertTrue(Arrays.equals(array, Bytes.toArray(floats)));
assertTrue(Arrays.equals(array, Bytes.toArray(longs)));
assertTrue(Arrays.equals(array, Bytes.toArray(doubles)));
assertThat(Bytes.toArray(bytes)).isEqualTo(array);
assertThat(Bytes.toArray(shorts)).isEqualTo(array);
assertThat(Bytes.toArray(ints)).isEqualTo(array);
assertThat(Bytes.toArray(floats)).isEqualTo(array);
assertThat(Bytes.toArray(longs)).isEqualTo(array);
assertThat(Bytes.toArray(doubles)).isEqualTo(array);
}

public void testAsList_isAView() {
byte[] array = {(byte) 0, (byte) 1};
List<Byte> list = Bytes.asList(array);
list.set(0, (byte) 2);
assertTrue(Arrays.equals(new byte[] {(byte) 2, (byte) 1}, array));
assertThat(array).isEqualTo(new byte[] {(byte) 2, (byte) 1});
array[1] = (byte) 3;
assertEquals(Arrays.asList((byte) 2, (byte) 3), list);
assertThat(list).containsExactly((byte) 2, (byte) 3).inOrder();
}

public void testAsList_toArray_roundTrip() {
Expand All @@ -222,21 +224,21 @@ public void testAsList_toArray_roundTrip() {

// Make sure it returned a copy
list.set(0, (byte) 4);
assertTrue(Arrays.equals(new byte[] {(byte) 0, (byte) 1, (byte) 2}, newArray));
assertThat(newArray).isEqualTo(new byte[] {(byte) 0, (byte) 1, (byte) 2});
newArray[1] = (byte) 5;
assertEquals((byte) 1, (byte) list.get(1));
assertThat((byte) list.get(1)).isEqualTo((byte) 1);
}

// This test stems from a real bug found by andrewk
public void testAsList_subList_toArray_roundTrip() {
byte[] array = {(byte) 0, (byte) 1, (byte) 2, (byte) 3};
List<Byte> list = Bytes.asList(array);
assertTrue(Arrays.equals(new byte[] {(byte) 1, (byte) 2}, Bytes.toArray(list.subList(1, 3))));
assertTrue(Arrays.equals(new byte[] {}, Bytes.toArray(list.subList(2, 2))));
assertThat(Bytes.toArray(list.subList(1, 3))).isEqualTo(new byte[] {(byte) 1, (byte) 2});
assertThat(Bytes.toArray(list.subList(2, 2))).isEqualTo(new byte[] {});
}

public void testAsListEmpty() {
assertSame(Collections.emptyList(), Bytes.asList(EMPTY));
assertThat(Bytes.asList(EMPTY)).isSameInstanceAs(Collections.emptyList());
}

public void testReverse() {
Expand All @@ -250,13 +252,13 @@ public void testReverse() {
private static void testReverse(byte[] input, byte[] expectedOutput) {
input = Arrays.copyOf(input, input.length);
Bytes.reverse(input);
assertTrue(Arrays.equals(expectedOutput, input));
assertThat(input).isEqualTo(expectedOutput);
}

private static void testReverse(byte[] input, int fromIndex, int toIndex, byte[] expectedOutput) {
input = Arrays.copyOf(input, input.length);
Bytes.reverse(input, fromIndex, toIndex);
assertTrue(Arrays.equals(expectedOutput, input));
assertThat(input).isEqualTo(expectedOutput);
}

public void testReverseIndexed() {
Expand Down

0 comments on commit 9f8615e

Please sign in to comment.