// -*- C++ -*- Time-stamp: <09/01/28 15:00:46 ptr>

/*
 * Copyright (c) 2004-2009
 * Petr Ovtchenkov
 *
 * Copyright (c) 2004-2008
 * Francois Dumont
 *
 * Licensed under the Academic Free License Version 3.0
 *
 */

//Has to be first for StackAllocator swap overload to be taken
//into account (at least using GCC 4.0.1)
#include "stack_allocator.h"

#include "slist_test.h"

#include <algorithm>
#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
#  include <slist>
#  if !defined (_STLP_USE_NO_IOSTREAMS)
#    include <sstream>
#  endif
#  include <iterator>
#  include <functional>
#endif

#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
using namespace std;
#endif

#if !defined (STLPORT) && defined(__GNUC__)
using namespace __gnu_cxx;
#endif

int EXAM_IMPL(slist_test::slist1)
{
#if defined (STLPORT) && !defined (_STLP_USE_NO_IOSTREAMS) && !defined (_STLP_NO_EXTENSIONS)
/*
original: xlxtss
reversed: sstxlx
removed: sstl
uniqued: stl
sorted: lst
*/

  char array [] = { 'x', 'l', 'x', 't', 's', 's' };
  ostringstream os;
  ostream_iterator<char> o(os,"");
  slist<char> str(array+0, array + 6);
  slist<char>::iterator i;
  //Check const_iterator construction from iterator
  slist<char>::const_iterator ci(i);
  slist<char>::const_iterator ci2(ci);
//  cout << "reversed: ";
  str.reverse();
  for(i = str.begin(); i != str.end(); i++)
    os << *i;
  stringbuf* buff=os.rdbuf();
  string result=buff->str();
  EXAM_CHECK(!strcmp(result.c_str(),"sstxlx"));

  //cout << "removed: ";
  str.remove('x');
  ostringstream os2;
  for(i = str.begin(); i != str.end(); i++)
    os2 << *i;
  buff=os2.rdbuf();
  result=buff->str();
  EXAM_CHECK(!strcmp(result.c_str(),"sstl"));


  //cout << "uniqued: ";
  str.unique();
  ostringstream os3;
  for(i = str.begin(); i != str.end(); i++)
    os3 << *i;
  buff=os3.rdbuf();
  result=buff->str();
  EXAM_CHECK(!strcmp(result.c_str(),"stl"));

  //cout << "sorted: ";
  str.sort();
  ostringstream os4;
  for(i = str.begin(); i != str.end(); i++)
    os4 << *i;
  buff = os4.rdbuf();
  result = buff->str();
  EXAM_CHECK(!strcmp(result.c_str(),"lst"));

  //A small compilation time check to be activated from time to time:
#  if 0
  {
    slist<char>::iterator sl_char_ite;
    slist<int>::iterator sl_int_ite;
    EXAM_CHECK( sl_char_ite != sl_int_ite );
  }
#  endif
#else
  throw exam::skip_exception();
#endif

  return EXAM_RESULT;
}

int EXAM_IMPL(slist_test::erase)
{
#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
  int array[] = { 0, 1, 2, 3, 4 };
  slist<int> sl(array, array + 5);
  slist<int>::iterator slit;

  slit = sl.erase(sl.begin());
  EXAM_CHECK( *slit == 1);

  ++slit++; ++slit;
  slit = sl.erase(sl.begin(), slit);
  EXAM_CHECK( *slit == 3 );

  sl.assign(array, array + 5);

  slit = sl.erase_after(sl.begin());
  EXAM_CHECK( *slit == 2 );

  slit = sl.begin(); ++slit; ++slit;
  slit = sl.erase_after(sl.begin(), slit);
  EXAM_CHECK( *slit == 3 );

  sl.erase_after(sl.before_begin());
  EXAM_CHECK( sl.front() == 3 );
#else
  throw exam::skip_exception();
#endif

  return EXAM_RESULT;
}

int EXAM_IMPL(slist_test::insert)
{
#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
  int array[] = { 0, 1, 2, 3, 4 };

  //insert
  {
    slist<int> sl;

    sl.insert(sl.begin(), 5);
    EXAM_CHECK( sl.front() == 5 );
    EXAM_CHECK( sl.size() == 1 );

    //debug mode check:
    //sl.insert(sl.before_begin(), array, array + 5);

    sl.insert(sl.begin(), array, array + 5);
    EXAM_CHECK( sl.size() == 6 );
    int i;
    slist<int>::iterator slit(sl.begin());
    for (i = 0; slit != sl.end(); ++slit, ++i) {
      EXAM_CHECK( *slit == i );
    }
  }

  //insert_after
  {
    slist<int> sl;

    //debug check:
    //sl.insert_after(sl.begin(), 5);

    sl.insert_after(sl.before_begin(), 5);
    EXAM_CHECK( sl.front() == 5 );
    EXAM_CHECK( sl.size() == 1 );

    sl.insert_after(sl.before_begin(), array, array + 5);
    EXAM_CHECK( sl.size() == 6 );
    int i;
    slist<int>::iterator slit(sl.begin());
    for (i = 0; slit != sl.end(); ++slit, ++i) {
      EXAM_CHECK( *slit == i );
    }
  }
#else
  throw exam::skip_exception();
#endif

  return EXAM_RESULT;
}

int EXAM_IMPL(slist_test::splice)
{
#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
  int array[] = { 0, 1, 2, 3, 4 };

  //splice
  {
    slist<int> sl1(array, array + 5);
    slist<int> sl2(array, array + 5);
    slist<int>::iterator slit;

    //a no op:
    sl1.splice(sl1.begin(), sl1, sl1.begin());
    EXAM_CHECK( sl1 == sl2 );

    slit = sl1.begin(); ++slit;
    //a no op:
    sl1.splice(slit, sl1, sl1.begin());
    EXAM_CHECK( sl1 == sl2 );

    sl1.splice(sl1.end(), sl1, sl1.begin());
    slit = sl1.begin();
    EXAM_CHECK( *(slit++) == 1 );
    EXAM_CHECK( *(slit++) == 2 );
    EXAM_CHECK( *(slit++) == 3 );
    EXAM_CHECK( *(slit++) == 4 );
    EXAM_CHECK( *slit == 0 );
    sl1.splice(sl1.begin(), sl1, slit);
    EXAM_CHECK( sl1 == sl2 );

    sl1.splice(sl1.begin(), sl2);
    size_t i;
    for (i = 0, slit = sl1.begin(); slit != sl1.end(); ++slit, ++i) {
      if (i == 5) i = 0;
      EXAM_CHECK( *slit == array[i] );
    }

    slit = sl1.begin();
    advance(slit, 5);
    EXAM_CHECK( *slit == 0 );
    sl2.splice(sl2.begin(), sl1, sl1.begin(), slit);
    EXAM_CHECK( sl1 == sl2 );

    slit = sl1.begin(); ++slit;
    sl1.splice(sl1.begin(), sl1, slit, sl1.end());
    slit = sl1.begin();
    EXAM_CHECK( *(slit++) == 1 );
    EXAM_CHECK( *(slit++) == 2 );
    EXAM_CHECK( *(slit++) == 3 );
    EXAM_CHECK( *(slit++) == 4 );
    EXAM_CHECK( *slit == 0 );

    // a no op
    sl2.splice(sl2.end(), sl2, sl2.begin(), sl2.end());
    for (i = 0, slit = sl2.begin(); slit != sl2.end(); ++slit, ++i) {
      EXAM_CHECK( i < 5 );
      EXAM_CHECK( *slit == array[i] );
    }

    slit = sl2.begin();
    advance(slit, 3);
    sl2.splice(sl2.end(), sl2, sl2.begin(), slit);
    slit = sl2.begin();
    EXAM_CHECK( *(slit++) == 3 );
    EXAM_CHECK( *(slit++) == 4 );
    EXAM_CHECK( *(slit++) == 0 );
    EXAM_CHECK( *(slit++) == 1 );
    EXAM_CHECK( *slit == 2 );
  }

  //splice_after
  {
    slist<int> sl1(array, array + 5);
    slist<int> sl2(array, array + 5);
    slist<int>::iterator slit;

    //a no op:
    sl1.splice_after(sl1.begin(), sl1, sl1.begin());
    EXAM_CHECK( sl1 == sl2 );

    sl1.splice_after(sl1.before_begin(), sl1, sl1.begin());
    slit = sl1.begin();
    EXAM_CHECK( *(slit++) == 1 );
    EXAM_CHECK( *(slit++) == 0 );
    EXAM_CHECK( *(slit++) == 2 );
    EXAM_CHECK( *(slit++) == 3 );
    EXAM_CHECK( *slit == 4 );
    sl1.splice_after(sl1.before_begin(), sl1, sl1.begin());
    EXAM_CHECK( sl1 == sl2 );

    sl1.splice_after(sl1.before_begin(), sl2);
    size_t i;
    for (i = 0, slit = sl1.begin(); slit != sl1.end(); ++slit, ++i) {
      if (i == 5) i = 0;
      EXAM_CHECK( *slit == array[i] );
    }

    slit = sl1.begin();
    advance(slit, 4);
    EXAM_CHECK( *slit == 4 );
    sl2.splice_after(sl2.before_begin(), sl1, sl1.before_begin(), slit);
    EXAM_CHECK( sl1 == sl2 );

    sl1.splice_after(sl1.before_begin(), sl1, sl1.begin(), sl1.previous(sl1.end()));
    slit = sl1.begin();
    EXAM_CHECK( *(slit++) == 1 );
    EXAM_CHECK( *(slit++) == 2 );
    EXAM_CHECK( *(slit++) == 3 );
    EXAM_CHECK( *(slit++) == 4 );
    EXAM_CHECK( *slit == 0 );

    // a no op
    sl2.splice_after(sl2.before_begin(), sl2, sl2.before_begin(), sl2.previous(sl2.end()));
    for (i = 0, slit = sl2.begin(); slit != sl2.end(); ++slit, ++i) {
      EXAM_CHECK( i < 5 );
      EXAM_CHECK( *slit == array[i] );
    }

    slit = sl2.begin();
    advance(slit, 2);
    sl2.splice_after(sl2.previous(sl2.end()), sl2, sl2.before_begin(), slit);
    slit = sl2.begin();
    EXAM_CHECK( *(slit++) == 3 );
    EXAM_CHECK( *(slit++) == 4 );
    EXAM_CHECK( *(slit++) == 0 );
    EXAM_CHECK( *(slit++) == 1 );
    EXAM_CHECK( *slit == 2 );
  }
#else
  throw exam::skip_exception();
#endif

  return EXAM_RESULT;
}


int EXAM_IMPL(slist_test::allocator_with_state)
{
#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
  char buf1[1024];
  StackAllocator<int> stack1(buf1, buf1 + sizeof(buf1));

  char buf2[1024];
  StackAllocator<int> stack2(buf2, buf2 + sizeof(buf2));

  typedef slist<int, StackAllocator<int> > SlistInt;
  {
    SlistInt slint1(10, 0, stack1);
    SlistInt slint1Cpy(slint1);

    SlistInt slint2(10, 1, stack2);
    SlistInt slint2Cpy(slint2);

    slint1.swap(slint2);

    EXAM_CHECK( slint1.get_allocator().swaped() );
    EXAM_CHECK( slint2.get_allocator().swaped() );

    EXAM_CHECK( slint1 == slint2Cpy );
    EXAM_CHECK( slint2 == slint1Cpy );
    EXAM_CHECK( slint1.get_allocator() == stack2 );
    EXAM_CHECK( slint2.get_allocator() == stack1 );
  }
  EXAM_CHECK( stack1.ok() );
  EXAM_CHECK( stack2.ok() );
  stack1.reset(); stack2.reset();

  {
    SlistInt slint1(stack1);
    SlistInt slint1Cpy(slint1);

    SlistInt slint2(10, 1, stack2);
    SlistInt slint2Cpy(slint2);

    slint1.swap(slint2);

    EXAM_CHECK( slint1.get_allocator().swaped() );
    EXAM_CHECK( slint2.get_allocator().swaped() );

    EXAM_CHECK( slint1 == slint2Cpy );
    EXAM_CHECK( slint2 == slint1Cpy );
    EXAM_CHECK( slint1.get_allocator() == stack2 );
    EXAM_CHECK( slint2.get_allocator() == stack1 );
  }
  EXAM_CHECK( stack1.ok() );
  EXAM_CHECK( stack2.ok() );
  stack1.reset(); stack2.reset();

  {
    SlistInt slint1(10, 0, stack1);
    SlistInt slint1Cpy(slint1);

    SlistInt slint2(stack2);
    SlistInt slint2Cpy(slint2);

    slint1.swap(slint2);

    EXAM_CHECK( slint1.get_allocator().swaped() );
    EXAM_CHECK( slint2.get_allocator().swaped() );

    EXAM_CHECK( slint1 == slint2Cpy );
    EXAM_CHECK( slint2 == slint1Cpy );
    EXAM_CHECK( slint1.get_allocator() == stack2 );
    EXAM_CHECK( slint2.get_allocator() == stack1 );
  }
  EXAM_CHECK( stack1.ok() );
  EXAM_CHECK( stack2.ok() );
  stack1.reset(); stack2.reset();

  //splice(iterator, slist)
  {
    SlistInt slint1(10, 0, stack1);
    SlistInt slint2(10, 1, stack2);

    slint1.splice(slint1.begin(), slint2);
    EXAM_CHECK( slint1.size() == 20 );
    EXAM_CHECK( slint2.empty() );
  }
  EXAM_CHECK( stack1.ok() );
  EXAM_CHECK( stack2.ok() );
  stack1.reset(); stack2.reset();

  //splice(iterator, slist, iterator)
  {
    SlistInt slint1(10, 0, stack1);
    SlistInt slint2(10, 1, stack2);

    slint1.splice(slint1.begin(), slint2, slint2.begin());
    EXAM_CHECK( slint1.size() == 11 );
    EXAM_CHECK( slint2.size() == 9 );
  }
  EXAM_CHECK( stack1.ok() );
  EXAM_CHECK( stack2.ok() );
  stack1.reset(); stack2.reset();

  //splice(iterator, slist, iterator, iterator)
  {
    SlistInt slint1(10, 0, stack1);
    SlistInt slint2(10, 1, stack2);

    SlistInt::iterator lit(slint2.begin());
    advance(lit, 5);
    slint1.splice(slint1.begin(), slint2, slint2.begin(), lit);
    EXAM_CHECK( slint1.size() == 15 );
    EXAM_CHECK( slint2.size() == 5 );
  }
  EXAM_CHECK( stack1.ok() );
  EXAM_CHECK( stack2.ok() );
  stack1.reset(); stack2.reset();

  //splice_after(iterator, slist)
  {
    SlistInt slint1(10, 0, stack1);
    SlistInt slint2(10, 1, stack2);

    slint1.splice_after(slint1.before_begin(), slint2);
    EXAM_CHECK( slint1.size() == 20 );
    EXAM_CHECK( slint2.empty() );
  }
  EXAM_CHECK( stack1.ok() );
  EXAM_CHECK( stack2.ok() );
  stack1.reset(); stack2.reset();

  //splice_after(iterator, slist, iterator)
  {
    SlistInt slint1(10, 0, stack1);
    SlistInt slint2(10, 1, stack2);

    slint1.splice_after(slint1.before_begin(), slint2, slint2.before_begin());
    EXAM_CHECK( slint1.size() == 11 );
    EXAM_CHECK( slint2.size() == 9 );
  }
  EXAM_CHECK( stack1.ok() );
  EXAM_CHECK( stack2.ok() );
  stack1.reset(); stack2.reset();

  //splice_after(iterator, slist, iterator, iterator)
  {
    SlistInt slint1(10, 0, stack1);
    SlistInt slint2(10, 1, stack2);

    SlistInt::iterator lit(slint2.begin());
    advance(lit, 4);
    slint1.splice_after(slint1.before_begin(), slint2, slint2.before_begin(), lit);
    EXAM_CHECK( slint1.size() == 15 );
    EXAM_CHECK( slint2.size() == 5 );
  }
  EXAM_CHECK( stack1.ok() );
  EXAM_CHECK( stack2.ok() );
  stack1.reset(); stack2.reset();

  //merge(slist)
  {
    SlistInt slint1(10, 0, stack1);
    SlistInt slint2(10, 1, stack2);

    SlistInt slintref(stack2);
    slintref.insert_after(slintref.before_begin(), 10, 1);
    slintref.insert_after(slintref.before_begin(), 10, 0);

    slint1.merge(slint2);
    EXAM_CHECK( slint1.size() == 20 );
    EXAM_CHECK( slint1 == slintref );
    EXAM_CHECK( slint2.empty() );
  }
  EXAM_CHECK( stack1.ok() );
  EXAM_CHECK( stack2.ok() );

  //merge(slist, predicate)
#  if (!defined (STLPORT) ) && \
      (!defined (_MSC_VER) || (_MSC_VER >= 1300))
  {
    SlistInt slint1(10, 0, stack1);
    SlistInt slint2(10, 1, stack2);

    SlistInt slintref(stack2);
    slintref.insert_after(slintref.before_begin(), 10, 0);
    slintref.insert_after(slintref.before_begin(), 10, 1);

    slint1.merge(slint2, greater<int>());
    EXAM_CHECK( slint1.size() == 20 );
    EXAM_CHECK( slint1 == slintref );
    EXAM_CHECK( slint2.empty() );
  }
  EXAM_CHECK( stack1.ok() );
  EXAM_CHECK( stack2.ok() );

  //sort
  {
    //This is rather a compile time test.
    //We check that sort implementation is correct when list is instanciated
    //with an allocator that do not have a default constructor.
    SlistInt slint1(stack1);
    slint1.push_front(1);
    slint1.insert_after(slint1.before_begin(), 10, 0);
    greater<int> gt;
    slint1.sort(gt);
    EXAM_CHECK( slint1.front() == 1 );
    slint1.sort();
    SlistInt::iterator slit(slint1.begin());
    advance(slit, 10);
    EXAM_CHECK( *slit == 1 );
  }
#  endif
#else
  throw exam::skip_exception();
#endif

  return EXAM_RESULT;
}

#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) && \
    (!defined (_STLP_USE_PTR_SPECIALIZATIONS) || defined (_STLP_CLASS_PARTIAL_SPECIALIZATION))
#  if !defined (__DMC__)
/* Simple compilation test: Check that nested types like iterator
 * can be access even if type used to instanciate container is not
 * yet completely defined.
 */
class IncompleteClass
{
  slist<IncompleteClass> instances;
  typedef slist<IncompleteClass>::iterator it;
};
#  endif
#endif
