From 5b48ef4e53b7d840ed5b4f12d3d3343d4b261d3f Mon Sep 17 00:00:00 2001 From: ibaker Date: Thu, 7 Mar 2024 07:47:31 -0800 Subject: [PATCH] Add tests for `Util.toByteArray` The implementation of these methods is changed in a follow-up CL, and these tests help to ensure nothing breaks. This doesn't include tests for `toByteArray(InputStream)` or `toByteArray(int)` because these implementations are fully replaced by Guava equivalents in a follow-up CL. PiperOrigin-RevId: 613581845 --- .../androidx/media3/common/util/UtilTest.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/libraries/common/src/test/java/androidx/media3/common/util/UtilTest.java b/libraries/common/src/test/java/androidx/media3/common/util/UtilTest.java index 9d57fd1f88..9888c1dc87 100644 --- a/libraries/common/src/test/java/androidx/media3/common/util/UtilTest.java +++ b/libraries/common/src/test/java/androidx/media3/common/util/UtilTest.java @@ -47,8 +47,10 @@ import android.os.Looper; import android.util.SparseArray; import android.util.SparseLongArray; import androidx.media3.common.C; +import androidx.media3.test.utils.TestUtil; import androidx.test.ext.junit.runners.AndroidJUnit4; import com.google.common.io.ByteStreams; +import com.google.common.primitives.Bytes; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.SettableFuture; @@ -76,6 +78,33 @@ public class UtilTest { private static final int TIMEOUT_MS = 10000; + @Test + public void toByteArray_fromIntArray() { + assertThat(Util.toByteArray(Integer.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE)) + .isEqualTo( + Bytes.concat( + TestUtil.createByteArray(0x80, 0, 0, 0), + TestUtil.createByteArray(0xFF, 0xFF, 0xFF, 0xFF), + TestUtil.createByteArray(0, 0, 0, 0), + TestUtil.createByteArray(0, 0, 0, 1), + TestUtil.createByteArray(0x7F, 0xFF, 0xFF, 0xFF))); + } + + @Test + public void toByteArray_fromFloat() { + assertThat(Util.toByteArray(Float.MAX_VALUE)) + .isEqualTo(TestUtil.createByteArray(0x7F, 0x7F, 0xFF, 0xFF)); + + assertThat(Util.toByteArray(Float.MIN_VALUE)) + .isEqualTo(TestUtil.createByteArray(0x00, 0x00, 0x00, 0x01)); + + assertThat(Util.toByteArray(0)).isEqualTo(TestUtil.createByteArray(0x00, 0x00, 0x00, 0x00)); + + assertThat(Util.toByteArray(1.0f)).isEqualTo(TestUtil.createByteArray(0x3F, 0x80, 0x00, 0x00)); + + assertThat(Util.toByteArray(-1.0f)).isEqualTo(TestUtil.createByteArray(0xBF, 0x80, 0x00, 0x00)); + } + @Test public void addWithOverflowDefault_withoutOverFlow_returnsSum() { long res = Util.addWithOverflowDefault(5, 10, /* overflowResult= */ 0);