testIOS.cxx 4.16 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing#kwsys for details.  */
3
#include "kwsysPrivate.h"
Brad King's avatar
Brad King committed
4
#include KWSYS_HEADER(Configure.hxx)
5

6 7
#include <fstream>
#include <iostream>
8
#include <sstream>
9
#include <string.h> /* strlen */
10
#include <vector>
11

12 13 14
// Work-around CMake dependency scanning limitation.  This must
// duplicate the above list of headers.
#if 0
15
#include "Configure.hxx.in"
16 17
#endif

18
int testIOS(int, char* [])
19
{
20
  std::ostringstream ostr;
21 22
  const char hello[] = "hello";
  ostr << hello;
23
  if (ostr.str() != hello) {
24
    std::cerr << "failed to write hello to ostr" << std::endl;
25
    return 1;
26
  }
27
  const char world[] = "world";
28
  std::ostringstream ostr2;
29 30 31 32
  ostr2.write(hello, strlen(hello)); /* I could do sizeof */
  ostr2.put('\0');
  ostr2.write(world, strlen(world));
  if (ostr2.str().size() != strlen(hello) + 1 + strlen(world)) {
33
    std::cerr << "failed to write hello to ostr2" << std::endl;
34
    return 1;
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  }
  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
  };
53
  const size_t narray = sizeof(array); // 180
54
  std::stringstream strstr;
55 56 57
  strstr.write((char*)array, narray);
  // strstr.seekp( narray / 2 ); // set position of put pointer in mid string
  if (strstr.str().size() != narray) {
58
    std::cerr << "failed to write array to strstr" << std::endl;
59
    return 1;
60
  }
61

62
  std::istringstream istr(" 10 20 str ");
Brad King's avatar
Brad King committed
63
  std::string s;
64
  int x;
65 66
  if (istr >> x) {
    if (x != 10) {
67
      std::cerr << "x != 10" << std::endl;
68 69
      return 1;
    }
70
  } else {
71
    std::cerr << "Failed to read 10 from istr" << std::endl;
72
    return 1;
73 74 75
  }
  if (istr >> x) {
    if (x != 20) {
76
      std::cerr << "x != 20" << std::endl;
77 78
      return 1;
    }
79
  } else {
80
    std::cerr << "Failed to read 20 from istr" << std::endl;
81
    return 1;
82 83 84
  }
  if (istr >> s) {
    if (s != "str") {
85
      std::cerr << "s != \"str\"" << std::endl;
86 87
      return 1;
    }
88
  } else {
89
    std::cerr << "Failed to read str from istr" << std::endl;
90
    return 1;
91 92
  }
  if (istr >> s) {
93
    std::cerr << "Able to read past end of stream" << std::endl;
94
    return 1;
95
  } else {
96
    // Clear the failure.
97 98
    istr.clear(istr.rdstate() & ~std::ios::eofbit);
    istr.clear(istr.rdstate() & ~std::ios::failbit);
99
  }
100
  istr.str("30");
101 102
  if (istr >> x) {
    if (x != 30) {
103
      std::cerr << "x != 30" << std::endl;
104 105
      return 1;
    }
106
  } else {
107
    std::cerr << "Failed to read 30 from istr" << std::endl;
108
    return 1;
109
  }
110

111
  std::stringstream sstr;
112
  sstr << "40 str2";
113 114
  if (sstr >> x) {
    if (x != 40) {
115
      std::cerr << "x != 40" << std::endl;
116 117
      return 1;
    }
118
  } else {
119
    std::cerr << "Failed to read 40 from sstr" << std::endl;
120
    return 1;
121 122 123
  }
  if (sstr >> s) {
    if (s != "str2") {
124
      std::cerr << "s != \"str2\"" << std::endl;
125 126
      return 1;
    }
127
  } else {
128
    std::cerr << "Failed to read str2 from sstr" << std::endl;
129
    return 1;
130
  }
131

132
  // Just try to compile this.
133 134 135
  if (x == 12345) {
    std::ifstream fin("/does_not_exist", std::ios::in | std::ios::binary);
  }
136

137
  std::cout << "IOS tests passed" << std::endl;
138 139
  return 0;
}