# Tests for the 'DiffUtil' package. -*- tcl -*-
#
# Copyright (c) 2004 by Peter Spjuth. All rights reserved.
#
# $Revision: 1.5 $

# This file does not contain any real tests, it is a debugging tool
# used to visualize the candidates in the LCS algorithm.

package require DiffUtil

proc + {a b} {expr {$a + $b}}
proc - {a b} {expr {$a - $b}}

# Draw the candidates in a grid
proc visualize {list1 list2} {
    #puts "L1: [join $list1]"
    #puts "L2: [join $list2]"
    #puts "C : $::DiffUtil::Candidates"

    set i 1
    while {[winfo exists .t$i]} {incr i}
    set top .t$i
    toplevel $top

    set size 30
    set r1 6
    set r2 8
    set m [llength $list1]
    set n [llength $list2]
    set width  [expr {($m + 1) * $size}]
    set height [expr {($n + 1) * $size}]
    set c $top.c
    canvas $c -width $width -height $height
    pack $c -side top -fill both -expand 1

    set i 1
    set y [expr {$height - $r2}]
    foreach item $list1 {
        set x [expr {$i * $size}]
        $c create text $x $y -text $item -anchor s
        incr i
    }
    set j 1
    set x $r2
    foreach item $list2 {
        set y [expr {$height - $j * $size}]
        $c create text $x $y -text $item -anchor w
        incr j
    }

    foreach {type i j score wasK ci cj pi pj} $::DiffUtil::Candidates {
        set x [expr {$i * $size}]
        set y [expr {$height - $j * $size}]
        if {$type == "K"} {
            set k $score
            $c create oval [- $x $r2] [- $y $r2] [+ $x $r2] [+ $y $r2] -fill red \
                    -tags "k" -outline red
        } elseif {$type == "C"} {
            $c create oval [- $x $r1] [- $y $r1] [+ $x $r1] [+ $y $r1] -fill white \
                    -tags "c" -outline white
            $c create text $x $y -text $score -fill black -tags "score"
            #$c create text $x $y -text $wasK -fill black -tags "score"
            if {$ci <= $m && $cj <= $n} {
                set cx [expr {$ci * $size}]
                set cy [expr {$height - $cj * $size}]

                $c create line $x $y $cx $cy -fill green -tag "peer" \
                        -arrow last
            }
            if {$pi > 0 && $pj > 0} {
                set px [expr {$pi * $size}]
                set py [expr {$height - $pj * $size}]

                $c create line $x $y $px $py -fill black -tag "prev" \
                        -arrow last
            }
            if {$wasK > 0} {
                $c create oval [- $x $r2] [- $y $r2] [+ $x $r2] [+ $y $r2] -fill purple \
                        -tags "wasK" -outline purple
            }
        } else {
            puts "MIFFFFOOO"
        }
    }
    $c raise "score"
    $c lower "prev"
    $c lower "peer"
    $c lower "k"
    $c lower "wasK"
}

# Run two lists through diffFiles and draw the candidates.
proc cand {list1 list2 args} {
    set ch [open _1 w]
    if {[llength $list1] > 0} {
        puts $ch [join $list1 \n]
    }
    close $ch
    set ch [open _2 w]
    if {[llength $list2] > 0} {
        puts $ch [join $list2 \n]
    }
    close $ch

    set apa [eval DiffUtil::diffFiles $args _1 _2]
    file delete -force _1 _2
    visualize $list1 $list2
}
# Invert order
proc candi {list1 list2 args} {
    eval [list cand $list2 $list1] $args
}
# Test cases
if 0 {
    package require Tk
    #cand {a b c a b b a} {c b a b a c}
    cand {a b r a c a d a b r a} {b r i c a b r a c}
    #cand {a b} {b b}
    #cand {b r i c a b r a c} {a b r a c a d a b r a}
    #cand {a b r a c a d a b i a} {b r i c a b r a c}
    #cand {a b} {a a b}
    #candi {a b} {a a b}
    #cand {a b c d e} {a b c d f e E e}
    #cand {a b c d e} {a b c d f e E}
    #cand {a b c d e} {a b c d f e E} -nocase
    #cand {a b c d e} {a b c d f E E} -nocase
    #cand {a b c d e f g h i j k l} {a b c d X e f g h Y e f g h Z i j k l}
    #cand {a b c d e f g h i j k l} {a b c d X e f g h Y e f g h i j k l}
    #candi {a b c d e f g h i j k l} {a b c d X e f g h Y e f g h Z i j k l}
    #candi {a b c d e f g h i j k l} {a b c d X e f g h Y e f g h i j k l}
    #cand {a b c {} d {} e f g} {a b c {} {} d e f g}
    #cand {a b c {} d {} e f g} {a b c {} {} d e f g} -noempty
    #cand {{} b c {} d e {} f g {} h i {} j k {} l m} \
            {{} b c {} d e {} f G {} h i {} j K {} l m}
    #cand {{} b c {} d e {} f g {} h i {} j k {} l m} \
            {{} b c {} d e {} f G {} h i {} j K {} l m} -noempty
    #cand {{} b c {} d e {} {} {} {} f g {} h i {} j k {} l m} \
            {{} b c {} d e {} {} f {} {} G {} h i {} j K {} l m}
    #candi {{} b c {} d e {} {} {} {} f g {} h i {} j k {} l m} \
            {{} b c {} d e {} {} f {} {} G {} h i {} j K {} l m}
    #cand {a b c d e {} a b f g h {} a b i j k} \
            {a b c d e {}              a b i j k}
    #candi {a b c d e {} a b f g h {} a b i j k} \
            {a b c d e {}              a b i j k}
    #cand [split {  abc d  f ghi } ""] [split { abc x y ghi  } ""]
    #cand {a b123 b456 x} {a b456      x} -nonumber
    wm withdraw .
    tkwait window .t1
}
