# Copyright (C) 1992, 1995, 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., 675 Mass Ave, Cambridge, MA 02139, USA.  

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

if $tracelevel then {
	strace $tracelevel
}

if [skip_chill_tests] then { continue }

set prms_id 0
set bug_id 0

# Set the current language to chill.  This counts as a test.  If it
# fails, then we skip the other tests.

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

global infinity
if [istarget "i*86-*-sysv4*"] then {
    set infinity "inf"
} else {
    set infinity "Infinity"
}

proc set_lang_chill {} {
    global gdb_prompt
    global binfile objdir subdir

    if ![file exists $objdir/$subdir/$binfile] then {
	return 0
    }
    verbose "loading file '$objdir/$subdir/$binfile'"
    gdb_load $objdir/$subdir/$binfile

    send_gdb "set language chill\n"
    gdb_expect {
	-re ".*$gdb_prompt $" {}
	timeout { fail "set language chill (timeout)" ; return 0 }
    }

    send_gdb "show language\n"
    gdb_expect {
	-re ".* source language is \"chill\".*$gdb_prompt $" {
	    pass "set language to \"chill\""
	    send_gdb "break dummyfunc\n"
	    gdb_expect {
		-re ".*$gdb_prompt $" {
		    send_gdb "run\n"
		    gdb_expect -re ".*$gdb_prompt $" {}
		    return 1
		}
		timeout {
		    fail "can't set breakpoint (timeout)"
		    return 0
		}
	    }
	}
	-re ".*$gdb_prompt $" {
	    fail "setting language to \"chill\""
	    return 0
	}
	timeout {
	    fail "can't show language (timeout)"
	    return 0
	}
    }
}

# checks if structure was accessed correctly
proc test_write { args } {
    global gdb_prompt

    if [llength $args]==5 then {
	set message [lindex $args 4]
	set extended [lindex $args 3]
	set matchval [lindex $args 2]
    } elseif [llength $args]==4 then {
	set message [lindex $args 3]
	set matchval [lindex $args 2]
	set extended ""
    } elseif [llength $args]==3 then {
	set message [lindex $args 2]
	set extended ""
    } else {
	warning "test ($args) write called with wrong number of arguments"
	return 
    }

    set location [lindex $args 0]
    set value [lindex $args 1]
    if ![info exists matchval] then {
	set matchval $value
    }
    verbose "loc: $location, val: $value, msg: $message, ext: $extended, match: $matchval"

    verbose "setting var $value..."
    send_gdb "set var $location.m$extended := $value\n"
    gdb_expect -re ".*$gdb_prompt $" {}
    gdb_test "print $location" \
      ".*= \[\[\]\\.p1: 2863311530, \\.m: $matchval, \\.p2: 1431655765\[\]\]"\
      "$message"
}

# test write access from gdb (setvar x:=y) from gdb
proc write_access { } {
    global infinity

    verbose "testing write access to locations"
    
    # discrete modes
    test_write b1 127 "byte write 1"
    test_write b1 -128 "byte write 2"
    test_write b1 0 "byte write 3"
    test_write ub1 255 "ubyte write 1"
    test_write ub1 0 "ubyte write 2"
    test_write ub1 42 "ubyte write 3"
    test_write i1 32767 "int write 1"
    test_write i1 -32768 "int write 2"
    test_write i1 0 "int write 3"
    test_write ui1 65535 "uint write 1"
    test_write ui1 0 "uint write 2"
    test_write ui1 123 "uint write 3"
    test_write l1 2147483647 "long write 1"
    test_write l1 -2147483648 "long write 2"
    test_write l1 0 "long write 3"
    test_write ul1 4294967295 "ulong write 1"
    test_write ul1 0 "ulong write 2"
    test_write ul1 1000000 "ulong write 3"
    test_write bo1 FALSE "bool write 1"
    test_write bo1 TRUE "bool write 2"
    test_write c1 \"1234\" "char write 1"
    test_write c2 \"1234567\" "char write 2"
    test_write c3 \"654321\" "char write 3"
    test_write c4 C'65' 'e' "char write 4"
    test_write bi1 B'10100101' "bitstring write 1"
    test_write bi2 B'0101001010' "bitstring write 2"
    test_write se1 a "set write 1"
    test_write se1 h "set write 2"
    # The following two use numbered sets with too-large values.
    setup_xfail "*-*-*"
    test_write nse1 nb "numbered set write 1"
    setup_xfail "*-*-*"
    test_write nse1 nc "numbered set write 2"
    test_write r1 127 "range write 1"
    test_write r2 32767 "range write 2"
    test_write r3 2147483647 "range write 3"

    # powerset modes
    test_write p1 {[pa:ph]} {\[pa:ph\]} "powerset write 1"
    test_write p1 {[pa, pc:pf, ph]} {\[pa, pc:pf, ph\]} "powerset write 2"
    test_write p1 {[pa, pc, pe, pg]} {\[pa, pc, pe, pg\]} "powerset write 3"
    test_write p1 {[]} {\[\]} "powerset write 4"
    test_write p2 {[1:32]} {\[1:32\]} "powerset write 5"
    test_write p2 {[1, 3:30, 32]} {\[1, 3:30, 32\]} "powerset write 6"
    test_write p2 {[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31]} {\[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31\]} \
	"powerset write 7"
    test_write p2 {[]} {\[\]} "powerset write 8"

# Fixme: this should be rejected by gnuchill
#    test_write p3 {[-2147483648:2147483647]} {\[-2147483648:2147483647\]} \
#	"powerset write 9"
#    test_write p3 {[-2147483648, -1000000:1000000, 2147483647]} \
#	{\[-2147483648, -1000000:1000000, 2147483647\]} \
#	"powerset write 10"
#    test_write p3 {[-99, -97, -95, 1001, 1003, 1005]} \
#	{\[-99, -97, -95, 1001, 1003, 1005\]} "powerset write 11"
#    test_write p3 {[]} {\[\]} "powerset write 12"
    
    # reference modes
    test_write ref1 ->ref1 {H'[0-9a-fA-F]+} "reference write 1"
    test_write ref2 ->b1 {H'[0-9a-fA-F]+} "reference write 2"
    test_write ref1 NULL "reference write 3"
    test_write ref2 NULL "reference write 4"

    # procedure modes
    test_write pr1 NULL "procefure write 1"
    # FIXME: remove when NULL is understood
    test_write pr1 0 NULL "procefure write 2"
    test_write pr1 dummyfunc {H'[0-9a-fA-F]+ <dummyfunc>} "procedure write 3"

    # timing modes, FIXME when callbacks to timefunctions are implemented
    #test_write ti1 abstime(1970, 3, 12, 10, 43, 0) {} "time write 1"
    #test_write ti2 <set somehow a duration>
    xfail "timing modes not implemented yet"
    
    # real modes
    # This ones 
    test_write re1 42.03 {42.0[0-9]*} "real write 1"
    test_write re1 0 "real write 2"
    test_write re1 "1e+38" {1e\+38|1\.0[0-9]*e\+38|9\.9[0-9]*e\+37} \
	"real write 3"
    setup_xfail "i*86-pc-linux-gnu" "m68*-*-hpux*"
    test_write re1 "1e+39" $infinity "real write 4"
    test_write re2 42.03 {42.0[0-9]*} "real write 5"
    test_write re2 0 "real write 6"
    test_write re2 "1e+308" {1e\+308} "real write 7"
    setup_xfail "i*86-pc-linux-gnu" "m68*-*-hpux*"
    test_write re2 "1e+309" $infinity "real write 8"
    # array modes
    test_write arrl1 {[(1:3): [(1:2): -128]]} {\[\(1:3\): \[\(1:2\): -128\]\]}\
	"array write 1"
    test_write arrl1 {[(1:3): [(1:2): 0]]} {\[\(1:3\): \[\(1:2\): 0\]\]}\
	"array write 2"
    test_write arrl1 {[(1): [(1:2): 127], (2): [(1:2): -128], (3): [(1:2): 127]]} {\[\(1\): \[\(1:2\): 127\], \(2\): \[\(1:2\): -128\], \(3\): \[\(1:2\): 127\]\]}\
	"array write 3"
     test_write arrl1 {[(1:3): [(1:2): 0]]} {\[\(1:3\): \[\(1:2\): 0\]\]}\
 	"array write 4"
     setup_xfail "*-*-*"
     # Bogus test case - type mismatch?
     test_write arrl1 {[(1): 127, (2): -128]} "array write 5"
     test_write arrl1 {[(1:3): [(1:2): 0]]} {\[\(1:3\): \[\(1:2\): 0\]\]}\
 	"array write 6"

    # structure modes
    test_write strul1 {[.a: -32768, .b: 32767, .ch: "ZZZZ"]} \
	{\[\.a: -32768, \.b: 32767, \.ch: \"ZZZZ\"\]} \
	"structure write 1"
    test_write strul1 {[.a: 0, .b: 0, .ch: "0000"]} \
	{\[\.a: 0, \.b: 0, \.ch: \"0000\"\]} \
	"structure write 2"
    test_write strul1 -32768 {\[\.a: -32768, \.b: 0, \.ch: \"0000\"\]} \
	{.a} "structure write 3"
    test_write strul1 {[.a: 0, .b: 0, .ch: "0000"]} \
	{\[\.a: 0, \.b: 0, \.ch: \"0000\"\]} \
	"structure write 4"
    test_write strul1 -32768 {\[\.a: 0, \.b: -32768, \.ch: \"0000\"\]} \
	{.b} "structure write 5"
    test_write strul1 {[.a: 0, .b: 0, .ch: "0000"]} \
	{\[\.a: 0, \.b: 0, \.ch: \"0000\"\]} \
	"structure write 6"
    test_write strul1 \"HUGO\" {\[\.a: 0, \.b: 0, \.ch: \"HUGO\"\]} \
	{.ch} "structure write 7"
}

# Start with a fresh gdb.

set binfile "tests2.exe"

gdb_exit
gdb_start
gdb_reinitialize_dir $srcdir/$subdir

gdb_test "set print sevenbit-strings" ".*"

if [set_lang_chill] then {
    write_access
} else {
    warning "$test_name tests suppressed."
}
