# -*- tcl -*-
# Tests for the find function.
#
# Sourcing this file into Tcl runs the tests and generates output for errors.
# No output means no errors were found.
#
# Copyright (c) 2001 by Andreas Kupries <a.kupries@westend.com>
# All rights reserved.
#
# RCS: @(#) $Id: csv.test,v 1.5 2002/02/01 22:59:08 andreas_kupries Exp $

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest
    namespace import ::tcltest::*
}

if { [lsearch $auto_path [file dirname [info script]]] == -1 } {
    set auto_path [linsert $auto_path 0 [file dirname [info script]]]
}

package require csv
package require struct
puts "csv [package present csv]"
puts "- struct [package present struct]"

set str1 {"123","""a""",,hello}
set str2 {1," o, ""a"" ,b ", 3}
set str3 {"1"," o, "","" ,b ", 3}
set str4 {1," foo,bar,baz", 3}
set str5 {1,"""""a""""",b}
set str6 {123,"123,521.2","Mary says ""Hello, I am Mary"""}

set str1a {123,"""a""",,hello}
set str3a {1," o, "","" ,b ", 3}

test csv-1.1 {split} {
    csv::split $str1
} {123 {"a"} {} hello}

test csv-1.2 {split} {
    csv::split $str2
} {1 { o, "a" ,b } { 3}}

test csv-1.3 {split} {
    csv::split $str3
} {1 { o, "," ,b } { 3}}

test csv-1.4 {split} {
    csv::split $str4
} {1 { foo,bar,baz} { 3}}

test csv-1.5 {split} {
    csv::split $str5
} {1 {""a""} b}

test csv-1.6 {split} {
    csv::split $str6
} {123 123,521.2 {Mary says "Hello, I am Mary"}}

test csv-1.7 {split on join} {
    # csv 0.1 was exposed to the RE \A matching problem with regsub -all
    set x [list "\"hello, you\"" a b c]
    ::csv::split [::csv::join $x]
} [list "\"hello, you\"" a b c]

test csv-2.1 {join} {
    csv::join {123 {"a"} {} hello}
} $str1a

test csv-2.2 {join} {
    csv::join {1 { o, "a" ,b } { 3}}
} $str2

test csv-2.3 {join} {
    csv::join {1 { o, "," ,b } { 3}}
} $str3a

test csv-2.4 {join} {
    csv::join {1 { foo,bar,baz} { 3}}
} $str4

test csv-2.5 {join} {
    csv::join {1 {""a""} b}
} $str5

test csv-2.6 {join} {
    csv::join {123 123,521.2 {Mary says "Hello, I am Mary"}}
} $str6

# Malformed inputs

test csv-3.1 {split} {
    csv::split {abcd,abc",abc} ; # "
} {abcd abc abc}

test csv-3.2 {split} {
    csv::split {abcd,abc"",abc}
} {abcd abc\" abc}


test csv-4.1 {joinlist} {
    csv::joinlist [list \
	    {123 {"a"} {} hello} 	\
	    {1 { o, "a" ,b } { 3}}	\
	    {1 { o, "," ,b } { 3}}	\
	    {1 { foo,bar,baz} { 3}}	\
	    {1 {""a""} b}		\
	    {123 123,521.2 {Mary says "Hello, I am Mary"}}]
} "$str1a\n$str2\n$str3a\n$str4\n$str5\n$str6\n"

test csv-4.2 {joinlist, sepChar} {
    csv::joinlist [list [list a b c] [list d e f]] @
} "a@b@c\nd@e@f\n"


test csv-5.1 {reading csv files} {
    set f [open [file join $::tcltest::testsDirectory mem_debug_bench.csv] r]
    ::struct::queue q
    ::csv::read2queue $f q
    close $f
    set result [list [q size] [q get 2]]
    q destroy
    set result
} {251 {{000 VERSIONS: 2:8.4a3 1:8.4a3 1:8.4a3%} {001 {CATCH return ok} 7 13 53.85}}}

test csv-5.2 {reading csv files} {
    set f [open [file join $::tcltest::testsDirectory mem_debug_bench_a.csv] r]
    ::struct::queue q
    ::csv::read2queue $f q
    close $f
    set result [list [q size] [q get 2]]
    q destroy
    set result
} {251 {{000 VERSIONS: 2:8.4a3 1:8.4a3 1:8.4a3%} {001 {CATCH return ok} 7 13 53.85}}}

test csv-5.3 {reading csv files} {
    set f [open [file join $::tcltest::testsDirectory mem_debug_bench.csv] r]
    ::struct::matrix m
    m add columns 5
    ::csv::read2matrix $f m
    close $f
    set result [m get rect 0 227 end 231]
    m destroy
    set result
} {{227 {STR append (1MB + 1MB * 3)} 125505 327765 38.29} {228 {STR append (1MB + 1MB * 5)} 158507 855295 18.53} {229 {STR append (1MB + (1b + 1K + 1b) * 100)} 33101 174031 19.02} {230 {STR info locals match} 946 1521 62.20} {231 {TRACE no trace set} 34 121 28.10}}

test csv-5.4 {reading csv files} {
    set f [open [file join $::tcltest::testsDirectory mem_debug_bench_a.csv] r]
    ::struct::matrix m
    m add columns 5
    ::csv::read2matrix $f m
    close $f
    set result [m get rect 0 227 end 231]
    m destroy
    set result
} {{227 {STR append (1MB + 1MB * 3)} 125505 327765 38.29} {228 {STR append (1MB + 1MB * 5)} 158507 855295 18.53} {229 {STR append (1MB + (1b + 1K + 1b) * 100)} 33101 174031 19.02} {230 {STR info locals match} 946 1521 62.20} {231 {TRACE no trace set} 34 121 28.10}}

test csv-5.5 {reading csv files} {
    set f [open [file join $::tcltest::testsDirectory mem_debug_bench.csv] r]
    ::struct::matrix m
    m add columns 5
    ::csv::read2matrix $f m
    close $f

    set result [list]
    foreach c {0 1 2 3 4} {
	lappend result [m columnwidth $c]
    }
    m destroy
    set result
} {3 39 7 7 8}

test csv-5.6 {reading csv files, linking} {
    set f [open [file join $::tcltest::testsDirectory mem_debug_bench.csv] r]
    ::struct::matrix m
    m add columns 5
    ::csv::read2matrix $f m
    close $f
    m link a
    set result [array size a]
    m destroy
    set result
} {1255}


test csv-5.7 {reading csv files, empty expansion mode} {
    set f [open [file join $::tcltest::testsDirectory mem_debug_bench.csv] r]
    ::struct::matrix m
    ::csv::read2matrix $f m , empty
    close $f
    set result [m get rect 0 227 end 231]
    m destroy
    set result
} {{227 {STR append (1MB + 1MB * 3)} 125505 327765 38.29} {228 {STR append (1MB + 1MB * 5)} 158507 855295 18.53} {229 {STR append (1MB + (1b + 1K + 1b) * 100)} 33101 174031 19.02} {230 {STR info locals match} 946 1521 62.20} {231 {TRACE no trace set} 34 121 28.10}}

test csv-5.8 {reading csv files, auto expansion mode} {
    set f [open [file join $::tcltest::testsDirectory mem_debug_bench.csv] r]
    ::struct::matrix m
    m add columns 1
    ::csv::read2matrix $f m , auto
    close $f
    set result [m get rect 0 227 end 231]
    m destroy
    set result
} {{227 {STR append (1MB + 1MB * 3)} 125505 327765 38.29} {228 {STR append (1MB + 1MB * 5)} 158507 855295 18.53} {229 {STR append (1MB + (1b + 1K + 1b) * 100)} 33101 174031 19.02} {230 {STR info locals match} 946 1521 62.20} {231 {TRACE no trace set} 34 121 28.10}}

tcltest::makeFile {} eval-out1.csv
tcltest::makeFile {} eval-out2.csv
tcltest::makeFile {} eval-out3.csv

test csv-6.1 {writing csv files} {
    set f [open [file join $::tcltest::testsDirectory eval.csv] r]
    ::struct::matrix m
    m add columns 5
    ::csv::read2matrix $f m
    close $f

    set f [open eval-out1.csv w]
    ::csv::writematrix m $f
    close $f

    set result [tcltest::viewFile eval-out1.csv]
    m destroy
    set result
} {023,EVAL cmd eval in list obj var,26,45,57.78
024,EVAL cmd eval as list,23,42,54.76
025,EVAL cmd eval as string,53,92,57.61
026,EVAL cmd and mixed lists,3805,11276,33.74
027,EVAL list cmd and mixed lists,3812,11325,33.66
028,EVAL list cmd and pure lists,592,1598,37.05}

test csv-6.2 {writing csv files} {
    set f [open [file join $::tcltest::testsDirectory eval.csv] r]
    ::struct::queue q
    ::csv::read2queue $f q
    close $f

    set f [open eval-out2.csv w]
    ::csv::writequeue q $f
    close $f

    set result [tcltest::viewFile eval-out2.csv]
    q destroy
    set result
} {023,EVAL cmd eval in list obj var,26,45,57.78
024,EVAL cmd eval as list,23,42,54.76
025,EVAL cmd eval as string,53,92,57.61
026,EVAL cmd and mixed lists,3805,11276,33.74
027,EVAL list cmd and mixed lists,3812,11325,33.66
028,EVAL list cmd and pure lists,592,1598,37.05}


test csv-7.1 {reporting} {
    set f [open [file join $::tcltest::testsDirectory eval.csv] r]
    ::struct::matrix m
    m add columns 5
    ::csv::read2matrix $f m
    close $f

    set result [m format 2string csv::report]
    m destroy
    set result
} {023,EVAL cmd eval in list obj var,26,45,57.78
024,EVAL cmd eval as list,23,42,54.76
025,EVAL cmd eval as string,53,92,57.61
026,EVAL cmd and mixed lists,3805,11276,33.74
027,EVAL list cmd and mixed lists,3812,11325,33.66
028,EVAL list cmd and pure lists,592,1598,37.05
}

test csv-7.2 {reporting} {
    set f [open [file join $::tcltest::testsDirectory eval.csv] r]
    ::struct::matrix m
    m add columns 5
    ::csv::read2matrix $f m
    close $f

    set f [open eval-out3.csv w]
    m format 2chan csv::report $f
    close $f

    set result [tcltest::viewFile eval-out3.csv]
    m destroy
    set result
} {023,EVAL cmd eval in list obj var,26,45,57.78
024,EVAL cmd eval as list,23,42,54.76
025,EVAL cmd eval as string,53,92,57.61
026,EVAL cmd and mixed lists,3805,11276,33.74
027,EVAL list cmd and mixed lists,3812,11325,33.66
028,EVAL list cmd and pure lists,592,1598,37.05}


test csv-7.3 {report error} {
    catch {::csv::report printmatrix foomatrix blarg} msg
    set msg
} {wrong # args: ::csv::report printmatrix matrix}

test csv-7.4 {report error} {
    catch {::csv::report printmatrix2channel foomatrix} msg
    set msg
} {wrong # args: ::csv::report printmatrix2channel matrix chan}

test csv-7.5 {report error} {
    catch {::csv::report printmatrix2channel foomatrix foo bar} msg
    set msg
} {wrong # args: ::csv::report printmatrix2channel matrix chan}

test csv-7.6 {report error} {
    catch {::csv::report foocmd foomatrix} msg
    set msg
} {Unknown method foocmd}



::tcltest::cleanupTests
return
