153537Sbrian// 2000-08-17 Benjamin Kosnik <bkoz@cygnus.com>
280728Sbrian
353537Sbrian// Copyright (C) 2000-2015 Free Software Foundation, Inc.
453537Sbrian//
553537Sbrian// This file is part of the GNU ISO C++ Library.  This library is free
653537Sbrian// software; you can redistribute it and/or modify it under the
753537Sbrian// terms of the GNU General Public License as published by the
853537Sbrian// Free Software Foundation; either version 3, or (at your option)
953537Sbrian// any later version.
1053537Sbrian
1153537Sbrian// This library is distributed in the hope that it will be useful,
1253537Sbrian// but WITHOUT ANY WARRANTY; without even the implied warranty of
1353537Sbrian// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1453537Sbrian// GNU General Public License for more details.
1553537Sbrian
1653537Sbrian// You should have received a copy of the GNU General Public License along
1753537Sbrian// with this library; see the file COPYING3.  If not see
1853537Sbrian// <http://www.gnu.org/licenses/>.
1953537Sbrian
2053537Sbrian// 22.2.1.5 - Template class codecvt [lib.locale.codecvt]
2153537Sbrian
2253537Sbrian#include <locale>
2353537Sbrian#include <cstring>
2453537Sbrian#include <testsuite_hooks.h>
2553537Sbrian
2653537Sbrian// Need to explicitly set the state(mbstate_t) to zero.
2753537Sbrian// How to do this is not specified by the ISO C99 standard, so we
2853537Sbrian// might need to add some operators to make the intuitive case
2953537Sbrian// work:
3053537Sbrian//   w_codecvt::state_type state00;
3153537Sbrian//   state00 = 0;
3253537Sbrian// or, can use this explicit "C" initialization:
3353537Sbrian//   w_codecvt::state_type state01 = {0, 0};
3453537Sbrian// .. except Ulrich says: Use memset. Always use memset. Feel the force...
3553537Sbrianvoid
3653537Sbrianzero_state(std::mbstate_t& state)
3753537Sbrian{ std::memset(&state, 0, sizeof(std::mbstate_t)); }
3853537Sbrian
3953537Sbrian// Required instantiation
4053537Sbrian// codecvt<wchar_t, char, mbstate_t>
4153537Sbrian//
4253537Sbrian// Baseline test for "C" locale
4353537Sbrianvoid test01()
4453537Sbrian{
4553537Sbrian  using namespace std;
4653537Sbrian  typedef codecvt<wchar_t, char, mbstate_t> 	w_codecvt;
4753537Sbrian  typedef codecvt_base::result			result;
4866602Sbrian  typedef wchar_t				int_type;
4953537Sbrian  typedef char					ext_type;
5053537Sbrian  typedef char_traits<wchar_t>			int_traits;
5153537Sbrian
5253537Sbrian  bool test __attribute__((unused)) = true;
5353537Sbrian  const ext_type* 	e_lit = "black pearl jasmine tea";
5453537Sbrian  const ext_type*       efrom_next;
5553537Sbrian  const int_type* 	i_lit = L"black pearl jasmine tea";
5653537Sbrian  size_t 		size = strlen(e_lit);
5753537Sbrian  int_type* 		i_arr = new int_type[size + 1];
5853537Sbrian  int_type* 		i_ref = new int_type[size + 1];
5953537Sbrian  wmemset(i_arr, static_cast<wchar_t>(0xdeadbeef), size + 1);
6053537Sbrian  wmemset(i_ref, static_cast<wchar_t>(0xdeadbeef), size + 1);
6153537Sbrian  int_type*		ito_next;
6253537Sbrian
6353537Sbrian  locale 		loc;
6453537Sbrian  const w_codecvt* 	cvt = &use_facet<w_codecvt>(loc);
6553537Sbrian
6669582Sbrian  // in
6769582Sbrian  w_codecvt::state_type state01;
6853537Sbrian  zero_state(state01);
6953537Sbrian  result r1 = cvt->in(state01, e_lit, e_lit + size, efrom_next,
7053537Sbrian		      i_arr, i_arr + size, ito_next);
7180728Sbrian  VERIFY( r1 == codecvt_base::ok );
7253609Sbrian  VERIFY( efrom_next == e_lit + size );
7353537Sbrian  VERIFY( ito_next == i_arr + size );
7453537Sbrian  VERIFY( !int_traits::compare(i_arr, i_lit, size) );
7553537Sbrian  VERIFY( !int_traits::compare(ito_next, i_ref, 1) );
7653537Sbrian
7769582Sbrian  delete [] i_arr;
7853537Sbrian  delete [] i_ref;
7969582Sbrian}
8053537Sbrian
8153537Sbrianint main ()
8253537Sbrian{
8353537Sbrian  test01();
8453537Sbrian  return 0;
8553537Sbrian}
8653537Sbrian