49 : internalCopy (std::move (source))
52 dataSize = internalCopy.
getSize();
59 return (int64) dataSize;
64 jassert (buffer !=
nullptr &&
howMany >= 0);
69 auto num = jmin ((
size_t)
howMany, dataSize - position);
73 memcpy (buffer, addBytesToPointer (data, position), num);
82 return position >= dataSize;
87 position = (
size_t) jlimit ((int64) 0, (int64) dataSize, pos);
93 return (int64) position;
111 :
UnitTest (
"MemoryInputStream & MemoryOutputStream", UnitTestCategories::streams)
114 void runTest()
override
116 beginTest (
"Basics");
117 Random r = getRandom();
119 int randomInt = r.nextInt();
120 int64 randomInt64 = r.nextInt64();
121 double randomDouble = r.nextDouble();
122 String randomString (createRandomWideCharString (r));
124 MemoryOutputStream mo;
125 mo.writeInt (randomInt);
126 mo.writeIntBigEndian (randomInt);
127 mo.writeCompressedInt (randomInt);
128 mo.writeString (randomString);
129 mo.writeInt64 (randomInt64);
130 mo.writeInt64BigEndian (randomInt64);
131 mo.writeDouble (randomDouble);
132 mo.writeDoubleBigEndian (randomDouble);
134 MemoryInputStream mi (mo.getData(), mo.getDataSize(),
false);
135 expect (mi.readInt() == randomInt);
136 expect (mi.readIntBigEndian() == randomInt);
137 expect (mi.readCompressedInt() == randomInt);
138 expectEquals (mi.readString(), randomString);
139 expect (mi.readInt64() == randomInt64);
140 expect (mi.readInt64BigEndian() == randomInt64);
141 expectEquals (mi.readDouble(), randomDouble);
142 expectEquals (mi.readDoubleBigEndian(), randomDouble);
144 const MemoryBlock data (
"abcdefghijklmnopqrstuvwxyz", 26);
145 MemoryInputStream stream (data,
true);
149 expectEquals (stream.getPosition(), (int64) 0);
150 expectEquals (stream.getTotalLength(), (int64) data.getSize());
151 expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
152 expect (! stream.isExhausted());
154 size_t numBytesRead = 0;
155 MemoryBlock readBuffer (data.getSize());
157 while (numBytesRead < data.getSize())
159 numBytesRead += (size_t) stream.read (&readBuffer[numBytesRead], 3);
161 expectEquals (stream.getPosition(), (int64) numBytesRead);
162 expectEquals (stream.getNumBytesRemaining(), (int64) (data.getSize() - numBytesRead));
163 expect (stream.isExhausted() == (numBytesRead == data.getSize()));
166 expectEquals (stream.getPosition(), (int64) data.getSize());
167 expectEquals (stream.getNumBytesRemaining(), (int64) 0);
168 expect (stream.isExhausted());
170 expect (readBuffer == data);
174 stream.setPosition (0);
175 expectEquals (stream.getPosition(), (int64) 0);
176 expectEquals (stream.getTotalLength(), (int64) data.getSize());
177 expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
178 expect (! stream.isExhausted());
181 const int numBytesToSkip = 5;
183 while (numBytesRead < data.getSize())
185 stream.skipNextBytes (numBytesToSkip);
186 numBytesRead += numBytesToSkip;
187 numBytesRead = std::min (numBytesRead, data.getSize());
189 expectEquals (stream.getPosition(), (int64) numBytesRead);
190 expectEquals (stream.getNumBytesRemaining(), (int64) (data.getSize() - numBytesRead));
191 expect (stream.isExhausted() == (numBytesRead == data.getSize()));
194 expectEquals (stream.getPosition(), (int64) data.getSize());
195 expectEquals (stream.getNumBytesRemaining(), (int64) 0);
196 expect (stream.isExhausted());
199 static String createRandomWideCharString (Random& r)
201 juce_wchar buffer [50] = { 0 };
203 for (
int i = 0; i < numElementsInArray (buffer) - 1; ++i)
209 buffer[i] = (juce_wchar) (1 + r.nextInt (0x10ffff - 1));
214 buffer[i] = (juce_wchar) (1 + r.nextInt (0xff));
217 return CharPointer_UTF32 (buffer);
221static MemoryStreamTests memoryInputStreamUnitTests;
static bool canRepresent(juce_wchar character) noexcept
void * getData() noexcept
size_t getSize() const noexcept