text:
#2018 Klaus Stricker - Tests for ReadWriteDlm2 - License is MIT: http://julialang.org/license

#Start  Test Script
using DelimitedFiles
using ReadWriteDlm2
using Test
using Random
using Dates
import ReadWriteDlm2.dfregex

# Run tests

# 1st block modified standardtests

isequaldlm(m1, m2, t) = isequal(m1, m2) && (eltype(m1) == eltype(m2) == t)

@testset "readdlm2" begin
    @test isequaldlm(readdlm2(IOBuffer("1;2\n3;4\n5;6\n"), Float64), [1. 2; 3 4; 5 6], Float64)
    @test isequaldlm(readdlm2(IOBuffer("1;2\n3;4\n5;6\n"), Int), [1 2; 3 4; 5 6], Int)
    @test isequaldlm(readdlm2(IOBuffer("1 2\n3 4\n5 6\n"),' ', Float64), [1. 2; 3 4; 5 6], Float64)
    @test isequaldlm(readdlm2(IOBuffer("1 2\n3 4\n5 6\n"), ' ', Int), [1 2; 3 4; 5 6], Int)

    @test size(readdlm2(IOBuffer("1;2;3;4\n1;2;3"))) == (2,4)
    @test size(readdlm2(IOBuffer("1; 2;3;4\n1;2;3"))) == (2,4)
    @test size(readdlm2(IOBuffer("1; 2;3;4\n1;2;3\n"))) == (2,4)
    @test size(readdlm2(IOBuffer("1;;2;3;4\n1;2;3\n"))) == (2,5)

    let result1 = reshape(Any["", "", "", "", "", "", 1.0, 1.0, "", "", "", "", "", 1.0, 2.0, "", 3.0, "", "", "", "", "", 4.0, "", "", ""], 2, 13),
        result2 = reshape(Any[1.0, 1.0, 2.0, 1.0, 3.0, "", 4.0, ""], 2, 4)

        @test isequaldlm(readdlm2(IOBuffer(";;;1;;;;2;3;;;4;\n;;;1;;;1\n")), result1, Any)
        @test isequaldlm(readdlm2(IOBuffer("   1    2 3   4 \n   1   1\n"), ' '), result1, Any)
        @test isequaldlm(readdlm2(IOBuffer("   1    2 3   4 \n   1   1\n"), ' '), result1, Any)
        @test isequaldlm(readdlm2(IOBuffer("1;2\n3;4 \n"), Float64), [[1.0, 3.0] [2.0, 4.0]], Float64)
    end

    let result1 = reshape(Any["", "", "", "", "", "", "भारत", 1.0, "", "", "", "", "", 1.0, 2.0, "", 3.0, "", "", "", "", "", 4.0, "", "", ""], 2, 13)
        @test isequaldlm(readdlm2(IOBuffer(",,,भारत,,,,2,3,,,4,\n,,,1,,,1\n"), ',', rs=()) , result1, Any)
    end

    let result1 = reshape(Any[1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, ""], 2, 4)
        @test isequaldlm(readdlm2(IOBuffer("1;2;3;4\n1;2;3")), result1, Any)
        @test isequaldlm(readdlm2(IOBuffer("1;2;3;4\n1;2;3;"),';'), result1, Any)
        @test isequaldlm(readdlm2(IOBuffer("1,2,3,4\n1,2,3"), ',', rs=()), result1, Any)
        @test isequaldlm(readdlm2(IOBuffer("1,2,3,4\r\n1,2,3\r\n"), ',', rs=()), result1, Any)
        @test isequaldlm(readdlm2(IOBuffer("1,2,3,\"4\"\r\n1,2,3\r\n"), ',', rs=()), result1, Any)
    end

    let result1 = reshape(Any["abc", "hello", "def,ghi", " \"quote\" ", "new\nline", "world"], 2, 3),
        result2 = reshape(Any["abc", "line\"", "\"hello\"", "\"def", "", "\" \"\"quote\"\" \"", "ghi\"", "", "world", "\"new", "", ""], 3, 4)

        @test isequaldlm(readdlm2(IOBuffer("abc,\"def,ghi\",\"new\nline\"\n\"hello\",\" \"\"quote\"\" \",world"), ',', rs=()), result1, Any)
        @test isequaldlm(readdlm2(IOBuffer("abc,\"def,ghi\",\"new\nline\"\n\"hello\",\" \"\"quote\"\" \",world"), ',', rs=(), quotes=false), result2, Any)
        @test isequaldlm(readdlm2(IOBuffer("abc,\"def,ghi\",\"new\nline\"\n\"hello\",\" \"\"quote\"\" \",world"), ',', rs=()), result1, Any)
        @test isequaldlm(readdlm2(IOBuffer("abc,\"def,ghi\",\"new\nline\"\n\"hello\",\" \"\"quote\"\" \",world"), ',', rs=(), quotes=false), result2, Any)

        @test isequaldlm(readdlm2(IOBuffer("abc;\"def,ghi\";\"new\nline\"\n\"hello\";\" \"\"quote\"\" \";world")), result1, Any)
        @test isequaldlm(readdlm2(IOBuffer("abc;\"def;ghi\";\"new\nline\"\n\"hello\";\" \"\"quote\"\" \";world"), quotes=false), result2, Any)
        @test isequaldlm(readdlm2(IOBuffer("abc;\"def,ghi\";\"new\nline\"\n\"hello\";\" \"\"quote\"\" \";world"), rs=()), result1, Any)
        @test isequaldlm(readdlm2(IOBuffer("abc;\"def;ghi\";\"new\nline\"\n\"hello\";\" \"\"quote\"\" \";world"), rs=(), quotes=false), result2, Any)
    end

    let result1 = reshape(Any["t", "c", "", "c"], 2, 2),
        result2 = reshape(Any["t", "\"c", "t", "c"], 2, 2)
        @test isequaldlm(readdlm2(IOBuffer("t;\n\"c\";c")), result1, Any)
        @test isequaldlm(readdlm2(IOBuffer("t;t\n\"\"\"c\";c")), result2, Any)
        @test isequaldlm(readdlm2(IOBuffer("t;\n\"c\";c"), rs=()), result1, Any)
        @test isequaldlm(readdlm2(IOBuffer("t;t\n\"\"\"c\";c"), rs=()), result2, Any)
    end

    @test isequaldlm(readdlm2(IOBuffer("\n1;2;3\n4;5;6\n\n\n"), skipblanks=false),
                    reshape(Any["",1.0,4.0,"","","",2.0,5.0,"","","",3.0,6.0,"",""], 5, 3), Any)
    @test isequaldlm(readdlm2(IOBuffer("\n1;2;3\n4;5;6\n\n\n"), Float64, skipblanks=true),
                    reshape([1.0,4.0,2.0,5.0,3.0,6.0], 2, 3), Float64)
    @test isequaldlm(readdlm2(IOBuffer("1;2\n\n4;5"), skipblanks=false),
                    reshape(Any[1.0,"",4.0,2.0,"",5.0], 3, 2), Any)
    @test isequaldlm(readdlm2(IOBuffer("1;2\n\n4;5"), Float64, skipblanks=true),
                    reshape([1.0,4.0,2.0,5.0], 2, 2), Float64)
end

@testset "writedlm2" begin
    let x = bitrand(5, 10), io = IOBuffer()
        writedlm2(io, x)
        seek(io, 0)
        @test readdlm2(io, Bool) == x
    end

    let x = bitrand(5, 10)
        writedlm2("test.csv", x, ";")
        @test readdlm2("test.csv", ';', Bool, '\n') == x
        @test readdlm2("test.csv", ';', '\n') == x
    end

    let x = bitrand(5, 10)
        writedlm2("test.csv", x, ";")
        @test readdlm2("test.csv", ';', Bool) == x
        @test readdlm2("test.csv", ';') == x
    end

    let x = bitrand(5, 10)
        writedlm2("test.csv", x)
        @test readdlm2("test.csv", Bool) == x
        @test readdlm2("test.csv") == x
    end

    let x = [1,2,3], y = [4,5,6], io = IOBuffer()
        writedlm2(io, zip(x,y), ",  ")
        seek(io, 0)
        @test readdlm2(io, ',', rs=()) == [x y]
    end

    let x = [0.1 0.3 0.5], io = IOBuffer()
        writedlm2(io, x, ", ")
        seek(io, 0)
        @test read(io, String) == "0,1, 0,3, 0,5\n"
    end

    let x = [0.1 0.3 0.5], io = IOBuffer()
        writedlm2(io, x, "; ")
        seek(io, 0)
        @test read(io, String) == "0,1; 0,3; 0,5\n"
    end

    let x = [0.1 0.3 0.5], io = IOBuffer()
        writedlm2(io, x)
        seek(io, 0)
        @test readdlm2(io) == [0.1 0.3 0.5]
    end

    let x = [0.1 0.3 0.5], io = IOBuffer()
        writedlm2(io, x, decimal='€')
        seek(io, 0)
        @test readdlm2(io, rs=(r"(\d)€(\d)", s"\1.\2")) == [0.1 0.3 0.5]
    end

    let x = [0.1 0.3 0.5], io = IOBuffer()
        writedlm2(io, x, ':', decimal='€')
        seek(io, 0)
        @test readdlm2(io, ':', rs=(r"(\d)€(\d)", s"\1.\2")) == [0.1 0.3 0.5]
    end

    let x = ["abc", "def\"ghi", "jk\nl"], y = [1, ",", "\"quoted\""], io = IOBuffer()
        writedlm2(io, zip(x,y))
        seek(io, 0)
        @test readdlm2(io) == [x y]
    end

    let x = ["abc", "def\"ghi", "jk\nl"], y = [1, ",", "\"quoted\""], io = IOBuffer()
        writedlm2(io, zip(x,y))
        seek(io, 0)
        @test readdlm2(io, Any) == [x y]
    end

    let x = ["a" "b"; "d" ""], io = IOBuffer()
        writedlm2(io, x)
        seek(io, 0)
        @test readdlm2(io) == x
    end

    let x = ["\"hello\"", "world\""], io = IOBuffer()
        writedlm2(io, x, quotes=false)
        @test String(take!(io)) == "\"hello\"\nworld\"\n"
        writedlm2(io, x)
        @test String(take!(io)) == "\"\"\"hello\"\"\"\n\"world\"\"\"\n"
    end
end

@testset "comments" begin
    @test isequaldlm(readdlm2(IOBuffer("#this is comment\n1;2;3\n#one more comment\n4;5;6"), Float64, comments=true), [1. 2. 3.;4. 5. 6.], Float64)
    @test isequaldlm(readdlm2(IOBuffer("#this is \n#comment\n1;2;3\n#one more \n#comment\n4;5;6"), Float64, comments=true), [1. 2. 3.;4. 5. 6.], Float64)
    @test isequaldlm(readdlm2(IOBuffer("1;2;#3\n4;5;6"), comments=true), [1. 2. "";4. 5. 6.], Any)
    @test isequaldlm(readdlm2(IOBuffer("1#;2;3\n4;5;6"), comments=true), [1. "" "";4. 5. 6.], Any)
    @test isequaldlm(readdlm2(IOBuffer("1;2;\"#3\"\n4;5;6"), comments=true), [1. 2. "#3";4. 5. 6.], Any)
    @test isequaldlm(readdlm2(IOBuffer("1;2;3\n #with leading whitespace\n4;5;6"), comments=true), [1. 2. 3.;" " "" "";4. 5. 6.], Any)
end

@testset "skipstart" begin
    x = ["a" "b" "c"; "d" "e" "f"; "g" "h" "i"; "A" "B" "C"; 1 2 3; 4 5 6; 7 8 9]
    io = IOBuffer()

    writedlm2(io, x, quotes=false)
    seek(io, 0)
    (data, hdr) = readdlm2(io, header=true, skipstart=3)
    @test data == [1 2 3; 4 5 6; 7 8 9]
    @test hdr == ["A" "B" "C"]

    x = ["a" "b" "\nc"; "d" "\ne" "f"; "g" "h" "i\n"; "A" "B" "C"; 1 2 3; 4 5 6; 7 8 9]
    io = IOBuffer()

    writedlm2(io, x, quotes=true)
    seek(io, 0)
    (data, hdr) = readdlm2(io, header=true, skipstart=6)
    @test data == [1 2 3; 4 5 6; 7 8 9]
    @test hdr == ["A" "B" "C"]

    io = IOBuffer()
    writedlm2(io, x, quotes=false)
    seek(io, 0)
    (data, hdr) = readdlm2(io, header=true, skipstart=6)
    @test data == [1 2 3; 4 5 6; 7 8 9]
    @test hdr == ["A" "B" "C"]
end

@testset "i18n" begin
    # source: http://www.i18nguy.com/unicode/unicode-example-utf8.zip
    let i18n_data = ["Origin (English)", "Name (English)", "Origin (Native)", "Name (Native)",
            "Australia", "Nicole Kidman", "Australia", "Nicole Kidman",
            "Austria", "Johann Strauss", "Österreich", "Johann Strauß",
            "Belgium (Flemish)", "Rene Magritte", "België", "René Magritte",
            "Belgium (French)", "Rene Magritte", "Belgique", "René Magritte",
            "Belgium (German)", "Rene Magritte", "Belgien", "René Magritte",
            "Bhutan", "Gonpo Dorji", "འབྲུག་ཡུལ།", "མགོན་པོ་རྡོ་རྗེ།",
            "Canada", "Celine Dion", "Canada", "Céline Dion",
            "Canada - Nunavut (Inuktitut)", "Susan Aglukark", "ᓄᓇᕗᒻᒥᐅᑦ", "ᓱᓴᓐ ᐊᒡᓗᒃᑲᖅ",
            "Democratic People's Rep. of Korea", "LEE Sol-Hee", "조선 민주주의 인민 공화국", "이설희",
            "Denmark", "Soren Hauch-Fausboll", "Danmark", "Søren Hauch-Fausbøll",
            "Denmark", "Soren Kierkegaard", "Danmark", "Søren Kierkegård",
            "Egypt", "Abdel Halim Hafez", "ﻣﺼﺮ", "ﻋﺑﺪﺍﻠﺣﻟﻳﻢ ﺤﺎﻓﻅ",
            "Egypt", "Om Kolthoum", "ﻣﺼﺮ", "ﺃﻡ ﻛﻟﺛﻭﻡ",
            "Eritrea", "Berhane Zeray", "ብርሃነ ዘርኣይ", "ኤርትራ",
            "Ethiopia", "Haile Gebreselassie", "ኃይሌ ገብረሥላሴ", "ኢትዮጵያ",
            "France", "Gerard Depardieu", "France", "Gérard Depardieu",
            "France", "Jean Reno", "France", "Jean Réno",
            "France", "Camille Saint-Saens", "France", "Camille Saint-Saëns",
            "France", "Mylene Demongeot", "France", "Mylène Demongeot",
            "France", "Francois Truffaut", "France", "François Truffaut",
            "France (Braille)", "Louis Braille", "⠋⠗⠁⠝⠉⠑", "⠇⠕⠥⠊⠎⠀<BR>⠃⠗⠁⠊⠇⠇⠑",
            "Georgia", "Eduard Shevardnadze", "საქართველო", "ედუარდ შევარდნაძე",
            "Germany", "Rudi Voeller", "Deutschland", "Rudi Völler",
            "Germany", "Walter Schultheiss", "Deutschland", "Walter Schultheiß",
            "Greece", "Giorgos Dalaras", "Ελλάς", "Γιώργος Νταλάρας",
            "Iceland", "Bjork Gudmundsdottir", "Ísland", "Björk Guðmundsdóttir",
            "India (Hindi)", "Madhuri Dixit", "भारत", "माधुरी दिछित",
            "Ireland", "Sinead O'Connor", "Éire", "Sinéad O'Connor",
            "Israel", "Yehoram Gaon", "ישראל", "יהורם גאון",
            "Italy", "Fabrizio DeAndre", "Italia", "Fabrizio De André",
            "Japan", "KUBOTA Toshinobu", "日本", "久保田    利伸",
            "Japan", "HAYASHIBARA Megumi", "日本", "林原 めぐみ",
            "Japan", "Mori Ogai", "日本", "森鷗外",
            "Japan", "Tex Texin", "日本", "テクス テクサン",
            "Norway", "Tor Age Bringsvaerd", "Noreg", "Tor Åge Bringsværd",
            "Pakistan (Urdu)", "Nusrat Fatah Ali Khan", "پاکستان", "نصرت فتح علی خان",
            "People's Rep. of China", "ZHANG Ziyi", "中国", "章子怡",
            "People's Rep. of China", "WONG Faye", "中国", "王菲",
            "Poland", "Lech Walesa", "Polska", "Lech Wałęsa",
            "Puerto Rico", "Olga Tanon", "Puerto Rico", "Olga Tañón",
            "Rep. of China", "Hsu Chi", "臺灣", "舒淇",
            "Rep. of China", "Ang Lee", "臺灣", "李安",
            "Rep. of Korea", "AHN Sung-Gi", "한민국", "안성기",
            "Rep. of Korea", "SHIM Eun-Ha", "한민국", "심은하",
            "Russia", "Mikhail Gorbachev", "Россия", "Михаил Горбачёв",
            "Russia", "Boris Grebenshchikov", "Россия", "Борис Гребенщиков",
            "Slovenia", "\"Frane \"\"Jezek\"\" Milcinski", "Slovenija", "Frane Milčinski - Ježek",
            "Syracuse (Sicily)", "Archimedes", "Συρακούσα", "Ἀρχιμήδης",
            "Thailand", "Thongchai McIntai", "ประเทศไทย", "ธงไชย แม็คอินไตย์",
            "U.S.A.", "Brad Pitt", "U.S.A.", "Brad Pitt",
            "Yugoslavia (Cyrillic)", "Djordje Balasevic", "Југославија", "Ђорђе Балашевић",
            "Yugoslavia (Latin)", "Djordje Balasevic", "Jugoslavija", "Đorđe Balašević"]

        i18n_arr = permutedims(reshape(i18n_data, 4, Int(floor(length(i18n_data)/4))), [2, 1])
        i18n_buff = PipeBuffer()

        writedlm2(i18n_buff, i18n_arr, ',', decimal='.')
        @test i18n_arr == readdlm2(i18n_buff, ',', rs=())

        writedlm2(i18n_buff, i18n_arr)
        @test i18n_arr == readdlm2(i18n_buff)

        hdr = i18n_arr[1:1, :]
        data = i18n_arr[2:end, :]

        writedlm2(i18n_buff, i18n_arr)
        @test (data, hdr) == readdlm2(i18n_buff, header=true)
    end
end

@testset "issue #13028" begin
    for data in ["A B C", "A B C\n"]
        data,hdr = readdlm2(IOBuffer(data), ' ', header=true)
        @test hdr == AbstractString["A" "B" "C"]
        @test data == Array{Float64}(undef, 0, 3)
    end
end

@testset "issue #13179" begin # fix #13179 parsing unicode lines with default delmiters
    @test isequaldlm(readdlm2(IOBuffer("# Should ignore this π\n1\tα\n2\tβ\n"), '\t', comments=true), Any[1 "α"; 2 "β"], Any)
    @test isequaldlm(readdlm2(IOBuffer("# Should ignore this π\n1;α\n2;β\n"), comments=true), Any[1 "α"; 2 "β"], Any)
    @test isequaldlm(readdlm2(IOBuffer("# Should ignore this π\n1\tα\n2\tβ\n"), '\t', rs=(), comments=true), Any[1 "α"; 2 "β"], Any)
    @test isequaldlm(readdlm2(IOBuffer("# Should ignore this π\n1;α\n2;β\n"), rs=(), comments=true), Any[1 "α"; 2 "β"], Any)
end

@testset "other issues" begin
    # BigInt parser
    let data = "1;2;3"
        @test readdlm2(IOBuffer(data), BigInt) == BigInt[1 2 3]
        @test readdlm2(IOBuffer(data), BigInt, rs=()) == BigInt[1 2 3]
    end
    let data = "1 2 3"
        @test readdlm2(IOBuffer(data), ' ', BigInt) == BigInt[1 2 3]
        @test readdlm2(IOBuffer(data), ' ', BigInt, rs=()) == BigInt[1 2 3]
    end

    # Test that we can read a write protected file
    let fn = tempname()
        open(fn, "w") do f
            write(f, "Julia")
        end
        chmod(fn, 0o444)
        @test readdlm2(fn)[] == "Julia"
        rm(fn)
    end

    # issue #21180
    let data = "\"721\",\"1438\",\"1439\",\"…\",\"1\""
        @test readdlm2(IOBuffer(data), ',', rs=()) == Any[721  1438  1439  "…"  1]
    end
    let data = "\"721\";\"1438\";\"1439\";\"…\";\"1\""
        @test readdlm2(IOBuffer(data)) == Any[721  1438  1439  "…"  1]
    end

    # issue #21207
    let data = "\"1\";\"灣\"\"灣灣灣灣\";\"3\""
        @test readdlm2(IOBuffer(data)) == Any[1 "灣\"灣灣灣灣" 3]
    end

    # issue #11484: useful error message for invalid readdlm filepath arguments
    # not implemented in ReadWriteDlm2
end

@testset "complex" begin
    @test readdlm2(IOBuffer("3+4im; 4+5im"), Complex{Int}) == [3+4im 4+5im]
end

# 2nd block
# Test the new functions of ReadWriteDlm2

@testset "dfregex" begin
    @test true  == occursin(dfregex("HH:MM"), "22:00")
    @test true  == occursin(dfregex("H:M"), "1:30")
    @test true  == occursin(dfregex("H:M"), "10:30")
    @test false == occursin(dfregex("H:M"), "10:62")
    @test true  == occursin(dfregex("HHhMM"), "22h00")
    @test false == occursin(dfregex("HHhMM"), "22h")
    @test true  == occursin(dfregex("Hh"), "22h")
    @test true  == occursin(dfregex("HhM\\min"), "22h15min")
    @test true  == occursin(dfregex("yyyy-mm-dd"), "2018-01-23")
    @test true  == occursin(dfregex("yyyy.mm.dd"), "2018.01.23")
    @test false == occursin(dfregex("yyyy-mm-dd"), "2018.01.23")
    @test true  == occursin(dfregex("yyyy-mm-ddTHH:MM:SS"), "2018-01-23T23:52:00")
    @test false == occursin(dfregex("yyyy-mm-ddTHH:MM:SS"), "2018-01-23T23:72:00")
    @test true  == occursin(dfregex("yyyy-mm-ddTHH:MMz"), "2018-01-23T23:52+01:00")
    @test true  == occursin(dfregex("yyyy-mm-ddTHH:MMz"), "2018-01-23T23:52+0100")
    @test true  == occursin(dfregex("yyyy-mm-ddTHH:MMz"), "2018-01-23T23:52-10:00")
    @test false == occursin(dfregex("yyyy-mm-ddTHH:MM z"), "2018-01-23T23:52+01:00")
    @test false == occursin(dfregex("yyyy-mm-ddTHH:MM z"), "2018-01-23T23:52 ")
    @test true  == occursin(dfregex("yyyy-mm-ddTHH:MM z"), "2018-01-23T23:52 10:00")
    @test true  == occursin(dfregex("yyyy-mm-ddTHH:MMZ"), "2018-01-23T23:52UTC")
    @test true  == occursin(dfregex("yyyy-mm-ddTHH:MMZ"), "2018-01-23T23:52CET")
    @test true  == occursin(dfregex("yyyy-mm-ddTHH:MMZ"), "2018-01-23T23:52CEST")
    @test false == occursin(dfregex("yyyy-mm-ddTHH:MM Z"), "2018-01-23T23:52CET")
    @test true  == occursin(dfregex("yyyy-mm-ddTHH:MM Z"), "2018-01-23T23:52 CEST")
    @test false == occursin(dfregex("yyyy-mm-ddTHH:MM Z"), "2018-01-23T23:52CEST")
    @test false == occursin(dfregex("yyyy-mm-ddTHH:MMZ"), "2018-01-23T23:52CE")
    @test true  == occursin(dfregex("yyyy-mm-ddTHH:MMZZZZZZ"), "2018-01-23T23:52CEST")
    @test true  == occursin(dfregex("yyyy-mm-ddTHH:MM ZZZZ"), "2018-01-23T23:52 CET")
    @test true  == occursin(dfregex("yyyy-mm-ddTHH:MM zzzzzz"), "2018-01-23T23:52 +11:00")
    @test true  == occursin(dfregex("yyyy-mm-ddTHH:MM zzzz"), "2018-01-23T23:52 -09:00")
    @test true  == occursin(dfregex("yyyy-mm-ddTHH:MM\\Z"), "2018-01-23T23:52Z")
end

@testset "new functions" begin
    let data = "2015-01-01;5,1;Text1\n10;19e6;4\n"
        @test isequaldlm(readdlm2(IOBuffer(data)), [Date(2015) 5.1 "Text1";10 190.0e5 4.0], Any)
        @test isequaldlm(readdlm2(IOBuffer(data)), [Date(2015) 5.1 "Text1";10 190.0e5 4.0], Any)
    end

    a = [Date(2015) 5.1 "Text1";10 190.0e5 4.0]
    writedlm2("test.csv", a, decimal='€')
    @test read("test.csv", String) == "2015-01-01;5€1;Text1\n10;1€9e7;4€0\n"
    b = readdlm2("test.csv", rs=(r"(\d)€(\d)", s"\1.\2"))
    rm("test.csv")
    @test b[1] == Date(2015)

    a = DateTime(2017)
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "2017-01-01T00:00:00.0\n"
    b = readdlm2("test.csv")
    rm("test.csv")
    @test b[1] == DateTime(2017)

    a = Any[Date(2017,1,14) DateTime(2017,2,15,23,40,59)]
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "2017-01-14;2017-02-15T23:40:59.0\n"
    b = readdlm2("test.csv")
    rm("test.csv")
    @test isequaldlm(a, b, Any)

    a = Any[Date(2017, 1, 1) DateTime(2017, 2, 15, 23, 0, 0)]
    writedlm2("test.csv", a, dfs="mm/yyyy", dtfs="dd.mm.yyyy/HH.h")
    @test read("test.csv", String) == "01/2017;15.02.2017/23.h\n"
    writedlm2("test.csv", a, dfs="", dtfs="")
    @test read("test.csv", String) == "2017-01-01;2017-02-15T23:00:00\n"
    writedlm2("test.csv", a, decimal='.')
    @test read("test.csv", String) == "2017-01-01;2017-02-15T23:00:00.0\n"
    writedlm2("test.csv", a, decimal='.', dfs="\\Y\\ear: yyyy", dtfs="\\Y\\ear: yyyy")
    @test read("test.csv", String) == "Year: 2017;Year: 2017\n"
    writedlm2("test.csv", a, dfs="mm/yyyy", dtfs="dd.mm.yyyy/HH.h")
    @test read("test.csv", String) == "01/2017;15.02.2017/23.h\n"
    b = readdlm2("test.csv", dfs="mm/yyyy", dtfs="dd.mm.yyyy/HH.h")
    @test isequaldlm(a, b, Any)
    b = readdlm2("test.csv", rs=(), dfs="mm/yyyy", dtfs="dd.mm.yyyy/HH.h")
    rm("test.csv")
    @test isequaldlm(a, b, Any)

    a = Date(2017,5,1)
    writedlm2("test.csv", a, dfs="dd.mm.yyyy")
    @test read("test.csv", String) == "01.05.2017\n"
    b = readdlm2("test.csv", dfs="dd.mm.yyyy")
    rm("test.csv")
    @test b[1] == a

    a = "ABC"
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "A\nB\nC\n"
    b = readdlm2("test.csv")
    rm("test.csv")
    @test b[1] == "A" && b[3] == "C"

    D = rand(1:9, 10, 5)
    writedlm2("test.csv", D)
    @test length(read("test.csv", String)) == 100
    b = readdlm2("test.csv")
    rm("test.csv")
    @test D == b

    a = 10.9
    writedlm2("test.csv", a, decimal='€')
    @test read("test.csv", String) == "10€9\n"
    b = readdlm2("test.csv")
    rm("test.csv")
    @test b[1] == "10€9"

    a = 10.9
    writedlm2("test.csv", a, decimal='.')
    @test read("test.csv", String) == "10.9\n"
    b = readdlm2("test.csv")
    rm("test.csv")
    @test b[1] == 10.9

    a = [10.9 12.5; Date(2017) DateTime(2017)]
    writedlm2("test.csv", a, ',', decimal='€')
    b = readdlm2("test.csv", ',', decimal='€')
    rm("test.csv")
    @test a == b

    a = [10.9 12.5; Date(2017) DateTime(2017)]
    writedlm2("test.csv", a, ',', decimal='.')
    b = readdlm2("test.csv", ',', decimal='.')
    rm("test.csv")
    @test a == b

    a = [10.9 12.5; Date(2017) DateTime(2017)]
    writedlm2("test.csv", a)
    b = readdlm2("test.csv")
    rm("test.csv")
    @test a == b
end

@testset "date format" begin
    # test date format strings with variable length
    a = DateTime(2017,5,1,5,59,1)
    writedlm2("test.csv", a, dtfs="E, dd.mm.yyyy H:M:S")
    @test read("test.csv", String) == "Monday, 01.05.2017 5:59:1\n"
    b = readdlm2("test.csv", dtfs="E, dd.mm.yyyy H:M:S")
    rm("test.csv")
    @test a == b[1]

    a = DateTime(2017,5,1,5,59,1,898)
    writedlm2("test.csv", a, dtfs="E, d.u yyyy H:M:S,s")
    @test read("test.csv", String) == "Monday, 1.May 2017 5:59:1,898\n"
    b = readdlm2("test.csv", dtfs="E, d.u yyyy H:M:S.s")
    @test a == b[1]
    b = readdlm2("test.csv", dtfs="E, d.u yyyy H:M:S,s")
    @test a == b[1]
    rm("test.csv")

    # test date format strings with fix length
    a = [DateTime(2017,5,1,5,59,1,898) 1.0 1.1 1.222e7 1 true]
    writedlm2("test.csv", a, dtfs="yyyyymmmdddTHHHMMMSSSsss")
    @test read("test.csv", String) == "02017005001T005059001898;1,0;1,1;1,222e7;1;true\n"
    b = readdlm2("test.csv", dtfs="yyyyymmmdddTHHHMMMSSSsss")
    rm("test.csv")
    @test b == a
end

@testset "Time parsing" begin
    a = [Dates.Time(23,55,56,123,456,789) Dates.Time(23,55,56,123,456) Dates.Time(23,55,56,123) Dates.Time(12,45) Dates.Time(11,23,11)]
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "23:55:56,123456789;23:55:56,123456;23:55:56,123;12:45:00;11:23:11\n"
    b = readdlm2("test.csv")
    @test b == a
    write("test.csv", "23:55:56.123456789;23:55:56,123456;23:55:56,123;12:45;11:23:11\n")
    b = readdlm2("test.csv")
    rm("test.csv")
    @test b == a

    a = [Dates.Time(23,55,56,123,456) Dates.Time(12,45) Dates.Time(11,23,11)]
    writedlm2("test.csv", a, decimal='.')
    @test read("test.csv", String) == "23:55:56.123456;12:45:00;11:23:11\n"
    @test readdlm2("test.csv", dtfs="", dfs="") == ["23:55:56.123456" "12:45:00" "11:23:11"]
    @test readdlm2("test.csv") == a
    rm("test.csv")
end

@testset "DateLocale" begin # Test locale for french and german
    Dates.LOCALES["french"] = Dates.DateLocale(
        ["janvier", "février", "mars", "avril", "mai", "juin",
            "juillet", "août", "septembre", "octobre", "novembre", "décembre"],
        ["janv", "févr", "mars", "avril", "mai", "juin",
            "juil", "août", "sept", "oct", "nov", "déc"],
        ["lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche"],
        ["lu", "ma", "me", "je", "ve", "sa", "di"],
        )

    Dates.LOCALES["german"] = Dates.DateLocale(
        ["Januar", "Februar", "März", "April", "Mai", "Juni",
            "Juli", "August", "September", "Oktober", "November", "Dezember"],
        ["Jan", "Feb", "Mar", "Apr", "Mai", "Jun",
            "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"],
        ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"],
        ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"],
        )

    a = DateTime(2017,5,1,5,59,1)
    writedlm2("test.csv", a, dtfs="E, dd.mm.yyyy H:M:S", locale="french")
    @test read("test.csv", String) == "lundi, 01.05.2017 5:59:1\n"
    b = readdlm2("test.csv", dtfs="E, dd.mm.yyyy H:M:S", locale="french")
    rm("test.csv")
    @test b[1] == a

    a = DateTime(2017,1,1,5,59,1,898)
    writedlm2("test.csv", a, dtfs="E, d.u yyyy H:M:S,s", locale="french")
    @test read("test.csv", String) == "dimanche, 1.janv 2017 5:59:1,898\n"
    b = readdlm2("test.csv", dtfs="E, d.u yyyy H:M:S.s", locale="french")
    @test b[1] == a
    b = readdlm2("test.csv", dtfs="E, d.u yyyy H:M:S,s", locale="french")
    @test b[1] == a
    rm("test.csv")

    a = DateTime(2017,8,1,5,59,1)
    writedlm2("test.csv", a, dtfs="E, dd.mm.yyyy H:M:S", locale="german")
    @test read("test.csv", String) == "Dienstag, 01.08.2017 5:59:1\n"
    b = readdlm2("test.csv", dtfs="E, dd.mm.yyyy H:M:S", locale="german")
    rm("test.csv")
    @test b[1] == a

    a = DateTime(2017,11,1,5,59,1,898)
    writedlm2("test.csv", a, dtfs="E, d. U yyyy H:M:S,s", locale="german")
    @test read("test.csv", String) == "Mittwoch, 1. November 2017 5:59:1,898\n"
    b = readdlm2("test.csv", dtfs="E, d. U yyyy H:M:S.s", locale="german")
    @test b[1] == a
    b = readdlm2("test.csv", dtfs="E, d. U yyyy H:M:S,s", locale="german")
    @test b[1] == a
    rm("test.csv")
end

@testset "other types" begin

    # Test Complex and Rational parsing
    a = Any[complex(-1,-2) complex(1.2,-2) complex(1e9,3e19) 1//3 -1//5 -2//-4 1//-0 -0//1]
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "-1 - 2im;1,2 - 2,0im;1,0e9 + 3,0e19im;1//3;-1//5;1//2;1//0;0//1\n"
    b = readdlm2("test.csv", Any)
    rm("test.csv")
    @test isequaldlm(a, b, Any)

    # Complex and Rational - tolerance with blancs, i/j and different signes
    write("test.csv", "    -1-2j; 1,2 - 2,0i ;1.0E9+3.0E19im;   -1//-3;1//-5;  1//2 ;1//-0;-0//1 \n")
    b = readdlm2("test.csv", Any)
    rm("test.csv")
    @test b == a

    # Test Complex and Rational parsing - decimal = '.', delimiter = \t
    a = Any[complex(-1,-2) complex(1.2,-2) complex(1e9,3e19) 1//3 -1//5 -2//-4 1//-0 -0//1]
    writedlm2("test.csv", a, '\t', decimal='.')
    @test read("test.csv", String) == "-1 - 2im\t1.2 - 2.0im\t1.0e9 + 3.0e19im\t1//3\t-1//5\t1//2\t1//0\t0//1\n"
    b = readdlm2("test.csv",'\t', Any, decimal='.')
    rm("test.csv")
    @test isequaldlm(a, b, Any)

    #  Test different types with header and Any Array - decimal comma
    a = Any["Nr" "Wert";1 Date(2017);2 DateTime(2018);3 Dates.Time(23,54,45,123,456,78);4 1.5e10+5im;5 1500//5;6 1.5e10]
    writedlm2("test.csv", a)
    @test read("test.csv", String) ==
    """
    Nr;Wert
    1;2017-01-01
    2;2018-01-01T00:00:00.0
    3;23:54:45,123456078
    4;1,5e10 + 5,0im
    5;300//1
    6;1,5e10
    """
    b = readdlm2("test.csv", Any, header=true)
    rm("test.csv")
    @test a[2:end,:] == b[1]
    @test a[1:1,:] == b[2]

    #  Test different types with header and Any Array - english decimal
    a = Any["Nr" "Value";1 Date(2017);2 DateTime(2018);3 Dates.Time(23,54,45,123,456,78);4 1.5e10+5im;5 1500//5;6 1.5e10]
    writedlm2("test.csv", a, '\t', decimal='.')
    @test read("test.csv", String) ==
    """
    Nr\tValue
    1\t2017-01-01
    2\t2018-01-01T00:00:00.0
    3\t23:54:45.123456078
    4\t1.5e10 + 5.0im
    5\t300//1
    6\t1.5e10
    """
    b = readdlm2("test.csv", '\t', Any, decimal='.', header=true)
    rm("test.csv")
    @test a[2:end,:] == b[1]
    @test a[1:1,:] == b[2]

    # Test Complex Array read and write
    a = Complex[complex(-1,-2) complex(1.2,-2) complex(-1e9,3e-19)]
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "-1 - 2im;1,2 - 2,0im;-1,0e9 + 3,0e-19im\n"
    b = readdlm2("test.csv", Complex)
    rm("test.csv")
    @test a == b
    @test typeof(b) == Array{Complex,2}

    a = Complex[complex(-1,-2) complex(1.2,-2) complex(-1e9,3e-19)]
    writedlm2("test.csv", a, imsuffix="i")
    @test read("test.csv", String) == "-1 - 2i;1,2 - 2,0i;-1,0e9 + 3,0e-19i\n"
    b = readdlm2("test.csv", Complex)
    rm("test.csv")
    @test a == b
    @test typeof(b) == Array{Complex,2}

    a = Any["test" "test2";complex(-1,-2) complex(1.2,-2);complex(-1e9,3e-19) complex(1,115)]
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "test;test2\n-1 - 2im;1,2 - 2,0im\n-1,0e9 + 3,0e-19im;1 + 115im\n"
    b = readdlm2("test.csv", Any)
    rm("test.csv")
    @test a == b
    @test typeof(b) == Array{Any,2}

    a = Any["test1" "test2";complex(-1,-2) complex(1.2,-2);complex(-1e9,3e-19) complex(1,115)]
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "test1;test2\n-1 - 2im;1,2 - 2,0im\n-1,0e9 + 3,0e-19im;1 + 115im\n"
    b, h = readdlm2("test.csv", Complex, header=true)
    rm("test.csv")
    @test b == Complex[complex(-1,-2) complex(1.2,-2);complex(-1e9,3e-19) complex(1,115)]
    @test h == AbstractString["test1" "test2"]

    a = Complex[complex(-1//3,-7//5) complex(1,-1//3) complex(-1//2,3e-19)]
    writedlm2("test.csv", a, imsuffix="i")
    @test read("test.csv", String) == "-1//3 - 7//5*i;1//1 - 1//3*i;-0,5 + 3,0e-19i\n"
    b = readdlm2("test.csv", Complex)
    rm("test.csv")
    @test a == b
    @test typeof(b) == Array{Complex,2}

    a = Complex[complex(-1//3,-7//5) complex(1,-1//3) complex(-1//2,3e-19)]
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "-1//3 - 7//5*im;1//1 - 1//3*im;-0,5 + 3,0e-19im\n"
    b = readdlm2("test.csv", Complex)
    rm("test.csv")
    @test a == b
    @test typeof(b) == Array{Complex,2}

    # Test Time read and write
    a = Time[Time(12,54,43,123,456,789) Time(13,45);Time(1,45,58,0,0,1) Time(23,59,59)]
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "12:54:43,123456789;13:45:00\n01:45:58,000000001;23:59:59\n"
    b = readdlm2("test.csv", Time)
    rm("test.csv")
    @test b == a
    @test typeof(b) == Array{Time,2}

    a = Any["test1" "test2";Time(12,54,43,123,456,789) Time(13,45);Time(1,45,58,0,0,1) Time(23,59,59)]
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "test1;test2\n12:54:43,123456789;13:45:00\n01:45:58,000000001;23:59:59\n"
    b, h = readdlm2("test.csv", Time, header=true)
    rm("test.csv")
    @test b == Time[Time(12,54,43,123,456,789) Time(13,45);Time(1,45,58,0,0,1) Time(23,59,59)]
    @test h == AbstractString["test1" "test2"]

    # Test Rational read and write
    a = Rational[456//123 123//45;456//23 1203//45]
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "152//41;41//15\n456//23;401//15\n"
    b = readdlm2("test.csv", Rational)
    rm("test.csv")
    @test b == a
    @test typeof(b) == Array{Rational,2}

    a = Any["test1" "test2";456//123 123//45;456//23 1203//45]
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "test1;test2\n152//41;41//15\n456//23;401//15\n"
    b, h = readdlm2("test.csv", Rational, header=true)
    rm("test.csv")
    @test b == Rational[456//123 123//45;456//23 1203//45]
    @test h == AbstractString["test1" "test2"]

    # Test DateTime read and write
    a = DateTime[DateTime(2017) DateTime(2016);DateTime(2015) DateTime(2014)]
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "2017-01-01T00:00:00.0;2016-01-01T00:00:00.0\n2015-01-01T00:00:00.0;2014-01-01T00:00:00.0\n"
    b = readdlm2("test.csv", DateTime)
    rm("test.csv")
    @test b == a
    @test typeof(b) == Array{DateTime,2}

    a = Any["test1" "test2";DateTime(2017) DateTime(2016);DateTime(2015) DateTime(2014)]
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "test1;test2\n2017-01-01T00:00:00.0;2016-01-01T00:00:00.0\n2015-01-01T00:00:00.0;2014-01-01T00:00:00.0\n"
    b, h = readdlm2("test.csv", DateTime, header=true)
    rm("test.csv")
    @test b == DateTime[DateTime(2017) DateTime(2016);DateTime(2015) DateTime(2014)]
    @test h == AbstractString["test1" "test2"]

    # Test Date read and write
    a = Date[Date(2017) Date(2016);Date(2015) Date(2014)]
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "2017-01-01;2016-01-01\n2015-01-01;2014-01-01\n"
    b = readdlm2("test.csv", Date)
    rm("test.csv")
    @test b == a
    @test typeof(b) == Array{Date,2}

    a = Any["test1" "test2";Date(2017) Date(2016);Date(2015) Date(2014)]
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "test1;test2\n2017-01-01;2016-01-01\n2015-01-01;2014-01-01\n"
    b, h = readdlm2("test.csv", Date, header=true)
    rm("test.csv")
    @test b == Date[Date(2017) Date(2016);Date(2015) Date(2014)]
    @test h == AbstractString["test1" "test2"]

    # Test alternative rs Tuple - is decimal ignored?
    a = Float64[1.1 1.2;2.1 2.2]
    writedlm2("test.csv", a, decimal='€')
    @test read("test.csv", String) == "1€1;1€2\n2€1;2€2\n"
    b = readdlm2("test.csv", Any, rs=(r"(\d)€(\d)", s"\1.\2"), decimal='n')
    rm("test.csv")
    @test a == b
end

# Tests for readcsv2 and writecsv2
# ================================

@testset "readwritecsv2" begin
    # test comments with readcsv2
    @test isequaldlm(readcsv2(IOBuffer("#this is comment\n1,2,3\n#one more comment\n4,5,6"), comments=true), Any[1 2 3;4 5 6], Any)
    @test isequaldlm(readcsv2(IOBuffer("#this is \n#comment\n1,2,3\n#one more \n#comment\n4,5,6"), comments=true), Any[1 2 3;4 5 6], Any)

    # test readcsv2 and writecsv2 with alternative decimal
    a = Float64[1.1 1.2;2.1 2.2]
    writecsv2("test.csv", a, decimal='€')
    @test read("test.csv", String) == "1€1,1€2\n2€1,2€2\n"
    b = readcsv2("test.csv", Any, rs=(r"(\d)€(\d)", s"\1.\2"), decimal='n')
    rm("test.csv")
    @test a == b

    #  Test different types with header for readcsv2 and writecsv2
    a = Any["Nr" "Value";1 Date(2017);2 DateTime(2018);3 Dates.Time(23,54,45,123,456,78);4 1.5e10+5im;5 1500//5;6 1.5e10]
    writecsv2("test.csv", a)
    @test read("test.csv", String) ==
    """
    Nr,Value
    1,2017-01-01
    2,2018-01-01T00:00:00.0
    3,23:54:45.123456078
    4,1.5e10 + 5.0im
    5,300//1
    6,1.5e10
    """
    b = readcsv2("test.csv", header=true)
    rm("test.csv")
    @test a[2:end,:] == b[1]
    @test a[1:1,:] == b[2]

    # Test size for readcsv2
    @test size(readcsv2(IOBuffer("1,2,3,4"))) == (1,4)
    @test size(readcsv2(IOBuffer("1,2,3,"))) == (1,4)
    @test size(readcsv2(IOBuffer("1,2,3,4\n"))) == (1,4)
    @test size(readcsv2(IOBuffer("1,2,3,\n"))) == (1,4)
    @test size(readcsv2(IOBuffer("1,2,3,4\n1,2,3,4"))) == (2,4)
    @test size(readcsv2(IOBuffer("1,2,3,4\n1,2,3,"))) == (2,4)
    @test size(readcsv2(IOBuffer("1,2,3,4\n1,2,3"))) == (2,4)

    @test size(readcsv2(IOBuffer("1,2,3,4\r\n"))) == (1,4)
    @test size(readcsv2(IOBuffer("1,2,3,4\r\n1,2,3\r\n"))) == (2,4)
    @test size(readcsv2(IOBuffer("1,2,3,4\r\n1,2,3,4\r\n"))) == (2,4)
    @test size(readcsv2(IOBuffer("1,2,3,\"4\"\r\n1,2,3,4\r\n"))) == (2,4)

    #Time types for readcsv2 and writecsv2
    a = [Dates.Time(23,55,56,123,456,789) Dates.Time(23,55,56,123,456) Dates.Time(23,55,56,123) Dates.Time(12,45) Dates.Time(11,23,11)]
    writecsv2("test.csv", a)
    @test read("test.csv", String) == "23:55:56.123456789,23:55:56.123456,23:55:56.123,12:45:00,11:23:11\n"
    b = readcsv2("test.csv")
    rm("test.csv")
    @test b == a

    # Test readcsv2/writecsv2 with Complex - Rationals
    a = Complex[complex(-1//3,-7//5) complex(1,-1//3) complex(-1//2,3e-19)]
    writecsv2("test.csv", a)
    @test read("test.csv", String) == "-1//3 - 7//5*im,1//1 - 1//3*im,-0.5 + 3.0e-19im\n"
    b = readcsv2("test.csv", Complex)
    rm("test.csv")
    @test a == b
    @test typeof(b) == Array{Complex,2}
end

@testset "empty IO-data" begin
    # Tests for empty IO-Data
    a = ""
    writedlm2("test.csv", a)
    @test read("test.csv", String) == ""
    b = readdlm2("test.csv")
    @test typeof(b) == Array{Any,2}
    @test typeof(readdlm2("test.csv", Any)) == Array{Any,2}
    rm("test.csv")
    @test isempty(b)
    writecsv2("test.csv", a)
    @test read("test.csv", String) == ""
    b = readcsv2("test.csv")
    @test typeof(b) == Array{Any,2}
    @test typeof(readcsv2("test.csv", Float64)) == Array{Float64,2}
    rm("test.csv")
    @test isempty(b)

    a = [""]
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "\n"
    b = readdlm2("test.csv")
    @test typeof(b) == Array{Any,2}
    @test typeof(readdlm2("test.csv", Any)) == Array{Any,2}
    rm("test.csv")
    @test isempty(b)
    writecsv2("test.csv", a)
    @test read("test.csv", String) == "\n"
    b = readcsv2("test.csv")
    @test typeof(b) == Array{Any,2}
    @test typeof(readcsv2("test.csv", Float64)) == Array{Float64,2}
    rm("test.csv")
    @test isempty(b)

    a = missing
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "na\n"
    b = readcsv2("test.csv")
    rm("test.csv")
    @test typeof(b) == Array{Any,2}
    @test isequal(a, b[1])
    writecsv2("test.csv", a)
    @test read("test.csv", String) == "na\n"
    b = readcsv2("test.csv")
    rm("test.csv")
    @test typeof(b) == Array{Any,2}
    @test isequal(a, b[1])

    a = [nothing]
    a = reshape(a, 1,1)
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "nothing\n"
    b = readdlm2("test.csv")
    rm("test.csv")
    @test typeof(b) == Array{Any,2}
    @test isequal(a, b)
    writecsv2("test.csv", a)
    @test read("test.csv", String) == "nothing\n"
    b = readdlm2("test.csv")
    rm("test.csv")
    @test typeof(b) == Array{Any,2}
    @test isequal(a, b)

    a = [1.2 NaN "" nothing missing]
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "1,2;NaN;;nothing;na\n"
    b = readdlm2("test.csv")
    rm("test.csv")
    @test isequal(a, b)

    a = [1.2 NaN "" nothing missing]
    writecsv2("test.csv", a)
    @test read("test.csv", String) == "1.2,NaN,,nothing,na\n"
    b = readcsv2("test.csv")
    rm("test.csv")
    @test isequal(a, b)

end

@testset "random data" begin
    # Test write and read of random Any array with 10000 rows
    n = 1000
    A = Array{Any}(undef, n, 9)
    for i = 1:n
        A[i,:] = Any[randn() rand(Int) rand(Bool) rand(Date(1980,1,1):Day(1):Date(2017,12,31)) rand(Dates.Time(0,0,0,0,0,0):Dates.Nanosecond(1):Dates.Time(23,59,59,999,999,999)) rand(DateTime(1980,1,1,0,0,0,0):Dates.Millisecond(1):DateTime(2017,12,31,23,59,59,999)) randstring(24) complex(randn(), randn()) (rand(Int)//rand(Int))]
    end

    writedlm2("test.csv", A)
    B = readdlm2("test.csv")
    rm("test.csv")
    @test isequaldlm(A, B, Any)

    writecsv2("test.csv", A)
    B = readcsv2("test.csv")
    rm("test.csv")
    @test isequaldlm(A, B, Any)
end

@testset "abstract type" begin

    #Abstract Time Types
    a = TimeType[Date(2017, 1, 1) DateTime(2017, 2, 15, 23, 0, 0)]
    writedlm2("test.csv", a, dfs="mm/yyyy", dtfs="dd.mm.yyyy/HH.h")
    b = readdlm2("test.csv", TimeType, dfs="mm/yyyy", dtfs="dd.mm.yyyy/HH.h")
    @test isequaldlm(a, b, TimeType)
    b = readdlm2("test.csv", TimeType, rs=(), dfs="mm/yyyy", dtfs="dd.mm.yyyy/HH.h")
    rm("test.csv")
    @test isequaldlm(a, b, TimeType)

    # Abstract Number
    a = Number[1 1.1 1//3 complex(-1,-2) complex(1.2,-2) complex(-1e9,3e-19)]
    writedlm2("test.csv", a)
    @test read("test.csv", String) == "1;1,1;1//3;-1 - 2im;1,2 - 2,0im;-1,0e9 + 3,0e-19im\n"
    b = readdlm2("test.csv", Number)
    rm("test.csv")
    @test a == b
    @test typeof(b) == Array{Number,2}

    # Abstract real
    a = readdlm2(IOBuffer("1,1;2\n3;4,5\n5;1//3\n"), Real)
    b = Real[1.1 2; 3 4.5; 5 1//3]
    @test a == b
    @test typeof(a) == typeof(b)

end

@testset "1 0 1 0 0" begin

    # Different Types for "1 0 1 0 0"
    a = readdlm2(IOBuffer("1;0;1;0;0\n"))
    b = Any[1 0 1 0 0]
    @test a == b
    @test typeof(a) == typeof(b)

    a = readdlm2(IOBuffer("1;0;1;0;0\n"), Float64)
    b = [1.0 0.0 1.0 0.0 0.0]
    @test a == b
    @test typeof(a) == typeof(b)

    a = readdlm2(IOBuffer("1;0;1;0;0\n"), Float16)
    b = Float16[1.0 0.0 1.0 0.0 0.0]
    @test a == b
    @test typeof(a) == typeof(b)

    a = readdlm2(IOBuffer("1;0;1;0;0\n"), Int)
    b = [1 0 1 0 0]
    @test a == b
    @test typeof(a) == typeof(b)

    a = readdlm2(IOBuffer("1;0;1;0;0\n"), Char)
    b = ['1' '0' '1' '0' '0']
    @test a == b
    @test typeof(a) == typeof(b)

    a = readdlm2(IOBuffer("1;0;1;0;0\n"), AbstractString)
    b = AbstractString["1" "0" "1" "0" "0"]
    @test a == b
    @test typeof(a) == typeof(b)

    a = readdlm2(IOBuffer("1;0;1;0;0\n"), String)
    b = ["1" "0" "1" "0" "0"]
    @test a == b
    @test typeof(a) == typeof(b)

    a = readdlm2(IOBuffer("1;0;1;0;0\n"), Bool)
    b = [true false true false false]
    @test a == b
    @test typeof(a) == typeof(b)

    a = readdlm2(IOBuffer("1;0;1;0;0\n"), Complex{Int})
    b = [1+0im 0+0im 1+0im 0+0im 0+0im]
    @test a == b
    @test typeof(a) == typeof(b)

    a = readdlm2(IOBuffer("1;0;1;0;0\n"), Complex{Float64})
    b = [1.0+0.0im 0.0+0.0im 1.0+0.0im 0.0+0.0im 0.0+0.0im]
    @test a == b
    @test typeof(a) == typeof(b)

    a = readdlm2(IOBuffer("1;0;1;0;0\n"), Complex{Rational{Int}})
    b = [1//1+0//1*im 0//1+0//1*im 1//1+0//1*im 0//1+0//1*im 0//1+0//1*im]
    @test a == b
    @test typeof(a) == typeof(b)

    a = readdlm2(IOBuffer("1;0;1;0;0\n"), Rational)
    b = Rational[1//1 0//1 1//1 0//1 0//1]
    @test a == b
    @test typeof(a) == typeof(b)

end

@testset "Examples" begin

    # README.md - Basic Example

    a = Any[1 1.2; "text" Date(2017,1,1)]   # create array with: Int, Float64, String and Date type
    writedlm2("test.csv", a)                # test.csv(decimal comma): "1;1,2\ntext;2017-01-01\n"
    @test readdlm2("test.csv") == a         # read `CSV` data: All four types are parsed correctly!
    rm("test.csv")

    # README.md - More Examples

    # `writecsv2()` And `readcsv2()`
    a = Any[1 complex(1.5,2.7);1.0 1//3]    # create array with: Int, Complex, Float64 and Rational type
    writecsv2("test.csv", a)                # test.csv(decimal dot): "1,1.5 + 2.7im\n1.0,1//3\n"
    @test readcsv2("test.csv")  == a        # read CSV data: All four types are parsed correctly!
    rm("test.csv")

    # `writedlm2()` And `readdlm2()` With Special `decimal=`
    a = Float64[1.1 1.2;2.1 2.2]
    writedlm2("test.csv", a; decimal='€')     # '€' is decimal Char in 'test.csv'
    @test readdlm2("test.csv", Float64; decimal='€') == a     # standard: use keyword argument
    @test readdlm2("test.csv", Float64; rs=(r"(\d)€(\d)", s"\1.\2")) == a   # alternativ: rs-Regex-Tupel
    rm("test.csv")

    # `writedlm2()` And `readdlm2()` With `Union{Missing, Float64}`
    a = Union{Missing, Float64}[1.1 0/0;missing 2.2;1/0 -1/0]
    writedlm2("test.csv", a; missingstring="???")     # use "???" for missing data
    @test read("test.csv", String) == "1,1;NaN\n???;2,2\nInf;-Inf\n"
    b = readdlm2("test.csv", Union{Missing, Float64}; missingstring="???")
    @test typeof(a) == typeof(b)
    @test isequal(a, b)
    rm("test.csv")

    # `Date` And `DateTime` With `locale="french"`
    Dates.LOCALES["french"] = Dates.DateLocale(
        ["janvier", "février", "mars", "avril", "mai", "juin",
            "juillet", "août", "septembre", "octobre", "novembre", "décembre"],
        ["janv", "févr", "mars", "avril", "mai", "juin",
            "juil", "août", "sept", "oct", "nov", "déc"],
        ["lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche"],
        ["lu", "ma", "me", "je", "ve", "sa", "di"],
        );

    a = hcat([Date(2017,1,1), DateTime(2017,1,1,5,59,1,898), 1, 1.0, "text"])
    writedlm2("test.csv", a; dfs="E, d.U yyyy", dtfs="e, d.u yyyy H:M:S,s", locale="french")

    @test read("test.csv", String) == "dimanche, 1.janvier 2017\ndi, 1.janv 2017 5:59:1,898\n1\n1,0\ntext\n"

    @test readdlm2("test.csv"; dfs="E, d.U yyyy", dtfs="e, d.u yyyy H:M:S,s", locale="french") == a
    rm("test.csv")

    # `readdlm2()` With Header for `DataFrames`
    a = ["date" "value"; Date(2017,1,1) 1.4; Date(2017,1,2) 1.8]
    writedlm2("test.csv", a)   # writes: "date;value\n2017-01-01;1,4\n2017-01-02;1,8\n"
    @test read("test.csv", String) == "date;value\n2017-01-01;1,4\n2017-01-02;1,8\n"
    @test readdlm2("test.csv"; dfheader=true) ==
        (Any[Date(2017,1,1) 1.4; Date(2017,1,2) 1.8], Symbol[:date, :value])
    rm("test.csv")
end


text:
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENSE in the project root.
# ------------------------------------------------------------------

"""
    Coboundary{P,Q}(topology)

The co-boundary relation from rank `P` to greater rank `Q` for
a given `topology`.
"""
struct Coboundary{P,Q,D,T<:Topology} <: TopologicalRelation
  topology::T
end

function Coboundary{P,Q}(topology) where {P,Q}
  D = paramdim(topology)
  T = typeof(topology)

  @assert P < Q ≤ D "invalid coboundary relation"

  Coboundary{P,Q,D,T}(topology)
end

# -------------------
# HALF-EDGE TOPOLOGY
# -------------------

function (𝒞::Coboundary{0,1,2,T})(vert::Integer) where {T<:HalfEdgeTopology}
  t = 𝒞.topology
  𝒜 = Adjacency{0}(t)
  [edge4pair(t, (vert, other)) for other in 𝒜(vert)]
end

function (𝒞::Coboundary{0,2,2,T})(vert::Integer) where {T<:HalfEdgeTopology}
  e = half4vert(𝒞.topology, vert)

  # initialize result
  elements = [e.elem]

  # search in CCW orientation
  p = e.prev
  h = p.half
  while !isnothing(h.elem) && h != e
    push!(elements, h.elem)
    p = h.prev
    h = p.half
  end

  # if border edge is hit
  if isnothing(h.elem)
    # search in CW orientation
    h = e.half
    while !isnothing(h.elem)
      pushfirst!(elements, h.elem)
      n = h.next
      h = n.half
    end
  end

  elements
end

function (𝒞::Coboundary{1,2,2,T})(edge::Integer) where {T<:HalfEdgeTopology}
  e = half4edge(𝒞.topology, edge)
  isnothing(e.half.elem) ? [e.elem] : [e.elem, e.half.elem]
end

ext: jl


text:
function spgr(; M0, angle, TR, R1, R2star = 0.0, TE = 0.0)
    return @. M0 * sin(angle) * exp(-R2star * TE) * (1 - exp(-R1 * TR)) /
              (1 - cos(angle) * exp(-R1 * TR))
end

function conc_to_R1(Ct; r1, R10)
    return @. R10 + r1 * Ct
end

function R1_to_concentration(R1; R10, r1)
    return @. (R1 - R10) / r1
end

function R1_to_signal(R1; M0, angle, TR)
    return spgr(; M0, angle, TR, R1)
end

function signal_to_R1(signal; R10, angle, TR, BAF::Int = 1, mask = [true])
    if typeof(signal) <: AbstractVector
        signal = reshape(signal, 1, length(signal))
        input_was_vector = true
    else
        input_was_vector = false
    end
    volume_size = size(signal)[1:end-1]
    R1 = fill(NaN, size(signal)...)
    resolved_R10 = unify_size(R10, volume_size)
    resolved_mask = resolve_mask_size(mask, volume_size)

    for voxel in findall(resolved_mask)
        R1[voxel, :] = _signal_to_R1(signal[voxel, :], resolved_R10[voxel], angle, TR, BAF)
    end
    if input_was_vector
        R1 = R1[:]
    end
    return R1
end

function _signal_to_R1(signal, R10, angle, TR, BAF)
    S_pre = mean(signal[1:BAF])
    cosα = cos(angle)
    E1 = exp(-TR * R10)
    s = signal ./ S_pre
    log_term = @. (1 - s + s * E1 - cosα * E1) / (1 - s * cosα + s * E1 * cosα - E1 * cosα)
    R1 = [val > 0 ? (-1 / TR) * log(val) : NaN for val in log_term]
    return R1
end

function concentration_to_signal(Ct; r1, M0, angle, TR, R10)
    R1 = concentration_to_R1(Ct; r1, R10)
    signal = R1_to_signal(R1; M0, angle, TR)
    return signal
end

function signal_to_concentration(S; R10, angle, TR, r1, BAF::Int = 1, mask = true)
    R1 = signal_to_R1(S; R10, angle, TR, BAF, mask)
    Ct = R1_to_concentration(R1; R10, r1)
    return Ct
end

function fit_relaxation_despot(;
    signal::AbstractArray,
    angles::AbstractVector,
    TR::Real,
    mask = true,
)
    (signal, angles, mask, num_angles, volume_size) =
        resolve_relaxation_inputs(; signal, angles, mask)
    M0, T1 = (fill(NaN, volume_size...) for _ = 1:2)

    for idx in eachindex(IndexCartesian(), mask)
        if mask[idx] == false
            continue
        end
        M0[idx], T1[idx] = _despot(signal[idx, :], angles, TR)
    end
    return (; est = (; M0, T1))
end

# Refs: Deoni, S. C. L., Peters, T. M., & Rutt, B. K. (2005). MRM 53(1), 237–241. https://doi.org/10.1002/mrm.20314
function _despot(signal::AbstractVector, alpha::AbstractVector, TR)
    x = signal ./ tan.(alpha)
    y = signal ./ sin.(alpha)
    numerator = sum(x .* y) - sum(x) .* sum(y) / length(alpha)
    denominator = sum(x .^ 2) - sum(x) .^ 2 / length(alpha)
    slope = numerator ./ denominator
    intercept = mean(y) - slope * mean(x)
    M0 = intercept / (1 - slope)
    if slope > 0
        T1 = -TR / log(slope)
    else
        T1 = NaN
    end
    return M0, T1
end

function fit_relaxation_novifast(;
    signal::AbstractArray,
    angles::AbstractVector,
    TR::Real,
    mask = true,
)
    (signal, angles, mask, num_angles, volume_size) =
        resolve_relaxation_inputs(; signal, angles, mask)
    M0, T1 = (fill(NaN, volume_size...) for _ = 1:2)

    for idx in eachindex(IndexCartesian(), mask)
        if mask[idx] == false
            continue
        end
        M0[idx], T1[idx] = _novifast(signal[idx, :], angles, TR)
    end
    return (; est = (; M0, T1))
end

# Refs: Ramos-Llorden, G., Vegas-Sanchez-Ferrero, G., Bjork, M., Vanhevel, F., Parizel, P. M., San Jose Estepar, R., … Sijbers, J. (2018). IEEE Transactions on Medical Imaging, 37(11), 2414–2427. https://doi.org/10.1109/TMI.2018.2833288
function _novifast(
    signal::AbstractVector,
    alpha::AbstractVector,
    TR;
    initialvalues = (M0 = 5000, T1 = 1500),
    maxiter = 10,
    tol = 1e-6,
)
    est =
        [initialvalues.M0 * (1 * exp(-TR / initialvalues.T1)), exp(-TR / initialvalues.T1)]
    sinα = sin.(alpha)
    cosα = cos.(alpha)
    k = 0
    relerr = 1e10
    while k <= maxiter && relerr > tol
        previous_est = copy(est)

        commondenom = 1 ./ (1 .- est[2] .* cosα)
        a = signal .* cosα .* commondenom
        b = sinα .* commondenom
        ahat = est[1] .* b .* cosα .* commondenom
        svec = signal .* commondenom

        svec_b = svec' * b
        b_a = b' * a
        svec_ahat = svec' * ahat
        a_ahat = a' * ahat
        b_b = b' * b
        b_ahat = b' * ahat

        num1 = svec_b * a_ahat - b_a * svec_ahat
        num2 = b_b * svec_ahat - svec_b * b_ahat
        denom = b_b * a_ahat - b_ahat * b_a
        est = [num1 / denom, num2 / denom]

        k = k + 1
        rel_err = norm(est .- previous_est) / norm(previous_est)
    end
    M0 = est[1] / (1 - est[2])
    if est[2] > 0
        T1 = -TR / log(est[2])
    else
        T1 = NaN
    end
    return M0, T1
end

function fit_relaxation_nls(;
    signal::AbstractArray,
    angles::AbstractVector,
    TR::Real,
    initialvalues = (M0 = 5000.0, T1 = 1500.0),
    mask = true,
)
    (signal, angles, mask, num_angles, volume_size) =
        resolve_relaxation_inputs(; signal, angles, mask)
    M0, T1 = (fill(NaN, volume_size...) for _ = 1:2)

    @. model(x, p) = _spgr(x, p[1], 1 / p[2], TR)
    for idx in eachindex(IndexCartesian(), mask)
        if mask[idx] == false
            continue
        end
        fit = curve_fit(model, angles, signal[idx, :], [initialvalues.M0, initialvalues.T1])
        M0[idx], T1[idx] = fit.param
    end
    return (; est = (; M0, T1))
end

# [Todo] Replace with spgr() --- but check for speed difference first
function _spgr(angle, M0, R1, TR)
    return @. M0 * sin(angle) * (1 - exp(-R1 * TR)) / (1 - cos(angle) * exp(-R1 * TR))
end

const relaxation_dict = Dict{Symbol,Function}(
    :nls => fit_relaxation_nls,
    :despot => fit_relaxation_despot,
    :novifast => fit_relaxation_novifast,
)

function fit_relaxation(fittype::Symbol = :novifast; kwargs...)
    return relaxation_dict[fittype](; kwargs...)
end


text:
const DOC_ROOT_PATH   = "public"
const CONFIG_PATH     = "config"
const ENV_PATH        = joinpath(CONFIG_PATH, "env")
const APP_PATH        = "app"
const RESOURCES_PATH  = joinpath(APP_PATH, "resources")
const TEST_PATH       = "test"
const TEST_PATH_UNIT  = joinpath(TEST_PATH, "unit")
const LIB_PATH        = "lib"
const HELPERS_PATH    = joinpath(APP_PATH, "helpers")
const LOG_PATH        = "log"
const LAYOUTS_PATH    = joinpath(APP_PATH, "layouts")
const TASKS_PATH      = "task"
const BUILD_PATH      = "build"
const PLUGINS_PATH    = "plugins"
const SESSIONS_PATH   = "sessions"
const CACHE_PATH      = "cache"

const GENIE_CONTROLLER_FILE_POSTFIX     = "Controller.jl"

const ROUTES_FILE_NAME  = "routes.jl"

const PARAMS_REQUEST_KEY    = :REQUEST
const PARAMS_RESPONSE_KEY   = :RESPONSE
const PARAMS_SESSION_KEY    = :SESSION
const PARAMS_FLASH_KEY      = :FLASH
const PARAMS_POST_KEY       = :POST
const PARAMS_GET_KEY        = :GET
const PARAMS_WS_CLIENT      = :WS_CLIENT
const PARAMS_JSON_PAYLOAD   = :JSON_PAYLOAD
const PARAMS_RAW_PAYLOAD    = :RAW_PAYLOAD
const PARAMS_FILES          = :FILES

const TEST_FILE_IDENTIFIER = "_test.jl"

const VIEWS_FOLDER = "views"
const LAYOUTS_FOLDER = "layouts"


text:
using Base.Test
using DataFrames

let
	#
	# IndexedVector tests
	#

	srand(1)
	a = DataArray(rand(1:5,20))
	a[1:2] = NA
	ia = IndexedVector(a)
	b = DataArray(rand(5:8,20))
	ib = IndexedVector(b)

	ia .== 4
	v = [1:20]
	@assert v[ia .== 4] == v[a .== 4]
	@assert sort(v[(ia .== 4) | (ia .== 5)]) == v[(a .== 4) | (a .== 5)]
	@assert sort(v[(ia .>= 4) & (ia .== 5)]) == v[(a .>= 4) & (a .== 5)]
	@assert sort(v[!(ia .== 4)]) == v[!(a .== 4)]
	@assert sort(v[findin(ia, [3:6])]) == v[findin(a, [3:6])]
	@assert sort(v[(ia .== 4) | (ib .== 6)]) == v[(a .== 4) | (b .== 6)]

	df = DataFrame(quote
	         x1 = IndexedVector(vcat(fill([1:5],4)...))
	         x2 = IndexedVector(vcat(fill(letters[1:10],2)...))
	     end)

	df[:(x2 .== "a"), :]
	df[:( (x2 .== "a") | (x1 .== 2) ), :]
	df[:( ("b" .<= x2 .<= "c") | (x1 .== 5) ), :]
	df[:( (x1 .== 1) & (x2 .== "a") ), :]

	df[findin(df["x2"], ["c","e","X"]), :]
end


text:
# This file was generated, do not modify it. # hide
@load BayesianLDA pkg=MultivariateStats

clf = machine(BayesianLDA(), X3, y)
fit!(clf, rows=train)
ŷ = predict_mode(clf, rows=test)

accuracy(ŷ, y[test]) |> r3


text:
# Extra stuff

# Utilities to make life easier when interoperating with C
const HSA_MUTABLE_TYPES = Union{hsa_region_t}

#=
Base.setproperty!(ref::Ref{T}, value, prop::Symbol) where T<:HSA_MUTABLE_TYPES =
    setproperty!(ref, value, Val(prop)
@generated function Base.setproperty!(ref::Ref{T}, value, prop::Val{V}) where {T<:HSA_MUTABLE_TYPES,V} =
    ftype = fieldtype(ref.parameters[1], V)
    return quote
        ptr = Ptr{$ftype}
        unsafe_store!(ptr,
    end
end
=#

#= FIXME
function memset!(ref::Ref{Nothing}, value)
    _ptr = Base.unsafe_convert(Ptr{Nothing}, ref)
    ptr = Base.unsafe_convert(Ptr{UInt}, _ptr)
    value = UInt(value)
    unsafe_store!(ptr, value)
end
=#
memset!(ref::Ref{T}, value::T) where T<:Union{Unsigned,Signed} =
    _memset!(ref, value, T)
function _memset!(ref, value, ::Type{T}; len=1) where T
    ptr = Base.unsafe_convert(Ptr{T}, ref)
    for i in 1:len
        unsafe_store!(ptr, value, i)
    end
end
function _memset!(ref::Ref{Ptr{Nothing}}, value; len=1)
    ptr = Base.unsafe_convert(Ptr{UInt8}, ref[])
    value = UInt8(value)
    for i in 1:len
        unsafe_store!(ptr, value, i)
    end
end

# Convenience constructors
const HSA_ZEROABLE_TYPES = Union{hsa_region_t}

newref!(::Type{Ref{T}}, value) where T<:HSA_ZEROABLE_TYPES = Ref{T}(T(value))

# Overloads for interface functions
function hsa_agent_get_info(agent, attribute, value::String)
    ccall((:hsa_agent_get_info, "libhsa-runtime64"), hsa_status_t, (hsa_agent_t, hsa_agent_info_t, Cstring), agent, attribute, value)
end
function hsa_agent_get_info(agent, attribute, value::Ref{T}) where T
    ccall((:hsa_agent_get_info, "libhsa-runtime64"), hsa_status_t, (hsa_agent_t, hsa_agent_info_t, Ptr{T}), agent, attribute, value)
end

function hsa_isa_get_info_alt(isa, attribute, value::Ref{T}) where T
    ccall((:hsa_isa_get_info_alt, "libhsa-runtime64"), hsa_status_t, (hsa_isa_t, hsa_isa_info_t, Ptr{T}), isa, attribute, value)
end
function hsa_isa_get_info_alt(isa, attribute, value::String)
    ccall((:hsa_isa_get_info_alt, "libhsa-runtime64"), hsa_status_t, (hsa_isa_t, hsa_isa_info_t, Cstring), isa, attribute, value)
end

function hsa_executable_symbol_get_info(executable_symbol, attribute, value::String)
    ccall((:hsa_executable_symbol_get_info, "libhsa-runtime64"), hsa_status_t, (hsa_executable_symbol_t, hsa_executable_symbol_info_t, Cstring), executable_symbol, attribute, value)
end

function hsa_memory_allocate(region, size, ref::Ref{Ptr{T}}) where T
    ccall((:hsa_memory_allocate, "libhsa-runtime64"), hsa_status_t, (hsa_region_t, Csize_t, Ptr{Ptr{T}}), region, size, ref)
end


text:
test_bike_week_1_env = {
    "shift_time_min" => 3,
    "shift_time_max" => 8,
    "coverage"  => Array[
        # Monday
        [3, 4, 5 * ones(Int, 6), 4 * ones(Int, 2), 2 * ones(Int, 2)],
        # Tuesday
        [3, 4, 5 * ones(Int, 6), 4 * ones(Int, 2), 2 * ones(Int, 2)],
        # Wednesday
        [3, 4, 5 * ones(Int, 6), 4 * ones(Int, 2), 2 * ones(Int, 2)],
        # Thursday
        [3, 4, 5 * ones(Int, 6), 4 * ones(Int, 2), 2 * ones(Int, 2)],
        # Friday
        [3, 4, 5 * ones(Int, 6), 4 * ones(Int, 2), 2 * ones(Int, 2)],
        # Saturday
        [1, 2 * ones(Int, 11)],
        # Sunday
        [1, 2 * ones(Int, 11)],
    ],
    "time_between_coverage" => 12,
    "intershift" => 13, # Time periods between shifts   # only serve 1 shift/day
}

# Coverage requires 291 man hours

test_bike_week_1_employees = {
    "Cody" => {
        "hours_min" => 18, # hours worked over simulation
        "hours_max" => 32, # hours worked over simulation
        "shift_time_min" => 4,
        "shift_count_min" => 3,
        "shift_count_max" => 5,
        "availability" => Array[
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
        ],
    },
    "Damian" => {
        "hours_min" => 12, # hours worked over simulation
        "hours_max" => 28, # hours worked over simulation
        "intershift" => 4,
        "shift_count_min" => 2,
        "shift_count_max" => 4,
        "availability" => Array[
            [ones(Int, 5), zeros(Int, 7)],
            [ones(Int, 5), zeros(Int, 7)],
            [ones(Int, 5), zeros(Int, 7)],
            [ones(Int, 5), zeros(Int, 7)],
            [ones(Int, 5), zeros(Int, 7)],
            [zeros(Int, 12)],
            [zeros(Int, 12)],
        ],
    },
    "David" => {
        "hours_min" => 12, # hours worked over simulation
        "hours_max" => 18, # hours worked over simulation
        "shift_count_min" => 2,
        "shift_count_max" => 3,
        "availability" => Array[
            [zeros(Int, 12)],
            [zeros(Int, 2), ones(Int, 10)],
            [zeros(Int, 2), ones(Int, 10)],
            [zeros(Int, 2), ones(Int, 10)],
            [zeros(Int, 12)],
            [zeros(Int, 12)],
            [zeros(Int, 12)],
        ],
    },
    "Douglas" => {
        "hours_min" => 12, # hours worked over simulation
        "hours_max" => 28, # hours worked over simulation
        "shift_count_min" => 2,
        "shift_count_max" => 4,
        "availability" => Array[
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
        ],
    },
    "Dylan" => {
        # deliberately no shift count
        "hours_min" => 28, # hours worked over simulation
        "hours_max" => 32, # hours worked over simulation
        "shift_time_min" => 6,
        "shift_count_min" => 4,
        "shift_count_max" => 5,
        "availability" => Array[
            [ones(Int, 7), zeros(Int, 5)],
            [zeros(Int, 12)],
            [zeros(Int, 12)],
            [ones(Int, 7), zeros(Int, 5)],
            [ones(Int, 7), zeros(Int, 5)],
            [ones(Int, 7), zeros(Int, 5)],
            [ones(Int, 7), zeros(Int, 5)],
        ],
    },
    "Jaeger" => {
        "hours_min" => 18, # hours worked over simulation
        "hours_max" => 32, # hours worked over simulation
        "shift_count_min" => 3,
        "shift_count_max" => 5,
        "availability" => Array[
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
        ],
    },
    "Julian" => {
        "hours_min" => 28, # hours worked over simulation
        "hours_max" => 32, # hours worked over simulation
        "shift_count_min" => 4,
        "shift_count_max" => 5,
        "availability" => Array[
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [zeros(Int, 4), ones(Int, 8)],
        ],
    },
    "Leonardo" => {
        "hours_min" => 18, # hours worked over simulation
        "hours_max" => 32, # hours worked over simulation
        "shift_count_min" => 3,
        "shift_count_max" => 5,
        "availability" => Array[
            [ones(Int, 7), zeros(Int, 5)],
            [ones(Int, 7), zeros(Int, 5)],
            [ones(Int, 7), zeros(Int, 5)],
            [ones(Int, 7), zeros(Int, 5)],
            [ones(Int, 7), zeros(Int, 5)],
            [ones(Int, 7), zeros(Int, 5)],
            [ones(Int, 7), zeros(Int, 5)],
        ],
    },
    "Patrick" => {
        "hours_min" => 12, # hours worked over simulation
        "hours_max" => 32, # hours worked over simulation
        "shift_count_min" => 2,
        "shift_count_max" => 5,
        "availability" => Array[
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
        ],
    },
    "R" => {
        "hours_min" => 12, # hours worked over simulation
        "hours_max" => 32, # hours worked over simulation
        "shift_count_min" => 2,
        "shift_count_max" => 5,
        "availability" => Array[
            [ones(Int, 8), zeros(Int, 4)],
            [ones(Int, 8), zeros(Int, 4)],
            [ones(Int, 8), zeros(Int, 4)],
            [ones(Int, 8), zeros(Int, 4)],
            [ones(Int, 8), zeros(Int, 4)],
            [ones(Int, 12)],
            [ones(Int, 12)],
        ],
    },
    "Shaun" => {
        "hours_min" => 18, # hours worked over simulation
        "hours_max" => 32, # hours worked over simulation
        "shift_count_min" => 3,
        "shift_count_max" => 5,
        "availability" => Array[
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [ones(Int, 12)],
            [zeros(Int, 2), ones(Int, 10)],
            [zeros(Int, 2), ones(Int, 10)],
        ],
    },
}

max_stars_repo_name: shdann/autoshift

max_issues_repo_name: shdann/autoshift

max_forks_repo_name: shdann/autoshift


text:
include("$MASTER/lib/ncmprojFINDINITIALDATA.jl")
include("$MASTER/lib/ncmprojMKCONTROLGRID.jl")

"""
"Implementation" of `SystemCore`; Only requires the ode `f` and its derivative `f'`, the
respective homotopy and its jacobian are derived. Again, look up the theoretical
documentation for more detail.
"""
type Galerkin <: SystemCore
	parent::Session
	f::Function
	projection::Function
	H::Function #derived
	J::Function #derived
	dataGUI::Dict{AbstractString, Any}
	Galerkin(parent,f,f′,projection) = new(parent,f,projection,fToH(f),f′ToJ(f′),Dict{AbstractString, Any}())
	Galerkin(parent,f,H,J,projection) = new(parent,f,projection,H,J,Dict{AbstractString, Any}())
end

#TODO dimensions

H(G::Galerkin) = G.H
J(G::Galerkin) = G.J

function Base.show(G::Galerkin)
	windowGalerkin = @Window("Galerkin Controls", 256, 100, false, true)
	setproperty!(windowGalerkin, :resizable, false)
	# setproperty!(windowGalerkin, :type_hint, Gtk.GdkWindowTypeHint.TOOLBAR)

	gridGalerkin = @Grid()
	setproperty!(gridGalerkin, :column_spacing, 5)
	setproperty!(gridGalerkin, :row_spacing, 5)
	push!(windowGalerkin, gridGalerkin)

	buttonFindInitial = @ToggleButton("Find Initial")
	local taskInitial
	signal_connect(buttonFindInitial, "toggled") do w
		if getproperty(w, :active, Bool)
			taskInitial = @schedule try handlerFindInitialData(w, G)
				finally @schedule setproperty!(w, :active, false) end
		else
			isa(taskInitial, Task) && !istaskdone(taskInitial) && schedule(taskInitial, InterruptException(); error=true)
		end
	end

	gridGalerkin[1:2,1] = mkControlGrid(G.dataGUI, Array[
		[ ("Trans. Iterations", Int, 4000, 1000:1000:1e8), Void, Void ],
		[ ("Trans. StepSize", Float64, .01, 1e-3:1e-3:.1), ("SS StepSize", Float64, .01, 1e-3:1e-3:.1) ],
		[ ("Max. period", Int, 30, 1:128), ("Intersections", Int, 120, 1:128) ],
		[ ("c₀", Float64, 350, -500:.01:500), ("m", Int, 64, 8:4096) ],
		[ buttonFindInitial ],
		[ ("Samples", Int, 64, 8:4096), ("Factor", Float64, 1.0, .0:.1:8.0) ],
		[ button(w->handlerResample(w, G), "Process") ]
	])

	showall(windowGalerkin)
end

function handlerResample(w, G)
	setproperty!(w, :sensitive, false)
	try
		#handle !isa Solution
		C = resample(G.parent.P.activeSolution, G.dataGUI["Samples"], G.dataGUI["Factor"])
		ω,ℵ = C[end-1:end]

		Htmp(V) = G.H([V; ℵ])
		Jtmp(V) = G.J([V; ℵ])[:, 1:end-1]
		tmp = newton(Htmp, Jtmp, C[1:end-1], predCount(10) ∧ predEps(1e-10))

		setActiveSolution(G.parent.P, [tmp; ℵ])
	finally
		setproperty!(w, :sensitive, true)
	end
	return Void
end

function handlerFindInitialData(w, G)
	try
		#TODO function/macro bringIntoScope(D::Dict)
		# TIters, SSIters, TStepSize, SSStepSize, Periods, m, c₀ = map(x->G.dataGUI[x], ["Trans. Iterations", "SS Iterations", "Trans. StepSize", "SS StepSize", "Periods", "m", "c₀"])
		TIters, TStepSize, SSStepSize, maxCycles, nIntersections, m, c₀ = map(x->G.dataGUI[x], ["Trans. Iterations", "Trans. StepSize", "SS StepSize", "Max. period", "Intersections", "m", "c₀"])

		# dataT, dataSS, P = findCycle((t,v)->f(t,[v;c₀]), .0, rand(3), TIters, TStepSize, SSIters, SSStepSize)
		# cyc,ω = prepareCycle(dataSS, SSStepSize, P; fac=Periods)
		cyc, P, ω = findCyclePoincare((t,v)->G.f(t,[v;c₀]), rand(3),
			nIntersections=nIntersections, maxCycles=maxCycles, sampleSize=m,
			transientIterations=TIters, transientStepSize=TStepSize,
			steadyStateStepSize=SSStepSize)

		fs = mapslices(x->interpolateLanczos(x,3), cyc, [1])
		T = linspace(1,size(cyc,1),2m)
		cyc = reduce(hcat, map(f->rfft(f(T)), fs))

		C = wrap(cyc, ω, c₀)
		C = resample(C, m)
		C = C[1:end-1]

		Htmp(V) = G.H([V; c₀])
		Jtmp(V) = G.J([V; c₀])[:, 1:end-1]
		C = newton(Htmp, Jtmp, C, predCount(25) ∧ predEps(1e-8))

		setActiveSolution(G.parent.P, [C; c₀])
	catch e
		println(e)
	end
	return Void
end

function unwrap(V)
	isa(V, Solution) && (V=V.data)
	ω,ℵ = V[end-1:end]
	m = (length(V)-5)÷6
	W = reshape(V[1:end-2], 2m+1, 3)
	W = complex(W[1:m+1,:], [zeros(1, size(W,2)); W[m+2:end,:]])
	return W,ω,ℵ
end

function wrap(V,ω,ℵ)
	return [vec(vcat(real(V), imag(V[2:end,:]))); ω; ℵ]
end

# array of functions interpolating a component each
function interps(W)
	m = size(W,1)-1
	fs = mapslices(W, [1]) do w
		interpolateTrigonometric(real(w[1])/2m, real(w[2:end])/m, -imag(w[2:end])/m)
	end
	return vec(fs)
end

# complete vectorized interpolated trajectory
#TODO this is trivially parallelizable...
function interp(W)
	fs = interps(W)
	return t -> mapreduce(g -> g(t), hcat, fs)
end

function projection(G::Galerkin, V)
	# W,ω,ℵ = unwrap(V)
	# m = size(W,1)-1
	# return G.projection(interps(W), ℵ, 2m)
	G.projection(V)
end

# takes real F-coefficients  V  , returns resampled version
function resample(V, m::Int, scale=1.0)
	W,ω,ℵ = unwrap(V)
	f = interp(W)
	T = linspace(.0,2pi*scale,2*m+2)[1:end-1]
	wrap(rfft(f(T),[1]), ω/scale, ℵ)
end

function fToH(f)
	return function H(V::Vector{Float64})
		m = length(V-5)÷6
		anchor = sum(V[1:m+1]) #TODO hm...

		ω,ρ = V[end-1:end]
		V = reshape(V[1:end-2],2m+1,3)
		V = complex(V[1:m+1,:], [zeros(3)'; V[m+2:end,:]])

		defect = rfft(mapslices(x->f(0,[x;ρ]), irfft(V, 2m, [1]), [2]), [1]) - im*ω*(0:m) .* V
		defect = vec([ real(defect); imag(defect)[2:end,:] ])

		return [defect; anchor]
	end
end

function f′ToJ(f′)
	return function J(V::Vector{Float64})
		m = length(V-5)÷6
		ω,ρ = V[end-1:end]
		V = reshape(V[1:end-2],2m+1,3)
		V = complex(V[1:m+1,:], [zeros(3)'; V[m+2:end,:]])

		T = irfft(V, 2m, [1])
		M = Array{Float64}(size(T,1), size(T,2), size(T,2)+1) #n, comp, deriv
		for i in 1:size(T,1) M[i,:,:] = f′(0,[T[i,:]'; ρ]) end

		M′ = [ rCCD(rfft(M[:,i,j])) for i in 1:size(T,2), j in 1:size(T,2) ]

		for i in 1:size(T,2)
			D = diagm(ω*(0:m))
			M′[i,i][m+2:end,1:m+1] -= D[2:end,:]
			M′[i,i][1:m+1, m+2:end] += D[:,2:end]
		end

		M′ = reducedim(vcat, M′, [1], Array{Float64}(0,size(M′[1,1],2)))
		M′ = reducedim(hcat, M′, [2], Array{Float64}(size(M′[1,1],1),0))[1]

		ddω = -im*(0:m).*V
		ddω = vec([real(ddω); imag(ddω)[2:end,:]])

		ddρ = reduce(hcat, [ rfft(M[:,i,size(T,2)+1]) for i in 1:size(T,2) ])
		ddρ = vec([real(ddρ); imag(ddρ)[2:end,:]])

		return [
			M′ ddω ddρ
			[ones(1,m+1) zeros(1,5m+2)] 0 0
		]
	end
end

# Jacobian of circular convolution of coefficients of real functions in appropriate format.
# ∂/∂X cconv(X,Y) = ∂/∂X F(x⋅y) = rCCD(Y)
rCCD(C) = rCCD(real(C[1]), real(C[2:end]), imag(C[2:end]))
function rCCD(V₀,Vᵣ,Vᵢ)
	m = length(Vᵣ)
	I1 = [ mod(i-j, 2m+1)+1 for i in 0:m, j in 0:m ]
	I2 = [ mod(i+j, 2m+1)+1 for i in 0:m, j in 0:m ]
	Wᵣ = [V₀; Vᵣ; Vᵣ[end:-1:1]]
	Wᵢ = [.0; Vᵢ; -Vᵢ[end:-1:1]]

	local rtn =  [
		(Wᵣ[I1]+Wᵣ[I2])					(-Wᵢ[I1]+Wᵢ[I2])[:,2:end]
		(Wᵢ[I1]+Wᵢ[I2])[2:end,:]		(Wᵣ[I1]-Wᵣ[I2])[2:end,2:end]
	] / (2m)
	rtn[:,1] /= 2
	return rtn
end