END_TEST


START_TEST (test_XMLNamespaces_add)
{
  fail_unless( XMLNamespaces_getLength(NS) == 0 );
  fail_unless( XMLNamespaces_isEmpty(NS) == 1 );

  XMLNamespaces_add(NS, "http://test1.org/", "test1");
  fail_unless( XMLNamespaces_getLength(NS) == 1 );
  fail_unless( XMLNamespaces_isEmpty(NS) == 0 );

  XMLNamespaces_add(NS, "http://test2.org/", "test2");
  fail_unless( XMLNamespaces_getLength(NS) == 2 );
  fail_unless( XMLNamespaces_isEmpty(NS) == 0 );

  XMLNamespaces_add(NS, "http://test1.org/", "test1a");
  fail_unless( XMLNamespaces_getLength(NS) == 3 );
  fail_unless( XMLNamespaces_isEmpty(NS) == 0 );

  XMLNamespaces_add(NS, "http://test1.org/", "test1a");
  fail_unless( XMLNamespaces_getLength(NS) == 3 );
  fail_unless( XMLNamespaces_isEmpty(NS) == 0 );


  fail_if( XMLNamespaces_getIndex(NS, "http://test1.org/") == -1);
}
END_TEST


START_TEST (test_XMLNamespaces_add1)
{
  fail_unless( XMLNamespaces_getLength(NS) == 0 );
  fail_unless( XMLNamespaces_isEmpty(NS) == 1 );

  int i = XMLNamespaces_add(NS, "http://test1.org/", "test1");

  fail_unless( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless( XMLNamespaces_getLength(NS) == 1 );
  fail_unless( XMLNamespaces_isEmpty(NS) == 0 );
}
END_TEST

START_TEST (test_XMLNamespaces_accessWithNULL)
{
  fail_unless( XMLNamespaces_add(NULL, NULL, NULL) == LIBSBML_INVALID_OBJECT);
  fail_unless( XMLNamespaces_clear(NULL) == LIBSBML_OPERATION_FAILED);
  fail_unless( XMLNamespaces_clone(NULL) == NULL);

  XMLNamespaces_free(NULL);

  fail_unless( XMLNamespaces_getIndex(NULL, NULL) == -1);
  fail_unless( XMLNamespaces_getIndexByPrefix(NULL, NULL) == -1);
  fail_unless( XMLNamespaces_getLength(NULL) == 0);
  fail_unless( XMLNamespaces_getPrefix(NULL, 0) == NULL);
  fail_unless( XMLNamespaces_getPrefixByURI(NULL, NULL) == NULL);
  fail_unless( XMLNamespaces_getURI(NULL, 0) == NULL);
  fail_unless( XMLNamespaces_getURIByPrefix(NULL, NULL) == NULL);
  fail_unless( XMLNamespaces_hasNS(NULL, NULL, NULL) == 0);
  fail_unless( XMLNamespaces_hasPrefix(NULL, NULL) == 0);
  fail_unless( XMLNamespaces_hasURI(NULL, NULL) == 0);
  fail_unless( XMLNamespaces_isEmpty(NULL) == 1);
  fail_unless( XMLNamespaces_remove(NULL, 0) == LIBSBML_INVALID_OBJECT);
  fail_unless( XMLNamespaces_removeByPrefix(NULL, NULL) == LIBSBML_INVALID_OBJECT);
}
END_TEST


START_TEST (test_XMLNamespaces_add1)
{
  char * test;
  fail_unless( XMLNamespaces_getLength(NS) == 0 );
  fail_unless( XMLNamespaces_isEmpty(NS) == 1 );

  int i = XMLNamespaces_add(NS, "http://test1.org/", "test1");

  fail_unless( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless( XMLNamespaces_getLength(NS) == 1 );
  fail_unless( XMLNamespaces_isEmpty(NS) == 0 );

  test = XMLNamespaces_getPrefix(NS, 0);
  fail_unless( strcmp(test, "test1") == 0 );
  free(test);

  test = XMLNamespaces_getPrefixByURI(NS, "http://test1.org/");
  fail_unless( strcmp(test, "test1") == 0 );
  free(test);

  test = XMLNamespaces_getURI(NS, 0);
  fail_unless( strcmp(test, "http://test1.org/") == 0 );
  free(test);


  test = XMLNamespaces_getURIByPrefix(NS, "test1");
  fail_unless( strcmp(test, "http://test1.org/") == 0 );
  free(test);

  // add with existing prefix
  i = XMLNamespaces_add(NS, "http://test2.org/", "test1");

  fail_unless( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless( XMLNamespaces_getLength(NS) == 1 );
  fail_unless( XMLNamespaces_isEmpty(NS) == 0 );
  
  test = XMLNamespaces_getPrefix(NS, 0);
  fail_unless( strcmp(test, "test1") == 0 );
  free(test);

  test = XMLNamespaces_getPrefixByURI(NS, "http://test2.org/");
  fail_unless( strcmp(test, "test1") == 0 );
  free(test);

  test = XMLNamespaces_getURI(NS, 0);
  fail_unless( strcmp(test, "http://test2.org/") == 0 );
  free(test);


  test = XMLNamespaces_getURIByPrefix(NS, "test1");
  fail_unless( strcmp(test, "http://test2.org/") == 0 );
  free(test);

  // add 
  i = XMLNamespaces_add(NS, "http://test.org/", "");

  fail_unless( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless( XMLNamespaces_getLength(NS) == 2 );
  fail_unless( XMLNamespaces_isEmpty(NS) == 0 );

  fail_unless( XMLNamespaces_getPrefix(NS, 1) == NULL );
  fail_unless( XMLNamespaces_getPrefixByURI(NS, "http://test.org/") == NULL);

  test = XMLNamespaces_getURI(NS, 1);
  fail_unless( strcmp(test, "http://test.org/") == 0 );
  free(test);


  // add repeat with empty prefix
  i = XMLNamespaces_add(NS, "http://test3.org/", "");

  fail_unless( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless( XMLNamespaces_getLength(NS) == 2 );
  fail_unless( XMLNamespaces_isEmpty(NS) == 0 );
  fail_unless( XMLNamespaces_getPrefix(NS, 1) == NULL );
  fail_unless( XMLNamespaces_getPrefixByURI(NS, "http://test3.org/") == NULL);

  test = XMLNamespaces_getURI(NS, 1);
  fail_unless( strcmp(test, "http://test3.org/") == 0 );
  free(test);


  // add sbml ns
  i = XMLNamespaces_add(NS, "http://www.sbml.org/sbml/level1", "");

  fail_unless( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless( XMLNamespaces_getLength(NS) == 2 );
  fail_unless( XMLNamespaces_isEmpty(NS) == 0 );
  fail_unless( XMLNamespaces_getPrefix(NS, 1) == NULL );
  fail_unless( XMLNamespaces_getPrefixByURI(NS, 
                     "http://www.sbml.org/sbml/level1") == NULL);

  test = XMLNamespaces_getURI(NS, 1);
  fail_unless( strcmp(test, "http://www.sbml.org/sbml/level1") == 0 );
  free(test);


  // add a repeat of the sbml prefix ns
  i = XMLNamespaces_add(NS, "http://test_sbml_prefix/", "");

  fail_unless( i == LIBSBML_OPERATION_FAILED);
  fail_unless( XMLNamespaces_getLength(NS) == 2 );
  fail_unless( XMLNamespaces_isEmpty(NS) == 0 );
  fail_unless( XMLNamespaces_getPrefix(NS, 1) == NULL );
  fail_unless( XMLNamespaces_getPrefixByURI(NS, 
                     "http://www.sbml.org/sbml/level1") == NULL);
  
  test = XMLNamespaces_getURI(NS, 1);
  fail_unless( strcmp(test, "http://www.sbml.org/sbml/level1") == 0 );
  free(test);


  // add repeat sbml ns uri
  i = XMLNamespaces_add(NS, "http://www.sbml.org/sbml/level1", "newprefix");

  fail_unless( i == LIBSBML_OPERATION_SUCCESS);
  fail_unless( XMLNamespaces_getLength(NS) == 3 );
  fail_unless( XMLNamespaces_isEmpty(NS) == 0 );
  
  test = XMLNamespaces_getPrefix(NS, 2);
  fail_unless( strcmp(test, "newprefix") == 0 );
  free(test);

  // this fails because the search finds the uri with the empty prefix first
  fail_unless( XMLNamespaces_getPrefixByURI(NS, 
                     "http://www.sbml.org/sbml/level1") == NULL);

  test = XMLNamespaces_getURI(NS, 2);
  fail_unless( strcmp(test, "http://www.sbml.org/sbml/level1") == 0 );
  free(test);


  // add repeat prefix
  i = XMLNamespaces_add(NS, "http://www.foo", "newprefix");

  fail_unless( i == LIBSBML_OPERATION_FAILED);
  fail_unless( XMLNamespaces_getLength(NS) == 3 );
  fail_unless( XMLNamespaces_isEmpty(NS) == 0 );

  test = XMLNamespaces_getPrefix(NS, 2);
  fail_unless( strcmp(test, "newprefix") == 0 );
  free(test);

  test = XMLNamespaces_getURI(NS, 2);
  fail_unless( strcmp(test, "http://www.sbml.org/sbml/level1") == 0 );
  free(test);

  // change sbml ns uri - it will not do this
  i = XMLNamespaces_add(NS, "http://www.sbml.org/sbml/level2", "newprefix");

  fail_unless( i == LIBSBML_OPERATION_FAILED);
  fail_unless( XMLNamespaces_getLength(NS) == 3 );
  fail_unless( XMLNamespaces_isEmpty(NS) == 0 );
  test = XMLNamespaces_getPrefix(NS, 2);
  fail_unless( strcmp(test, "newprefix") == 0 );
  free(test);

  test = XMLNamespaces_getURI(NS, 2);
  fail_unless( strcmp(test, "http://www.sbml.org/sbml/level1") == 0 );
  free(test);
}