testIOS.cxx 4.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*============================================================================
  KWSys - Kitware System Library
  Copyright 2000-2009 Kitware, Inc., Insight Software Consortium

  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.

  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the License for more information.
============================================================================*/
12 13
#include "kwsysPrivate.h"
#include KWSYS_HEADER(stl/vector)
14

15 16 17 18 19 20
// Work-around CMake dependency scanning limitation.  This must
// duplicate the above list of headers.
#if 0
# include "kwsys_stl_vector.h.in"
#endif

21 22 23
#include <sstream>
#include <fstream>
#include <iostream>
24 25
#include <string.h> /* strlen */

26
int testIOS(int, char*[])
27
{
28
  std::ostringstream ostr;
29 30 31
  const char hello[] = "hello";
  ostr << hello;
  if(ostr.str() != hello)
32
    {
33
    std::cerr << "failed to write hello to ostr" << std::endl;
34 35
    return 1;
    }
36
  const char world[] = "world";
37
  std::ostringstream ostr2;
38
  ostr2.write( hello, strlen(hello) ); /* I could do sizeof */
39 40 41 42
  ostr2.put( '\0' );
  ostr2.write( world, strlen(world) );
  if(ostr2.str().size() !=  strlen(hello) + 1 + strlen(world) )
    {
43
    std::cerr << "failed to write hello to ostr2" << std::endl;
44 45
    return 1;
    }
46
  static const unsigned char array[] = { 0xff,0x4f,0xff,0x51,0x00,0x29,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x07,0x01,0x01,0xff,0x52,0x00,0x0c,0x00,0x00,0x00,0x01,0x00,0x05,0x04,0x04,0x00,0x01,0xff,0x5c,0x00,0x13,0x40,0x40,0x48,0x48,0x50,0x48,0x48,0x50,0x48,0x48,0x50,0x48,0x48,0x50,0x48,0x48,0x50,0xff,0x64,0x00,0x2c,0x00,0x00,0x43,0x72,0x65,0x61,0x74,0x65,0x64,0x20,0x62,0x79,0x20,0x49,0x54,0x4b,0x2f,0x47,0x44,0x43,0x4d,0x2f,0x4f,0x70,0x65,0x6e,0x4a,0x50,0x45,0x47,0x20,0x76,0x65,0x72,0x73,0x69,0x6f,0x6e,0x20,0x31,0x2e,0x30,0xff,0x90,0x00,0x0a,0x00,0x00,0x00,0x00,0x06,0x2c,0x00,0x01,0xff,0x93,0xcf,0xb0,0x18,0x08,0x7f,0xc6,0x99,0xbf,0xff,0xc0,0xf8,0xc1,0xc1,0xf3,0x05,0x81,0xf2,0x83,0x0a,0xa5,0xff,0x10,0x90,0xbf,0x2f,0xff,0x04,0xa8,0x7f,0xc0,0xf8,0xc4,0xc1,0xf3,0x09,0x81,0xf3,0x0c,0x19,0x34 };
47
  const size_t narray = sizeof(array); // 180
48
  std::stringstream strstr;
49
  strstr.write( (char*)array, narray );
50
  //strstr.seekp( narray / 2 ); // set position of put pointer in mid string
51 52
  if(strstr.str().size() != narray )
    {
53
    std::cerr << "failed to write array to strstr" << std::endl;
54 55 56
    return 1;
    }

57
  std::istringstream istr(" 10 20 str ");
58 59 60 61 62 63
  kwsys_stl::string s;
  int x;
  if(istr >> x)
    {
    if(x != 10)
      {
64
      std::cerr << "x != 10" << std::endl;
65 66 67 68 69
      return 1;
      }
    }
  else
    {
70
    std::cerr << "Failed to read 10 from istr" << std::endl;
71 72 73 74 75 76
    return 1;
    }
  if(istr >> x)
    {
    if(x != 20)
      {
77
      std::cerr << "x != 20" << std::endl;
78 79 80 81 82
      return 1;
      }
    }
  else
    {
83
    std::cerr << "Failed to read 20 from istr" << std::endl;
84 85 86 87 88 89
    return 1;
    }
  if(istr >> s)
    {
    if(s != "str")
      {
90
      std::cerr << "s != \"str\"" << std::endl;
91 92 93 94 95
      return 1;
      }
    }
  else
    {
96
    std::cerr << "Failed to read str from istr" << std::endl;
97 98 99 100
    return 1;
    }
  if(istr >> s)
    {
101
    std::cerr << "Able to read past end of stream" << std::endl;
102 103
    return 1;
    }
104 105 106
  else
    {
    // Clear the failure.
107 108
    istr.clear(istr.rdstate() & ~std::ios::eofbit);
    istr.clear(istr.rdstate() & ~std::ios::failbit);
109
    }
110 111 112 113 114
  istr.str("30");
  if(istr >> x)
    {
    if(x != 30)
      {
115
      std::cerr << "x != 30" << std::endl;
116 117 118 119 120
      return 1;
      }
    }
  else
    {
121
    std::cerr << "Failed to read 30 from istr" << std::endl;
122 123 124
    return 1;
    }

125
  std::stringstream sstr;
126 127 128 129 130
  sstr << "40 str2";
  if(sstr >> x)
    {
    if(x != 40)
      {
131
      std::cerr << "x != 40" << std::endl;
132 133 134 135 136
      return 1;
      }
    }
  else
    {
137
    std::cerr << "Failed to read 40 from sstr" << std::endl;
138 139 140 141 142 143
    return 1;
    }
  if(sstr >> s)
    {
    if(s != "str2")
      {
144
      std::cerr << "s != \"str2\"" << std::endl;
145 146 147 148 149
      return 1;
      }
    }
  else
    {
150
    std::cerr << "Failed to read str2 from sstr" << std::endl;
151 152 153
    return 1;
    }

154 155 156
  // Just try to compile this.
  if(x == 12345)
    {
157 158
    std::ifstream fin("/does_not_exist",
                      std::ios::in | std::ios::binary);
159 160
    }

161
  std::cout << "IOS tests passed" << std::endl;
162 163
  return 0;
}