# Tests for comparing directories.                          -*- tcl -*-
#----------------------------------------------------------------------
# $Revision$
#----------------------------------------------------------------------

set ::Pref(dir,ignorekey) 0
set ::Pref(comparelevel) 1

proc testCompareFiles {text1 text2 {sametime 0}} {
    set ch [open _f1_ w]
    puts -nonewline $ch $text1
    close $ch

    set ch [open _f2_ w]
    puts -nonewline $ch $text2
    close $ch

    set t [file mtime _f1_]
    if {$sametime} {
        file mtime _f2_ $t
    } else {
        file mtime _f2_ [expr {$t - 5}]
    }

    set res [CompareFiles _f1_ _f2_]
    append res [CompareFiles _f2_ _f1_]
    file delete -force _f1_ _f2_
    return $res
}

proc touch {name} {
    set ch [open $name w]
    puts $ch "a"
    close $ch
}

test dir-1.1 {
    CompareFiles
} -body {
    set text1 "Hej hopp"
    testCompareFiles $text1 $text1
} -result {11}

test dir-1.2 {
    CompareFiles
} -body {
    set text1 [string repeat "Abab" 16384]
    set text2 ${text1}Xx
    set res [testCompareFiles $text1 $text2]
} -result {00}

test dir-1.3 {
    CompareFiles
} -body {
    set text1 [string repeat "Abab" 16384]
    set text2 ${text1}Xx${text1}Xx
    set res [testCompareFiles $text1 $text2]
} -result {00}

test dir-2.1 {
    CompareFiles, ignore key
} -body {
    set text1 [string repeat "Abab" 1684]
    append text1 "\$"
    append text1 "Somekey: asdf s 178 flkjsf as lkj\$"
    append text1 Aaalkdalkioodi[string repeat "Abab" 16844]

    set text2 [string repeat "Abab" 1684]
    append text2 "\$"
    append text2 "Somekey: asdf s 17 flkjsf as lkj\$"
    append text2 Aaalkdalkioodi[string repeat "Abab" 16844]

    set res [testCompareFiles $text1 $text2]
    set ::Pref(dir,ignorekey) 1
    append res [testCompareFiles $text1 $text2]
    set ::Pref(dir,ignorekey) 0
    set res
} -result {0011}

test dir-2.2 {
    CompareFiles, ignore key
} -body {
    set text1 [string repeat "Abab" 1684]
    append text1 "\$"
    append text1 "Somekey: asdf s 178 flkjsf as lkj\$"
    append text1 Aaalkdalkioodi[string repeat "Abab" 16844]

    set text2 [string repeat "Abab" 1684]
    append text2 "\$"
    append text2 "Somekey: asdf s 17 flkjsf as lkj\$"
    append text2 Aaalkdalkiood[string repeat "Abab" 16844]

    set res [testCompareFiles $text1 $text2]
    set ::Pref(dir,ignorekey) 1
    append res [testCompareFiles $text1 $text2]
    set ::Pref(dir,ignorekey) 0
    set res
} -result {0000}

test dir-3.1 {
    CompareFiles, bogus files
} -body {
    touch _f1_
    set res [CompareFiles _bogus_ _f1_]
    append res [CompareFiles _f1_ _bogus]
    append res [CompareFiles _bogus_ _bogus]
} -cleanup {
    file delete -force _f1_
} -result {000}

test dir-4.1 {
    CompareFiles, no contents
} -body {
    set ::Pref(comparelevel) 0
    set res [testCompareFiles "abab" "baba"]
    # Different size
    append res [testCompareFiles "abab" "babax"]
    set ::Pref(comparelevel) 1
    append res [testCompareFiles "abab" "baba"]
    # Same time
    append res [testCompareFiles "abab" "baba" 1]
} -result {11000011}

test dir-5.1 {
    CompareFiles, directories
} -body {
    touch _f1_
    file mkdir _d1_
    file mkdir _d2_
    touch _d2_/_f2_

    set res [CompareFiles _f1_ _d1_]
    append res [CompareFiles _d1_ _d2_]
} -cleanup {
    file delete -force _f1_ _d1_ _d2_
} -result {00}

test dir-6.1 {
    CompareFiles, binary
} -body {
    set ::Pref(comparelevel) 1b
    set res [testCompareFiles "short" "longer"]
} -cleanup {
    set ::Pref(comparelevel) 1
} -result {00}

test dir-6.2 {
    CompareFiles, binary
} -body {
    set ::Pref(comparelevel) 1b
    set text1 [string repeat hugg 1534]\rhejsan
    set text2 [string repeat hugg 1534]\nhejsan

    set res [testCompareFiles $text1 $text2]
    set ::Pref(comparelevel) 1
    append res [testCompareFiles $text1 $text2]
} -result {0011}
