我想使用ScalaCheck生成器模拟设备读数。读数的属性是每个后续读数应该大于前一个读数。我已经使用vars编写了一些代码,但是希望使用Gens上的高阶函数和组合生成器来实现这一点。有人有更好的建议吗?
import org.scalacheck.Gen
class DeviceReadingGenerator(deviceId: DeviceId = Generate.deviceId.sample.get) {
private var timeSlot = Generate.epochInSec.sample.get
private var reading = Gen.choose(0, 100).sample.get
def nextReading(): Reading = {
val gen: Gen[Event] = for {
r <- Generate.event
time <- Gen.const(30)
cons <- Generate.consumption
} yield {
timeSlot += time
reading += cons
r
.copy(timeSlot = timeSlot)
.copy(readingWh = reading)
}
Reading(deviceId, gen.sample.get)
}
}