PageRenderTime 103ms CodeModel.GetById 40ms app.highlight 34ms RepoModel.GetById 27ms app.codeStats 0ms

/mordor/tests/memory_stream.cpp

http://github.com/mozy/mordor
C++ | 178 lines | 165 code | 11 blank | 2 comment | 60 complexity | c213cb7d4665e6ee20fd7748405c8fc3 MD5 | raw file
  1// Copyright (c) 2009 - Mozy, Inc.
  2
  3#include "mordor/streams/memory.h"
  4#include "mordor/version.h"
  5#include "mordor/test/test.h"
  6
  7using namespace Mordor;
  8using namespace Mordor::Test;
  9
 10MORDOR_UNITTEST(MemoryStream, basic)
 11{
 12    MemoryStream stream;
 13    Buffer buffer;
 14    MORDOR_TEST_ASSERT_EQUAL(stream.size(), 0);
 15    MORDOR_TEST_ASSERT_EQUAL(stream.tell(), 0);
 16    MORDOR_TEST_ASSERT_EQUAL(stream.write("cody", 4), 4u);
 17    MORDOR_TEST_ASSERT_EQUAL(stream.size(), 4);
 18    MORDOR_TEST_ASSERT_EQUAL(stream.tell(), 4);
 19    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
 20    MORDOR_TEST_ASSERT(stream.readBuffer() == "");
 21    MORDOR_TEST_ASSERT_EQUAL(stream.seek(0, Stream::BEGIN), 0);
 22    MORDOR_TEST_ASSERT_EQUAL(stream.tell(), 0);
 23    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
 24    MORDOR_TEST_ASSERT(stream.readBuffer() == "cody");
 25    MORDOR_TEST_ASSERT_EQUAL(stream.read(buffer, 2), 2u);
 26    MORDOR_TEST_ASSERT_EQUAL(stream.size(), 4);
 27    MORDOR_TEST_ASSERT_EQUAL(stream.tell(), 2);
 28    MORDOR_TEST_ASSERT(buffer == "co");
 29    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
 30    MORDOR_TEST_ASSERT(stream.readBuffer() == "dy");
 31}
 32
 33MORDOR_UNITTEST(MemoryStream, absoluteSeek)
 34{
 35    MemoryStream stream;
 36    stream.write("cody", 4u);
 37    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
 38    MORDOR_TEST_ASSERT(stream.readBuffer() == "");
 39    MORDOR_TEST_ASSERT_EXCEPTION(stream.seek(-1, Stream::BEGIN), std::invalid_argument);
 40#ifdef X86
 41    MORDOR_TEST_ASSERT_EXCEPTION(stream.seek(0x100000000ull, Stream::BEGIN), std::invalid_argument);
 42#endif
 43    MORDOR_TEST_ASSERT_EQUAL(stream.seek(0, Stream::BEGIN), 0);
 44    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
 45    MORDOR_TEST_ASSERT(stream.readBuffer() == "cody");
 46    MORDOR_TEST_ASSERT_EQUAL(stream.seek(2, Stream::BEGIN), 2);
 47    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
 48    MORDOR_TEST_ASSERT(stream.readBuffer() == "dy");
 49    MORDOR_TEST_ASSERT_EQUAL(stream.seek(4, Stream::BEGIN), 4);
 50    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
 51    MORDOR_TEST_ASSERT(stream.readBuffer() == "");
 52    MORDOR_TEST_ASSERT_EQUAL(stream.seek(6, Stream::BEGIN), 6);
 53    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
 54    MORDOR_TEST_ASSERT(stream.readBuffer() == "");
 55}
 56
 57MORDOR_UNITTEST(MemoryStream, forwardSeek)
 58{
 59    MemoryStream stream;
 60    stream.write("cody", 4u);
 61    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
 62    MORDOR_TEST_ASSERT(stream.readBuffer() == "");
 63    MORDOR_TEST_ASSERT_EQUAL(stream.seek(2, Stream::BEGIN), 2);
 64    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
 65    MORDOR_TEST_ASSERT(stream.readBuffer() == "dy");
 66    MORDOR_TEST_ASSERT_EQUAL(stream.seek(1, Stream::CURRENT), 3);
 67    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
 68    MORDOR_TEST_ASSERT(stream.readBuffer() == "y");
 69    MORDOR_TEST_ASSERT_EQUAL(stream.seek(1, Stream::CURRENT), 4);
 70    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
 71    MORDOR_TEST_ASSERT(stream.readBuffer() == "");
 72    MORDOR_TEST_ASSERT_EQUAL(stream.seek(1, Stream::CURRENT), 5);
 73    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
 74    MORDOR_TEST_ASSERT(stream.readBuffer() == "");
 75#ifdef X86
 76    MORDOR_TEST_ASSERT_EXCEPTION(stream.seek(0x100000000ull, Stream::CURRENT), std::invalid_argument);
 77#endif
 78}
 79
 80MORDOR_UNITTEST(MemoryStream, backwardSeek)
 81{
 82    MemoryStream stream;
 83    stream.write("cody", 4u);
 84    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
 85    MORDOR_TEST_ASSERT(stream.readBuffer() == "");
 86    MORDOR_TEST_ASSERT_EQUAL(stream.seek(2, Stream::BEGIN), 2);
 87    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
 88    MORDOR_TEST_ASSERT(stream.readBuffer() == "dy");
 89    MORDOR_TEST_ASSERT_EQUAL(stream.seek(-1, Stream::CURRENT), 1);
 90    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
 91    MORDOR_TEST_ASSERT(stream.readBuffer() == "ody");
 92    MORDOR_TEST_ASSERT_EXCEPTION(stream.seek(-2, Stream::CURRENT), std::invalid_argument);
 93}
 94
 95MORDOR_UNITTEST(MemoryStream, seekFromEnd)
 96{
 97    MemoryStream stream;
 98    stream.write("cody", 4u);
 99    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
100    MORDOR_TEST_ASSERT(stream.readBuffer() == "");
101    MORDOR_TEST_ASSERT_EQUAL(stream.seek(0, Stream::END), 4);
102    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
103    MORDOR_TEST_ASSERT(stream.readBuffer() == "");
104    MORDOR_TEST_ASSERT_EQUAL(stream.seek(1, Stream::END), 5);
105    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
106    MORDOR_TEST_ASSERT(stream.readBuffer() == "");
107    MORDOR_TEST_ASSERT_EQUAL(stream.seek(-2, Stream::END), 2);
108    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
109    MORDOR_TEST_ASSERT(stream.readBuffer() == "dy");
110    // Should catch an optimized forward seek
111    MORDOR_TEST_ASSERT_EQUAL(stream.seek(-1, Stream::END), 3);
112    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
113    MORDOR_TEST_ASSERT(stream.readBuffer() == "y");
114    MORDOR_TEST_ASSERT_EXCEPTION(stream.seek(-5, Stream::CURRENT), std::invalid_argument);
115}
116
117MORDOR_UNITTEST(MemoryStream, truncate)
118{
119    MemoryStream stream;
120    stream.write("cody", 4u);
121    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
122    MORDOR_TEST_ASSERT(stream.readBuffer() == "");
123    MORDOR_TEST_ASSERT_EQUAL(stream.tell(), 4);
124    stream.truncate(4);
125    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
126    MORDOR_TEST_ASSERT(stream.readBuffer() == "");
127    MORDOR_TEST_ASSERT_EQUAL(stream.tell(), 4);
128    stream.truncate(3);
129    MORDOR_TEST_ASSERT(stream.buffer() == "cod");
130    MORDOR_TEST_ASSERT(stream.readBuffer() == "");
131    MORDOR_TEST_ASSERT_EQUAL(stream.tell(), 4);
132    stream.truncate(5);
133    MORDOR_TEST_ASSERT(stream.buffer() == std::string("cod\0\0", 5));
134    MORDOR_TEST_ASSERT(stream.readBuffer() == std::string("\0", 1));
135    MORDOR_TEST_ASSERT_EQUAL(stream.tell(), 4);
136    stream.truncate(0);
137    MORDOR_TEST_ASSERT(stream.buffer() == "");
138    MORDOR_TEST_ASSERT(stream.readBuffer() == "");
139    MORDOR_TEST_ASSERT_EQUAL(stream.tell(), 4);
140#ifdef X86
141    MORDOR_TEST_ASSERT_EXCEPTION(stream.truncate(0x100000000ull), std::invalid_argument);
142#endif
143}
144
145MORDOR_UNITTEST(MemoryStream, writeExtension)
146{
147    MemoryStream stream;
148    stream.write("cody", 4u);
149    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
150    MORDOR_TEST_ASSERT_EQUAL(stream.seek(1, Stream::END), 5);
151    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
152    MORDOR_TEST_ASSERT_EQUAL(stream.write("cutrer", 6), 6u);
153    MORDOR_TEST_ASSERT_EQUAL(stream.size(), 11);
154    MORDOR_TEST_ASSERT(stream.buffer() == std::string("cody\0cutrer", 11));
155}
156
157MORDOR_UNITTEST(MemoryStream, writep1)
158{
159    MemoryStream stream;
160    stream.write("cody", 4u);
161    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
162    MORDOR_TEST_ASSERT_EQUAL(stream.seek(1, Stream::BEGIN), 1);
163    MORDOR_TEST_ASSERT_EQUAL(stream.write("c", 1), 1u);
164    MORDOR_TEST_ASSERT_EQUAL(stream.tell(), 2);
165    MORDOR_TEST_ASSERT(stream.buffer() == "ccdy");
166}
167
168MORDOR_UNITTEST(MemoryStream, writep2)
169{
170    MemoryStream stream;
171    stream.write("cody", 4u);
172    MORDOR_TEST_ASSERT(stream.buffer() == "cody");
173    MORDOR_TEST_ASSERT_EQUAL(stream.seek(1, Stream::BEGIN), 1);
174    MORDOR_TEST_ASSERT_EQUAL(stream.write("cutrer", 6), 6u);
175    MORDOR_TEST_ASSERT_EQUAL(stream.tell(), 7);
176    MORDOR_TEST_ASSERT_EQUAL(stream.size(), 7);
177    MORDOR_TEST_ASSERT(stream.buffer() == "ccutrer");
178}