2015-08-20 21:09:14 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
|
|
|
//
|
|
|
|
// This code is licensed under the MIT License (MIT).
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include <UnitTest++/UnitTest++.h>
|
2015-11-04 15:42:27 -05:00
|
|
|
#include <string_span.h>
|
2015-08-20 21:09:14 -04:00
|
|
|
#include <vector>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
using namespace std;
|
2015-09-29 19:41:37 -04:00
|
|
|
using namespace gsl;
|
2015-08-20 21:09:14 -04:00
|
|
|
|
2015-11-05 12:29:30 -05:00
|
|
|
|
2015-11-04 15:42:27 -05:00
|
|
|
SUITE(string_span_tests)
|
2015-08-20 21:09:14 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
TEST(TestLiteralConstruction)
|
|
|
|
{
|
2015-11-04 15:42:27 -05:00
|
|
|
cwstring_span<> v = ensure_z(L"Hello");
|
2015-08-20 21:09:14 -04:00
|
|
|
|
|
|
|
CHECK(5 == v.length());
|
|
|
|
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2015-11-04 15:42:27 -05:00
|
|
|
wstring_span<> v2 = ensure0(L"Hello");
|
2015-08-20 21:09:14 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TestConstructFromStdString)
|
|
|
|
{
|
|
|
|
std::string s = "Hello there world";
|
2015-11-04 15:42:27 -05:00
|
|
|
cstring_span<> v = s;
|
2015-11-12 21:57:23 -05:00
|
|
|
CHECK(v.length() == static_cast<cstring_span<>::size_type>(s.length()));
|
2015-08-20 21:09:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TestConstructFromStdVector)
|
|
|
|
{
|
2015-10-04 01:02:49 -04:00
|
|
|
std::vector<char> vec(5, 'h');
|
2015-11-04 15:42:27 -05:00
|
|
|
string_span<> v = vec;
|
2015-11-12 21:57:23 -05:00
|
|
|
CHECK(v.length() == static_cast<string_span<>::size_type>(vec.size()));
|
2015-08-20 21:09:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TestStackArrayConstruction)
|
|
|
|
{
|
|
|
|
wchar_t stack_string[] = L"Hello";
|
|
|
|
|
|
|
|
{
|
2015-11-04 15:42:27 -05:00
|
|
|
cwstring_span<> v = ensure_z(stack_string);
|
2015-08-20 21:09:14 -04:00
|
|
|
CHECK(v.length() == 5);
|
|
|
|
CHECK(v.used_length() == v.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2015-11-04 15:42:27 -05:00
|
|
|
cwstring_span<> v = stack_string;
|
2015-08-20 21:09:14 -04:00
|
|
|
CHECK(v.length() == 6);
|
|
|
|
CHECK(v.used_length() == v.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2015-11-04 15:42:27 -05:00
|
|
|
wstring_span<> v = ensure_z(stack_string);
|
2015-08-20 21:09:14 -04:00
|
|
|
CHECK(v.length() == 5);
|
|
|
|
CHECK(v.used_length() == v.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2015-11-04 15:42:27 -05:00
|
|
|
wstring_span<> v = stack_string;
|
2015-08-20 21:09:14 -04:00
|
|
|
CHECK(v.length() == 6);
|
|
|
|
CHECK(v.used_length() == v.length());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 21:23:14 -04:00
|
|
|
TEST(TestConstructFromConstCharPointer)
|
|
|
|
{
|
|
|
|
const char* s = "Hello";
|
2015-11-04 15:42:27 -05:00
|
|
|
cstring_span<> v = ensure_z(s);
|
2015-10-26 21:23:14 -04:00
|
|
|
CHECK(v.length() == 5);
|
|
|
|
CHECK(v.used_length() == v.length());
|
|
|
|
}
|
|
|
|
|
2015-08-20 21:09:14 -04:00
|
|
|
TEST(TestConversionToConst)
|
|
|
|
{
|
|
|
|
char stack_string[] = "Hello";
|
2015-11-04 15:42:27 -05:00
|
|
|
string_span<> v = ensure_z(stack_string);
|
|
|
|
cstring_span<> v2 = v;
|
2015-08-20 21:09:14 -04:00
|
|
|
CHECK(v.length() == v2.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TestConversionFromConst)
|
|
|
|
{
|
|
|
|
char stack_string[] = "Hello";
|
2015-11-04 15:42:27 -05:00
|
|
|
cstring_span<> v = ensure_z(stack_string);
|
2015-11-12 22:27:53 -05:00
|
|
|
(void)v;
|
2015-08-20 21:09:14 -04:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2015-11-04 15:42:27 -05:00
|
|
|
string_span<> v2 = v;
|
|
|
|
string_span<> v3 = "Hello";
|
2015-08-20 21:09:14 -04:00
|
|
|
#endif
|
|
|
|
}
|
2015-11-05 12:29:30 -05:00
|
|
|
|
|
|
|
TEST(TestToString)
|
|
|
|
{
|
|
|
|
auto s = gsl::to_string(cstring_span<>{});
|
|
|
|
CHECK(s.length() == 0);
|
|
|
|
|
|
|
|
char stack_string[] = "Hello";
|
|
|
|
cstring_span<> v = ensure_z(stack_string);
|
|
|
|
auto s2 = gsl::to_string(v);
|
2015-11-12 21:57:23 -05:00
|
|
|
CHECK(static_cast<cstring_span<>::size_type>(s2.length()) == v.length());
|
2015-11-05 12:29:30 -05:00
|
|
|
CHECK(s2.length() == 5);
|
|
|
|
}
|
2015-08-20 21:09:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int, const char *[])
|
|
|
|
{
|
|
|
|
return UnitTest::RunAllTests();
|
|
|
|
}
|