Created
September 27, 2019 08:09
-
-
Save 410063005/9c95e917eb8557f17621e6adb95d9de8 to your computer and use it in GitHub Desktop.
用于验证 java.lang.IllegalArgumentException: Comparison method violates its general contract! 的测试用例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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