Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code maintainability measures #12

Merged
merged 9 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions Aeu.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,14 @@ class Aeu final {
std::size_t position = 0;

if constexpr (std::is_same_v<Char, char>) {
for(; !std::isalnum(data[position]) && position < size; ++position) ;
for(; position < size && !std::isalnum(data[position]); ++position) ;
} else {
for(; !std::iswalnum(data[position]) && position < size; ++position) ;
for(; position < size && !std::iswalnum(data[position]); ++position) ;
}

if(position == size)
return;

const auto base = [&data, &size, &position, &characters] {
if (data[position] == characters[0] && size > position + 1) {
switch (data[position + 1]) {
Expand All @@ -192,8 +195,9 @@ class Aeu final {
}
} return 10u;
} ();

for(; position < size; ++position) {
const auto digit = [&characters] (Char ch) {
const auto digit = [] (Char ch) {
if(characters[0] <= ch && ch <= characters[1])
return static_cast<unsigned>(ch) - static_cast<unsigned>(characters[0]);
if(characters[2] <= ch && ch <= characters[4])
Expand Down Expand Up @@ -252,7 +256,7 @@ class Aeu final {
*/
constexpr Aeu(const mpz_class& value) : Aeu {} {
const auto bitLength = mpz_sizeinbase(value.get_mpz_t(), 2);
auto tBlocksNumber = static_cast<std::size_t>(1 + ((bitLength - 1) / sizeof(block) * 8));
auto tBlocksNumber = 1 + (bitLength - 1) / sizeof(block) * 8;

std::basic_string<block> buffer (tBlocksNumber, 0u);
mpz_export(buffer.data(), &tBlocksNumber, -1, sizeof(block), -1, 0, value.get_mpz_t());
Expand Down Expand Up @@ -423,7 +427,8 @@ class Aeu final {
template <typename Unsigned> requires (std::is_unsigned_v<Unsigned>)
gpu constexpr auto operator*=(Unsigned factor) noexcept -> Aeu& {
if constexpr (std::is_same_v<Unsigned, uint64_t>) {
const auto longerLength = filledBlocksNumber(), smallerLength = (factor > blockMax ? 2UL : 1UL);
const auto longerLength = filledBlocksNumber();
const auto smallerLength = (factor > blockMax ? 2UL : 1UL);
blockLine buffer {};

for(std::size_t i = 0; i < longerLength; ++i) {
Expand Down Expand Up @@ -463,7 +468,8 @@ class Aeu final {
blockLine buffer {};

for(std::size_t i = 0; i < longerLength; ++i) {
uint64_t tBlock = longerLine[i], carryOut = 0;
const uint64_t tBlock = longerLine[i];
uint64_t carryOut = 0;

for(std::size_t j = 0; j < smallerLength && i + j < buffer.size(); ++j) {
const auto product = tBlock * static_cast<uint64_t>(smallerLine[j]) + carryOut;
Expand All @@ -479,8 +485,8 @@ class Aeu final {
return buffer;
};

const std::size_t thisLength = this->filledBlocksNumber(), valueLength = factor.filledBlocksNumber();
if(thisLength > valueLength)
const std::size_t thisLength = this->filledBlocksNumber();
if(const std::size_t valueLength = factor.filledBlocksNumber(); thisLength > valueLength)
blocks = multiplyLines(blocks, thisLength, factor.blocks, valueLength);
else
blocks = multiplyLines(factor.blocks, valueLength, blocks, thisLength);
Expand Down Expand Up @@ -703,7 +709,7 @@ class Aeu final {
* @param other Aeu
* @return Boolean
*/
gpu constexpr auto operator==(const Aeu& other) const noexcept -> bool { return blocks == other.blocks; };
gpu constexpr auto operator==(const Aeu& other) const noexcept -> bool = default;

/**
* @brief Templated Equality check operator for numbers of different precision
Expand Down
92 changes: 57 additions & 35 deletions test/operations/display/unsigned.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,42 @@ TEST(Unsigned_Display, Zero) {

std::array<char, 10> askii{};
std::array<wchar_t, 10> utf{};
auto size = m.getString<10>(askii.data(), 10, false);
m.getString<10>(askii.data(), 10, false);
EXPECT_EQ(std::string_view(askii.data()), "0"sv);

askii = {};
size = m.getString<2>(askii.data(), 10, true);
m.getString<2>(askii.data(), 10, true);
EXPECT_EQ(std::string_view(askii.data()), "0b0"sv);

askii = {};
size = m.getString<8>(askii.data(), 10, true);
m.getString<8>(askii.data(), 10, true);
EXPECT_EQ(std::string_view(askii.data()), "0o0"sv);

askii = {};
size = m.getString<10>(askii.data(), 10, true);
m.getString<10>(askii.data(), 10, true);
EXPECT_EQ(std::string_view(askii.data()), "0"sv);

askii = {};
size = m.getString<16>(askii.data(), 10, true);
m.getString<16>(askii.data(), 10, true);
EXPECT_EQ(std::string_view(askii.data()), "0x0"sv);

size = m.getString<10>(utf.data(), 10);
m.getString<10>(utf.data(), 10);
EXPECT_EQ(std::wstring_view(utf.data()), L"0"sv);

utf = {};
size = m.getString<2>(utf.data(), 10, true);
m.getString<2>(utf.data(), 10, true);
EXPECT_EQ(std::wstring_view(utf.data()), L"0b0"sv);

utf = {};
size = m.getString<8>(utf.data(), 10, true);
m.getString<8>(utf.data(), 10, true);
EXPECT_EQ(std::wstring_view(utf.data()), L"0o0"sv);

utf = {};
size = m.getString<10>(utf.data(), 10, true);
m.getString<10>(utf.data(), 10, true);
EXPECT_EQ(std::wstring_view(utf.data()), L"0"sv);

utf = {};
size = m.getString<16>(utf.data(), 10, true);
m.getString<16>(utf.data(), 10, true);
EXPECT_EQ(std::wstring_view(utf.data()), L"0x0"sv);
}
}
Expand All @@ -70,7 +70,8 @@ TEST(Unsigned_Display, DecimalStreams) {
const auto value = Generation::getRandomWithBits(blocksNumber * 32 - 32);
const Aeu<blocksNumber * 32> aeu = value;

std::stringstream ss, ss2; ss << std::dec << std::noshowbase << value;
std::stringstream ss, ss2;
ss << std::dec << std::noshowbase << value;
ss2 << std::dec << std::noshowbase << aeu;
EXPECT_EQ(ss2.str(), ss.str());
}
Expand All @@ -79,8 +80,11 @@ TEST(Unsigned_Display, DecimalStreams) {
const auto value = Generation::getRandomWithBits(blocksNumber * 32 - 32);
const Aeu<blocksNumber * 32> aeu = value;

std::stringstream ss; ss << std::dec << std::noshowbase << value;
std::wstringstream ss2; ss2 << std::dec << std::noshowbase << aeu;
std::stringstream ss;
ss << std::dec << std::noshowbase << value;
std::wstringstream ss2;
ss2 << std::dec << std::noshowbase << aeu;

const auto& ref = ss.str();
std::wstring wstring (ref.begin(), ref.end());
EXPECT_EQ(ss2.str(), wstring);
Expand All @@ -94,19 +98,28 @@ TEST(Unsigned_Display, OctalStreams) {
const auto value = Generation::getRandomWithBits(blocksNumber * 32 - 32);
const Aeu<blocksNumber * 32> aeu = value;

std::stringstream ss, ss2; ss << "0o" << std::oct << std::noshowbase << value;
std::stringstream ss {};
ss << "0o" << std::oct << std::noshowbase << value;

std::stringstream ss2 {};
ss2 << "0o" << std::oct << std::noshowbase << aeu;

EXPECT_EQ(ss2.str(), ss.str());
}

for (std::size_t i = 0; i < testsAmount; ++i) {
const auto value = Generation::getRandomWithBits(blocksNumber * 32 - 32);
const Aeu<blocksNumber * 32> aeu = value;

std::stringstream ss; ss << "0o" << std::oct << std::noshowbase << value;
std::wstringstream ss2; ss2 << "0o" << std::oct << std::noshowbase << aeu;
std::stringstream ss {};
ss << "0o" << std::oct << std::noshowbase << value;

std::wstringstream ss2 {};
ss2 << "0o" << std::oct << std::noshowbase << aeu;

const auto& ref = ss.str();
std::wstring wstring (ref.begin(), ref.end());

EXPECT_EQ(ss2.str(), wstring);
}
}
Expand All @@ -118,19 +131,28 @@ TEST(Unsigned_Display, HexadecimalStreams) {
const auto value = Generation::getRandomWithBits(blocksNumber * 32 - 32);
const Aeu<blocksNumber * 32> aeu = value;

std::stringstream ss, ss2; ss << "0x" << std::hex << std::noshowbase << value;
std::stringstream ss {};
ss << "0x" << std::hex << std::noshowbase << value;

std::stringstream ss2 {};
ss2 << "0x" << std::hex << std::noshowbase << aeu;

EXPECT_EQ(ss2.str(), ss.str());
}

for (std::size_t i = 0; i < testsAmount; ++i) {
const auto value = Generation::getRandomWithBits(blocksNumber * 32 - 32);
const Aeu<blocksNumber * 32> aeu = value;

std::stringstream ss; ss << "0x" << std::hex << std::noshowbase << value;
std::wstringstream ss2; ss2 << "0x" << std::hex << std::noshowbase << aeu;
std::stringstream ss;
ss << "0x" << std::hex << std::noshowbase << value;

std::wstringstream ss2;
ss2 << "0x" << std::hex << std::noshowbase << aeu;

const auto& ref = ss.str();
std::wstring wstring (ref.begin(), ref.end());

EXPECT_EQ(ss2.str(), wstring);
}
}
Expand All @@ -155,22 +177,22 @@ TEST(Unsigned_Display, FormatAskii) {
switch(i % 5) {
case 0: { /* Octal */
ss << std::oct << std::noshowbase << value;
const auto size = aeu.getString<8>(askii.data(), askii.size(), false);
aeu.getString<8>(askii.data(), askii.size(), false);
break;
}
case 1: { /* Hexadecimal, lowercase */
ss << std::hex << std::noshowbase << std::nouppercase << value;
const auto size = aeu.getString<16>(askii.data(), askii.size(), false, false);
aeu.getString<16>(askii.data(), askii.size(), false, false);
break;
}
case 2: { /* Hexadecimal, uppercase */
ss << std::hex << std::noshowbase << std::uppercase << value;
const auto size = aeu.getString<16>(askii.data(), askii.size(), false, true);
aeu.getString<16>(askii.data(), askii.size(), false, true);
break;
}
case 3: { /* Decimal */
ss << std::dec << std::noshowbase << value;
const auto size = aeu.getString<10>(askii.data(), askii.size(), false);
aeu.getString<10>(askii.data(), askii.size(), false);
break;
}
default: { /* Binary */
Expand Down Expand Up @@ -208,22 +230,22 @@ TEST(Unsigned_Display, FormatUtf) {
switch (i % 5) {
case 0: { /* Octal */
ss << std::oct << std::noshowbase << value;
const auto size = aeu.getString<8>(utf.data(), utf.size(), false);
aeu.getString<8>(utf.data(), utf.size(), false);
break;
}
case 1: { /* Hexadecimal, lowercase */
ss << std::hex << std::noshowbase << std::nouppercase << value;
const auto size = aeu.getString<16>(utf.data(), utf.size(), false, false);
aeu.getString<16>(utf.data(), utf.size(), false, false);
break;
}
case 2: { /* Hexadecimal, uppercase */
ss << std::hex << std::noshowbase << std::uppercase << value;
const auto size = aeu.getString<16>(utf.data(), utf.size(), false, true);
aeu.getString<16>(utf.data(), utf.size(), false, true);
break;
}
case 3: { /* Decimal */
ss << std::dec << std::noshowbase << value;
const auto size = aeu.getString<10>(utf.data(), utf.size(), false);
aeu.getString<10>(utf.data(), utf.size(), false);
break;
}
default: { /* Binary */
Expand Down Expand Up @@ -292,25 +314,25 @@ TEST(Unsigned_Display, ShowBaseAskii) {
}
case 4: { /* C-style ASKII Octal */
ss << "0o" << std::oct << std::noshowbase << value;
const auto size = aeu.getString<8>(askii.data(), askii.size(), true);
aeu.getString<8>(askii.data(), askii.size(), true);
EXPECT_EQ(std::string_view(askii.data()), ss.str());
break;
}
case 5: { /* C-style ASKII Hexadecimal, lowercase */
ss << "0x" << std::hex << std::noshowbase << std::nouppercase << value;
const auto size = aeu.getString<16>(askii.data(), askii.size(), true, false);
aeu.getString<16>(askii.data(), askii.size(), true, false);
EXPECT_EQ(std::string_view(askii.data()), ss.str());
break;
}
case 6: { /* C-style ASKII Hexadecimal, uppercase */
ss << "0x" << std::hex << std::noshowbase << std::nouppercase << value;
const auto size = aeu.getString<16>(askii.data(), askii.size(), true, false);
aeu.getString<16>(askii.data(), askii.size(), true, false);
EXPECT_EQ(std::string_view(askii.data()), ss.str());
break;
}
case 7: { /* C-style ASKII Decimal */
ss << std::dec << std::noshowbase << value;
const auto size = aeu.getString<10>(askii.data(), askii.size(), true);
aeu.getString<10>(askii.data(), askii.size(), true);
EXPECT_EQ(std::string_view(askii.data()), ss.str());
break;
}
Expand Down Expand Up @@ -386,31 +408,31 @@ TEST(Unsigned_Display, ShowBaseUtf) {
}
case 4: { /* C-style ASKII Octal */
ss << "0o" << std::oct << std::noshowbase << value;
const auto size = aeu.getString<8>(utf.data(), utf.size(), true);
aeu.getString<8>(utf.data(), utf.size(), true);
const auto &ref = ss.str();
const std::wstring comparative(ref.begin(), ref.end());
EXPECT_EQ(std::wstring_view(utf.data()), comparative);
break;
}
case 5: { /* C-style ASKII Hexadecimal, lowercase */
ss << "0x" << std::hex << std::noshowbase << std::nouppercase << value;
const auto size = aeu.getString<16>(utf.data(), utf.size(), true, false);
aeu.getString<16>(utf.data(), utf.size(), true, false);
const auto &ref = ss.str();
const std::wstring comparative(ref.begin(), ref.end());
EXPECT_EQ(std::wstring_view(utf.data()), comparative);
break;
}
case 6: { /* C-style ASKII Hexadecimal, uppercase */
ss << "0x" << std::hex << std::noshowbase << std::nouppercase << value;
const auto size = aeu.getString<16>(utf.data(), utf.size(), true, false);
aeu.getString<16>(utf.data(), utf.size(), true, false);
const auto &ref = ss.str();
const std::wstring comparative(ref.begin(), ref.end());
EXPECT_EQ(std::wstring_view(utf.data()), comparative);
break;
}
case 7: { /* C-style ASKII Decimal */
ss << std::dec << std::noshowbase << value;
const auto size = aeu.getString<10>(utf.data(), utf.size(), true);
aeu.getString<10>(utf.data(), utf.size(), true);
const auto &ref = ss.str();
const std::wstring comparative(ref.begin(), ref.end());
EXPECT_EQ(std::wstring_view(utf.data()), comparative);
Expand Down
Loading