2016-07-25 4 views
2

scalacheck (버전 1.12.2)를 사용하여 임의의 UUID 목록을 생성하려고합니다. 어떤 이유로 든 생성 된 목록의 모든 UUID는 동일합니다. List [String] 또는 List [Int]와 같은 다른 유형에는 해당되지 않습니다.Scalacheck은 항상 임의의 List [UUID]에 대해 동일한 UUID를 생성합니다.

import org.scalacheck.Arbitrary.arbitrary 
import org.scalacheck.Arbitrary 
import java.util.UUID 

case class SomeUUIDClass(field: List[UUID]) 
case class SomeOtherClass(field: List[Int]) 

object Arb { 
    implicit def arbUUID: Arbitrary[UUID] = Arbitrary { 
     UUID.randomUUID() 
    } 

    implicit def arbUUIDClass = Arbitrary { 
     for { 
      field <- arbitrary[List[UUID]] 
     } yield SomeUUIDClass(field) 
    } 

    implicit def arbOtherClass = Arbitrary { 
     for { 
      field <- arbitrary[List[Int]] 
     } yield SomeOtherClass(field) 
    } 

    def main(args: Array[String]) { 
     println("without uuids:") 
     arbitrary[SomeOtherClass].sample.get.field.foreach(println(_)) 
     println("") 

     println("with uuids:") 
     arbitrary[SomeUUIDClass].sample.get.field.foreach(println(_)) 
    } 
} 

그리고 샘플 실행 : : 여기가 작성한 코드

without uuids: 
-1 
0 
2147483647 
-1 
-2147483648 
527079214 
-698179980 
1192016877 
-1001957700 
0 
682853458 
-1 
-2147483648 
109314552 
1130736291 
1080418 
1771214863 
1164874892 
-1306566270 
2147483647 
-2009106057 
2147483647 
-2147483648 
-1 
-1 
-1 
945958506 
777623735 
-490377345 
-272177229 
0 
-2147483648 
-1753697474 
-1 
736327057 
415072340 
0 

with uuids: 
a49540b4-29ce-464f-946d-3649f38fb8a6 
a49540b4-29ce-464f-946d-3649f38fb8a6 
a49540b4-29ce-464f-946d-3649f38fb8a6 
a49540b4-29ce-464f-946d-3649f38fb8a6 
a49540b4-29ce-464f-946d-3649f38fb8a6 
a49540b4-29ce-464f-946d-3649f38fb8a6 
a49540b4-29ce-464f-946d-3649f38fb8a6 
a49540b4-29ce-464f-946d-3649f38fb8a6 
a49540b4-29ce-464f-946d-3649f38fb8a6 

답변

2

사용 Gen.wrap는()와 그것을 작동합니다.

import org.scalacheck.Gen 

implicit def arbUUID: Arbitrary[UUID] = Arbitrary { 
    Gen.wrap(UUID.randomUUID) 
} 
+0

감사 :

그래서 샘플 코드의 첫 번째 암시에 당신은 그것을 바꿀 것입니다! 그것은 그것을 고쳤다. – emccorson