# Copyright 2015-2020 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 3 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, see <http://www.gnu.org/licenses/>.

# This file is part of the gdb testsuite.

# Test the operation of the "history remove-duplicates" option.


# Check that the previous history entry is ENTRY.

proc check_prev_history_entry { entry { test_suffix "" } } {
    set test_name "history entry is $entry"
    if { $test_suffix != "" } {
	append test_name " $test_suffix"
    }

    # Send ^P followed by ^L.
    send_gdb "\x10\x0c"

    gdb_expect {
	-re $entry {
	    pass $test_name
	}
	timeout {
	    fail $test_name
	}
    }
}

# Foreach element ELT in THINGS, run the command "print $ELT", making sure that
# each invocation of "print" has a unique test name.

proc run_print_on_each_thing { things } {
    set index 0

    foreach thing $things {
	gdb_test "print $thing" "" "printing $thing (item #$index)"
	incr index
    }
}

gdb_exit
gdb_start

# These tests require readline support.
if { ![readline_is_used] } {
    unsupported "readline isn't used."
    return -1
}

# By default the option is set to 0.
gdb_test "show history remove-duplicates" "is 0\\."

# Test the "unlimited" setting.
with_test_prefix "remove-duplicates=unlimited" {
    gdb_exit
    gdb_start
    gdb_test "set history remove-duplicates unlimited"

    run_print_on_each_thing { 0 1 2 1 1 2 3 3 4 1 2 3 4 }

    check_prev_history_entry "print 4"
    check_prev_history_entry "print 3"
    check_prev_history_entry "print 2"
    check_prev_history_entry "print 1"
    check_prev_history_entry "print 0"
}


# Test the "1" setting.
with_test_prefix "remove-duplicates=1" {
    gdb_exit
    gdb_start
    gdb_test "set history remove-duplicates 1"

    run_print_on_each_thing { 0 1 0 2 2 1 }

    check_prev_history_entry "print 1"
    check_prev_history_entry "print 2"
    check_prev_history_entry "print 0"
    check_prev_history_entry "print 1" "(again)"
    check_prev_history_entry "print 0" "(again)"
}


# Test the "0" setting.
with_test_prefix "remove-duplicates=0" {
    gdb_exit
    gdb_start
    gdb_test "set history remove-duplicates 0"

    run_print_on_each_thing { 0 0 1 1 }

    check_prev_history_entry "print 1"
    check_prev_history_entry "print 1" "(again)"
    check_prev_history_entry "print 0"
    check_prev_history_entry "print 0" "(again)"
}


# Test the "2" setting.
with_test_prefix "remove-duplicates=2" {
    gdb_exit
    gdb_start
    gdb_test "set history remove-duplicates 2"

    run_print_on_each_thing { 1 2 0 2 0 }

    check_prev_history_entry "print 0"
    check_prev_history_entry "print 2"
    check_prev_history_entry "print 1"
}
