# This html document was prepared by gri2html based on the Gri script named
# tst_var_syn.gri
# Gri is available for free at http://gri.sourceforge.net

    show "doc/tst_suite/tst_var_syn.gri ..." ...
    rpnfunction same - abs 1e-10 >          # Are numbers virtually same?

    # New and delete (variables)
     .v. = 1
    new .v.
    .v. = 2
    assert {rpn .v. 2 same}                 " failed test 1.1"
    assert {rpn ".v." defined}              " failed test 1.2"
    delete .v.
    assert {rpn .v. 1 same}                 " failed test 1.3"
    assert {rpn ".v." defined}              " failed test 1.4"
    delete .v.
    assert {rpn ".v." defined !}            " failed test 1.4"

    # New and delete (synonyms)
    \v = "hi"
    new \v
    \v = "hello"
    assert {rpn "\v" "hello" ==}            " failed test 2.1"
    assert {rpn "\\v" defined}              " failed test 2.2"
    delete \v
    assert {rpn "\v" "hi" ==}               " failed test 2.3"
    assert {rpn "\\v" defined}              " failed test 2.4"
    delete \v
    assert {rpn "\\v" defined !}            " failed test 2.5"

    # Multi-word synonyms
    \h = "Hi there buddy"
    assert {rpn  \[]h    3      ==}         " failed test 3.1"
    .i. = \[]h
    assert {rpn  .i.     3      ==}         " failed test 3.2"
    assert {rpn "\[0]h" "Hi"    ==}         " failed test 3.3"
    assert {rpn "\[1]h" "there" ==}         " failed test 3.4"
    assert {rpn "\[2]h" "buddy" ==}         " failed test 3.5"
    \h = "Hi means \"hello\""
    assert {rpn "\[0]h" "Hi"        ==}     " failed test 3.6"
    assert {rpn "\[1]h" "means"     ==}     " failed test 3.7"
    assert {rpn "\[2]h" "\"hello\"" ==}     " failed test 3.8"
    .i. = 1
    assert {rpn "\[.i.]h" "means"   ==}     " failed test 3.9"
    \i = "1"
    assert {rpn "\[\i]h"  "means"   ==}     " failed test 3.10"

    # Setting by quoted name
    set ".var." to 10
    assert {rpn .var. 10 ==}                " failed test 4.1"
    set "\\syn" to "hi"
    assert {rpn "\syn" "hi" ==}             " failed test 4.2"

    # Setting by quoted name, in a new command
    `hi pi "\\Greeting" ".Pi."'
    {
        set "\.word2." to "hi"
        set "\.word3." to 3.14
    }
    hi pi "\\g" ".p."
    assert {rpn "\g" "hi" ==}                       " failed test 5.1"
    assert {rpn .p.  3.14 ==}                       " failed test 5.2"

    # Execution of synonyms
    \ret = "\string = \"Hello\""
    \ret
    assert {rpn "\string" "Hello" ==}               " failed test 6.1"

    # Parsing of individual words in synonyms (tests SourceForge bug 114983)
    `Newcommand "\pat"''
    {
        \t = "A .2"
        assert {rpn "\[0].word1." "\[0]t" ==}       " failed test 7.1"
    }
    Newcommand "A .2"

    # Test aliased synonyms

    # Part 1. check to see if can delete without either a
    # single or a double backslash, and either with
    # enclosing double-quotes, or not.
    \a = "HI"
    assert {rpn "\\a"    defined   }                " failed test 8.1"
    delete \a
    assert {rpn "\\a"    defined ! }                " failed test 8.2"
    \a = "HI"
    delete \a
    assert {rpn "\\a"    defined ! }                " failed test 8.3"

    new \a
    assert {rpn "\\a"     defined   }               " failed test 9.1"
    \a = "greeting"
    assert {rpn "\\a"     defined   }               " failed test 9.2"
    delete \a
    assert {rpn "\\a"     defined ! }               " failed test 9.3"

    # Part 5. sprintf
    sprintf \a "%.1f" 10
    assert {rpn "\a" "10.0" == }                    " failed test 10.1"
    \alias_for_a = "\\a"
    sprintf \@alias_for_a "%.1f" 20
    assert {rpn "\a" "20.0" == }                    " failed test 10.2"

    # Part 6. Environment variables (SF bug 117415)
    \alias_for_a = "\\a"
    get env \@alias_for_a "SHELL"
    get env \b "SHELL"
    assert {rpn "\a" "\b" ==}                       " failed test 11.1"

    # Part 7 'read' (SF bug 117412)
    \TMPFILE = tmpname
    system echo "Hello" > \TMPFILE
    open "\TMPFILE"
    read \a
    assert {rpn "\a" "Hello" ==}                    " failed test 12.1"
    \a = "JUNK"
    \alias_for_a = "\\a"
    rewind
    read \\a
    assert {rpn "\a" "Hello" ==}                    " failed test 12.2"
    \a = "JUNK"
    \alias_for_a = "\\a"
    rewind
    read \@alias_for_a
    close
    assert {rpn "\a" "Hello" ==}                    " failed test 12.3"
    system rm -f \TMPFILE

    # Part 8 -- ensure not trying to evaluate inside 'false' if parts.
    \str = "Hello"
    \t = {rpn "\s" "tr" strcat}	# construct a name
    if {rpn "\\t" defined}		# test if defined
        .val. = 1
    end if
    if {rpn "\\x" defined}		# this one is not defined
        .val. = 2
    end if
    assert {rpn .val. 1 ==}                         " failed test 12.4"

    #
    \a1 = "1"
    \b1 = "2"
    \a2 = "10"
    \b2 = "20"
    read columns x y
    \a1 \b1
    \a2 \b2

    assert {rpn x 0 @  1 ==}                        " failed test 13.1"
    assert {rpn x 1 @ 10 ==}                        " failed test 13.2"
    assert {rpn y 0 @  2 ==}                        " failed test 13.3"
    assert {rpn y 1 @ 20 ==}                        " failed test 13.4"

    `NC ...'
    {
        read columns x y
        \.word1 \.word2.

    }
    NC 1 10
    assert {rpn x 0 @  1 ==}                        " failed test 14.1"
    assert {rpn y 0 @ 10 ==}                        " failed test 14.2"

    \a = "1  2"
    \b = "10 20"
    \index = "0"
    read columns x y
    \[\index]a \[1]b

    assert {rpn x 0 @  1 ==}                        " failed test 15.1"
    assert {rpn y 0 @ 20 ==}                        " failed test 15.2"

    `NC2 ...'
    {
        read columns x y
        \[0].word1. \[0].word2.
        \[1].word1. \[1].word2.

    }
    NC2 "1 2" "100 200"
    assert {rpn x 0 @   1 ==}                        " failed test 16.1"
    assert {rpn x 1 @   2 ==}                        " failed test 16.2"
    assert {rpn y 0 @ 100 ==}                        " failed test 16.3"
    assert {rpn y 1 @ 200 ==}                        " failed test 16.4"

    # Part 9. New commands with variables

    # Rvalue: synonyms/variables
    `ampersand_rvalue &.v. &\s'
    {
        new \syn
        \syn = "Shadow"
        new .a.
        .a. = 0
        assert {rpn \.word1. 1 ==}                   " failed test 17.1"
        assert {rpn "\.word2." "Brightness" ==}      " failed test 17.2"
        delete \syn
        delete .a.
    }
    .a. = 1
    \syn = "Brightness"
    ampersand_rvalue &.a. &\syn

    # Rvalue + Lvalue: synonym
    `append &\s'
    {
        \.word1. = "\.word1. and bye"
    }
    \a = "hi"
    append &\a
    assert {rpn "\a" "hi and bye" ==}                "failed test 18.3"

    # Rvalue + Lvalue: variable in the = form of assignment
    `double &.var.'
    {
        \.word1. = {rpn \.word1. 2 *}
    }
    .a. = 10
    double &.a.
    assert {rpn .a. 20 ==}                           "failed test 18.4"

    `Double &.var.'
    {
        \.word1. *= 2
    }
    .a. = 10
    Double &.a.
    assert {rpn .a. 20 ==}                          " failed test 18.5"

    `halve &.var.'
    {
        \.word1. = {rpn \.word1. 2 /}
    }
    halve &.a.
    assert {rpn .a. 10 ==}                          " failed test 18.6"

    `NC3 &\friendly'
    {
        \.word1. = "Howdy"
    }
    \greeting = "Hi"
    NC3 &\greeting
    assert {rpn "\greeting" "Howdy" ==}             " failed test 18.7"

    `postpend_dat &\filename'
    {
        \.word1. = {rpn "\.word1." ".dat" strcat}
    }
    \filename = "test"
    postpend_dat &\filename
    assert {rpn "test.dat" "\filename" ==}           " failed test 18.8"

    # new
    `test_new &\s'
    {
        new \.word1.
        new \a
        \a = "This will dissapear"
        \.word1. = "Think locally"
        delete \a
    }
    \a = "Act globally"
    test_new &\a
    assert {rpn "\a" "Think locally" ==}             " failed test 18.9"
    delete \a
    assert {rpn "\a" "Act globally" ==}              " failed test 18.10"

    `test_new_delete &\s'
    {
        new \a
        \a = "Insurrection"
        assert {rpn "\.word1." "Act globally" ==}    " failed test 18.11"
        delete \.word1.
        assert {rpn "\.word1." "Or else" ==}         " failed test 18.12"
        delete \a
    }
    \a = "Or else"
    new \a
    \a = "Act globally"
    test_new_delete &\a

    # Nexting
    `nest2 &.v. &\s'
    {
        assert {rpn "\.word1." "HI" ==}              " failed test 18.13"
        assert {rpn \.word2. 0.5 ==}                 " failed test 18.14"
    }
    `nest1 &.v. &\s'
    {
        nest2 &\.word2. &\.word1.	# reversed
    }
    .a. = 0.5
    \s = "HI"
    nest1 &.a. &\s
    # defined
    `check_defined &.v.'
    {
        assert {rpn "\\.word1." defined}             " failed test 18.15"
        delete .a.
        assert {rpn "\\.word1." defined !}           " failed test 18.16"
    }
    .a. = 100
    check_defined &.a.

    # System
    \t = system perl <<"EOF"
    {print "Hi"};
    EOF
    assert {rpn "\t" "Hi" ==}                       " failed test 19.1"
    \t = system perl <<"EOF"
    {print "Hello"};
     EOF
    assert {rpn "\t" "Hello" ==}                    " failed test 19.2"
    `NC4'
    {
        \t = system perl <<"EOF"
        {print "Howdie"};
    EOF
    }
    NC4
    assert {rpn "\t" "Howdie" ==}                   " failed test 19.3"
    `NC5'
    {
        \t = system perl <<"EOF"
            {print "G'day"};
        EOF
    }
    NC5
    assert {rpn "\t" "G'day" ==}                    " failed test 19.4"

    ### BLOCK 20 ###
    # Alias as lvalue (e.g. \@ptr = ...)
    # (1) lvalue synonyms
    \greeting = "You say goodbye"
    \alias = "\\greeting"
    \@alias = "And I say hello"
    assert {rpn "\greeting" "And I say hello" ==}   " failed test 20.1"
    # (2) lvalue variables
    .b. = 1
    \bptr = ".b."
    \@bptr *= 2
    assert {rpn .b. 2 ==}                           " failed test 20.2"
    \@bptr = 10
    assert {rpn .b. 10 ==}                          " failed test 20.3"

    # Alias as rvalue (e.g. show "\@ptr")
    # (1) rvalue synonyms
    \a = "HI"
    \aptr = "\\a"
    assert {rpn "\@aptr" "HI" ==}                   " failed test 20.4"
    \@aptr = "BYE"
    assert {rpn "\@aptr" "BYE" ==}                  " failed test 20.5"
    \aa = "\@aptr"
    assert {rpn "\@aptr" "BYE" ==}                  " failed test 20.6"
    # (2) rvalue variables
    .a. = 1
    \aptr = ".a."
    assert {rpn \@aptr 1 ==}                        " failed test 20.7"
    .aa. = {rpn .a. 1 +}
    assert {rpn .aa. 2 ==}                          " failed test 20.8"

    # Missing values
    set missing value 0
    .x. = 0
    assert {rpn .x. ismissing}                      " failed test 21.1"
    assert {rpn .x. 1 + ismissing}                  " failed test 21.2"
    set missing value 1e33

    show " passed"