/*
 *  issue-105.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/>.
 *
 */

/* BeginDocumentation

Name: testsuite::issue-105 Ensure that min_delay and max_delay are set correctly

Synopsis: (issue-105) run -> NEST exits if test fails

Description:
This ticket ensures that NEST sets min_delay and max_delay correctly also for 
delays that are not multiples of the resolution.

Author: Hans E Plesser, 2015-12-01 based on material by Jafet Diaz
 */

(unittest) run
/unittest using

M_ERROR setverbosity

% Test min_delay on fresh connection
{
  ResetKernel
  
  0 << /resolution 0.1 >> SetStatus
  
  /n /iaf_neuron Create def
  
  [  0.81 0.82 0.8499 ]
  {
    /delay Set
    [ n ] dup /one_to_one << /delay delay >> Connect
  } forall
  
  << >> GetConnections { /delay get } Map
  
  % should all be 0.8
  dup true exch { 0.8 eq and } Fold
  exch
  0 get 
  0 /min_delay get 
  eq 
  and
} assert_or_die

% Test min_delay on SetStatus
{
  ResetKernel
  
  0 << /resolution 0.1 >> SetStatus
  
  /n /iaf_neuron Create def
  
  /delays [ 0.81 0.82 0.8499 ] def
  
  delays
  {
    ;
    [ n ] dup Connect
  } forall

  [ << >> GetConnections delays ]
  { /d Set << /delay d >> SetStatus }
  ScanThread
  
  << >> GetConnections { GetStatus /delay get } Map
  
  % should all be 0.8 
  dup true exch { 0.8 eq and } Fold
  exch
  0 get 
  0 /min_delay get 
  eq 
  and
} assert_or_die

% Test max_delay on fresh connection
{
  ResetKernel
  
  0 << /resolution 0.1 >> SetStatus
  
  /n /iaf_neuron Create def
  
  [ 0.85 0.86 0.8999 ]
  {
    /delay Set
    [ n ] dup /one_to_one << /delay delay >> Connect
  } forall
  
  << >> GetConnections { /delay get } Map
  
  % should all be 0.9
  dup true exch { 0.9 eq and } Fold
  exch
  0 get 
  0 /max_delay get 
  eq 
  and
} assert_or_die

% Test max_delay on SetStatus
{
  ResetKernel
  
  0 << /resolution 0.1 >> SetStatus
  
  /n /iaf_neuron Create def
  
  % test delays must be longer than 1.0, since we
  % first create default connections with delay 1
  /delays [ 1.95 1.96 1.9999 ] def
  
  delays
  {
    ;
    [ n ] dup Connect
  } forall

  [ << >> GetConnections delays ]
  { /d Set << /delay d >> SetStatus }
  ScanThread
  
  << >> GetConnections { GetStatus /delay get } Map

  % should all be 2.0
  dup true exch { 2.0 eq and } Fold
  exch
  0 get 
  0 /max_delay get 
  eq 
  and
} assert_or_die

% Test that setting min_delay and max_delay works so that setting certain
% min/max values will allow those values to be used in subsequent connections.
% Note that effects can only be seen in the kernel status after a connection
% has been created.

{
  ResetKernel
  
  /min_delay 0.81 def  % rounded to 0.8
  /max_delay 1.49 def  % rounded to 1.5

  0 << /min_delay min_delay /max_delay max_delay >> SetStatus
  
  /n /iaf_neuron Create def
  
  [ n ] dup /one_to_one << /delay min_delay >> Connect
  [ n ] dup /one_to_one << /delay max_delay >> Connect
  
  0 /min_delay get 0.8 eq
  0 /max_delay get 1.5 eq
  and
} assert_or_die

{
  ResetKernel
  
  /min_delay 0.89 def  % rounded to 0.8
  /max_delay 1.41 def  % rounded to 1.5
  
  0 << /min_delay min_delay /max_delay max_delay >> SetStatus
  
  /n /iaf_neuron Create def
  
  [ n ] dup /one_to_one << /delay min_delay >> Connect
  [ n ] dup /one_to_one << /delay max_delay >> Connect
  
  0 /min_delay get 0.8 eq
  0 /max_delay get 1.5 eq
  and
} assert_or_die
