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

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

Description:
This test ensures that ConnectLayers correctly handles devices with thread
siblings as sources and targets.

Author: Hans Ekkehard Plesser
FirstVersion: February 2017
SeeAlso:
*/

(unittest) run
/unittest using

skip_if_not_threaded

% First set of tests: Single generator to neuron layer
/gen_to_layer_test
{
  /connspec Set
  
  ResetKernel
  0 << /local_num_threads 4 >> SetStatus
  
  /pgl 
  << /elements /poisson_generator 
     /rows 1 /columns 1 >>
  CreateLayer def
  
  /nnl
  << /elements /iaf_psc_alpha 
     /rows 2 /columns 2 >>
  CreateLayer def
  
  pgl nnl connspec ConnectLayers
  
  /src pgl GetGlobalLeaves 1 Take def
  /tgts nnl GetGlobalLeaves Sort def
    
  /conns << /source src >> GetConnections def
  /ctgts conns { cva 1 get } Map Sort def
    
  tgts ctgts eq
} 
def

{
  << /connection_type /convergent >> gen_to_layer_test
}
assert_or_die

{
  << /connection_type /divergent >> gen_to_layer_test
}
assert_or_die

{
  % one incoming connection to each neuron in layer
  << /connection_type /convergent 
     /number_of_connections 1 >> gen_to_layer_test
}
assert_or_die

{
  % four outgoing connections, one to each neuron in layer
  % prohibit multapses to ensure consistent result
  << /connection_type /divergent 
     /number_of_connections 4
     /allow_multapses false >> gen_to_layer_test
}
assert_or_die

% -----------------------------------------------

% Second set of tests: Neuron layer to single detector
/layer_to_det_test
{
  /connspec Set
  
  ResetKernel
  0 << /local_num_threads 4 >> SetStatus
  
  /nnl
  << /elements /iaf_psc_alpha 
     /rows 2 /columns 2 >>
  CreateLayer def

  /sdl 
  << /elements /spike_detector 
     /rows 1 /columns 1 >>
  CreateLayer def
    
  nnl sdl connspec ConnectLayers
  
  /tgt sdl GetGlobalLeaves 1 Take def
  /srcs nnl GetGlobalLeaves Sort def
    
  /conns << /target tgt >> GetConnections def
  /csrcs conns { cva 0 get } Map Sort def
  
  srcs csrcs eq
} 
def

{
  << /connection_type /convergent >> layer_to_det_test
}
assert_or_die

{
  << /connection_type /divergent >> layer_to_det_test
}
fail_or_die
clear

{
  % four incoming connections, one to each neuron in layer
  % prohibit multapses to ensure consistent result
  << /connection_type /convergent 
     /number_of_connections 4
     /allow_multapses false >> layer_to_det_test
}
fail_or_die
clear

{
  % one outgoing connection from each neuron in layer
  << /connection_type /divergent 
     /number_of_connections 1 >> layer_to_det_test
}
fail_or_die
clear

endusing
