/*****************************************************************************
 * $LastChangedDate: 2011-04-23 21:07:07 -0400 (Sat, 23 Apr 2011) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   Test of Multivar template class.
 *//*
 * LEGAL:   COPYRIGHT (C) 2006 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#define TESTING 1
#include <list>
#include "test.hh"
#include "base/module.hh"
#include "base/types.hh"
#include "base/multivar.hh"
using namespace base;

/*****************************************************************************
 * Test.
 *****************************************************************************/
bool
MultivarTest( void )
{
    Multivar<int> mv;
    if ( ! mv.IfEmpty() ) { return Fail(); }

    // Set 1, Remove 1.
    mv.Set( 1 );
    if ( mv.IfEmpty() ) { return Fail(); }
    if ( ! mv.Query( 1 ) ) { return Fail(); }
    mv.Remove( 1 );
    if ( ! mv.IfEmpty() ) { return Fail(); }
    if ( mv.Query( 1 ) ) { return Fail(); }

    // Test Clear().
    mv.Set( 0 );
    mv.Clear();
    if ( ! mv.IfEmpty() ) { return Fail(); }
    if ( mv.Query( 0 ) ) { return Fail(); }

    // Set 2, Add 3.
    mv.Set( 2 );
    mv.Add( 3 );
    if ( mv.IfEmpty() ) { return Fail(); }
    if ( ! mv.Query( 2 ) ) { return Fail(); }
    if ( ! mv.Query( 3 ) ) { return Fail(); }
    if ( mv.Query( 1 ) ) { return Fail(); }  // should be gone, replaced by Set()
    if ( mv.Query( 0 ) ) { return Fail(); }  // likewise

    // Set 2, Set 3 (latter should replace former).
    mv.Clear();
    mv.Set( 2 );
    mv.Set( 3 );
    if ( mv.IfEmpty() ) { return Fail(); }
    if ( mv.Query( 2 ) ) { return Fail(); }   // should've been replaced by 3
    if ( ! mv.Query( 3 ) ) { return Fail(); }

    // Test twice.
    mv.Set( 4 );
    mv.Add( 4 );
    mv.Add( 5 );
    mv.Add( 5 );
    if ( mv.IfEmpty() ) { return Fail(); }
    if ( ! mv.Query(4) ) { return Fail(); }
    if ( ! mv.Query(5) ) { return Fail(); }
    if ( mv.Query( 2 ) ) { return Fail(); }  // 2 gone
    if ( mv.Query( 3 ) ) { return Fail(); }  // 3 gone

    // Test toggle.
    mv.Clear();
    mv.Toggle( 10 );  // add
    if ( mv.IfEmpty() ) { return Fail(); }
    if ( ! mv.Query( 10 ) ) { return Fail(); }
    mv.Toggle( 10 );  // remove
    if ( ! mv.IfEmpty() ) { return Fail(); }
    if ( mv.Query( 10 ) ) { return Fail(); }

    // Test toggle adds, not sets.
    mv.Set( 20 );
    mv.Toggle( 21 );  // add
    if ( mv.IfEmpty() ) { return Fail(); }
    if ( ! mv.Query( 20 ) ) { return Fail(); }
    if ( ! mv.Query( 21 ) ) { return Fail(); }
    mv.Toggle( 21 );  // remove
    if ( mv.IfEmpty() ) { return Fail(); }
    if ( ! mv.Query( 20 ) ) { return Fail(); }  // 20 should remain/unaffected
    if ( mv.Query( 21 ) ) { return Fail(); }    // 21 should be toggled off

//---
    return true;
}

/*****************************************************************************
 * main.
 *****************************************************************************/
int
main( int argc, char** argv )
{
    RUN_TEST( "Multivar", TEST_ERROR_MODE_ABORT,
              MultivarTest() )
}
