# Copyright (C) 1992, 1997 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  

# Please email any bugs, comments, and/or additions to this file to:
# bug-gdb@prep.ai.mit.edu

# This file was written by Fred Fish. (fnf@cygnus.com)

if $tracelevel then {
	strace $tracelevel
}

if [skip_chill_tests] then { continue }

set testfile "chillvars"
set srcfile ${srcdir}/$subdir/${testfile}.ch
set binfile ${objdir}/${subdir}/${testfile}.exe
if  { [compile "${srcfile} -g -o ${binfile} ${CHILL_RT0} ${CHILL_LIB}"] != "" } {
    perror "Couldn't compile ${srcfile}"
    return -1
}

proc do_tests {} {
    global prms_id bug_id subdir objdir srcdir binfile gdb_prompt

    set prms_id 0
    set bug_id 0

    # Start with a fresh gdb.

    gdb_exit
    gdb_start
    gdb_reinitialize_dir $srcdir/$subdir
    gdb_load $binfile

    gdb_test "set language chill ""

    # This is needed (at least on SunOS4) to make sure the
    # the symbol table is read.
    gdb_test "break chillvars.ch:3 ""
    gdb_test "delete 1 ""

    gdb_test "set width 0 ""
    gdb_test "set print sevenbit-strings ""
    gdb_test "set print address off ""

    test_BOOL
    test_CHAR
    test_BYTE
    test_UBYTE
    test_INT
    test_UINT
    test_LONG
    test_ULONG
    test_REAL
    test_LONG_REAL
    test_POWERSET
    test_arrays
    test_strings
    test_structs

    test_ptr
}

proc test_BOOL {} {

    gdb_test "ptype bool_true"		"type = (BOOL|bool)"
    gdb_test "ptype bool_false"		"type = (BOOL|bool)"
    gdb_test "whatis bool_true"		"type = (BOOL|bool)"
    gdb_test "whatis bool_false"	"type = (BOOL|bool)"
    gdb_test "print bool_false"		" = FALSE"
    gdb_test "print bool_true"		" = TRUE"

}

proc test_CHAR {} {
    gdb_test "ptype control_char"	"type = (CHAR|char)"
    gdb_test "whatis control_char"	"type = (CHAR|char)"
    gdb_test "print control_char"	{ = '\^[(]7[)]'}
    gdb_test "ptype printable_char"	"type = (CHAR|char)"
    gdb_test "whatis printable_char"	"type = (CHAR|char)"
    gdb_test "print printable_char"	" = 'a'"

    gdb_test "print lower(char)"	{ = '\^[(]0[)]'}
    gdb_test "print upper(char)"	{ = '\^[(]255[)]'}
}

proc test_BYTE {} {
    gdb_test "ptype byte_low"		"type = (BYTE|byte)"
    gdb_test "whatis byte_low"		"type = (BYTE|byte)"
    gdb_test "print byte_low"		" = -128"
    gdb_test "ptype byte_high"		"type = (BYTE|byte)"
    gdb_test "whatis byte_high"		"type = (BYTE|byte)"
    gdb_test "print byte_high"		" = 127"

    gdb_test "print lower(byte)"	" = -128"
    gdb_test "print upper(byte)"	" = 127"
    gdb_test "print lower(byte_high)"	" = -128"
    gdb_test "print upper(byte_high)"	" = 127"
}

proc test_UBYTE {} {
    gdb_test "ptype ubyte_low"		"type = (UBYTE|ubyte)"
    gdb_test "whatis ubyte_low"		"type = (UBYTE|ubyte)"
    gdb_test "print ubyte_low"		" = 0"
    gdb_test "ptype ubyte_high"		"type = (UBYTE|ubyte)"
    gdb_test "whatis ubyte_high"	"type = (UBYTE|ubyte)"
    gdb_test "print ubyte_high"		" = 255"
}

proc test_INT {} {
    gdb_test "ptype int_low"		"type = (INT|int)"
    gdb_test "whatis int_low"		"type = (INT|int)"
    gdb_test "print int_low"		" = -32768"
    gdb_test "ptype int_high"		"type = (INT|int)"
    gdb_test "whatis int_high"		"type = (INT|int)"
    gdb_test "print int_high"		" = 32767"
}

proc test_UINT {} {
    gdb_test "ptype uint_low"		"type = (UINT|uint)"
    gdb_test "whatis uint_low"		"type = (UINT|uint)"
    gdb_test "print uint_low"		" = 0"
    gdb_test "ptype uint_high"		"type = (UINT|uint)"
    gdb_test "whatis uint_high"		"type = (UINT|uint)"
    gdb_test "print uint_high"		" = 65535"
}

proc test_LONG {} {
    gdb_test "ptype long_low"		"type = (LONG|long)"
    gdb_test "whatis long_low"		"type = (LONG|long)"
    gdb_test "print long_low"		" = -2147483648"
    gdb_test "ptype long_high"		"type = (LONG|long)"
    gdb_test "whatis long_high"		"type = (LONG|long)"
    gdb_test "print long_high"		" = 2147483647"
}

proc test_ULONG {} {
    gdb_test "ptype ulong_low"		"type = (ULONG|ulong)"
    gdb_test "whatis ulong_low"		"type = (ULONG|ulong)"
    gdb_test "print ulong_low"		" = 0"
    gdb_test "ptype ulong_high"		"type = (ULONG|ulong)"
    gdb_test "whatis ulong_high"	"type = (ULONG|ulong)"
    gdb_test "print ulong_high"		" = 4294967295"
}

proc test_REAL {} {
    gdb_test "ptype real1"		"type = (FLOAT|float)"
    gdb_test "whatis real1"		"type = (FLOAT|float)"
    gdb_test "print real1"		" = 3.14159274"
}

proc test_LONG_REAL {} {
    gdb_test "ptype long_real1"		"type = (DOUBLE|double)"
    gdb_test "whatis long_real1"	"type = (DOUBLE|double)"
    gdb_test "print long_real1"		" = 3\\.1400000000000001e\\+300"
}

proc test_POWERSET {} {
}

proc test_arrays {} {
    gdb_test "ptype booltable1"		"type = ARRAY \\(+0:3\\)+ (BOOL|bool)"
    gdb_test_exact "print booltable1"	\
	{ = [(0): TRUE, (1:2): FALSE, (3): TRUE]}

    gdb_test "ptype booltable2"		"type = ARRAY \\(+4:7\\)+ (BOOL|bool)"
    gdb_test_exact "print booltable2" { = [(4): TRUE, (5:6): FALSE, (7): TRUE]}

    gdb_test "ptype chartable1"		"type = ARRAY \\(+0:2\\)+ (CHAR|char)"
    gdb_test_exact "print chartable1"  {= [(0): '^(0)', (1): '^(1)', (2): '^(2)']}

    gdb_test "ptype chartable2"		"type = ARRAY \\(+3:5\\)+ (CHAR|char)"
    gdb_test_exact "print chartable2"	\
	{= [(3): '^(0)', (4): '^(1)', (5): '^(2)']}

    gdb_test "ptype bytetable1"		"type = ARRAY \\(+0:4\\)+ (BYTE|byte)"
    gdb_test_exact "print bytetable1"		\
       {= [(0): -2, (1): -1, (2): 0, (3): 1, (4): 2]}

    gdb_test "ptype bytetable2"		"type = ARRAY \\(+5:9\\)+ (BYTE|byte)"
    gdb_test_exact "print bytetable2"	\
	{= [(5): -2, (6): -1, (7): 0, (8): 1, (9): 2]}

    gdb_test "ptype bytetable3" \
	"type = ARRAY \\(1:2\\) ARRAY \\('c':'d'\\) ARRAY \\(FALSE:TRUE\\) (BYTE|byte)"
    gdb_test_exact "print bytetable3" \
    {= [(1): [('c'): [(FALSE): 0, (TRUE): 1], ('d'): [(FALSE): 2, (TRUE): 3]], (2): [('c'): [(FALSE): 4, (TRUE): 5], ('d'): [(FALSE): 6, (TRUE): 7]]]}
    gdb_test "ptype bytetable4" \
	"type = ARRAY \\(1:2\\) ARRAY \\('c':'d'\\) ARRAY \\(FALSE:TRUE\\) (BYTE|byte)"
    gdb_test_exact "print bytetable4" \
    {= [(1): [('c'): [(FALSE): 0, (TRUE): 1], ('d'): [(FALSE): 2, (TRUE): 3]], (2): [('c'): [(FALSE): 4, (TRUE): 5], ('d'): [(FALSE): 6, (TRUE): 7]]]}

    gdb_test "ptype ubytetable1"	"type = ARRAY \\(+0:4\\)+ (UBYTE|ubyte)"
    gdb_test_exact "print ubytetable1"	\
	{= [(0): 0, (1): 1, (2): 2, (3): 3, (4): 4]}

    gdb_test "ptype ubytetable2"	"type = ARRAY \\(+5:9\\)+ (UBYTE|ubyte)"
    gdb_test_exact "print ubytetable2"	\
	{= [(5): 0, (6): 1, (7): 2, (8): 3, (9): 4]}

    gdb_test "ptype inttable1"		"type = ARRAY \\(+0:4\\)+ (INT|int)"
    gdb_test_exact "print inttable1"	\
	{= [(0): -2, (1): -1, (2): 0, (3): 1, (4): 2]}

    gdb_test "ptype inttable2"		"type = ARRAY \\(+5:9\\)+ (INT|int)"
    gdb_test_exact "print inttable2"		\
	{= [(5): -2, (6): -1, (7): 0, (8): 1, (9): 2]}

    gdb_test "ptype uinttable1"		"type = ARRAY \\(+0:4\\)+ (UINT|uint)"
    gdb_test_exact "print uinttable1"	\
	{= [(0): 0, (1): 1, (2): 2, (3): 3, (4): 4]}

    gdb_test "ptype uinttable2"		"type = ARRAY \\(+5:9\\)+ (UINT|uint)"
    gdb_test_exact "print uinttable2"	\
	{= [(5): 0, (6): 1, (7): 2, (8): 3, (9): 4]}

    gdb_test "ptype longtable1"		"type = ARRAY \\(+0:4\\)+ (LONG|long)"
    gdb_test_exact "print longtable1"	\
	{= [(0): -2, (1): -1, (2): 0, (3): 1, (4): 2]}

    gdb_test "ptype longtable2"		"type = ARRAY \\(+5:9\\)+ (LONG|long)"
    gdb_test_exact "print longtable2"	\
	{= [(5): -2, (6): -1, (7): 0, (8): 1, (9): 2]}

    gdb_test "ptype ulongtable1"	"type = ARRAY \\(+0:4\\)+ (ULONG|ulong)"
    gdb_test_exact "print ulongtable1"	\
	{= [(0): 0, (1): 1, (2): 2, (3): 3, (4): 4]}

    gdb_test "ptype ulongtable2"	"type = ARRAY \\(+5:9\\)+ (ULONG|ulong)"
    gdb_test_exact "print ulongtable2"	\
	{= [(5): 0, (6): 1, (7): 2, (8): 3, (9): 4]}

    gdb_test "ptype realtable1"		"type = ARRAY \\(+0:4\\)+ (FLOAT|float)"
    gdb_test_exact "print realtable1"	\
	{= [(0): -2, (1): -1, (2): 0, (3): 1, (4): 2]}

    gdb_test "ptype realtable2"		"type = ARRAY \\(+5:9\\)+ (FLOAT|float)"
    gdb_test_exact "print realtable2"	\
	{= [(5): -2, (6): -1, (7): 0, (8): 1, (9): 2]}

    gdb_test "ptype longrealtable1"	"type = ARRAY \\(+0:4\\)+ (DOUBLE|double)"
    gdb_test_exact "print longrealtable1" \
	{= [(0): -2, (1): -1, (2): 0, (3): 1, (4): 2]}

    gdb_test "ptype longrealtable2"	"type = ARRAY \\(+5:9\\)+ (DOUBLE|double)"
    gdb_test_exact "print longrealtable2"	\
	{= [(5): -2, (6): -1, (7): 0, (8): 1, (9): 2]}

    gdb_test "print length(longrealtable2)" {= 5}
    gdb_test "print lower(longrealtable2)" {= 5}
    gdb_test "print upper(longrealtable2)" {= 9}
}

proc test_strings {} {

    gdb_test "ptype string1"		"type = CHARS \[(\]4\[)\]+"
    gdb_test "print string1"		" = \"abcd\""

    gdb_test "ptype string2"		"type = CHARS \[(\]+5\[)\]+"
    gdb_test "print string2"		{ = \"ef\^\(0\)gh\"}

    gdb_test "ptype string3"		"type = CHARS \[(\]+6\[)\]+"
    gdb_test "print string3"		" = \"efghij\""

    gdb_test "ptype string4"		"type = CHARS \[(\]+7\[)\]+"
    gdb_test "print string4"		{ = \"zzzzzz\^\(0\)\"}

    # These tests require a running process, so run to one of the procs
    # and then do the tests.

    if [runto scalar_arithmetic] then {
	gdb_test "ptype string1//string2"	"type = CHARS \\(9\\)"
	gdb_test "print string1//string2"    { = \"abcdef\^\(0\)gh\"}
	gdb_test_exact {ptype "a chill string"} {type = CHARS (14)}
	gdb_test "print 'a chill string'"	" = \"a chill string\""
	gdb_test "print \"ef\"//c'00'//\"gh\""	{ = \"ef\^\(0\)gh\"}
	gdb_test "print string1 // \"efgh\""	" = \"abcdefgh\""
	gdb_test "print (6) 'z'"		" = \"zzzzzz\""
	gdb_test "ptype (6) 'z'"		"type = CHARS \[(\]+6\[)\]+"
	gdb_test "print (1+2*3) 'x'"		" = \"xxxxxxx\""
	gdb_test "ptype (1+2*3) 'x'"		"type = CHARS \[(\]+7\[)\]+"
    }

}

proc test_structs {} {
    gdb_test "ptype struct1" \
	"type = STRUCT \\(+.*abool (BOOL|bool),.*aint (INT|int),.*astring CHARS \\(+8\\)+.*\\)+"
    gdb_test "print struct1" \
	".* = \\\[\.abool: TRUE, \.aint: 123, \.astring: \"a string\"\\\]"
    gdb_test "ptype struct2" \
	"type = STRUCT \\(+.*abool (BOOL|bool),.*nstruct simple_struct,.*aint (INT|int).*\\)+"
    gdb_test "print struct2" \
	".* = \\\[.abool: TRUE, \.nstruct: \\\[\.abool: FALSE, \.aint: 456, \.astring: \"deadbeef\"\\\], \.aint: 789\\\]"
}

proc test_ptr {} {
    # This is to test Cygnus PR 6932
    gdb_test "print xptr->int" ".* = 32767"
}

do_tests
