string_stream_test.cc 1.74 KB
Newer Older
SWHL's avatar
SWHL committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#define BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
#define BOOST_TEST_MODULE FakeOStreamTest

#include "string_stream.hh"
#include <boost/test/unit_test.hpp>
#include <boost/lexical_cast.hpp>

#include <cstddef>
#include <limits>

namespace util { namespace {

template <class T> void TestEqual(const T value) {
  StringStream strme;
  strme << value;
  BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), strme.str());
}

template <class T> void TestCorners() {
  TestEqual(std::numeric_limits<T>::max());
  TestEqual(std::numeric_limits<T>::min());
  TestEqual(static_cast<T>(0));
  TestEqual(static_cast<T>(-1));
  TestEqual(static_cast<T>(1));
}

BOOST_AUTO_TEST_CASE(Integer) {
  TestCorners<char>();
  TestCorners<signed char>();
  TestCorners<unsigned char>();

  TestCorners<short>();
  TestCorners<signed short>();
  TestCorners<unsigned short>();

  TestCorners<int>();
  TestCorners<unsigned int>();
  TestCorners<signed int>();

  TestCorners<long>();
  TestCorners<unsigned long>();
  TestCorners<signed long>();

  TestCorners<long long>();
  TestCorners<unsigned long long>();
  TestCorners<signed long long>();

  TestCorners<std::size_t>();
}

enum TinyEnum { EnumValue };

BOOST_AUTO_TEST_CASE(EnumCase) {
  TestEqual(EnumValue);
}

BOOST_AUTO_TEST_CASE(Strings) {
  TestEqual("foo");
  const char *a = "bar";
  TestEqual(a);
  StringPiece piece("abcdef");
  TestEqual(piece);
  TestEqual(StringPiece());

  char non_const[3];
  non_const[0] = 'b';
  non_const[1] = 'c';
  non_const[2] = 0;

  StringStream out;
  out << "a" << non_const << 'c';
  BOOST_CHECK_EQUAL("abcc", out.str());

  // Now test as a separate object.
  StringStream stream;
  stream << "a" << non_const << 'c' << piece;
  BOOST_CHECK_EQUAL("abccabcdef", stream.str());
}

}} // namespaces