问题描述:

I want to simulate device readings using a ScalaCheck generators. The property of the readings is that every subsequent reading should be greater than the previous one. I have written some code using vars but would like to achieve this using higher order functions on the Gens and by composing generators. Does anyone have a better suggestion?

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)

}

}

相关阅读:
Top