comparison ext/UnitTest++/src/MemoryOutStream.cpp @ 37:0d325e9d5953

added unittest++ files into ext. Not hooked into build scripts yet
author jasoka@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 12 Jul 2008 12:00:57 +0000
parents
children
comparison
equal deleted inserted replaced
36:6f1227f4932b 37:0d325e9d5953
1 #include "MemoryOutStream.h"
2
3 #ifndef UNITTEST_USE_CUSTOM_STREAMS
4
5
6 namespace UnitTest {
7
8 char const* MemoryOutStream::GetText() const
9 {
10 m_text = this->str();
11 return m_text.c_str();
12 }
13
14
15 }
16
17
18 #else
19
20
21 #include <cstring>
22 #include <cstdio>
23
24 namespace UnitTest {
25
26 namespace {
27
28 template<typename ValueType>
29 void FormatToStream(MemoryOutStream& stream, char const* format, ValueType const& value)
30 {
31 char txt[32];
32 std::sprintf(txt, format, value);
33 stream << txt;
34 }
35
36 int RoundUpToMultipleOfPow2Number (int n, int pow2Number)
37 {
38 return (n + (pow2Number - 1)) & ~(pow2Number - 1);
39 }
40
41 }
42
43
44 MemoryOutStream::MemoryOutStream(int const size)
45 : m_capacity (0)
46 , m_buffer (0)
47
48 {
49 GrowBuffer(size);
50 }
51
52 MemoryOutStream::~MemoryOutStream()
53 {
54 delete [] m_buffer;
55 }
56
57 char const* MemoryOutStream::GetText() const
58 {
59 return m_buffer;
60 }
61
62 MemoryOutStream& MemoryOutStream::operator << (char const* txt)
63 {
64 int const bytesLeft = m_capacity - (int)std::strlen(m_buffer);
65 int const bytesRequired = (int)std::strlen(txt) + 1;
66
67 if (bytesRequired > bytesLeft)
68 {
69 int const requiredCapacity = bytesRequired + m_capacity - bytesLeft;
70 GrowBuffer(requiredCapacity);
71 }
72
73 std::strcat(m_buffer, txt);
74 return *this;
75 }
76
77 MemoryOutStream& MemoryOutStream::operator << (int const n)
78 {
79 FormatToStream(*this, "%i", n);
80 return *this;
81 }
82
83 MemoryOutStream& MemoryOutStream::operator << (long const n)
84 {
85 FormatToStream(*this, "%li", n);
86 return *this;
87 }
88
89 MemoryOutStream& MemoryOutStream::operator << (unsigned long const n)
90 {
91 FormatToStream(*this, "%lu", n);
92 return *this;
93 }
94
95 MemoryOutStream& MemoryOutStream::operator << (float const f)
96 {
97 FormatToStream(*this, "%ff", f);
98 return *this;
99 }
100
101 MemoryOutStream& MemoryOutStream::operator << (void const* p)
102 {
103 FormatToStream(*this, "%p", p);
104 return *this;
105 }
106
107 MemoryOutStream& MemoryOutStream::operator << (unsigned int const s)
108 {
109 FormatToStream(*this, "%u", s);
110 return *this;
111 }
112
113 MemoryOutStream& MemoryOutStream::operator <<(double const d)
114 {
115 FormatToStream(*this, "%f", d);
116 return *this;
117 }
118
119 int MemoryOutStream::GetCapacity() const
120 {
121 return m_capacity;
122 }
123
124
125 void MemoryOutStream::GrowBuffer(int const desiredCapacity)
126 {
127 int const newCapacity = RoundUpToMultipleOfPow2Number(desiredCapacity, GROW_CHUNK_SIZE);
128
129 char* buffer = new char[newCapacity];
130 if (m_buffer)
131 std::strcpy(buffer, m_buffer);
132 else
133 std::strcpy(buffer, "");
134
135 delete [] m_buffer;
136 m_buffer = buffer;
137 m_capacity = newCapacity;
138 }
139
140 }
141
142
143 #endif