
proc test( code, expected ) is
  write( "quake(", code, ")", CR, "expected: ", expected, CR, "result:   ")
  quake( code )
  write( CR )
end

proc quote( s ) is
  return encode( s )
end

proc header( id ) is
  write( CR, "------ " & id & 
         " ------------------------------------------------------------------",
         CR )
end

proc section( name ) is
  write( CR, CR, "---------------------------------------" &
        "---------------------------------------", CR )
  write( name )
  write( CR, "---------------------------------------" &
        "---------------------------------------", CR, CR )
end

oks = []
kos = []

proc summary() is
  section( "summary" )
  nok = len( oks )
  nko = len( kos )
  write( nok, " tests succeeded:", CR, oks, CR, CR )
  write( nko, " tests failed:", CR, kos, CR )
end

%
% some tests have two acceptable outputs
%
proc check2( id, code, expected, expected2 ) is
  header( id )
  if equal (expected, expected2)
    write( "quake(", quote(code), ")", CR, "expected: ", 
           sub(quote(expected), 0, 68), CR)
  else
    write( "quake(", quote(code), ")", CR, "expected: ", sub(quote(expected), 0, 68), CR,
           "or expected: ", sub(quote(expected2), 0, 68), CR)
  end
  quake( code )
  write( "result:   " & sub(quote(res), 0, 68), CR )
  if equal( res & "", expected & "") or equal( res & "", expected2 & "")
    write( "==> OK", CR )
    oks += id
    %return "T"
  else
    write( "==> FAILED", CR )
    kos += id
    %return ""
  end
end

proc check( id, code, expected ) is
  check2 (id, code, expected, expected )
end

proc checkExec( id, code ) is
  header( id )
  write( "quake(", code, ") --> " )
  quake( code )
  if equal( res, 0 )
    write( "OK", CR )
    oks += id
  else
    write( "FAILED (ret = " & res & ")", CR )
    kos += id
  end
end

write( "quake extension tests", CR, CR )

proc findexe( cmd ) is
  write( "findexe(" & cmd & ")", CR )
  local paths = []
  if equal( OS_TYPE, "WIN32" )
    paths = split( $PATH, ";" )
  else
    paths = split( $PATH, ":" )
  end
  local p = ""
  foreach p in paths
    local pn = p & SL & cmd
    if fs_isfile( pn ) and fs_executable( pn )
      return pn
    end
    if equal( OS_TYPE, "WIN32" )
      local ext = ""
      foreach ext in [ "exe", "cmd", "bat" ]
        local pne = pn & "." & ext
        if fs_isfile( pne ) and fs_executable( pne )
          return pne
        end
      end
    end
  end
  return "false"
end

%-----------------------------------------------------------------------------
section( "string function tests" )

%tx = "x = 3 write(\"x = \" & x, CR)"
%test( tx, "x = 3" )

t = "a = \" \t ha ha\" res = skipl( a )"
check( "t001", t, "ha ha" )

t = "a = \"  ha\" res = skipl( a )"
check( "t002", t, "ha" )

t = "a = \"  ha  \" res = skipr( a ) & \"x\""
check( "t003", t, "  hax" )

t = "a = \"  ha  \" res = compress( a ) & \"x\""
check( "t004", t, "hax" )

t = "a = \"apple plum  orange\" b = split(a, \" \") res = b[0] & b[2]"
check( "t005", t, "appleorange" )

t = "a = \"applepie\" res = sub(a, 5, 3)"
check( "t006", t, "pie" )

t = "a = \"applepie\" res = sub(a, 7, 3)"
check( "t007", t, "e" )

t = "a = \"a\n\n\nb\n\n\n\nc\n\" res = squeeze(a)"
check( "t008", t, "a\n\nb\n\nc\n" )

t = "a = \"applepie\" res = tcontains(a, \"pie\")"
check( "t009", t, "TRUE" )

t = "a = \"applepie\" res = tcontains(a, \"pies\")"
check( "t010", t, "" )

t = "a = \"applepie\" res = pos(a, \"pie\")"
check( "t011", t, 5 )

t = "a = \"applepie\" res = pos(a, \"pies\")"
check( "t012", t, "-1" )

t = "a = \"applepie\" n = pos(a, \"pie\") res = sub(a, n, 1)"
check( "t013", t, "p" )

t = "res = bool(\"true\")"
check( "t014", t, "TRUE")

t = "res = bool(\"tRuE\")"
check( "t015", t, "TRUE")

t = "res = bool(\"TRUE\")"
check( "t016", t, "TRUE")

t = "res = bool(\"y\")"
check( "t017", t, "TRUE")

t = "res = bool(\"yes\")"
check( "t018", t, "TRUE")

t = "res = bool(\"Y\")"
check( "t019", t, "TRUE")

t = "res = bool(\"YES \")"
check( "t020", t, "TRUE")

t = "res = bool(\"no\")"
check( "t021", t, "")

t = "res = bool(\"false\")"
check( "t022", t, "")

t = "res = bool(\"foo\")"
check( "t023", t, "")

t = "res = bool(\"0\")"
check( "t024", t, "")

t = "res = bool(\"1\")"
check( "t025", t, "TRUE")

t = "a = \"aabaacabbbaccbca\" res = subst_chars(a, \"b\", \"d\")"
check( "t026", t, "aadaacadddaccdca")

t = "a = \"aabaacabbbaccbca\" res = subst_chars(a, \"bc\", \"dd\")"
check( "t027", t, "aadaadadddadddda")

t = "a = \"aabaacabbbaccbca\" res = del_chars(a, \"b\")"
check( "t028", t, "aaaacaaccca")

t = "a = \"aabaacabbbaccbca\" res = del_chars(a, \"bc\")"
check( "t029", t, "aaaaaaa")

t = "a = \"aabaacabbbaaccbca\" res = subst(a, \"aa\", \" 42 \", 1)"
check( "t030", t, " 42 baacabbbaaccbca")

t = "a = \"aabaacabbbaaccbca\" res = subst(a, \"aa\", \" 42 \", 2)"
check( "t031", t, " 42 b 42 cabbbaaccbca")

t = "a = \"aabaacabbbaaccbca\" res = subst(a, \"aa\", \" 42 \", 99)"
check( "t032", t, " 42 b 42 cabbb 42 ccbca")

t = "a = [ \"a\", \"b\", \"c\" ] res = add_prefix(a, \"pre-\")"
check( "t033", t, [ "pre-a", "pre-b", "pre-c" ] )

t = "a = [ \"a\", \"b\", \"c\" ] res = add_suffix(a, \"-suf\")"
check( "t034", t, [ "a-suf", "b-suf", "c-suf" ] )

t = "a = \"0123456789\"res = len( a )"
check( "t035", t, "10")

t = "a = [ \"a\", \"b\", \"c\" ] res = len( a )"
check( "t036", t, "3")

t = "a = { \"a\" : \"b\", \"c\" : \"d\" } res = len( a )"
check( "t037", t, "2")


%-----------------------------------------------------------------------------
section( "large string tests" )
SP = " "
write( "16", SP )
b = "0123456789abcdef"
write( "32", SP )
b = b & b
write( "64", SP )
b = b & b
write( "128", SP )
b = b & b
write( "256", SP )
b = b & b
write( "512", SP )
b = b & b
write( "1k", SP )
b = b & b
write( "2k", SP )
b = b & b
write( "4k", SP )
b = b & b
write( "8k", SP )
b = b & b
write( "16k", SP )
b = b & b
write( "32k", SP )
b = b & b
write( "64k", SP )
b = b & b
write( "128k", SP )
b = b & b
write( "256k", SP )
b = b & b
write( "512k", SP )
b = b & b
write( "1m", SP )
b = b & b
write( "OK", CR )

t = "a = subst_chars(b, \"bc\", \"xy\") res = subst_chars(b, \"xy\", \"bc\")"
check( "t100", t, b)

t = "res = sub(del_chars(b, \"0123456789cdef\"), 0, 10)"
check( "t101", t, "ababababab")

t = "res = len( b )"
check( "t102", t, "1048576")


%-----------------------------------------------------------------------------
section( "pathname tests" )

a = SL & "a" & SL & "b" & SL & "c"
a2 = SL & "a" & SL & "b"
a3 = SL & "a"
d = "d" & SL & "e"
e = "e.ext"

t = "res = pn_valid(a)"
check( "p001", t, "TRUE" )

t = "res = pn_valid(d)"
check( "p002", t, "TRUE" )

t = "res = pn_valid(e)"
check( "p003", t, "TRUE" )

t = "res = pn_absolute(a)"
check( "p004", t, "TRUE" )

t = "res = pn_absolute(d)"
check( "p005", t, "" )

t = "res = pn_absolute(e)"
check( "p006", t, "" )

t = "res = pn_decompose(a)"
check( "p007", t, "/ a b c" )

t = "res = pn_decompose(d)"
check( "p008", t, " d e" )

t = "res = pn_decompose(e)"
check( "p009", t, " e.ext" )

t = "res = pn_decompose(d)[0]"
check( "p010", t, "" )

t = "res = pn_decompose(d)[1]"
check( "p011", t, "d" )

t = "res = pn_decompose(d)[2]"
check( "p012", t, "e" )

t = "res = pn_compose(pn_decompose(a))"
check( "p013", t, a )

t = "res = pn_compose(pn_decompose(d))"
check( "p014", t, d )

t = "res = pn_prefix(a)"
check( "p015", t, a2 )

t = "res = pn_prefix(pn_prefix(a))"
check( "p016", t, a3 )

t = "res = pn_prefix(e)"
check( "p017", t, "" )

t = "res = pn_last(a)"
check( "p018", t, "c" )

t = "res = pn_last(a2)"
check( "p019", t, "b" )

t = "res = pn_last(d)"
check( "p020", t, "e" )

t = "res = pn_last(e)"
check( "p021", t, "e.ext" )

t = "res = pn_base(a & \".txt\")"
check( "p022", t, a )

t = "res = pn_base(a & \".\")"
check( "p023", t, a & "." )

t = "res = pn_base(a)"
check( "p024", t, a )

t = "res = pn_base(e)"
check( "p025", t, "e" )

t = "res = pn_lastbase(a & \".txt\")"
check( "p026", t, "c" )

t = "res = pn_lastbase(e)"
check( "p027", t, "e" )

t = "res = pn_lastext(a)"
check( "p028", t, "" )

t = "res = pn_lastext(a & \".txt\")"
check( "p029", t, "txt" )

t = "res = pn_lastext(e)"
check( "p030", t, "ext" )

t = "res = pn_replace_ext(a & \".txt\", \"log\")"
check( "p031", t, a & ".log" )

t = "res = pn_replace_ext(e, \"log\")"
check( "p032", t, "e.log" )

t = "res = pn_join(a, d)"
check( "p033", t, a & SL & d )

t = "res = pn_join2(a, d, \"log\")"
check( "p034", t, a & SL & d & ".log" )

t = "res = pn_join(a, pn_parent())"
check( "p035", t, a & SL & ".." )

t = "res = pn_join(pn_current(), d)"
check( "p036", t, "." & SL & d )



%-----------------------------------------------------------------------------
section( "file system tests" )

f = "res = fs_exists(\".\")"
check( "f001", f, "TRUE" )

f = "res = fs_exists(\"..\")"
check( "f002", f, "TRUE" )

f = "res = fs_exists(\"..\" & SL & \"src\")"
check( "f003", f, "TRUE" )

f = "res = fs_isdir(\".\")"
check( "f004", f, "TRUE" )

f = "res = fs_isdir(\"..\")"
check( "f005", f, "TRUE" )

f = "res = fs_isdir(\"..\" & SL & \"src\")"
check( "f006", f, "TRUE" )

f = "res = fs_isfile(\".\")"
check( "f007", f, "" )

f = "res = fs_isfile(\"..\")"
check( "f008", f, "" )

f = "res = fs_isfile(\"..\" & SL & \"src\")"
check( "f009", f, "" )

f = "res = fs_isfile(\"..\" & SL & \"src\" & SL & \"m3makefile\")"
check( "f010", f, "TRUE" )

f = "res = fs_isdir(\"..\" & SL & \"src\" & SL & \"m3makefile\")"
check( "f011", f, "" )

more = findexe( "more" )
f = "res = fs_executable( more )"
check( "f012", f, "TRUE" )

% readonly is a Quake reserved word.
onlyread = "onlyread"
fs_rmfile( onlyread )
> onlyread in
end
exec( "chmod -w onlyread" )
f = "res = fs_writable( onlyread )"
check( "f013", f, "" )

f = "res = fs_writable(\"..\" & SL & \"src\" & SL & \"m3makefile\")"
check( "f014", f, "TRUE" )

%write( pn, CR )
cm3 = findexe( "cm3" )
%write( pn , CR )

orange = "orange" data = "line1\nline2\line3\n"
f = "fs_putfile( orange, data ) res = fs_contents( orange )"
check( "f015", f, data )

dirs   = "a" & SL & "b" 
dirs_0 = dirs & SL & "c"
dirs_1 = dirs & SL & "cc"
dirs_2 = dirs & SL & "ccc"
dirs_3 = "a" & SL & "bb"
fn_a = dirs_0 & SL & "a"
fn_b = dirs_0 & SL & "b"
fn_c = dirs_0 & SL & "c"

write( CR, "--------------------------------------", CR )
write( "dirs   = ", dirs, CR )
write( "dirs_0 = ", dirs_0, CR )
write( "dirs_1 = ", dirs_1, CR )
write( "dirs_2 = ", dirs_2, CR )
write( "dirs_3 = ", dirs_3, CR )
write( "fn_a   = ", fn_a, CR )
write( "fn_b   = ", fn_b, CR )
write( "fn_c   = ", fn_c, CR )

f = "fs_mkdir( dirs_0 ) res = fs_isdir( dirs_0 )"
check( "f016", f, "TRUE" )

f = "fs_mkdir( dirs_1 ) res = fs_isdir( dirs_1 )"
check( "f017", f, "TRUE" )

f = "fs_mkdir( dirs_2 ) res = fs_isdir( dirs_2 )"
check( "f018", f, "TRUE" )

f = "fs_mkdir( dirs_3 ) res = fs_isdir( dirs_3 )"
check( "f019", f, "TRUE" )

f = "res = fs_lsdirs( dirs, \"\" )"
check( "f020", f, [dirs_0, dirs_1, dirs_2] )

f = "res = fs_lsdirs( dirs, \"T\" )"
check( "f021", f, "c cc ccc" )

f = "fs_touch( fn_a ) res = fs_isfile( fn_a )"
check( "f022", f, "TRUE" )

f = "fs_touch( fn_b ) res = fs_isfile( fn_b )"
check( "f023", f, "TRUE" )

f = "fs_touch( fn_c ) res = fs_isfile( fn_c )"
check( "f024", f, "TRUE" )

f = "res = fs_lsfiles( dirs_0, \"\" )"
check( "f025", f, [fn_a, fn_b, fn_c] )

f = "res = fs_lsfiles( dirs_0, \"T\" )"
check( "f026", f, "a b c" )

f = "res = fs_lsfiles( dirs, \"T\" )"
check( "f027", f, "" )

f = "fs_rmfile(fn_b) res = fs_lsfiles( dirs_0, \"T\" )"
check( "f028", f, "a c" )

f = "fs_rmfile(fn_b) res = fs_lsfiles( dirs_0, \"T\" )"
check( "f029", f, "a c" )

f = "fs_rmdir(dirs_3) res = fs_lsdirs( \"a\", \"T\" )"
check( "f030", f, "b" )

f = "fs_rmdir(dirs_3) res = fs_lsdirs( \"a\", \"T\" )"
check( "f031", f, "b" )

f = "fs_rmrec(dirs) res = fs_lsdirs( \"a\", \"T\" )"
check( "f032", f, "" )

f = "fs_touch(dirs) res = fs_lsfiles( \"a\", \"T\" )"
check( "f033", f, "b" )

apple = "apple"
f = "fs_cp( orange, apple )  res = fs_contents( apple )"
check( "f034", f, data )

apple2 = "a" & SL & apple
f = "fs_cp( orange, apple2 )  res = fs_contents( apple2 )"
check( "f035", f, data )

f = "res = fs_lsfiles( \"a\", \"T\" )"
check2( "f036", f, "b apple", "apple b" )

f = "fs_rmfile(apple2) res = fs_lsfiles( \"a\", \"T\" )"
check( "f037", f, "b" )

%f = "fs_cp( orange, \"a\" )  res = fs_contents( apple2 )"
%check( "f038", f, data )


f = "fs_rmrec(\"a\") res = fs_lsdirs( \".\", \"T\" )"
check( "f099", f, "" )


%-----------------------------------------------------------------------------
section( "exec tests" )

header( "e001" )
res = q_exec_get( "ls -l" )
write( "rc = ", res[0], CR, "out = ", res[1], CR )

checkExec( "e002", "res = q_exec( \"cm3 -version > cm3.version\" )" )
checkExec( "e003", "res = q_exec( \"rm cm3.version\" )" )

header( "e004" )
a = "a\nb\n\c"
res = q_exec_put( "cat -", a )

checkExec( "e005", "res = q_exec( \"echo a && echo b && echo c\" )" )
checkExec( "e006", "res = q_exec( \"echo a && false || echo c\" )" )
checkExec( "e007", "res = q_exec( \"echo a | cat -\" )" )
checkExec( "e008", "res = q_exec( \"echo abcdefgh | sed -e 's/d/4/g' | sed -e 's/h/8/g'\" )" )
checkExec( "e009", "res = q_exec( \"true && echo abcdefgh | sed -e 's/d/4/g' | sed -e 's/h/8/g'\" )" )
checkExec( "e010", "res = q_exec( \"echo abcdefgh | sed -e 's/d/4/g' ; echo abcdefgh | sed -e 's/h/8/g'\" )" )
checkExec( "e011", "res = q_exec( \"echo abcdefgh | sed -e 's/d/4/g' && echo abcdefgh | sed -e 's/h/8/g'\" )" )

checkExec( "e012", "res = q_exec( \"echo a&&echo b;echo c\" )" )
checkExec( "e013", "res = q_exec( \"false||echo b;echo c\" )" )
checkExec( "e014", "res = q_exec( \"false||false;echo c\" )" )
% checkExec( "e015", "res = q_exec( \"false|&false;echo c\" )" )
checkExec( "e016", "res = q_exec( \"echo 1 && echo 2 && echo 10 && echo 20\" )" )
checkExec( "e017", "res = q_exec( \"echo 1 x 2 x 10 x 20\" )" )
checkExec( "e018", "res = q_exec( \"echo 1>x && echo 2>>x\" )" )
checkExec( "e019", "res = q_exec( \"echo 11 22 33 1>x\" ) x = fs_contents(\"x\") if not equal(x, \"11 22 33\n\") res = 99 end" )
checkExec( "e020", "res = q_exec( \"echo 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16>x\" ) x = fs_contents(\"x\") if not equal(x, \"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16\n\") res = 99 end" )


%-----------------------------------------------------------------------------
section( "directory stack tests" )

t = "pushd( \"..\" ) base = getwd() popd() res = tcontains( base, \"test\" )"
check( "d001", t, "TRUE" )
write( "base = ", base )

base = base & SL
src = ".." & SL & "src"
t = "pushd( src ) wd = getwd() res = subst( wd, base, \"\", 1 )"
check( "d002", t, "src" )

t = "cd( base ) wd = getwd() res = subst( base, wd, \"\", 1 )"
check( "d003", t, SL )

t = "popd() wd = getwd() res = subst( wd, base, \"\", 1 )"
check( "d004", t, BUILD_DIR )

%-----------------------------------------------------------------------------
section( "miscellanoues tests" )

write( "hostname  = ", hostname(), CR )
write( "date      = ", date(), CR )
write( "datetime  = ", datetime(), CR )
write( "datestamp = ", datestamp(), CR )

write( CR, "tests done", CR )

summary()
