xref: /aosp_15_r20/external/leakcanary2/shark-hprof/src/test/java/shark/HprofReaderPrimitiveArrayTest.kt (revision d9e8da70d8c9df9a41d7848ae506fb3115cae6e6)

<lambda>null1 package shark
2 
3 import org.assertj.core.api.Assertions.assertThat
4 import org.junit.Rule
5 import org.junit.Test
6 import kotlin.text.Charsets.UTF_8
7 
8 class HprofReaderPrimitiveArrayTest {
9 
10   @get:Rule
11   var heapDumpRule = HeapDumpRule()
12 
13   @Test
14   fun skips_primitive_arrays_correctly() {
15     val heapDump = heapDumpRule.dumpHeap()
16 
17     Hprof.open(heapDump).use { hprof ->
18       hprof.reader.readHprofRecords(
19         emptySet()
20       ) // skip everything including primitive arrays
21       { _, _ -> }
22     }
23   }
24 
25   @Test
26   fun reads_primitive_arrays_correctly() {
27     val byteArray = ("Sharks also have a sensory organ called the \"ampullae of Lorenzini\" " +
28       "which they use to \"feel\" the electrical field coming from its prey.")
29       .toByteArray(UTF_8)
30 
31     val heapDump = heapDumpRule.dumpHeap()
32 
33     var myByteArrayIsInHeapDump = false
34     Hprof.open(heapDump).use { hprof ->
35       hprof.reader.readHprofRecords(
36         setOf(HprofRecord.HeapDumpRecord.ObjectRecord.PrimitiveArrayDumpRecord::class)
37       ) { _, record ->
38         if (record is HprofRecord.HeapDumpRecord.ObjectRecord.PrimitiveArrayDumpRecord.ByteArrayDump) {
39           if (byteArray.contentEquals(record.array)) {
40             myByteArrayIsInHeapDump = true
41           }
42         }
43       }
44     }
45     assertThat(myByteArrayIsInHeapDump).isTrue()
46   }
47 }
48