"Fossies" - the Fresh Open Source Software Archive

Member "groovy-4.0.12/src/test/org/codehaus/groovy/runtime/memoize/AbstractMemoizeTestCase.groovy" (31 Jan 1980, 3443 Bytes) of package /linux/misc/apache-groovy-src-4.0.12.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Java source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  *  Licensed to the Apache Software Foundation (ASF) under one
    3  *  or more contributor license agreements.  See the NOTICE file
    4  *  distributed with this work for additional information
    5  *  regarding copyright ownership.  The ASF licenses this file
    6  *  to you under the Apache License, Version 2.0 (the
    7  *  "License"); you may not use this file except in compliance
    8  *  with the License.  You may obtain a copy of the License at
    9  *
   10  *    http://www.apache.org/licenses/LICENSE-2.0
   11  *
   12  *  Unless required by applicable law or agreed to in writing,
   13  *  software distributed under the License is distributed on an
   14  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15  *  KIND, either express or implied.  See the License for the
   16  *  specific language governing permissions and limitations
   17  *  under the License.
   18  */
   19 package org.codehaus.groovy.runtime.memoize
   20 
   21 import groovy.test.GroovyTestCase
   22 
   23 abstract class AbstractMemoizeTestCase extends GroovyTestCase {
   24 
   25     volatile int counter = 0
   26 
   27     AbstractMemoizeTestCase() {
   28         super()
   29     }
   30 
   31     void testCorrectness() {
   32         Closure cl = { it * 2 }
   33         Closure mem = buildMemoizeClosure(cl)
   34         assert 10 == mem(5)
   35         assert 4 == mem(2)
   36     }
   37 
   38     abstract Closure buildMemoizeClosure(Closure cl)
   39 
   40     void testNullParams() {
   41         Closure cl = { 2 }
   42         Closure mem = cl.memoize()
   43         assert 2 == mem(5)
   44         assert 2 == mem(2)
   45         assert 2 == mem(null)
   46     }
   47 
   48     void testNullResult() {
   49         Closure cl = { counter++; if (it == 5) return null else return 2 }
   50         Closure mem = cl.memoize()
   51         assert counter == 0
   52         assert null == mem(5)
   53         assert counter == 1
   54         assert 2 == mem(2)
   55         assert counter == 2
   56         assert null == mem(5)
   57         assert 2 == mem(2)
   58         assert counter == 2
   59     }
   60 
   61     void testNoParams() {
   62         Closure cl = { -> 2 }
   63         Closure mem = cl.memoize()
   64         assert 2 == mem()
   65         assert 2 == mem()
   66     }
   67 
   68     void testCaching() {
   69         def flag = false
   70         Closure cl = {
   71             flag = true
   72             it * 2
   73         }
   74         Closure mem = cl.memoize()
   75         assert 10 == mem(5)
   76         assert flag
   77         flag = false
   78         assert 4 == mem(2)
   79         assert flag
   80         flag = false
   81 
   82         assert 4 == mem(2)
   83         assert 4 == mem(2)
   84         assert 10 == mem(5)
   85         assert !flag
   86 
   87         assert 6 == mem(3)
   88         assert flag
   89         flag = false
   90         assert 6 == mem(3)
   91         assert !flag
   92     }
   93 
   94     void testComplexParameter() {
   95         def callFlag = []
   96 
   97         Closure cl = { a, b, c ->
   98             callFlag << true
   99             c
  100         }
  101         Closure mem = cl.memoize()
  102         checkParams(mem, callFlag, [1, 2, 3], 3)
  103         checkParams(mem, callFlag, [1, 2, 4], 4)
  104         checkParams(mem, callFlag, [1, [2], 4], 4)
  105         checkParams(mem, callFlag, [[1: '1'], [2], 4], 4)
  106         checkParams(mem, callFlag, [[1, 2], 2, 4], 4)
  107         checkParams(mem, callFlag, [[1, 2], null, 4], 4)
  108         checkParams(mem, callFlag, [null, null, 4], 4)
  109         checkParams(mem, callFlag, [null, null, null], null)
  110         checkParams(mem, callFlag, [null, [null], null], null)
  111     }
  112 
  113     def checkParams(Closure mem, callFlag, args, desiredResult) {
  114         assertEquals desiredResult, mem( * args )
  115         assert !callFlag.empty
  116         callFlag.clear()
  117         assert desiredResult == mem(*args)
  118         assert callFlag.empty
  119     }
  120 }