# -*- tcl -*-
# -------------------------------------------------------------------------

source [file join \
            [file dirname [file join [pwd] [info script]]] \
            testutilities.tcl]

testsNeedTcl     8.5
testsNeedTcltest 2

support { useLocalFile synth.tcl }
support { useC [mainPath _test/lib]/crimp_core* crimp::core no }
testing { useC [mainPath _test/lib]/crimp*      crimp       no }

# -------------------------------------------------------------------------
## Processing of summed area tables, aka integral images.
## Box filtering, in essence.
## See --> integrate.
##
## TODO (implementation): makes sense for fpcomplex images also.
# -------------------------------------------------------------------------

test crimp-regionsum-1.0 {region_sum images, wrong\#args, not enough} -body {
    crimp region_sum
} -returnCodes error -result {wrong # args: should be "crimp region_sum imageObj radius"}

test crimp-regionsum-1.1 {region_sum images, wrong\#args, not enough} -body {
    crimp region_sum IMAGE
} -returnCodes error -result {wrong # args: should be "crimp region_sum imageObj radius"}

test crimp-regionsum-1.2 {region_sum images, wrong\#args, too many} -body {
    crimp region_sum IMAGE RADIUS toomuch
} -returnCodes error -result {wrong # args: should be "crimp region_sum imageObj radius"}

test crimp-regionsum-1.3 {region_sum images, bogus image type} -body {
    crimp region_sum BOGUS 1
} -returnCodes error -result {invalid image format}

if 0 {
    # TODO : (implementation): Bad type checked in C ==> assert/crash, put a wrapper around it.
    set n 0
    foreach itype [types] {
        if {$itype eq "float"} continue ; # skip the good type
        test crimp-regionsum-1.4.$n "region_sum images, bogus image type" -body {
            crimp region_sum [$itype] 1
        } -returnCodes error -result {}
        incr n
    }
}

test crimp-regionsum-1.5 {region_sum images, invalid radius} -body {
    crimp region_sum [float] RADIUS
} -returnCodes error -result {expected integer but got "RADIUS"}

test crimp-regionsum-1.6 {region_sum images, invalid radius} -body {
    crimp region_sum [float] 0
} -returnCodes error -result {bad radius, expected positive value}

test crimp-regionsum-1.7 {region_sum images, invalid radius} -body {
    crimp region_sum [float] -3
} -returnCodes error -result {bad radius, expected positive value}

# TODO: radius > half of min (width, height) => shrink to empty, or failure.

# -------------------------------------------------------------------------
# Location handling, pass through, modified for the shrinkage

test crimp-regionsum-2.0 {region_sum images, float, location handling} -body {
    crimp geometry [crimp region_sum [float] 1]
} -result {2 2 1 1}

# -------------------------------------------------------------------------

test crimp-regionsum-3.0 {region_sum images, float} -body {
    astcl [crimp region_sum [float] 1]
} -result [iconst float 2 2 1 1 {
    0.0
}]

# TODO : check
test crimp-regionsum-3.1 {region_sum images, integrated float} -body {
    astcl [crimp region_sum [crimp integrate [float]] 1]
} -result [iconst float 2 2 1 1 {
    108.0
}]

# TODO: fix. radius too large, shrinks to empty, no protection in the C code against that. may crash.
test crimp-regionsum-3.2 {region_sum images, float, large radius} -constraints KnownBug -body {
    astcl [crimp region_sum [float] 3]
} -result [iconst float 2 2 1 1 {
    0.0
}]

# -------------------------------------------------------------------------
testsuiteCleanup

# Local variables:
# mode: tcl
# indent-tabs-mode: nil
# End:
