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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
/***
* Copyright (C) Microsoft. All rights reserved.
* Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
*
* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
*
* Basic tests for winrt interop streams.
*
* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
****/
#include "stdafx.h"
using namespace concurrency::streams;
using namespace utility;
using namespace ::pplx;
#if defined(__cplusplus_winrt)
using namespace Windows::Storage;
#endif
namespace tests
{
namespace functional
{
namespace streams
{
SUITE(winrt_interop_tests)
{
TEST(read_in)
{
producer_consumer_buffer<char> buf;
auto ostream = buf.create_ostream();
std::string strData("abcdefghij");
buf.putn_nocopy((char*)&strData[0], strData.size() * sizeof(char)).wait();
auto dr = ref new Windows::Storage::Streams::DataReader(winrt_stream::create_input_stream(buf));
dr->ByteOrder = Windows::Storage::Streams::ByteOrder::LittleEndian;
{
VERIFY_ARE_EQUAL(10, pplx::create_task(dr->LoadAsync(10)).get());
auto value = dr->ReadString(5);
VERIFY_ARE_EQUAL(utility::string_t(value->Data()), U("abcde"));
value = dr->ReadString(5);
VERIFY_ARE_EQUAL(utility::string_t(value->Data()), U("fghij"));
}
{
ostream.write(char(11)).wait();
ostream.write(char(17)).wait();
VERIFY_ARE_EQUAL(2, pplx::create_task(dr->LoadAsync(2)).get());
auto ival = dr->ReadByte();
VERIFY_ARE_EQUAL(ival, 11);
ival = dr->ReadByte();
VERIFY_ARE_EQUAL(ival, 17);
}
{
for (int i = 0; i < 100; i++)
{
ostream.write(char(i)).wait();
}
VERIFY_ARE_EQUAL(100, pplx::create_task(dr->LoadAsync(100)).get());
auto arr = ref new Platform::Array<unsigned char, 1>(100);
dr->ReadBytes(arr);
for (int i = 0; i < 100; i++)
{
VERIFY_ARE_EQUAL(arr[i], i);
}
}
buf.close(std::ios_base::out);
}
TEST(read_rand)
{
producer_consumer_buffer<char> buf;
auto ostream = buf.create_ostream();
std::string strData("abcdefghij");
buf.putn_nocopy((char*)&strData[0], strData.size() * sizeof(char)).wait();
auto dr = ref new Windows::Storage::Streams::DataReader(winrt_stream::create_random_access_stream(buf));
dr->ByteOrder = Windows::Storage::Streams::ByteOrder::LittleEndian;
{
VERIFY_ARE_EQUAL(10, pplx::create_task(dr->LoadAsync(10)).get());
auto value = dr->ReadString(5);
VERIFY_ARE_EQUAL(utility::string_t(value->Data()), U("abcde"));
value = dr->ReadString(5);
VERIFY_ARE_EQUAL(utility::string_t(value->Data()), U("fghij"));
}
{
ostream.write(char(11)).wait();
ostream.write(char(17)).wait();
VERIFY_ARE_EQUAL(2, pplx::create_task(dr->LoadAsync(2)).get());
auto ival = dr->ReadByte();
VERIFY_ARE_EQUAL(ival, 11);
ival = dr->ReadByte();
VERIFY_ARE_EQUAL(ival, 17);
}
{
for (int i = 0; i < 100; i++)
{
ostream.write(char(i)).wait();
}
VERIFY_ARE_EQUAL(100, pplx::create_task(dr->LoadAsync(100)).get());
auto arr = ref new Platform::Array<unsigned char, 1>(100);
dr->ReadBytes(arr);
for (int i = 0; i < 100; i++)
{
VERIFY_ARE_EQUAL(arr[i], i);
}
}
buf.close(std::ios_base::out);
}
pplx::task<bool> StoreAndFlush(Windows::Storage::Streams::DataWriter ^ dw)
{
return pplx::create_task(dw->StoreAsync()).then([dw](unsigned int) {
return pplx::create_task(dw->FlushAsync());
});
}
TEST(write_out)
{
producer_consumer_buffer<char> buf;
auto dw = ref new Windows::Storage::Streams::DataWriter(winrt_stream::create_output_stream(buf));
dw->ByteOrder = Windows::Storage::Streams::ByteOrder::LittleEndian;
auto value = ref new ::Platform::String(U("10 4711 -10.0 hello!"));
dw->WriteString(value);
dw->WriteByte(11); // Take care to make this a non-character!
dw->WriteUInt16(17);
dw->WriteUInt32(4711);
VERIFY_IS_TRUE(StoreAndFlush(dw).get());
buf.close(std::ios_base::out);
auto istream = buf.create_istream();
VERIFY_ARE_EQUAL(10, istream.extract<unsigned int>().get());
VERIFY_ARE_EQUAL(4711, istream.extract<int>().get());
VERIFY_ARE_EQUAL(-10.0, istream.extract<double>().get());
VERIFY_ARE_EQUAL(utility::string_t(U("hello!")), istream.extract<utility::string_t>().get());
VERIFY_ARE_EQUAL(11, istream.read().get());
uint16_t int16;
buf.getn((char*)&int16, sizeof(int16)).wait();
VERIFY_ARE_EQUAL(17, int16);
uint32_t int32;
buf.getn((char*)&int32, sizeof(int32)).wait();
VERIFY_ARE_EQUAL(4711, int32);
}
TEST(write_rand)
{
producer_consumer_buffer<char> buf;
auto dw = ref new Windows::Storage::Streams::DataWriter(winrt_stream::create_random_access_stream(buf));
dw->ByteOrder = Windows::Storage::Streams::ByteOrder::LittleEndian;
auto value = ref new ::Platform::String(U("10 4711 -10.0 hello!"));
dw->WriteString(value);
dw->WriteByte(11); // Take care to make this a non-character!
dw->WriteUInt16(17);
dw->WriteUInt32(4711);
VERIFY_IS_TRUE(StoreAndFlush(dw).get());
buf.close(std::ios_base::out);
auto istream = buf.create_istream();
VERIFY_ARE_EQUAL(10, istream.extract<unsigned int>().get());
VERIFY_ARE_EQUAL(4711, istream.extract<int>().get());
VERIFY_ARE_EQUAL(-10.0, istream.extract<double>().get());
VERIFY_ARE_EQUAL(utility::string_t(U("hello!")), istream.extract<utility::string_t>().get());
VERIFY_ARE_EQUAL(11, istream.read().get());
uint16_t int16;
buf.getn((char*)&int16, sizeof(int16)).wait();
VERIFY_ARE_EQUAL(17, int16);
uint32_t int32;
buf.getn((char*)&int32, sizeof(int32)).wait();
VERIFY_ARE_EQUAL(4711, int32);
}
TEST(read_write_attributes)
{
{
container_buffer<std::string> buf("test data");
auto rastr = winrt_stream::create_random_access_stream(buf);
VERIFY_IS_TRUE(rastr->CanRead);
VERIFY_IS_FALSE(rastr->CanWrite);
VERIFY_ARE_EQUAL(rastr->Position, 0);
VERIFY_ARE_EQUAL(rastr->Size, 9);
rastr->Size = 1024U;
VERIFY_ARE_EQUAL(rastr->Size, 9);
}
{
container_buffer<std::string> buf;
auto rastr = winrt_stream::create_random_access_stream(buf);
VERIFY_IS_FALSE(rastr->CanRead);
VERIFY_IS_TRUE(rastr->CanWrite);
VERIFY_ARE_EQUAL(rastr->Position, 0);
VERIFY_ARE_EQUAL(rastr->Size, 0);
rastr->Size = 1024U;
VERIFY_ARE_EQUAL(rastr->Size, 1024U);
VERIFY_ARE_EQUAL(buf.collection().size(), 1024U);
}
{
producer_consumer_buffer<uint8_t> buf;
auto rastr = winrt_stream::create_random_access_stream(buf);
VERIFY_IS_TRUE(rastr->CanRead);
VERIFY_IS_TRUE(rastr->CanWrite);
VERIFY_ARE_EQUAL(rastr->Position, 0);
VERIFY_ARE_EQUAL(rastr->Size, 0);
rastr->Size = 1024U;
VERIFY_ARE_EQUAL(rastr->Size, 1024U);
}
}
TEST(cant_write)
{
container_buffer<std::string> buf("test data");
auto ostr = winrt_stream::create_output_stream(buf);
auto dw = ref new Windows::Storage::Streams::DataWriter(ostr);
dw->ByteOrder = Windows::Storage::Streams::ByteOrder::LittleEndian;
auto value = ref new ::Platform::String(U("10 4711 -10.0 hello!"));
dw->WriteString(value);
VERIFY_IS_FALSE(StoreAndFlush(dw).get());
}
TEST(cant_read)
{
container_buffer<std::string> buf;
auto ostream = buf.create_ostream();
ostream.print<int>(10);
auto istr = winrt_stream::create_input_stream(buf);
auto dr = ref new Windows::Storage::Streams::DataReader(istr);
dr->ByteOrder = Windows::Storage::Streams::ByteOrder::LittleEndian;
VERIFY_ARE_EQUAL(0, pplx::create_task(dr->LoadAsync(2)).get());
}
} // SUITE
} // namespace streams
} // namespace functional
} // namespace tests
|