/*
 *  crash_distributed_process_invariant_events_assert_or_die.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::crash_distributed_process_invariant_events_assert_or_die - self test of distributed_invariant_events_assert_or_die

Synopsis: nest_indirect distributed_process_invariant_events_assert_or_die.sli -> -


Description:

 This is a self test of
 distributed_invariant_events_assert_or_die. The function needs to be
 able to collect the results of an arbitrary number of jobs and
 compare the set of results to the one of the same simulation carried
 out with a different number of jobs.

Author:  September 2010, Diesmann; May 2012, Plesser
SeeAlso: testsuite::fail_distributed_process_invariant_events_assert_or_die, testsuite::test_distributed_process_invariant_events_assert_or_die
*/

(unittest) run
/unittest using

% take array of dict, with all entries arrays; return single dict with all 
% data with same key merge to single array; all dicts must have same keys
/merge_dict_entries
{
  dup { keys } Map 
  dup First /keylits Set
  keylits { cvs } Map Sort /keystrings Set % only strings can be sorted  
  true exch { { cvs } Map Sort keystrings eq and } Fold assert_or_die % all have same keys
  
  % build new dict with merged arrays
  dup First /newdict Set
  Rest
  {
     keylits { /key Set dup key get newdict key get join newdict key rolld put } forall
     pop
  } forall  

  newdict   % leave on stack
}
def 

[1 2 4]
{
  % per vp dictionaries, four VPs
  /vp_events 
  [
    << /times [ 23 45 67 ] /senders [ 12 12 16 ] /offsets [ 0.035 0.01 0.02 ] >>
    << /times [ 11 99    ] /senders [ 13 17    ] /offsets [ 0.07  0.03      ] >>
    << /times [ 12 23 34 ] /senders [ 14 18 22 ] /offsets [ 0.05  0.04 0.08 ] >>
    << /times [ 24       ] /senders [ 15       ] /offsets [ 0.06            ] >>
  ] def 

  mark
    NumProcesses 1 eq { vp_events merge_dict_entries exit } case
    NumProcesses 2 eq { vp_events [Rank 1 add dup 2 add 2] Take merge_dict_entries exit } case
    NumProcesses 4 eq { vp_events Rank get exit } case
  switch

  3 Rank neq assert_or_die % provoke crash
}
distributed_process_invariant_events_assert_or_die
