/*
 *  ticket-349.sli
 *
 *  This file is part of NEST.
 *
 *  Copyright (C) 2004 The NEST Initiative
 *
 *  NEST 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.
 *
 *  NEST 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 NEST.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * Regression test for Ticket #349.
 *
 * Ensure that multimeter connects with entries in record_from list that do not match target node
 * do not leave any trace in target node, so that attempt with corrected list succeed.
 *
 * Ensure that simulation does not crash when multimeter has empty record_from list. 
 *
 * Hans Ekkehard Plesser, 2009-07-02
 *
 */

% The following test needs the model iaf_cond_alpha, so
% this test should only run if we have GSL
statusdict/have_gsl :: not {statusdict/exitcodes/success :: quit_i} if

(unittest) run
/unittest using

% First test: connect with illegal entry
{
  ResetKernel

  /mm /multimeter Create def
  /n  /iaf_cond_alpha Create def

  /r n /recordables get def

  % take first element, add _foo to create string not in recordable list, append
  /rfail r dup (foo_) exch { cvs join (_) join } Fold cvlit append def

  mm << /record_from rfail >> SetStatus

  % shall provoke error
  mm n Connect
} fail_or_die

% Second test: connect with illegal entry first, then re-try with legal list
{
  ResetKernel

  /mm /multimeter Create def
  /n  /iaf_cond_alpha Create def

  /r n /recordables get def

  % take first element, join all recordables to create non-existing entry
  /rfail r dup (foo_) exch { cvs join (_) join } Fold cvlit append def

  mm << /record_from rfail >> SetStatus

  % shall provoke error, we must catch it manually
  mark
  { mm n Connect }
  stopped
  {
    % remove error code
    errordict /message undef
    errordict /command undef
    errordict begin /newerror false def end

    % clear stack
    counttomark npop pop % need to pop mark separately
  } 
  {
    % we should have gotten an error, we didn't, so we raise one
    /ticket-349-test-2 /ExpectedErrorNotRaised raiseerror
  }
  ifelse

  % try with correct list
  mm << /record_from r >> SetStatus
  mm n Connect

} pass_or_die

% Third test: connect Multimeter with empty list, then simulate. Must not crash.
{
  ResetKernel

  /mm /multimeter Create def
  /n  /iaf_cond_alpha Create def
  mm n Connect

  10 Simulate

  % see if we can get an events dictionary, must contain empty times and senders arrays
  mm /events get 
  dup /senders get cva length 0 eq
  exch /times get cva length 0 eq
  and

} assert_or_die
