changed id to string and check if result of the custom unique-method is correct
the problem now is, that the groovy-unique-method is not that slow as before....
import groovy.transform.EqualsAndHashCode import org.junit.Test import static java.lang.System.currentTimeMillis as now class UniqueTest { @Test void "Check if unique (10.000 items) exceeds 2 seconds"() { def users = createUsers(10000) long a = now() assert Uniquer.unique(users) instanceof List assert now() - a < 2000 } @Test void "Check if result is correct"() { def users = createUsers(10000) def customUniqueUsers = Uniquer.unique(users) println customUniqueUsers.size() def uniqueUsers = users.unique() assert customUniqueUsers.size() == uniqueUsers.size() customUniqueUsers.each { assert customUniqueUsers.contains(it) } } static List<User> createUsers(int times) { List<User> users = [] Random rand = new Random() times.times { byte[] a = new byte[2]; rand.nextBytes(a); users.add(new User(id: new String(a))) } users } } @EqualsAndHashCode class User { String id }
- import groovy.transform.EqualsAndHashCode
- import org.junit.Test
- import static java.lang.System.currentTimeMillis as now
- class UniqueTest {
- @Test
void "Check if unique exceeds 2 seconds"() {List<User> users = []Random rand = new Random()10000.times { byte[] a = new byte[2]; rand.nextBytes(a); users.add(new User(id: a)) }println "x"long a = now()assert Uniquer.unique(users) instanceof Listassert now() - a < 2000}- void "Check if unique (10.000 items) exceeds 2 seconds"() {
- def users = createUsers(10000)
- long a = now()
- assert Uniquer.unique(users) instanceof List
- assert now() - a < 2000
- }
- @Test
- void "Check if result is correct"() {
- def users = createUsers(10000)
- def customUniqueUsers = Uniquer.unique(users)
- println customUniqueUsers.size()
- def uniqueUsers = users.unique()
- assert customUniqueUsers.size() == uniqueUsers.size()
- customUniqueUsers.each {
- assert customUniqueUsers.contains(it)
- }
- }
- static List<User> createUsers(int times) {
- List<User> users = []
- Random rand = new Random()
- times.times { byte[] a = new byte[2]; rand.nextBytes(a); users.add(new User(id: new String(a))) }
- users
- }
- }
- @EqualsAndHashCode
- class User {
byte[] idboolean equals(User user) {user.id == id}int hashCode() {((int) id[0]) << 8 | id[1]}- String id
- }