unit testing - using memoize in groovy -


i practicing test driven development in groovy using spock. have 1 set of tests 3 different implementations doing same thing: iterative, recursive, , memoized. have created abstract class hold tests, , created 3 different files return concrete class implementation run tests. have iterative , recursive working, having issues memoize()

import spock.lang.specification abstract class fibonaccitest extends specification {     private calculator     abstract def getcalculator()     def setup() {         calculator = getcalculator()     }     def "test canary"() {         expect:         true     }     // more tests }  class recursivefibonaccitest extends fibonaccitest {     def getcalculator() {         new recursivecalculator()     } }  class iterativefibonaccitest extends fibonaccitest {     def getcalculator() {         new iterativecalculator()     } }  class memoizefibonaccitest extends fibonaccitest {     def getcalculator() {         new memoizecalculator()     } }  class recursivecalculator {     def getfibonacci(position) {         if (position < 2) {             1         }         else {             getfibonacci(position - 1) + getfibonacci(position - 2)         }     } }  class iterativecalculator {     def getfibonacci(position) {         if (position < 2) {             1         }         else {             def value = 1             def previousvalue = 1             (i in 2..position) {                 def temporaryvalue = previousvalue                 previousvalue = value                 value = temporaryvalue + previousvalue             }         value         }     } } 

so i've got iterative , recursive working, having problems getting memoize work.. think should work this, not.. know i'm doing wrong?

class memoizecalculator {     def getfibonacci = { position ->         if (position < 2)             1         else             getfibonacci.call(position - 1) + getfibonacci.call(position - 2)     }.memoize() } 

you can't reference getfibonacci variable same statement declares it. either change getfibonacci.call call, or declare variable (def getfibonacci) before assigning (getfibonacci = ...).


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -