
// load a.m32 first

defstate testkeyavail
    for i range 27
    {
        ifholdkey alphakeys[i]
        {
            qsprintf TQUOTE "ALPHA KEY: %d (SCANCODE: %d)" i alphakeys[i]
            quote TQUOTE
        }
    }

    for i range 10
    {
        ifholdkey numberkeys[i]
        {
            qsprintf TQUOTE "NUMBER KEY: %d (SCANCODE: %d)" i numberkeys[i]
            quote TQUOTE
        }
    }
ends


// various tests of m32-script features

defstate arraytest
    "Array test"
    getarraysize ar tmp
    resizearray ar 65536
    getticks parm[2]
    for j range 65536
        set ar[j] j
    set i 0
    for j range 65536
        add i ar[j]
    getticks parm[3]
    resizearray ar tmp
    ife i 2147450880 quote "OK" else quote "DAMN"
    sub parm[3] parm[2]
    qsprintf TQUOTE "time: %d ms" parm[3]
    quote TQUOTE
ends

defstate itertest
    "Iteration test"
    var gi gj gk
    // iteration and break test

    for i range 10
    {
        addlogvar i
        ife i 5 break
    }
    ife i 5 quote "OK" else quote "DAMN"

    quote "FLOAT ACCESS TEST"
    set tmp pr_parallaxscale

    set gi tmp set gj tmp set gk tmp
    al gi al gj al gk
    ftoi gi 20 ftoi gj 200 ftoi gk 2000
    al gi al gj al gk
    mul gk 2
    itof gk 2000
ends

define TEST_ZERO 0
define TEST_PLUS_ONE 1
define TEST_MINUS_ONE -1

define MOST_POSITIVE_DIRECT 32767
define MOST_NEGATIVE_DIRECT -32768
define LEAST_POSITIVE_INDIRECT 32768
define LEAST_NEGATIVE_INDIRECT -32769

define HEX_MOST_POSITIVE_DIRECT 0x7fff
define HEX_MOST_NEGATIVE_DIRECT 0xffff8000
define HEX_LEAST_POSITIVE_INDIRECT 0x8000
define HEX_LEAST_NEGATIVE_INDIRECT 0xffff7fff

define MAX_CONSTANT 2147483647
define MIN_CONSTANT -2147483648

// tests various combinations of constants and labels
defstate consttest
    "Constants test"
    quote " --- Constants test ---", quote " "

    quote "Should be 0:"
    set i 0, set j TEST_ZERO
    qsprintf TQUOTE "%d %d  %d %d" 0 TEST_ZERO  i j
    quote TQUOTE, quote " "

    quote "Should be 1:"
    set i 1, set j TEST_PLUS_ONE
    qsprintf TQUOTE "%d %d  %d %d" 1 TEST_PLUS_ONE  i j
    quote TQUOTE, quote " "

    quote "Should be -1:"
    set i -1, set j TEST_MINUS_ONE
    qsprintf TQUOTE "%d %d  %d %d" -1 TEST_MINUS_ONE  i j
    quote TQUOTE, quote " "


    quote "Should be 32767:"
    set i 32767, set j MOST_POSITIVE_DIRECT
    qsprintf TQUOTE "%d %d  %d %d" 32767 MOST_POSITIVE_DIRECT  i j
    quote TQUOTE, quote " "

    quote "Should be -32768:"
    set i -32768, set j MOST_NEGATIVE_DIRECT
    qsprintf TQUOTE "%d %d  %d %d" -32768 MOST_NEGATIVE_DIRECT  i j
    quote TQUOTE, quote " "

    quote "Should be 32768:"
    set i 32768, set j LEAST_POSITIVE_INDIRECT
    qsprintf TQUOTE "%d %d  %d %d" 32768 LEAST_POSITIVE_INDIRECT  i j
    quote TQUOTE, quote " "

    quote "Should be -32769:"
    set i -32769, set j LEAST_NEGATIVE_INDIRECT
    qsprintf TQUOTE "%d %d  %d %d" -32769 LEAST_NEGATIVE_INDIRECT  i j
    quote TQUOTE, quote " "

    quote "Hex tests:"
    quote "Should be 32767:"
    set i 0x7fff, set j HEX_MOST_POSITIVE_DIRECT
    qsprintf TQUOTE "%d %d  %d %d" 0x7fff HEX_MOST_POSITIVE_DIRECT  i j
    quote TQUOTE, quote " "

    quote "Should be -32768:"
    set i 0xffff8000, set j HEX_MOST_NEGATIVE_DIRECT
    qsprintf TQUOTE "%d %d  %d %d" 0xffff8000 HEX_MOST_NEGATIVE_DIRECT  i j
    quote TQUOTE, quote " "

    quote "Should be 32768:"
    set i 0x8000, set j HEX_LEAST_POSITIVE_INDIRECT
    qsprintf TQUOTE "%d %d  %d %d" 0x8000 HEX_LEAST_POSITIVE_INDIRECT  i j
    quote TQUOTE, quote " "

    quote "Should be -32769:"
    set i 0xffff7fff, set j HEX_LEAST_NEGATIVE_INDIRECT
    qsprintf TQUOTE "%d %d  %d %d" 0xffff7fff HEX_LEAST_NEGATIVE_INDIRECT  i j
    quote TQUOTE, quote " "

    quote "min/max tests:"
    quote "Should be 2147483647:"
    set i 2147483647, set j MAX_CONSTANT
    qsprintf TQUOTE "%d %d  %d %d" 2147483647 MAX_CONSTANT  i j
    quote TQUOTE, quote " "

    quote "Should be -2147483648:"
    set i -2147483648, set j MIN_CONSTANT
    qsprintf TQUOTE "%d %d  %d %d" -2147483648 MIN_CONSTANT  i j
    quote TQUOTE, quote " "
ends

defstate anmtest
    "Sprite yoffsets"
    var yo

    for i range MAXTILES
    {
        set j tilesizx[i], or j tilesizy[i]

        ifn j 0
        {
            set yo picanm[i]
            shiftl yo 8, shiftr yo 24  // sign-extend

            ifl yo 0, inv yo

            ifge yo tilesizy[i]
            {
                qsprintf TQUOTE "Tile %d's y offset >= y size" i
                quote TQUOTE
            }
        }
    }
ends

// switch/break/default regression test, inspired by
// http://forums.duke4.net/topic/1348-mapster32-problems-and-bugs/page__view__findpost__p__101510
defstate switchtest
    "'switch' test"
    set i 3622
    whilevarn i 3779
    {
        // test for many things at once:

        // sorting of key values
        switch i
        case 3625 set j 666  /* fall-through */
        default set j -1 break
        case 3622 set j 3919 break
        case 3626 set j -100
        case 3623 set j 3685 break
        case 3627 set j 1234 break
        endswitch

        // 'break' should always lead us here
        ifle i 3627
        {
            qsprintf TQUOTE "j=%d" j
            quote TQUOTE
        }

        add i 1
    }

    quote "----"

    // same thing, slightly different syntax
    set i 3622
    whilevarn i 3779
    {
        switch i
        {
            case 3625 set j 666
            default set j -1 break
            case 3622 set j 3919 break
            case 3626 set j -100
            case 3623 set j 3685 break
            case 3627 set j 1234 break
        }
        endswitch

        ifle i 3627
        {
            qsprintf TQUOTE "j=%d" j
            quote TQUOTE
        }

        add i 1
    }

    // correct output:
    // j=3919
    // j=3685
    // j=-1
    // j=-1
    // j=3685
    // j=1234
ends
