Skip to content

Instantly share code, notes, and snippets.

@410063005
Created September 27, 2019 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 410063005/9c95e917eb8557f17621e6adb95d9de8 to your computer and use it in GitHub Desktop.
Save 410063005/9c95e917eb8557f17621e6adb95d9de8 to your computer and use it in GitHub Desktop.
用于验证 java.lang.IllegalArgumentException: Comparison method violates its general contract! 的测试用例
package com.tencent.tip.bitmapprofiler.extension
import org.junit.Test
import java.lang.ref.WeakReference
import java.util.*
import kotlin.collections.ArrayList
class ContractTest {
@Test
fun testContract() {
while (true) {
runTest()
}
}
private fun runTest() {
val n = 500000
val rand = Random()
val list = ArrayList<MockBitmapTrace>()
for (i in 0..n) {
list.add(MockBitmapTrace(Bmp(rand.nextInt())))
}
list.filter { it.isBitmapAlive() }
//.sortedBy { it.currentBitmapSize() }
.sortedBy { it.oldBitmapSize() }
.reversed()
}
}
private class MockBitmapTrace(referent: Bmp) : WeakReference<Bmp>(referent) {
private val allocationByteCount: Int = 0
fun isBitmapAlive() = get() != null
fun currentBitmapSize(): Int = get()?.allocationByteCount ?: 0
fun oldBitmapSize(): Int = allocationByteCount
}
data class Bmp(var allocationByteCount: Int)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment