/*
 *  ticket-460.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-460 - Check that local and global Get{Node/Leaves/Children} work

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

Description:
 This test creates a network and extracts nodes, leaves and children both locally 
 and globally.

Remarks:
 This should be run with 1 and 2 MPI processes automatically.
 
Author: Hans Ekkehard Plesser, 2010-09-24
 */

(unittest) run
/unittest using

[1 2]
{
 M_INFO setverbosity

 ResetKernel
 /s1 /subnet Create def   % gid 1
 s1 ChangeSubnet
 /s2 /subnet Create def   % gid 2
 /s3 /subnet Create def   % gid 3

 0 ChangeSubnet
 /voltmeter 2 Create  ;   % gid 4,5

 /iaf_neuron 2 Create ;   % gid 6,7

 s2 ChangeSubnet
 /iaf_neuron 6 Create ;   % gid 8,9,10,11,12,13
 s1 ChangeSubnet
 /iaf_neuron 4 Create ;   % gid 14,15,16,17
 s3 ChangeSubnet
 /iaf_neuron 3 Create ;   % gid 18,19,20

 0 GetGlobalNodes    Sort [1 20] Range eq % 1..20
 0 GetGlobalLeaves   Sort [4 20] Range eq  % 4..20
 0 GetGlobalChildren Sort [1 4 5 6 7]  eq  % 1, 4..5, 6..7

 NumProcesses 1 eq {
  % 1 mpi process: local == global
  0 GetLocalNodes    Sort [1 20] Range eq % 1..20
  0 GetLocalLeaves   Sort [4 20] Range eq  % 4..20
  0 GetLocalChildren Sort [1 4 5 6 7]  eq  % 1, 4..5, 6..7
 }
 {
  NumProcesses 2 eq {
     Rank 0 eq {
       0 GetLocalNodes    Sort [1 5] Range [6 20 2] Range join eq
       0 GetLocalLeaves   Sort [4 5] [6 20 2] Range join eq
       0 GetLocalChildren Sort [1 4 5 6] eq 
     }
     {
       0 GetLocalNodes    Sort [1 5] Range [7 20 2] Range join eq
       0 GetLocalLeaves   Sort [4 5] [7 20 2] Range join eq
       0 GetLocalChildren Sort [1 4 5 7] eq 
     } 
     ifelse
  }
  {
   (ERROR: only 1 or 2 procs) ==
  }
  ifelse
 }
 ifelse

 6 arraystore true exch { and } Fold  % all jobs of all runs should return true
}
distributed_collect_assert_or_die

