/**
 * \file    TestUnitDefinition.c
 * \brief   SBML UnitDefinition unit tests for new API
 * \author  sarah Keating
 *
 * $Id: TestUnitDefinition.c 8428 2008-11-20 20:18:15Z sarahkeating $
 * $HeadURL: https://sbml.svn.sourceforge.net/svnroot/sbml/branches/libsbml-new-api-3.3/src/sbml/test/TestUnitDefinition.c $
 *
 *<!---------------------------------------------------------------------------
 * This file is part of libSBML.  Please visit http://sbml.org for more
 * information about SBML, and the latest version of libSBML.
 *
 * Copyright 2005-2007 California Institute of Technology.
 * Copyright 2002-2005 California Institute of Technology and
 *                     Japan Science and Technology Corporation.
 * 
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.  A copy of the license agreement is provided
 * in the file named "LICENSE.txt" included with this software distribution
 * and also available online as http://sbml.org/software/libsbml/license.html
 *----------------------------------------------------------------------- -->*/


#include <sbml/common/common.h>

#include <sbml/SBase.h>
#include <sbml/Unit.h>
#include <sbml/UnitDefinition.h>
#include <sbml/xml/XMLNamespaces.h>
#include <sbml/SBMLDocument.h>

#include <check.h>


static UnitDefinition_t *UD;


void
UnitDefinitionTest1_setup (void)
{
  UD = UnitDefinition_create(2, 4);

  if (UD == NULL)
  {
    fail("UnitDefinition_create() returned a NULL pointer.");
  }
}


void
UnitDefinitionTest1_teardown (void)
{
  UnitDefinition_free(UD);
}


START_TEST (test_UnitDefinition_setId1)
{
  int i = UnitDefinition_setId(UD, "mmls");

  fail_unless( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless( !strcmp(UnitDefinition_getId(UD), "mmls") );
  fail_unless( UnitDefinition_isSetId(UD) );

  i = UnitDefinition_setId(UD, NULL);
  
  fail_unless( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless( !UnitDefinition_isSetId(UD) );

  i = UnitDefinition_setId(UD, "123");
  
  fail_unless( i == LIBSBML_INVALID_ATTRIBUTE_VALUE);
  fail_unless( !UnitDefinition_isSetId(UD) );

}
END_TEST


START_TEST (test_UnitDefinition_setName1)
{
  int i = UnitDefinition_setName(UD, "mmls");

  fail_unless( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless( !strcmp(UnitDefinition_getName(UD), "mmls") );
  fail_unless( UnitDefinition_isSetName(UD) );

  i = UnitDefinition_setName(UD, NULL);
  
  fail_unless( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless( !UnitDefinition_isSetName(UD) );

  i = UnitDefinition_setName(UD, "123");
  
  fail_unless( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless( UnitDefinition_isSetName(UD) );

}
END_TEST


START_TEST (test_UnitDefinition_setName2)
{
  int i = UnitDefinition_setName(UD, "mmls");

  fail_unless( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless( !strcmp(UnitDefinition_getName(UD), "mmls") );
  fail_unless( UnitDefinition_isSetName(UD) );

  i = UnitDefinition_unsetName(UD);
  
  fail_unless( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless( !UnitDefinition_isSetName(UD) );
}
END_TEST


START_TEST (test_UnitDefinition_addUnit1)
{
  UnitDefinition_t *m = UnitDefinition_create(2, 2);
  Unit_t *p 
    = Unit_create(2, 2);

  int i = UnitDefinition_addUnit(m, p);

  fail_unless( i == LIBSBML_INVALID_OBJECT);

  Unit_setKind(p, UNIT_KIND_MOLE);
  i = UnitDefinition_addUnit(m, p);

  fail_unless( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless( UnitDefinition_getNumUnits(m) == 1);

  Unit_free(p);
  UnitDefinition_free(m);
}
END_TEST


START_TEST (test_UnitDefinition_addUnit2)
{
  UnitDefinition_t *m = UnitDefinition_create(2, 2);
  Unit_t *p 
    = Unit_create(2, 1);
  Unit_setKind(p, UNIT_KIND_MOLE);

  int i = UnitDefinition_addUnit(m, p);

  fail_unless( i == LIBSBML_VERSION_MISMATCH);
  fail_unless( UnitDefinition_getNumUnits(m) == 0);

  Unit_free(p);
  UnitDefinition_free(m);
}
END_TEST


START_TEST (test_UnitDefinition_addUnit3)
{
  UnitDefinition_t *m = UnitDefinition_create(2, 2);
  Unit_t *p 
    = Unit_create(1, 2);
  Unit_setKind(p, UNIT_KIND_MOLE);

  int i = UnitDefinition_addUnit(m, p);

  fail_unless( i == LIBSBML_LEVEL_MISMATCH);
  fail_unless( UnitDefinition_getNumUnits(m) == 0);

  Unit_free(p);
  UnitDefinition_free(m);
}
END_TEST


START_TEST (test_UnitDefinition_addUnit4)
{
  UnitDefinition_t *m = UnitDefinition_create(2, 2);
  Unit_t *p = NULL; 

  int i = UnitDefinition_addUnit(m, p);

  fail_unless( i == LIBSBML_OPERATION_FAILED);
  fail_unless( UnitDefinition_getNumUnits(m) == 0);

  UnitDefinition_free(m);
}
END_TEST


START_TEST (test_UnitDefinition_createUnit)
{
  UnitDefinition_t *m = UnitDefinition_create(2, 2);
  
  Unit_t *p = UnitDefinition_createUnit(m);

  fail_unless( UnitDefinition_getNumUnits(m) == 1);
  fail_unless( SBase_getLevel((SBase_t *) (p)) == 2 );
  fail_unless( SBase_getVersion((SBase_t *) (p)) == 2 );

  UnitDefinition_free(m);
}
END_TEST


Suite *
create_suite_UnitDefinition_newSetters (void)
{
  Suite *suite = suite_create("UnitDefinition_newSetters");
  TCase *tcase = tcase_create("UnitDefinition_newSetters");


  tcase_add_checked_fixture( tcase,
                             UnitDefinitionTest1_setup,
                             UnitDefinitionTest1_teardown );

  tcase_add_test( tcase, test_UnitDefinition_setId1                 );
  tcase_add_test( tcase, test_UnitDefinition_setName1               );
  tcase_add_test( tcase, test_UnitDefinition_setName2               );
  tcase_add_test( tcase, test_UnitDefinition_addUnit1               );
  tcase_add_test( tcase, test_UnitDefinition_addUnit2               );
  tcase_add_test( tcase, test_UnitDefinition_addUnit3               );
  tcase_add_test( tcase, test_UnitDefinition_addUnit4               );
  tcase_add_test( tcase, test_UnitDefinition_createUnit             );

  suite_add_tcase(suite, tcase);

  return suite;
}
