/*
 *  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 FixedIndegree / FixedOutdegree Connect iterate over target/source subnets
which contain nested subnets
 
Author: Hans Ekkehard Plesser & Susanne Kunkel, 2012-02-08
 */

(unittest) run
/unittest using

M_ERROR setverbosity

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

/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] Connect
  << /source [source_neuron] >> GetConnections { /target get } Map
} def

/fod_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] [target_neuron] << /rule /fixed_outdegree /outdegree num_conns >> Connect
  << /source [source_neuron] >> GetConnections { /target get } Map
} def

/fid_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] << /rule /fixed_indegree /indegree num_conns >> Connect
  << /source [source_neuron] >> GetConnections { /target get } Map
} def

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

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

% third test: fixed indegree
{ 
  ResetKernel
  [5] Range { 1 fid_conn_test length 1 eq } Map 
  true exch { and } Fold
} assert_or_die

endusing
