|
/* |
|
* mul.cc - Copyright (c) 2024-2025 - Olivier Poncet |
|
* |
|
* This program is free software: you can redistribute it and/or modify |
|
* it under the terms of the GNU General Public License as published by |
|
* the Free Software Foundation, either version 2 of the License, or |
|
* (at your option) any later version. |
|
* |
|
* This program is distributed in the hope that it will be useful, |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
* GNU General Public License for more details. |
|
* |
|
* You should have received a copy of the GNU General Public License |
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
*/ |
|
#include <cstdio> |
|
#include <cstdlib> |
|
#include <cstring> |
|
#include <cstdint> |
|
#include <iostream> |
|
#include <stdexcept> |
|
|
|
// --------------------------------------------------------------------------- |
|
// Russian Multiplication Method |
|
// --------------------------------------------------------------------------- |
|
|
|
auto mul(uint16_t value, uint16_t scale) -> int16_t |
|
{ |
|
uint16_t result = 0; |
|
|
|
if(scale != 0) { |
|
do { |
|
if((scale & 1) != 0) { |
|
result += value; |
|
} |
|
scale >>= 1; |
|
value <<= 1; |
|
} while(scale != 0); |
|
} |
|
return result; |
|
} |
|
|
|
// --------------------------------------------------------------------------- |
|
// Assert |
|
// --------------------------------------------------------------------------- |
|
|
|
struct Assert |
|
{ |
|
static auto begin(const std::string& group) -> void |
|
{ |
|
success_count = 0; |
|
failure_count = 0; |
|
static_cast<void>(fprintf(stdout, "[ TEST ] %s\n", group.c_str())); |
|
static_cast<void>(fflush(stdout)); |
|
} |
|
|
|
static auto end(const std::string& group) -> void |
|
{ |
|
static_cast<void>(fprintf(stdout, "[ DONE ] %s [success=%d, failure=%d]\n", group.c_str(), success_count, failure_count)); |
|
static_cast<void>(fflush(stdout)); |
|
} |
|
|
|
static auto pass() -> const char* |
|
{ |
|
++success_count; |
|
return "PASS"; |
|
} |
|
|
|
static auto fail() -> const char* |
|
{ |
|
++failure_count; |
|
return "FAIL"; |
|
} |
|
|
|
static auto equals(const int expected, const int value) -> bool |
|
{ |
|
if(value == expected) { |
|
return true; |
|
} |
|
throw std::runtime_error(std::string("Assert::equals<int>() has failed") + ' ' + '(' + std::to_string(value) + " != " + std::to_string(expected) + ')'); |
|
} |
|
|
|
static int success_count; |
|
static int failure_count; |
|
}; |
|
|
|
int Assert::success_count = 0; |
|
int Assert::failure_count = 0; |
|
|
|
// --------------------------------------------------------------------------- |
|
// TestGroup |
|
// --------------------------------------------------------------------------- |
|
|
|
class TestGroup |
|
{ |
|
public: |
|
TestGroup(const std::string& group) |
|
: _group(group) |
|
{ |
|
Assert::begin(_group); |
|
} |
|
|
|
~TestGroup() |
|
{ |
|
Assert::end(_group); |
|
} |
|
|
|
void check() const |
|
{ |
|
if(Assert::failure_count != 0) { |
|
throw std::runtime_error(_group + ' ' + "has failed!"); |
|
} |
|
} |
|
|
|
private: |
|
const std::string _group; |
|
}; |
|
|
|
// --------------------------------------------------------------------------- |
|
// Program |
|
// --------------------------------------------------------------------------- |
|
|
|
struct Program |
|
{ |
|
static auto check_mul(const int lhs, const int rhs) -> void |
|
{ |
|
const int expected = (lhs * rhs); |
|
bool status = false; |
|
int result = 0; |
|
|
|
auto pass = [&]() -> void |
|
{ |
|
static_cast<void>(fprintf(stdout, "[ %s ] mul(%+d, %+d) -> %+d\n", Assert::pass(), lhs, rhs, result)); |
|
static_cast<void>(fflush(stdout)); |
|
}; |
|
|
|
auto fail = [&]() -> void |
|
{ |
|
static_cast<void>(fprintf(stdout, "[ %s ] mul(%+d, %+d) -> %+d (expected %+d)\n", Assert::fail(), lhs, rhs, result, expected)); |
|
static_cast<void>(fflush(stdout)); |
|
}; |
|
|
|
auto check = [&]() -> void |
|
{ |
|
try { |
|
result = mul(lhs, rhs); |
|
status = Assert::equals(expected, result); |
|
} |
|
catch(const std::exception& e) { |
|
status = false; |
|
} |
|
if(status != false) { |
|
pass(); |
|
} |
|
else { |
|
fail(); |
|
} |
|
}; |
|
|
|
return check(); |
|
} |
|
|
|
static auto main() -> void |
|
{ |
|
const TestGroup group("« Russian Multiplication Method »"); |
|
|
|
check_mul(0, 9); |
|
check_mul(1, 8); |
|
check_mul(2, 7); |
|
check_mul(3, 6); |
|
check_mul(4, 5); |
|
check_mul(5, 4); |
|
check_mul(6, 3); |
|
check_mul(7, 2); |
|
check_mul(8, 1); |
|
check_mul(9, 0); |
|
check_mul(-127, -257); |
|
check_mul(-127, +257); |
|
check_mul(+127, -257); |
|
check_mul(+127, +257); |
|
|
|
return group.check(); |
|
} |
|
}; |
|
|
|
// --------------------------------------------------------------------------- |
|
// main |
|
// --------------------------------------------------------------------------- |
|
|
|
int main(int argc, char* argv[]) |
|
{ |
|
try { |
|
Program::main(); |
|
} |
|
catch(const std::exception& e) { |
|
std::cerr << "fatal error: " << e.what() << std::endl; |
|
return EXIT_FAILURE; |
|
} |
|
return EXIT_SUCCESS; |
|
} |
|
|
|
// --------------------------------------------------------------------------- |
|
// End-Of-File |
|
// --------------------------------------------------------------------------- |