why add, when we can do it our self.
#include <bitset> #include <fmt/core.h> std::bitset<64> bitwiseAdd(std::bitset<64> x, std::bitset<64> y) { while ((x & y).any()) { std::bitset<64> carry = (x & y) << 1; x = x ^ y; y = carry; } return x ^ y; } int64_t multiply(int32_t a, int32_t b) { std::bitset<64> bitsetA(static_cast<uint64_t>(a)); std::bitset<64> bitsetB(static_cast<uint64_t>(b)); std::bitset<64> result; for (int i = 0; i < 64; ++i) { if (bitsetB.test(i)) { auto shiftedA = (bitsetA << i); result = bitwiseAdd(result, shiftedA); } } return static_cast<int64_t>(result.to_ullong()); }
int just_why(int a, int b) { return b ? a + just_why(a, b - 1) : 0; }int multiply(int a, int b) { return just_why(a, b); }- #include <bitset>
- #include <fmt/core.h>
- std::bitset<64> bitwiseAdd(std::bitset<64> x, std::bitset<64> y) {
- while ((x & y).any()) {
- std::bitset<64> carry = (x & y) << 1;
- x = x ^ y;
- y = carry;
- }
- return x ^ y;
- }
- int64_t multiply(int32_t a, int32_t b) {
- std::bitset<64> bitsetA(static_cast<uint64_t>(a));
- std::bitset<64> bitsetB(static_cast<uint64_t>(b));
- std::bitset<64> result;
- for (int i = 0; i < 64; ++i) {
- if (bitsetB.test(i)) {
- auto shiftedA = (bitsetA << i);
- result = bitwiseAdd(result, shiftedA);
- }
- }
- return static_cast<int64_t>(result.to_ullong());
- }
// TODO: Replace examples and use TDD by writing your own tests. The code provided here is just a how-to example. #include <criterion/criterion.h> // replace with the actual method being tested int multiply(int a,int b); Test(the_multiply_function, should_pass_all_the_tests_provided) { cr_assert_eq(multiply(1, 1), 1); cr_assert_eq(multiply(2, 2), 4); cr_assert_eq(multiply(2, -2), -4); cr_assert_eq(multiply(-2, 2), -4); cr_assert_eq(multiply(-2, -2), 4); cr_assert_eq(multiply(3, 7), 21); cr_assert_eq(multiply(3, -7), -21); cr_assert_eq(multiply(-3, -7), 21); }
- // TODO: Replace examples and use TDD by writing your own tests. The code provided here is just a how-to example.
- #include <criterion/criterion.h>
- // replace with the actual method being tested
- int multiply(int a,int b);
- Test(the_multiply_function, should_pass_all_the_tests_provided) {
- cr_assert_eq(multiply(1, 1), 1);
- cr_assert_eq(multiply(2, 2), 4);
- cr_assert_eq(multiply(2, -2), -4);
- cr_assert_eq(multiply(-2, 2), -4);
- cr_assert_eq(multiply(-2, -2), 4);
- cr_assert_eq(multiply(3, 7), 21);
- cr_assert_eq(multiply(3, -7), -21);
- cr_assert_eq(multiply(-3, -7), 21);
- }
Might have cone a bit crazy with the tests, but hey now you should be able to pass any container that satisfy the requirement. And if it doesn't it wont compile.
#include <string> #include <vector> #include <algorithm> template<typename Container> bool containsT_Rex(const Container &things){ using ValueType = typename Container::value_type; static_assert(std::is_convertible_v<decltype(std::declval<ValueType>() == std::declval<std::string>()), bool>, "Container elements must be comparable to std::string"); static constexpr auto trex = "Tyrannosaurus"; return std::find(things.cbegin(), things.cend(), trex) != things.cend(); }
public class findT_Rex {public static boolean containsT_Rex(String[] things) {//insert code!return true;}- #include <string>
- #include <vector>
- #include <algorithm>
- template<typename Container>
- bool containsT_Rex(const Container &things){
- using ValueType = typename Container::value_type;
- static_assert(std::is_convertible_v<decltype(std::declval<ValueType>() == std::declval<std::string>()), bool>, "Container elements must be comparable to std::string");
- static constexpr auto trex = "Tyrannosaurus";
- return std::find(things.cbegin(), things.cend(), trex) != things.cend();
- }
#include <vector> #include <set> #include <array> #include <list> #include <forward_list> #include <string> #include <string_view> // Helper type for SFINAE template <typename, typename = void> struct is_comparable_to_string : std::false_type {}; // Specialization when T can be compared to std::string template <typename T> struct is_comparable_to_string< T, std::void_t<decltype(std::declval<typename T::value_type>() == std::declval<std::string>())> > : std::true_type {}; // Alias for cleaner syntax template <typename T> constexpr bool is_comparable_to_string_v = is_comparable_to_string<T>::value; Describe(containsT_Rex_string) { It(does_contains_trex) { Assert::That(containsT_Rex(std::vector<std::string>{"Jackie Chan", "Charlize Theron", "Tyrannosaurus", "Tom Hardy", "Ruby Rose"}), Equals(true)); } It(does_not_contains_trex) { Assert::That(containsT_Rex(std::vector<std::string>{"Triceratops", "Megalosaurus", "Spinosaurus", "Archaeopteryx"}), Equals(false)); Assert::That(containsT_Rex(std::vector<std::string>{}), Equals(false)); } }; Describe(containsT_Rex_char_pointer) { It(does_contains_trex) { Assert::That(containsT_Rex(std::vector<const char*>{"Jackie Chan", "Charlize Theron", "Tyrannosaurus", "Tom Hardy", "Ruby Rose"}), Equals(true)); } It(does_not_contains_trex) { Assert::That(containsT_Rex(std::vector<const char*>{"Triceratops", "Megalosaurus", "Spinosaurus", "Archaeopteryx"}), Equals(false)); Assert::That(containsT_Rex(std::vector<const char*>{}), Equals(false)); } }; Describe(containsT_Rex_string_view) { It(does_contains_trex) { Assert::That(containsT_Rex(std::vector<std::string_view>{"Jackie Chan", "Charlize Theron", "Tyrannosaurus", "Tom Hardy", "Ruby Rose"}), Equals(true)); } It(does_not_contains_trex) { Assert::That(containsT_Rex(std::vector<std::string_view>{"Triceratops", "Megalosaurus", "Spinosaurus", "Archaeopteryx"}), Equals(false)); Assert::That(containsT_Rex(std::vector<std::string_view>{}), Equals(false)); } }; Describe(container_test) { It(should_work) { Assert::That(is_comparable_to_string_v<std::vector<std::string>>, Equals(true), "vector of string should work"); Assert::That(is_comparable_to_string_v<std::list<std::string>>, Equals(true), "list of string should work"); Assert::That(is_comparable_to_string_v<std::vector<const char*>>, Equals(true), "vector of const char* should work"); Assert::That(is_comparable_to_string_v<std::list<const char*>>, Equals(true), "list of const char* should work"); Assert::That(is_comparable_to_string_v<std::deque<std::string>>, Equals(true), "deque of string should work"); Assert::That(is_comparable_to_string_v<std::vector<std::string_view>>, Equals(true), "vector of string_view should work"); } It(should_not_work) { Assert::That(is_comparable_to_string_v<std::vector<int>>, Equals(false), "vector of int should not work"); Assert::That(is_comparable_to_string_v<std::list<double>>, Equals(false), "list of double should not work"); Assert::That(is_comparable_to_string_v<std::deque<void*>>, Equals(false), "deque of void* should not work"); Assert::That(is_comparable_to_string_v<std::vector<std::nullptr_t>>, Equals(false), "vector of nullptr_t should not work"); Assert::That(is_comparable_to_string_v<std::array<int, 4>>, Equals(false), "array of int should not work"); } It(mixed_edge_cases) { Assert::That(is_comparable_to_string_v<std::vector<char*>>, Equals(true), "vector of char* should work"); Assert::That(is_comparable_to_string_v<std::list<const char*>>, Equals(true), "list of const char* should work"); Assert::That(is_comparable_to_string_v<std::deque<const char*>>, Equals(true), "deque of const char* should work"); Assert::That(is_comparable_to_string_v<std::vector<std::pair<int, std::string>>>, Equals(false), "vector of pair<int, string> should not work"); Assert::That(is_comparable_to_string_v<std::list<std::pair<const char*, const char*>>>, Equals(false), "list of pair<const char*, const char*> should not work"); } }; Describe(containsT_Rex_string_difcontainer) { It(does_contains_trex) { Assert::That(containsT_Rex(std::array<std::string, 5>{"Jackie Chan", "Charlize Theron", "Tyrannosaurus", "Tom Hardy", "Ruby Rose"}), Equals(true)); Assert::That(containsT_Rex(std::deque<std::string>{"Jackie Chan", "Charlize Theron", "Tyrannosaurus", "Tom Hardy", "Ruby Rose"}), Equals(true)); Assert::That(containsT_Rex(std::list<std::string>{"Jackie Chan", "Charlize Theron", "Tyrannosaurus", "Tom Hardy", "Ruby Rose"}), Equals(true)); Assert::That(containsT_Rex(std::set<std::string>{"Jackie Chan", "Charlize Theron", "Tyrannosaurus", "Tom Hardy", "Ruby Rose"}), Equals(true)); Assert::That(containsT_Rex(std::forward_list<std::string>{"Jackie Chan", "Charlize Theron", "Tyrannosaurus", "Tom Hardy", "Ruby Rose"}), Equals(true)); } It(does_not_contains_trex) { Assert::That(containsT_Rex(std::array<std::string, 4>{"Triceratops", "Megalosaurus", "Spinosaurus", "Archaeopteryx"}), Equals(false)); Assert::That(containsT_Rex(std::deque<std::string>{"Triceratops", "Megalosaurus", "Spinosaurus", "Archaeopteryx"}), Equals(false)); Assert::That(containsT_Rex(std::list<std::string>{"Triceratops", "Megalosaurus", "Spinosaurus", "Archaeopteryx"}), Equals(false)); Assert::That(containsT_Rex(std::set<std::string>{"Triceratops", "Megalosaurus", "Spinosaurus", "Archaeopteryx"}), Equals(false)); Assert::That(containsT_Rex(std::forward_list<std::string>{"Triceratops", "Megalosaurus", "Spinosaurus", "Archaeopteryx"}), Equals(false)); Assert::That(containsT_Rex(std::array<std::string, 0>{}), Equals(false)); Assert::That(containsT_Rex(std::deque<std::string>{}), Equals(false)); Assert::That(containsT_Rex(std::list<std::string>{}), Equals(false)); Assert::That(containsT_Rex(std::set<std::string>{}), Equals(false)); Assert::That(containsT_Rex(std::forward_list<std::string>{}), Equals(false)); } };
import org.junit.Test;import static org.junit.Assert.assertEquals;- #include <vector>
- #include <set>
- #include <array>
- #include <list>
- #include <forward_list>
- #include <string>
- #include <string_view>
// TODO: Replace examples and use TDD by writing your own testsclass SolutionTest {@Testpublic void tests() {String[] things1 = new String[] {"Triceratops", "Megalosaurus", "Spinosaurus", "Archaeopteryx"};String[] things2 = new String[] {"Jackie Chan", "Charlize Theron", "Tyrannosaurus", "Tom Hardy", "Ruby Rose"};String[] things3 = new String[] {""};- // Helper type for SFINAE
- template <typename, typename = void>
- struct is_comparable_to_string : std::false_type {};
- // Specialization when T can be compared to std::string
- template <typename T>
- struct is_comparable_to_string<
- T,
- std::void_t<decltype(std::declval<typename T::value_type>() == std::declval<std::string>())>
- > : std::true_type {};
- // Alias for cleaner syntax
- template <typename T>
- constexpr bool is_comparable_to_string_v = is_comparable_to_string<T>::value;
- Describe(containsT_Rex_string)
- {
- It(does_contains_trex)
- {
- Assert::That(containsT_Rex(std::vector<std::string>{"Jackie Chan", "Charlize Theron", "Tyrannosaurus", "Tom Hardy", "Ruby Rose"}), Equals(true));
- }
- It(does_not_contains_trex)
- {
- Assert::That(containsT_Rex(std::vector<std::string>{"Triceratops", "Megalosaurus", "Spinosaurus", "Archaeopteryx"}), Equals(false));
- Assert::That(containsT_Rex(std::vector<std::string>{}), Equals(false));
- }
- };
- Describe(containsT_Rex_char_pointer)
- {
- It(does_contains_trex)
- {
- Assert::That(containsT_Rex(std::vector<const char*>{"Jackie Chan", "Charlize Theron", "Tyrannosaurus", "Tom Hardy", "Ruby Rose"}), Equals(true));
- }
- It(does_not_contains_trex)
- {
- Assert::That(containsT_Rex(std::vector<const char*>{"Triceratops", "Megalosaurus", "Spinosaurus", "Archaeopteryx"}), Equals(false));
- Assert::That(containsT_Rex(std::vector<const char*>{}), Equals(false));
- }
- };
- Describe(containsT_Rex_string_view)
- {
- It(does_contains_trex)
- {
- Assert::That(containsT_Rex(std::vector<std::string_view>{"Jackie Chan", "Charlize Theron", "Tyrannosaurus", "Tom Hardy", "Ruby Rose"}), Equals(true));
- }
- It(does_not_contains_trex)
- {
- Assert::That(containsT_Rex(std::vector<std::string_view>{"Triceratops", "Megalosaurus", "Spinosaurus", "Archaeopteryx"}), Equals(false));
- Assert::That(containsT_Rex(std::vector<std::string_view>{}), Equals(false));
- }
- };
- Describe(container_test)
- {
- It(should_work)
- {
- Assert::That(is_comparable_to_string_v<std::vector<std::string>>, Equals(true), "vector of string should work");
- Assert::That(is_comparable_to_string_v<std::list<std::string>>, Equals(true), "list of string should work");
- Assert::That(is_comparable_to_string_v<std::vector<const char*>>, Equals(true), "vector of const char* should work");
- Assert::That(is_comparable_to_string_v<std::list<const char*>>, Equals(true), "list of const char* should work");
- Assert::That(is_comparable_to_string_v<std::deque<std::string>>, Equals(true), "deque of string should work");
- Assert::That(is_comparable_to_string_v<std::vector<std::string_view>>, Equals(true), "vector of string_view should work");
- }
- It(should_not_work)
- {
- Assert::That(is_comparable_to_string_v<std::vector<int>>, Equals(false), "vector of int should not work");
- Assert::That(is_comparable_to_string_v<std::list<double>>, Equals(false), "list of double should not work");
- Assert::That(is_comparable_to_string_v<std::deque<void*>>, Equals(false), "deque of void* should not work");
- Assert::That(is_comparable_to_string_v<std::vector<std::nullptr_t>>, Equals(false), "vector of nullptr_t should not work");
- Assert::That(is_comparable_to_string_v<std::array<int, 4>>, Equals(false), "array of int should not work");
- }
- It(mixed_edge_cases)
- {
- Assert::That(is_comparable_to_string_v<std::vector<char*>>, Equals(true), "vector of char* should work");
- Assert::That(is_comparable_to_string_v<std::list<const char*>>, Equals(true), "list of const char* should work");
- Assert::That(is_comparable_to_string_v<std::deque<const char*>>, Equals(true), "deque of const char* should work");
- Assert::That(is_comparable_to_string_v<std::vector<std::pair<int, std::string>>>, Equals(false), "vector of pair<int, string> should not work");
- Assert::That(is_comparable_to_string_v<std::list<std::pair<const char*, const char*>>>, Equals(false), "list of pair<const char*, const char*> should not work");
- }
- };
- Describe(containsT_Rex_string_difcontainer)
- {
- It(does_contains_trex)
- {
- Assert::That(containsT_Rex(std::array<std::string, 5>{"Jackie Chan", "Charlize Theron", "Tyrannosaurus", "Tom Hardy", "Ruby Rose"}), Equals(true));
- Assert::That(containsT_Rex(std::deque<std::string>{"Jackie Chan", "Charlize Theron", "Tyrannosaurus", "Tom Hardy", "Ruby Rose"}), Equals(true));
- Assert::That(containsT_Rex(std::list<std::string>{"Jackie Chan", "Charlize Theron", "Tyrannosaurus", "Tom Hardy", "Ruby Rose"}), Equals(true));
- Assert::That(containsT_Rex(std::set<std::string>{"Jackie Chan", "Charlize Theron", "Tyrannosaurus", "Tom Hardy", "Ruby Rose"}), Equals(true));
- Assert::That(containsT_Rex(std::forward_list<std::string>{"Jackie Chan", "Charlize Theron", "Tyrannosaurus", "Tom Hardy", "Ruby Rose"}), Equals(true));
- }
- It(does_not_contains_trex)
- {
- Assert::That(containsT_Rex(std::array<std::string, 4>{"Triceratops", "Megalosaurus", "Spinosaurus", "Archaeopteryx"}), Equals(false));
- Assert::That(containsT_Rex(std::deque<std::string>{"Triceratops", "Megalosaurus", "Spinosaurus", "Archaeopteryx"}), Equals(false));
- Assert::That(containsT_Rex(std::list<std::string>{"Triceratops", "Megalosaurus", "Spinosaurus", "Archaeopteryx"}), Equals(false));
- Assert::That(containsT_Rex(std::set<std::string>{"Triceratops", "Megalosaurus", "Spinosaurus", "Archaeopteryx"}), Equals(false));
- Assert::That(containsT_Rex(std::forward_list<std::string>{"Triceratops", "Megalosaurus", "Spinosaurus", "Archaeopteryx"}), Equals(false));
// assertEquals("expected", "actual");assertEquals(false, containsT_Rex(things1));assertEquals(true, containsT_Rex(things2));assertEquals(false, containsT_Rex(things3));- Assert::That(containsT_Rex(std::array<std::string, 0>{}), Equals(false));
- Assert::That(containsT_Rex(std::deque<std::string>{}), Equals(false));
- Assert::That(containsT_Rex(std::list<std::string>{}), Equals(false));
- Assert::That(containsT_Rex(std::set<std::string>{}), Equals(false));
- Assert::That(containsT_Rex(std::forward_list<std::string>{}), Equals(false));
- }
}- };
#include <type_traits> #include <limits> #include <bitset> template<typename T> constexpr bool is_more_than_2(T n){ static_assert(std::is_integral_v<T>, "T must be an integral type."); constexpr size_t numBits = std::numeric_limits<T>::digits + std::is_signed_v<T>; std::bitset<numBits> bits(n); if constexpr (std::is_unsigned_v<T>) { // For unsigned, check if more than 1 bit is set return bits.count() > 1; } else { // For signed, ensure the sign bit is unset and more than 1 bit is set bool signBitUnset = !bits[numBits - 1]; bits.reset(numBits - 1); // Mask out the sign bit return signBitUnset && bits.count() > 1; } }
def is_more_than_2(n):return n > 2- #include <type_traits>
- #include <limits>
- #include <bitset>
- template<typename T>
- constexpr bool is_more_than_2(T n){
- static_assert(std::is_integral_v<T>, "T must be an integral type.");
- constexpr size_t numBits = std::numeric_limits<T>::digits + std::is_signed_v<T>;
- std::bitset<numBits> bits(n);
- if constexpr (std::is_unsigned_v<T>) {
- // For unsigned, check if more than 1 bit is set
- return bits.count() > 1;
- } else {
- // For signed, ensure the sign bit is unset and more than 1 bit is set
- bool signBitUnset = !bits[numBits - 1];
- bits.reset(numBits - 1); // Mask out the sign bit
- return signBitUnset && bits.count() > 1;
- }
- }
#include <limits> Describe(is_more_than_2_test) { It(signed_values) { Assert::That(is_more_than_2(std::numeric_limits<int>::min()), Equals(false)); Assert::That(is_more_than_2(-3), Equals(false)); Assert::That(is_more_than_2(-2), Equals(false)); Assert::That(is_more_than_2(-1), Equals(false)); Assert::That(is_more_than_2(0), Equals(false)); Assert::That(is_more_than_2(1), Equals(false)); Assert::That(is_more_than_2(2), Equals(false)); Assert::That(is_more_than_2(3), Equals(true)); Assert::That(is_more_than_2(10), Equals(true)); Assert::That(is_more_than_2(std::numeric_limits<int>::max()), Equals(true)); } It(unsigned_values) { Assert::That(is_more_than_2(0u), Equals(false)); Assert::That(is_more_than_2(1u), Equals(false)); Assert::That(is_more_than_2(2u), Equals(false)); Assert::That(is_more_than_2(3u), Equals(true)); Assert::That(is_more_than_2(10u), Equals(true)); Assert::That(is_more_than_2(std::numeric_limits<unsigned>::max()), Equals(true)); } };
import codewars_test as testimport random# TODO Write testsimport solution # or from solution import example- #include <limits>
# test.assert_equals(actual, expected, [optional] message)@test.describe("Random test cases")def random_tests():@test.it("Random test")def test_case():for x in range(100):randomValue = random.randint(-1000, 1000)test.assert_equals(is_more_than_2(randomValue), randomValue > 2)- Describe(is_more_than_2_test)
- {
- It(signed_values)
- {
- Assert::That(is_more_than_2(std::numeric_limits<int>::min()), Equals(false));
- Assert::That(is_more_than_2(-3), Equals(false));
- Assert::That(is_more_than_2(-2), Equals(false));
- Assert::That(is_more_than_2(-1), Equals(false));
- Assert::That(is_more_than_2(0), Equals(false));
- Assert::That(is_more_than_2(1), Equals(false));
- Assert::That(is_more_than_2(2), Equals(false));
- Assert::That(is_more_than_2(3), Equals(true));
- Assert::That(is_more_than_2(10), Equals(true));
- Assert::That(is_more_than_2(std::numeric_limits<int>::max()), Equals(true));
- }
- It(unsigned_values)
- {
- Assert::That(is_more_than_2(0u), Equals(false));
- Assert::That(is_more_than_2(1u), Equals(false));
- Assert::That(is_more_than_2(2u), Equals(false));
- Assert::That(is_more_than_2(3u), Equals(true));
- Assert::That(is_more_than_2(10u), Equals(true));
- Assert::That(is_more_than_2(std::numeric_limits<unsigned>::max()), Equals(true));
- }
- };
Checked math in c++, using some buildins for gcc/clang. Translated from Rust.
Can be made without the buildins, but you would have to make your own boundary checks.
#include <optional> #include <limits> std::optional<int32_t> add_checked(int32_t a, int32_t b){ int32_t r{}; if(!__builtin_sadd_overflow(a, b, &r)){ return r; } return std::nullopt; } std::optional<int32_t> sub_checked(int32_t a, int32_t b){ int32_t r{}; if(!__builtin_ssub_overflow(a, b, &r)){ return r; } return std::nullopt; } std::optional<int32_t> mul_checked(int32_t a, int32_t b){ int32_t r{}; if(!__builtin_smul_overflow(a, b, &r)){ return r; } return std::nullopt; } std::optional<int32_t> div_checked(int32_t a, int32_t b){ if(b == 0 || (a == std::numeric_limits<int32_t>::min() && b == -1)){ return std::nullopt; } return a / b; } std::optional<int32_t> mod_checked(int32_t a, int32_t b){ if(b == 0 || (a == std::numeric_limits<int32_t>::min() && b == -1)){ return std::nullopt; } return a % b; } std::optional<int32_t> calculator(Operation op, int32_t x, int32_t y) { switch(op){ case Operation::Add:return add_checked(x, y); case Operation::Subtract: return sub_checked(x, y); case Operation::Multiply: return mul_checked(x, y); case Operation::Divide: return div_checked(x, y); case Operation::Modulo: return mod_checked(x, y); default: return std::nullopt; } }
mod preloaded;use preloaded::Operation;- #include <optional>
- #include <limits>
fn calculator(op: Operation, x: i32, y: i32) -> Option<i32> {match op {Operation::Add => x.checked_add(y),Operation::Subtract => x.checked_sub(y),Operation::Multiply => x.checked_mul(y),Operation::Divide => x.checked_div(y),Operation::Modulo => x.checked_rem(y)}- std::optional<int32_t> add_checked(int32_t a, int32_t b){
- int32_t r{};
- if(!__builtin_sadd_overflow(a, b, &r)){
- return r;
- }
- return std::nullopt;
- }
- std::optional<int32_t> sub_checked(int32_t a, int32_t b){
- int32_t r{};
- if(!__builtin_ssub_overflow(a, b, &r)){
- return r;
- }
- return std::nullopt;
- }
- std::optional<int32_t> mul_checked(int32_t a, int32_t b){
- int32_t r{};
- if(!__builtin_smul_overflow(a, b, &r)){
- return r;
- }
- return std::nullopt;
- }
- std::optional<int32_t> div_checked(int32_t a, int32_t b){
- if(b == 0 || (a == std::numeric_limits<int32_t>::min() && b == -1)){
- return std::nullopt;
- }
- return a / b;
- }
- std::optional<int32_t> mod_checked(int32_t a, int32_t b){
- if(b == 0 || (a == std::numeric_limits<int32_t>::min() && b == -1)){
- return std::nullopt;
- }
- return a % b;
- }
- std::optional<int32_t> calculator(Operation op, int32_t x, int32_t y) {
- switch(op){
- case Operation::Add:return add_checked(x, y);
- case Operation::Subtract: return sub_checked(x, y);
- case Operation::Multiply: return mul_checked(x, y);
- case Operation::Divide: return div_checked(x, y);
- case Operation::Modulo: return mod_checked(x, y);
- default:
- return std::nullopt;
- }
- }
Describe(Sample_Tests) { It(Valid_Input_Test) { Assert::That(calculator(Operation::Add, 1, 1), Equals(2)); Assert::That(calculator(Operation::Add, 16, 9), Equals(25)); Assert::That(calculator(Operation::Subtract, 1, 1), Equals(0)); Assert::That(calculator(Operation::Subtract, 57, 62), Equals(-5)); Assert::That(calculator(Operation::Multiply, -10, 10), Equals(-100)); Assert::That(calculator(Operation::Multiply, -12, -12), Equals(144)); Assert::That(calculator(Operation::Divide, 2, 4), Equals(0)); Assert::That(calculator(Operation::Divide, 4, 2), Equals(2)); Assert::That(calculator(Operation::Modulo, 3, 5), Equals(3)); Assert::That(calculator(Operation::Modulo, 5, 3), Equals(2)); } It(Invalid_Input_Test) { Assert::That(calculator(Operation::Add, std::numeric_limits<int32_t>::max(), 1), Equals(std::nullopt)); Assert::That(calculator(Operation::Subtract, std::numeric_limits<int32_t>::min(), 1), Equals(std::nullopt)); Assert::That(calculator(Operation::Multiply, std::numeric_limits<int32_t>::max(), 2), Equals(std::nullopt)); Assert::That(calculator(Operation::Divide, 1, 0), Equals(std::nullopt)); Assert::That(calculator(Operation::Modulo, 5, 0), Equals(std::nullopt)); } It(Boundary_Cases_Test) { Assert::That(calculator(Operation::Divide, std::numeric_limits<int32_t>::min(), -1), Equals(std::nullopt)); Assert::That(calculator(Operation::Modulo, std::numeric_limits<int32_t>::min(), -1), Equals(std::nullopt)); } };
#[test]fn test() {assert_eq!(calculator(Operation::Add, 1, 1), Some(2));assert_eq!(calculator(Operation::Add, 16, 9), Some(25));assert_eq!(calculator(Operation::Add, i32::MAX, 1), None);- Describe(Sample_Tests)
- {
- It(Valid_Input_Test)
- {
- Assert::That(calculator(Operation::Add, 1, 1), Equals(2));
- Assert::That(calculator(Operation::Add, 16, 9), Equals(25));
- Assert::That(calculator(Operation::Subtract, 1, 1), Equals(0));
- Assert::That(calculator(Operation::Subtract, 57, 62), Equals(-5));
- Assert::That(calculator(Operation::Multiply, -10, 10), Equals(-100));
- Assert::That(calculator(Operation::Multiply, -12, -12), Equals(144));
- Assert::That(calculator(Operation::Divide, 2, 4), Equals(0));
- Assert::That(calculator(Operation::Divide, 4, 2), Equals(2));
- Assert::That(calculator(Operation::Modulo, 3, 5), Equals(3));
- Assert::That(calculator(Operation::Modulo, 5, 3), Equals(2));
- }
assert_eq!(calculator(Operation::Subtract, 1, 1), Some(0));assert_eq!(calculator(Operation::Subtract, 57, 62), Some(-5));assert_eq!(calculator(Operation::Subtract, i32::MIN, 1), None);- It(Invalid_Input_Test)
- {
- Assert::That(calculator(Operation::Add, std::numeric_limits<int32_t>::max(), 1), Equals(std::nullopt));
- Assert::That(calculator(Operation::Subtract, std::numeric_limits<int32_t>::min(), 1), Equals(std::nullopt));
- Assert::That(calculator(Operation::Multiply, std::numeric_limits<int32_t>::max(), 2), Equals(std::nullopt));
- Assert::That(calculator(Operation::Divide, 1, 0), Equals(std::nullopt));
- Assert::That(calculator(Operation::Modulo, 5, 0), Equals(std::nullopt));
- }
assert_eq!(calculator(Operation::Multiply, -10, 10), Some(-100));assert_eq!(calculator(Operation::Multiply, -12, -12), Some(144));assert_eq!(calculator(Operation::Multiply, i32::MAX, 2), None);assert_eq!(calculator(Operation::Divide, 2, 4), Some(0));assert_eq!(calculator(Operation::Divide, 4, 2), Some(2));assert_eq!(calculator(Operation::Divide, 1, 0), None);assert_eq!(calculator(Operation::Modulo, 3, 5), Some(3));assert_eq!(calculator(Operation::Modulo, 5, 3), Some(2));assert_eq!(calculator(Operation::Modulo, 5, 0), None);}- It(Boundary_Cases_Test)
- {
- Assert::That(calculator(Operation::Divide, std::numeric_limits<int32_t>::min(), -1), Equals(std::nullopt));
- Assert::That(calculator(Operation::Modulo, std::numeric_limits<int32_t>::min(), -1), Equals(std::nullopt));
- }
- };
#include <utility> template<typename T, typename U> constexpr auto multiply(T a, U b) -> decltype(std::declval<T>() * std::declval<U>()){ return a * b; }
#define multiply(a,b) a*b- #include <utility>
- template<typename T, typename U>
- constexpr auto multiply(T a, U b) -> decltype(std::declval<T>() * std::declval<U>()){
- return a * b;
- }
I know this doesn't work in the current c+ version. Needs c++20.
But with concepts, some of this can be simplified
Concept to check if T + T is valid.
Concept to check if U(T + T) + T is valid, and is U.
T + T could give a different type U, but T + U must be U again.
/* #include <concepts> #include <vector> #include <numeric> #include <type_traits> namespace{ // Concept to check if T + T yields a valid type template <typename T> concept SelfAddable = requires(T a) { { a + a }; // Ensure T + T is valid }; // Concept to check if U + T is valid and results in U template <typename T, typename U> concept CompatibleAddable = requires(U u, T t) { { u + t } -> std::same_as<U>; }; // Function template with inferred U template <SelfAddable T> requires CompatibleAddable<T, decltype(std::declval<T>() + std::declval<T>())> auto sum(const std::vector<T>& v) { using U = decltype(std::declval<T>() + std::declval<T>()); // Infer U return std::accumulate(v.cbegin(), v.cend(), U{}, [](U acc, const T& elem) { return acc + elem; }); } } */ //OLD #include <vector> #include <numeric> #include <type_traits> #include <utility> namespace { template<typename, typename, typename = void> struct has_plus : std::false_type {}; template<typename T1, typename T2> struct has_plus<T1, T2, std::void_t<decltype(std::declval<T1>() + std::declval<T2>())>> : std::true_type {}; template<typename T1, typename T2> inline constexpr bool has_plus_v = has_plus<T1, T2>::value; template<typename T1, typename T2, bool = has_plus_v<T1, T2>> struct plus_result { using type = decltype(std::declval<T1>() + std::declval<T2>()); }; template<typename T1, typename T2> struct plus_result<T1, T2, false> {}; template<typename T1, typename T2> using plus_result_t = typename plus_result<T1, T2>::type; template<typename T = double, typename = std::enable_if_t<has_plus_v<T, T>>> plus_result_t<T, T> sum(const std::vector<T>& v) { return std::accumulate(v.cbegin(), v.cend(), T{}); } }
- /*
- #include <concepts>
- #include <vector>
- #include <numeric>
- #include <type_traits>
- namespace{
- // Concept to check if T + T yields a valid type
- template <typename T>
- concept SelfAddable = requires(T a) {
- { a + a }; // Ensure T + T is valid
- };
- // Concept to check if U + T is valid and results in U
- template <typename T, typename U>
- concept CompatibleAddable = requires(U u, T t) {
- { u + t } -> std::same_as<U>;
- };
- // Function template with inferred U
- template <SelfAddable T>
- requires CompatibleAddable<T, decltype(std::declval<T>() + std::declval<T>())>
- auto sum(const std::vector<T>& v) {
- using U = decltype(std::declval<T>() + std::declval<T>()); // Infer U
- return std::accumulate(v.cbegin(), v.cend(), U{}, [](U acc, const T& elem) {
- return acc + elem;
- });
- }
- }
- */
- //OLD
- #include <vector>
- #include <numeric>
- #include <type_traits>
- #include <utility>
- namespace
- {
- template<typename, typename, typename = void>
- struct has_plus : std::false_type {};
- template<typename T1, typename T2>
- struct has_plus<T1, T2, std::void_t<decltype(std::declval<T1>() + std::declval<T2>())>> : std::true_type {};
- template<typename T1, typename T2>
- inline constexpr bool has_plus_v = has_plus<T1, T2>::value;
- template<typename T1, typename T2, bool = has_plus_v<T1, T2>>
- struct plus_result
- {
- using type = decltype(std::declval<T1>() + std::declval<T2>());
- };
- template<typename T1, typename T2>
- struct plus_result<T1, T2, false> {};
- template<typename T1, typename T2>
- using plus_result_t = typename plus_result<T1, T2>::type;
- template<typename T = double, typename = std::enable_if_t<has_plus_v<T, T>>>
- plus_result_t<T, T> sum(const std::vector<T>& v)
- {
- return std::accumulate(v.cbegin(), v.cend(), T{});
- }
- }