package com.test.hashset import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Test class HashSetUnitTest { class Some(var a: Int, var b: Int) { override fun equals(other: Any?): Boolean { if (this === other) return true if (other !is Some) return false if (a == other.a && b == other.b) return true return false } override fun hashCode(): Int { var result = 17 result = result * 31 + a result = result * 31 + b return result } override fun toString(): String { return "Some(a=$a, b=$b)" } } @Test fun objectModificationCausesBrokenContains() { val hashSet = HashSet() val control = Some(1, 2) hashSet.add(control) control.a = 42 val contains = hashSet.contains(Some(42, 2)) assertFalse(contains) } @Test fun objectWithouModificationAndCorrectContains() { val hashSet = HashSet() val control = Some(1, 2) hashSet.add(control) val contains = hashSet.contains(Some(1, 2)) assertTrue(contains) } @Test fun objectWithModificationAndSetResizeAndIncorrectContains() { val hashSet = HashSet() val control = Some(1, 2) hashSet.add(control) control.a = 42 for (i in 3..100000) { hashSet.add(Some(1, i)) } val contains = hashSet.contains(control) assertFalse(contains) } }