/*
 *  ticket-543.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::ticket-543 - Regression test showing too many connections being created on iteration over nested subnets

Synopsis: (ticket-543) run -> NEST exits if test fails

Description:
This test detects the creation of a too large number of connections
when Convergent/DivergentConnect iterate over target/source subnets
which contain nested subnets
 
Author: Hans Ekkehard Plesser & Susanne Kunkel, 2012-02-08
 */

/unittest (8831) require
/unittest using
M_ERROR setverbosity

% provide number of nesting levels as argument
% should return a list with a single element

/div_conn_test {
  ResetKernel
  { /subnet Create ChangeSubnet } repeat
  /source_neuron /iaf_neuron Create def
  0 ChangeSubnet
  /target_neuron /iaf_neuron Create def
  source_neuron [target_neuron] DivergentConnect
  << /source source_neuron >> FindConnections { /target get } Map
} def

/conv_conn_test {
  ResetKernel
  { /subnet Create ChangeSubnet } repeat  
  /target_neuron /iaf_neuron Create def
  0 ChangeSubnet
  /source_neuron /iaf_neuron Create def
  [source_neuron] target_neuron ConvergentConnect 
  << /source source_neuron >> FindConnections { /target get } Map
} def

/random_div_conn_test {
  /num_conns Set
  ResetKernel
  { /subnet Create ChangeSubnet } repeat
  /source_neuron /iaf_neuron Create def
  0 ChangeSubnet
  /target_neuron /iaf_neuron Create def
  source_neuron num_conns [target_neuron] RandomDivergentConnect
  << /source source_neuron >> FindConnections { /target get } Map 
} def

/random_conv_conn_test {
  /num_conns Set  
  ResetKernel
  { /subnet Create ChangeSubnet } repeat  
  /target_neuron /iaf_neuron Create def
  0 ChangeSubnet
  /source_neuron /iaf_neuron Create def
  [source_neuron] target_neuron num_conns RandomConvergentConnect 
  << /source source_neuron >> FindConnections { /target get } Map 
} def

 % first test: divergent
{ 
  [5] Range { div_conn_test length 1 eq } Map 
  true exch { and } Fold 
} assert_or_die

% second test: convergent
{ 
  ResetKernel
  [5] Range { conv_conn_test length 1 eq } Map 
  true exch { and } Fold
} assert_or_die

% third test: random divergent
{ 
  ResetKernel
  [5] Range { 1 random_div_conn_test length 1 eq } Map 
  true exch { and } Fold
} assert_or_die

% fourth test: random convergent
{ 
  ResetKernel
  [5] Range { 1 random_conv_conn_test length 1 eq } Map 
  true exch { and } Fold
} assert_or_die

endusing