28 dataSize (sourceDataSize)
32 internalCopy =
MemoryBlock (sourceData, sourceDataSize);
38 : data (sourceData.getData()),
39 dataSize (sourceData.getSize())
43 internalCopy = sourceData;
49 : internalCopy (std::move (source))
60 return (int64) dataSize;
65 jassert (buffer !=
nullptr && howMany >= 0);
67 if (howMany <= 0 || position >= dataSize)
70 auto num = jmin ((
size_t) howMany, dataSize - position);
74 memcpy (buffer, addBytesToPointer (data, position), num);
83 return position >= dataSize;
88 position = (size_t) jlimit ((int64) 0, (int64) dataSize, pos);
94 return (int64) position;
99 if (numBytesToSkip > 0)
108class MemoryStreamTests :
public UnitTest
112 :
UnitTest (
"MemoryInputStream & MemoryOutputStream", UnitTestCategories::streams)
115 void runTest()
override
117 beginTest (
"Basics");
118 Random r = getRandom();
120 int randomInt = r.nextInt();
121 int64 randomInt64 = r.nextInt64();
122 double randomDouble = r.nextDouble();
123 String randomString (createRandomWideCharString (r));
125 MemoryOutputStream mo;
126 mo.writeInt (randomInt);
127 mo.writeIntBigEndian (randomInt);
128 mo.writeCompressedInt (randomInt);
129 mo.writeString (randomString);
130 mo.writeInt64 (randomInt64);
131 mo.writeInt64BigEndian (randomInt64);
132 mo.writeDouble (randomDouble);
133 mo.writeDoubleBigEndian (randomDouble);
135 MemoryInputStream mi (mo.getData(), mo.getDataSize(),
false);
136 expect (mi.readInt() == randomInt);
137 expect (mi.readIntBigEndian() == randomInt);
138 expect (mi.readCompressedInt() == randomInt);
139 expectEquals (mi.readString(), randomString);
140 expect (mi.readInt64() == randomInt64);
141 expect (mi.readInt64BigEndian() == randomInt64);
142 expect (mi.readDouble() == randomDouble);
143 expect (mi.readDoubleBigEndian() == randomDouble);
145 const MemoryBlock data (
"abcdefghijklmnopqrstuvwxyz", 26);
146 MemoryInputStream stream (data,
true);
150 expectEquals (stream.getPosition(), (int64) 0);
151 expectEquals (stream.getTotalLength(), (int64) data.getSize());
152 expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
153 expect (! stream.isExhausted());
155 size_t numBytesRead = 0;
156 MemoryBlock readBuffer (data.getSize());
158 while (numBytesRead < data.getSize())
160 numBytesRead += (size_t) stream.read (&readBuffer[numBytesRead], 3);
162 expectEquals (stream.getPosition(), (int64) numBytesRead);
163 expectEquals (stream.getNumBytesRemaining(), (int64) (data.getSize() - numBytesRead));
164 expect (stream.isExhausted() == (numBytesRead == data.getSize()));
167 expectEquals (stream.getPosition(), (int64) data.getSize());
168 expectEquals (stream.getNumBytesRemaining(), (int64) 0);
169 expect (stream.isExhausted());
171 expect (readBuffer == data);
175 stream.setPosition (0);
176 expectEquals (stream.getPosition(), (int64) 0);
177 expectEquals (stream.getTotalLength(), (int64) data.getSize());
178 expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
179 expect (! stream.isExhausted());
182 const int numBytesToSkip = 5;
184 while (numBytesRead < data.getSize())
186 stream.skipNextBytes (numBytesToSkip);
187 numBytesRead += numBytesToSkip;
188 numBytesRead = std::min (numBytesRead, data.getSize());
190 expectEquals (stream.getPosition(), (int64) numBytesRead);
191 expectEquals (stream.getNumBytesRemaining(), (int64) (data.getSize() - numBytesRead));
192 expect (stream.isExhausted() == (numBytesRead == data.getSize()));
195 expectEquals (stream.getPosition(), (int64) data.getSize());
196 expectEquals (stream.getNumBytesRemaining(), (int64) 0);
197 expect (stream.isExhausted());
200 static String createRandomWideCharString (Random& r)
202 juce_wchar buffer [50] = { 0 };
204 for (
int i = 0; i < numElementsInArray (buffer) - 1; ++i)
210 buffer[i] = (juce_wchar) (1 + r.nextInt (0x10ffff - 1));
215 buffer[i] = (juce_wchar) (1 + r.nextInt (0xff));
218 return CharPointer_UTF32 (buffer);
222static MemoryStreamTests memoryInputStreamUnitTests;
static bool canRepresent(juce_wchar character) noexcept
Returns true if the given unicode character can be represented in this encoding.
A class to hold a resizable block of raw data.
void * getData() noexcept
Returns a void pointer to the data.
This is a base class for classes that perform a unit test.