/*
 *  test_connect.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::test_connect - SLI script to test runability of all Connect variants

   Synopsis: (test_connect) run

   Description:

   Tests if all variants of Connect can be run. This test checks the
   outcome of the calls only in a minimal way. A more comprehensive
   set of tests is available in PyNEST. It also checks the given
   parameters more thoroughly and looks at statistics of the resulting
   connectivity.

   FirstVersion: April 2014
   Author: Jochen Martin Eppler
   SeeAlso: Connect
 */

M_ERROR setverbosity

(unittest) run
/unittest using

(Running Connect tests with argument type ') 1 type cvs join (') join =

(  Testing variant "integer integer") =
ResetKernel
/iaf_neuron 2 Create ;
1 2 Connect
<< /source [1] >> GetConnections 0 get
/target get 2 eq assert_or_die

(  Testing variant "integer integer double double") =
ResetKernel
/iaf_neuron 2 Create ;
1 2 2.0 3.0 Connect
<< /source [1] >> GetConnections 0 get dup dup
/target get 2 eq assert_or_die
/weight get 2.0 eq assert_or_die
/delay get 3.0 eq assert_or_die

% We can run the tests for TokenArray, IntVectorDatum and
% GIDCollectionDatum in a loop using arrayload

[ [[1 2 3]             [4 5 6]            ]
  [[1 2 3] cv_iv       [4 5 6] cv_iv      ]
  [1 3 cvgidcollection 4 6 cvgidcollection]] {
  /addresses Set

  (Running Connect tests with argument type ') addresses 0 get type cvs join (') join =

  (  Testing variant "array array") =
  ResetKernel
  /iaf_neuron 6 Create ;
  addresses arrayload ; Connect
  << /source [1 2 3] >> GetConnections dup
  First /target get 4 eq assert_or_die
  Last /target get 6 eq assert_or_die

  (  Testing variant "array array literal") =
  ResetKernel
  /iaf_neuron 6 Create ;
  addresses arrayload ; /one_to_one Connect
  << /source [1 2 3] >> GetConnections dup
  First /target get 4 eq assert_or_die
  Last /target get 6 eq assert_or_die

  (  Testing variant "array array dict") =
  ResetKernel
  /iaf_neuron 6 Create ;
  addresses arrayload ; << /rule /one_to_one >> Connect
  << /source [1 2 3] >> GetConnections dup
  First /target get 4 eq assert_or_die
  Last /target get 6 eq assert_or_die

  (  Testing variant "array array empty_dict") =
  ResetKernel
  /iaf_neuron 6 Create ;
  addresses arrayload ; << >> Connect
  << /source [1 2 3] >> GetConnections dup
  First /target get 4 eq assert_or_die
  Last /target get 6 eq assert_or_die

  (  Testing variant "array array literal literal") =
  ResetKernel
  /iaf_neuron 6 Create ;
  addresses arrayload ; /one_to_one /stdp_synapse Connect
  << /source [1 2 3] >> GetConnections dup
  First /target get 4 eq assert_or_die
  Last /target get 6 eq assert_or_die

  (  Testing variant "array array dict literal") =
  ResetKernel
  /iaf_neuron 6 Create ;
  addresses arrayload ; << /rule /one_to_one >> /stdp_synapse Connect
  << /source [1 2 3] >> GetConnections dup
  First /target get 4 eq assert_or_die
  Last /target get 6 eq assert_or_die
  
  (  Testing variant "array array empty_dict literal") =
  ResetKernel
  /iaf_neuron 6 Create ;
  addresses arrayload ; << >> /stdp_synapse Connect
  << /source [1 2 3] >> GetConnections dup
  First /target get 4 eq assert_or_die
  Last /target get 6 eq assert_or_die
  
  (  Testing variant "array array literal dict") =
  ResetKernel
  /iaf_neuron 6 Create ;
  addresses arrayload ; /one_to_one << /model /stdp_synapse >> Connect
  << /source [1 2 3] >> GetConnections dup
  First /target get 4 eq assert_or_die
  Last /target get 6 eq assert_or_die
  
  (  Testing variant "array array literal empty_dict") =
  ResetKernel
  /iaf_neuron 6 Create ;
  addresses arrayload ; /one_to_one << >> Connect
  << /source [1 2 3] >> GetConnections dup
  First /target get 4 eq assert_or_die
  Last /target get 6 eq assert_or_die
  
  (  Testing variant "array array dict dict") =
  ResetKernel
  /iaf_neuron 6 Create ;
  addresses arrayload ; << /rule /one_to_one >> << /model /stdp_synapse >> Connect
  << /source [1 2 3] >> GetConnections dup
  First /target get 4 eq assert_or_die
  Last /target get 6 eq assert_or_die
  
  (  Testing variant "array array empty_dict dict") =
  ResetKernel
  /iaf_neuron 6 Create ;
  addresses arrayload ; << >> << /model /stdp_synapse >> Connect
  << /source [1 2 3] >> GetConnections dup
  First /target get 4 eq assert_or_die
  Last /target get 6 eq assert_or_die
  
  (  Testing variant "array array dict empty_dict") =
  ResetKernel
  /iaf_neuron 6 Create ;
  addresses arrayload ; << /rule /one_to_one >> << >> Connect
  << /source [1 2 3] >> GetConnections dup
  First /target get 4 eq assert_or_die
  Last /target get 6 eq assert_or_die
  
  (  Testing variant "array array empty_dict empty_dict") =
  ResetKernel
  /iaf_neuron 6 Create ;
  addresses arrayload ; << >> << >> Connect
  << /source [1 2 3] >> GetConnections dup
  First /target get 4 eq assert_or_die
  Last /target get 6 eq assert_or_die

} forall

(Running Connect tests with illegal combinations) =
ResetKernel

/n_connections 0 def
modeldict
{
  pop Create /node_a Set
  modeldict
  {
    pop Create /node_b Set

    % try to connect node_a and node_b
    {
      node_a node_b Connect
    } stopped not
    {  % if there is no error, check that a connection has been made
      {
        n_connections 1 add 
        0 GetStatus /num_connections get 
        eq
      } assert_or_die
      /n_connections n_connections 1 add def
    }
    {
      5 npop 
    } ifelse
  } forall

} forall

