terminal/src/til/ut_til/SomeTests.cpp
Michael Niksa 735d2e5613
Introduce til::some (#4123)
## Summary of the Pull Request
Introduces a type that is basically an array (stack allocated, fixed size) that reports size based on how many elements are actually filled (from the front), iterates only the filled ones, and has some basic vector push/pop semantics.

## PR Checklist
* [x] I work here
* [x] I work here
* [x] I work here
* [ ] I'd love to roll this out to SomeViewports.... maybe in this commit or a follow on one.
* [ ] We need a TIL tests library and I should test this there. 

## Detailed Description of the Pull Request / Additional comments
The original gist of this was used for `SomeViewports` which was a struct to hold between 0 and 4 viewports, based on how many were left after subtraction (since rectangle subtraction functions in Windows code simply fail for resultants that yield >=2 rectangle regions.)

I figured now that we're TIL-ifying useful common utility things that this would be best suited to a template because I'm certain there are other circumstances where we would like to iterate a partially filled array and want it to not auto-resize-up like a vector would.

## Validation Steps Performed
* [ ] TIL tests added
2020-01-09 09:07:52 -08:00

276 lines
6.5 KiB
C++

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "precomp.h"
#include "WexTestClass.h"
using namespace WEX::Common;
using namespace WEX::Logging;
using namespace WEX::TestExecution;
class SomeTests
{
TEST_CLASS(SomeTests);
TEST_METHOD(Construct)
{
Log::Comment(L"Default Constructor");
til::some<int, 2> s;
Log::Comment(L"Valid Initializer List Constructor");
til::some<int, 2> t{ 1 };
til::some<int, 2> u{ 1, 2 };
Log::Comment(L"Invalid Initializer List Constructor");
auto f = []() {
til::some<int, 2> v{ 1, 2, 3 };
};
VERIFY_THROWS(f(), std::invalid_argument);
}
TEST_METHOD(Fill)
{
til::some<int, 4> s;
const auto val = 12;
s.fill(val);
VERIFY_ARE_EQUAL(s.max_size(), s.size());
for (const auto& i : s)
{
VERIFY_ARE_EQUAL(val, i);
}
}
TEST_METHOD(Swap)
{
til::some<int, 4> a;
til::some<int, 4> b;
const auto aVal = 900;
a.fill(900);
const auto bVal = 45;
b.push_back(45);
const auto aSize = a.size();
const auto bSize = b.size();
a.swap(b);
VERIFY_ARE_EQUAL(aSize, b.size());
VERIFY_ARE_EQUAL(bSize, a.size());
VERIFY_ARE_EQUAL(bVal, a[0]);
for (const auto& i : b)
{
VERIFY_ARE_EQUAL(aVal, i);
}
}
TEST_METHOD(Size)
{
til::some<int, 2> c;
VERIFY_ARE_EQUAL(0u, c.size());
c.push_back(3);
VERIFY_ARE_EQUAL(1u, c.size());
c.push_back(12);
VERIFY_ARE_EQUAL(2u, c.size());
c.pop_back();
VERIFY_ARE_EQUAL(1u, c.size());
c.pop_back();
VERIFY_ARE_EQUAL(0u, c.size());
}
TEST_METHOD(MaxSize)
{
til::some<int, 2> c;
VERIFY_ARE_EQUAL(2u, c.max_size());
c.push_back(3);
VERIFY_ARE_EQUAL(2u, c.max_size());
c.push_back(12);
VERIFY_ARE_EQUAL(2u, c.size());
c.pop_back();
VERIFY_ARE_EQUAL(2u, c.max_size());
c.pop_back();
VERIFY_ARE_EQUAL(2u, c.max_size());
}
TEST_METHOD(PushBack)
{
til::some<int, 1> s;
s.push_back(12);
VERIFY_THROWS(s.push_back(12), std::out_of_range);
}
TEST_METHOD(PopBack)
{
til::some<int, 1> s;
VERIFY_THROWS(s.pop_back(), std::out_of_range);
s.push_back(12);
VERIFY_THROWS(s.push_back(12), std::out_of_range);
}
TEST_METHOD(Empty)
{
til::some<int, 2> s;
VERIFY_IS_TRUE(s.empty());
s.push_back(12);
VERIFY_IS_FALSE(s.empty());
s.pop_back();
VERIFY_IS_TRUE(s.empty());
}
TEST_METHOD(Data)
{
til::some<int, 2> s;
const auto one = 1;
const auto two = 2;
s.push_back(one);
s.push_back(two);
auto data = s.data();
VERIFY_ARE_EQUAL(one, *data);
VERIFY_ARE_EQUAL(two, *(data + 1));
}
TEST_METHOD(FrontBack)
{
til::some<int, 2> s;
const auto one = 1;
const auto two = 2;
s.push_back(one);
s.push_back(two);
VERIFY_ARE_EQUAL(one, s.front());
VERIFY_ARE_EQUAL(two, s.back());
}
TEST_METHOD(Indexing)
{
const auto one = 14;
const auto two = 28;
til::some<int, 2> s;
VERIFY_THROWS(s.at(0), std::out_of_range);
VERIFY_THROWS(s.at(1), std::out_of_range);
auto a = s[0];
a = s[1];
s.push_back(one);
VERIFY_ARE_EQUAL(one, s.at(0));
VERIFY_ARE_EQUAL(one, s[0]);
VERIFY_THROWS(s.at(1), std::out_of_range);
a = s[1];
s.push_back(two);
VERIFY_ARE_EQUAL(one, s.at(0));
VERIFY_ARE_EQUAL(one, s[0]);
VERIFY_ARE_EQUAL(two, s.at(1));
VERIFY_ARE_EQUAL(two, s[1]);
s.pop_back();
VERIFY_ARE_EQUAL(one, s.at(0));
VERIFY_ARE_EQUAL(one, s[0]);
VERIFY_THROWS(s.at(1), std::out_of_range);
a = s[1];
s.pop_back();
VERIFY_THROWS(s.at(0), std::out_of_range);
VERIFY_THROWS(s.at(1), std::out_of_range);
a = s[0];
a = s[1];
}
TEST_METHOD(ForwardIter)
{
const int vals[] = { 17, 99 };
const int valLength = ARRAYSIZE(vals);
til::some<int, 2> s;
VERIFY_ARE_EQUAL(s.begin(), s.end());
VERIFY_ARE_EQUAL(s.cbegin(), s.cend());
VERIFY_ARE_EQUAL(s.begin(), s.cbegin());
VERIFY_ARE_EQUAL(s.end(), s.cend());
s.push_back(vals[0]);
s.push_back(vals[1]);
VERIFY_ARE_EQUAL(s.begin() + valLength, s.end());
VERIFY_ARE_EQUAL(s.cbegin() + valLength, s.cend());
auto count = 0;
for (const auto& i : s)
{
VERIFY_ARE_EQUAL(vals[count], i);
++count;
}
VERIFY_ARE_EQUAL(valLength, count);
count = 0;
for (auto i = s.cbegin(); i < s.cend(); ++i)
{
VERIFY_ARE_EQUAL(vals[count], *i);
++count;
}
VERIFY_ARE_EQUAL(valLength, count);
count = 0;
for (auto i = s.begin(); i < s.end(); ++i)
{
VERIFY_ARE_EQUAL(vals[count], *i);
++count;
}
VERIFY_ARE_EQUAL(valLength, count);
}
TEST_METHOD(ReverseIter)
{
const int vals[] = { 17, 99 };
const int valLength = ARRAYSIZE(vals);
til::some<int, 2> s;
VERIFY_ARE_EQUAL(s.rbegin(), s.rend());
VERIFY_ARE_EQUAL(s.crbegin(), s.crend());
VERIFY_ARE_EQUAL(s.rbegin(), s.crbegin());
VERIFY_ARE_EQUAL(s.rend(), s.crend());
s.push_back(vals[0]);
s.push_back(vals[1]);
VERIFY_ARE_EQUAL(s.rbegin() + valLength, s.rend());
VERIFY_ARE_EQUAL(s.crbegin() + valLength, s.crend());
auto count = 0;
for (auto i = s.crbegin(); i < s.crend(); ++i)
{
VERIFY_ARE_EQUAL(vals[valLength - count - 1], *i);
++count;
}
VERIFY_ARE_EQUAL(valLength, count);
count = 0;
for (auto i = s.rbegin(); i < s.rend(); ++i)
{
VERIFY_ARE_EQUAL(vals[valLength - count - 1], *i);
++count;
}
VERIFY_ARE_EQUAL(valLength, count);
}
};