/*
 *  test_spin_detector.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_spin_detector - test decoding mechanism of binary events by spin_detector

    Synopsis: (test_spin_detector) run -> test decoding mechanism of binary events by spin_detector

    Description:
    Test, whether the communication mechanism for binary neurons
    works and whether the spin detector correctly decodes binary transitions.
    Finally, setting the precision is tested.

    FirstVersion: March 2013
    Author: Mortiz Helias, Sepehr Mahmoudian
*/

(unittest) run
/unittest using


/run_test
{

    0.1 /h Set

    ResetKernel

    0 <<
	/local_num_threads 1
	/resolution h
    >> SetStatus

    % check, if double spikes are correctly interpreted as up transition
    % and single spikes are interpreted as down transition

    /sp /spin_detector Create def

    /sg /spike_generator Create def


    sg << /spike_times [10. 10. 15.] >> SetStatus

    sg sp 1. 1. Connect

    20. Simulate

    % check if right state is decoded
    sp /events get /weights get /w Set
    w 0 get 1.0 eq assert_or_die
    w 1 get 0.0 eq assert_or_die


    sp /events get /times get /times Set
    times cva
    [ 10. 15.] eq
    assert_or_die

    % test the setting of the /precision value absent precise models
    % 1. checking the default value and changing it

      ResetKernel
      /spin_detector Create /sd Set

      % is the default correctly set to 3?
      sd GetStatus /precision get dup /p_val Set 3 eq assert_or_die

      % is the precision still the same after simulating for some time?
      100 Simulate
      sd GetStatus /precision get p_val eq assert_or_die

    % 2. setting Precision

      ResetKernel
      /spin_detector Create /sd Set

      % can the precision be correctly set?
      sd << /precision 15 >> SetStatus
      sd GetStatus /precision get 15 eq assert_or_die

      % is the precision the same after simulating?
      100 Simulate
      sd GetStatus /precision get 15 eq assert_or_die

    % test the behavior of the /precise_times flag
    % 3. if the user does not set it

      ResetKernel
      /spin_detector Create /sd Set

      % is the default correctly set to false?
      sd GetStatus /precise_times get dup /pt_flag Set false eq assert_or_die

      % is the flag still the same after simulating for some time?
      100 Simulate
      sd GetStatus /precise_times get pt_flag eq assert_or_die

      % is the flag still the same after the creation of a precise neuron?
      /iaf_psc_exp_ps Create ;
      sd GetStatus /precise_times get pt_flag eq assert_or_die

      % does the flag change to true if simulating with a precise neuron around?
      100 Simulate
      sd GetStatus /precise_times get true eq assert_or_die

      % check if precision correctly increased to 15.
      sd GetStatus /precision get 15 eq assert_or_die

    % 4.a if the user sets the flag to false

      ResetKernel
      /spin_detector Create /sd Set
      sd << /precise_times false >> SetStatus
      /iaf_psc_exp_ps Create ;
      100 Simulate
      sd GetStatus /precise_times get false eq assert_or_die

    % 4.b if the user sets the flag to true

      ResetKernel
      /spin_detector Create /sd Set
      sd << /precise_times true >> SetStatus
      /iaf_psc_exp_ps Create ;
      100 Simulate
      sd GetStatus /precise_times get true eq assert_or_die

    % 5. can precision be set correctly when precise models exist?

      ResetKernel
      /spin_detector Create /sd Set

      % checking to see if precision gets set correctly.
      /iaf_psc_exp_ps Create ;
      sd << /precision 15 >> SetStatus
      sd GetStatus /precision get 15 eq assert_or_die

      % does it remain the same after simulation?
      100 Simulate
      sd GetStatus /precision get 15 eq assert_or_die

} def

run_test
